Skip to content

Commit

Permalink
Refacto types
Browse files Browse the repository at this point in the history
  • Loading branch information
chabou committed Jan 24, 2021
1 parent 763c468 commit 031acbd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 73 deletions.
27 changes: 16 additions & 11 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@actions/github": "^4.0.0"
},
"devDependencies": {
"@octokit/types": "^6.5.0",
"@types/node": "^14.14.9",
"@typescript-eslint/parser": "^4.8.1",
"@vercel/ncc": "^0.27.0",
Expand Down
98 changes: 51 additions & 47 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {GetResponseDataTypeFromEndpointMethod} from '@octokit/types'

type PullRequest = GetResponseDataTypeFromEndpointMethod<
typeof octokit.pulls.get
>
type Reviews = GetResponseDataTypeFromEndpointMethod<
typeof octokit.pulls.listReviews
>
type RequestedReviewers = GetResponseDataTypeFromEndpointMethod<
typeof octokit.pulls.listRequestedReviewers
>

type PullRequest = {
number: number
state: string
draft?: boolean
mergeable_state: string
labels: Label[]
}

type Review = {
user: {
login: string
} | null
state: string
}

type RequestedReviewers = {
users: {login: string}[]
}

type PRStatus = 'TO_REVIEW' | 'TO_CHANGE' | 'TO_MERGE' | 'TO_REBASE'
type Label = {name: string; color?: string}

const DEFAULT_LABEL_MAP: Record<PRStatus, Label> = {
type Label = {name?: string; color?: string}

type LabelMap = Record<PRStatus, Label>

const DEFAULT_LABEL_MAP: LabelMap = {
TO_REVIEW: {name: '🚦status:to-review', color: 'FBCA04'},
TO_CHANGE: {name: '🚦status:to-change', color: 'C2E0C6'},
TO_MERGE: {name: '🚦status:to-merge', color: '0E8A16'},
TO_REBASE: {name: '🚦status:to-rebase', color: 'FBCA04'}
}

type LabelMap = typeof DEFAULT_LABEL_MAP

const token = core.getInput('GITHUB_TOKEN', {required: true})

if (!token) {
Expand All @@ -37,47 +47,40 @@ async function run(): Promise<void> {
try {
const labelMap = DEFAULT_LABEL_MAP //TODO permit to override

core.info('Fetching PR')
const pullRequest: PullRequest = await getPullRequest()

const {
number,
state,
draft,
mergeable_state: mergeableState,
labels: currentLabels
} = pullRequest
const {number, labels: currentLabels} = pullRequest

core.info('Fetching PR reviews and requestedReviewers')
const reviews = await getUniqueReviews(number)
const requestedReviewers = await getRequestReviewers(number)

const computedLabels = getComputedLabels({
draft,
mergeableState,
state,
pullRequest,
reviews,
requestedReviewers,
labelMap
})

const toAddLabels = getLabelsToAdd(currentLabels as Label[], computedLabels)
const toAddLabels = getLabelsToAdd(currentLabels, computedLabels)

const toRemoveLabels = getLabelsToRemove(
currentLabels as Label[],
currentLabels,
computedLabels,
labelMap
)

if (toAddLabels.length > 0) {
core.info(
`Adding labels : ${toAddLabels.map(label => label.name).join(',')}`
`Adding labels: ${toAddLabels.map(label => label.name).join(',')}`
)
await addLabels(number, toAddLabels)
}

if (toRemoveLabels.length > 0) {
core.info(
`Removing labels : ${toRemoveLabels.map(label => label.name).join(',')}`
`Removing labels: ${toRemoveLabels.map(label => label.name).join(',')}`
)
await removeLabels(number, toRemoveLabels)
}
Expand Down Expand Up @@ -106,13 +109,13 @@ async function getPullRequest(): Promise<PullRequest> {
return pullRequest
}

async function getUniqueReviews(pullRequestNumber: number): Promise<Reviews> {
async function getUniqueReviews(pullRequestNumber: number): Promise<Review[]> {
const {data: reviews} = await octokit.pulls.listReviews({
owner,
repo,
pull_number: pullRequestNumber
})
const uniqueByUserReviews: Reviews = []
const uniqueByUserReviews: Review[] = []
for (const candidate of reviews
.filter(review => ['CHANGES_REQUESTED', 'APPROVED'].includes(review.state))
.reverse()) {
Expand Down Expand Up @@ -144,18 +147,16 @@ async function getRequestReviewers(
function getComputedLabels({
reviews,
requestedReviewers,
draft,
state,
mergeableState,
pullRequest,
labelMap
}: {
reviews: Reviews
pullRequest: PullRequest
reviews: Review[]
requestedReviewers: RequestedReviewers
draft?: boolean
state: string
mergeableState: string
labelMap: LabelMap
}): Label[] {
const {state, draft, mergeable_state: mergeableState} = pullRequest

const reviewStatus = ((): PRStatus | null => {
if (draft || state !== 'open') {
return null
Expand All @@ -174,10 +175,7 @@ function getComputedLabels({
return 'TO_CHANGE'
}

if (
requestedReviewers.users.length > 0 ||
requestedReviewers.teams.length > 0
) {
if (requestedReviewers.users.length > 0) {
return 'TO_REVIEW'
}

Expand Down Expand Up @@ -243,10 +241,13 @@ async function addLabels(prNumber: number, labels: Label[]): Promise<void> {
repo
})
for (const label of labels) {
if (!label.name) {
continue
}
const remoteLabel = existingLabels.find(
label_ => label.name === label_.name
)
if (!remoteLabel) {
if (!remoteLabel && label.name) {
const response = await octokit.issues.createLabel({
owner,
repo,
Expand All @@ -261,20 +262,23 @@ async function addLabels(prNumber: number, labels: Label[]): Promise<void> {
owner,
repo,
issue_number: prNumber,
labels: labels.map(label => label.name)
labels: labels.map(label => label.name).filter(Boolean) as string[]
})
}

async function removeLabels(prNumber: number, labels: Label[]): Promise<void> {
await Promise.all(
labels.map(async label =>
octokit.issues.removeLabel({
labels.map(async label => {
if (!label.name) {
return
}
return octokit.issues.removeLabel({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
name: label.name
})
)
})
)
}

Expand Down
13 changes: 0 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-3.0.0.tgz#f73d48af2d21bf4f97fbf38fae43b54699e0dbba"
integrity sha512-jOp1CVRw+OBJaZtG9QzZggvJXvyzgDXuW948SWsDiwmyDuCjeYCiF3TDD/qvhpF580RfP7iBIos4AVU6yhgMlA==

"@octokit/openapi-types@^3.2.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-3.2.0.tgz#d62d0ff7147dbf4d218616b2484ee2a5d023055d"
integrity sha512-X7yW/fpzF3uTAE+LbPD3HEeeU+/49o0V4kNA/yv8jQ3BDpFayv/osTOhY1y1mLXljW2bOJcOCSGZo4jFKPJ6Vw==

"@octokit/plugin-paginate-rest@^2.2.3":
version "2.8.0"
resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-2.8.0.tgz#2b41e12b494e895bf5fb5b12565d2c80a0ecc6ae"
Expand Down Expand Up @@ -175,14 +170,6 @@
"@octokit/openapi-types" "^3.0.0"
"@types/node" ">= 8"

"@octokit/types@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-6.5.0.tgz#8f27c52d57eb4096fb05a290f4afc90194e08b19"
integrity sha512-mzCy7lkYQv+kM58W37uTg/mWoJ4nvRDRCkjSdqlrgA28hJEYNJTMYiGTvmq39cdtnMPJd0hshysBEAaH4D5C7w==
dependencies:
"@octokit/openapi-types" "^3.2.0"
"@types/node" ">= 8"

"@types/json-schema@^7.0.3":
version "7.0.7"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
Expand Down

0 comments on commit 031acbd

Please sign in to comment.