Skip to content

Commit

Permalink
feat: use review comments (#104)
Browse files Browse the repository at this point in the history
* use pull request reviews, reusing same review with each run
  • Loading branch information
mtfoley authored Dec 30, 2021
1 parent e006ec1 commit a162934
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 26 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ This action drives the following outcomes:
Check | Outcome on Failure
--- | ---
PR Title Lint | Action shows as failed check. Action leaves comment.
PR Refers to Issue | Action closes issue. Action leaves comment.
PR Originates from Protected Branch | Action closes issue. Action leaves comment.
PR Avoids Watched Files | Action leaves comment.
PR Title Lint | Action shows as failed check. Action leaves review comment.
PR Refers to Issue | Action closes issue. Action leaves review comment.
PR Originates from Protected Branch | Action closes issue. Action leaves review comment.
PR Avoids Watched Files | Action leaves review comment.
If a PR is initially deemed non-compliant by the action and a review comment is left, the action will update this same review comment each time it runs again (e.g. if the PR title or description changes). If the PR is found to be compliant after changes, the review comment will be updated to reflect this.
## Inputs
Expand Down
54 changes: 46 additions & 8 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.

87 changes: 74 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import * as github from '@actions/github'
import {context} from '@actions/github/lib/utils'
import {checkBody, checkTitle, checkBranch} from './checks'

type PullRequestReview = {
id: number
node_id: string
user: {
login: string
id: number
node_id: string
} | null
body: string
state: string
}

const repoToken = core.getInput('repo-token')
const ignoreAuthors = core.getMultilineInput('ignore-authors')
const ignoreTeamMembers = core.getBooleanInput('ignore-team-members')
Expand Down Expand Up @@ -121,27 +133,26 @@ async function run(): Promise<void> {
commentsToLeave.push(watchedFilesComment + filesList)
}
}
// Finally close PR if warranted
// Update Review as needed
let reviewBody = ''
if (commentsToLeave.length > 0)
await createComment(
pr.number,
[baseComment, ...commentsToLeave].join('\n\n')
)
reviewBody = [baseComment, ...commentsToLeave].join('\n\n')
await updateReview(
{owner: pr.owner, repo: pr.repo, pull_number: pr.number},
reviewBody
)
// Finally close PR if warranted
if (shouldClosePr) await closePullRequest(pr.number)
} else {
await updateReview(
{owner: pr.owner, repo: pr.repo, pull_number: pr.number},
''
)
}
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
}
}
async function createComment(number: number, comment: string) {
if (comment.trim() !== '')
await client.rest.issues.createComment({
...context.repo,
issue_number: number,
body: comment
})
}
async function closePullRequest(number: number) {
await client.rest.pulls.update({
...context.repo,
Expand All @@ -164,6 +175,56 @@ async function listFiles(pullRequest: {
const {data: files} = await client.rest.pulls.listFiles(pullRequest)
return files
}
async function findExistingReview(pullRequest: {
owner: string
repo: string
pull_number: number
}): Promise<PullRequestReview | null> {
let review
const {data: reviews} = await client.rest.pulls.listReviews(pullRequest)
review = reviews.find(review => {
return (review?.user?.login ?? '') == 'github-actions[bot]'
})
if (review === undefined) review = null
return review
}
async function updateReview(
pullRequest: {owner: string; repo: string; pull_number: number},
body: string
) {
const review = await findExistingReview(pullRequest)
// if blank body and no existing review, exit
if (body === '' && review === null) return
// if review body same as new body, exit
if (body === review?.body) return
// if no existing review, body non blank, create a review
if (review === null && body !== '') {
await client.rest.pulls.createReview({
...pullRequest,
body,
event: 'COMMENT'
})
return
}
// if body blank and review exists, update it to show passed
if (review !== null && body === '') {
await client.rest.pulls.updateReview({
...pullRequest,
review_id: review.id,
body: 'PR Compliance Checks Passed!'
})
return
}
// if body non-blank and review exists, update it
if (review !== null && body !== review?.body) {
await client.rest.pulls.updateReview({
...pullRequest,
review_id: review.id,
body
})
return
}
}
async function userIsTeamMember(login: string, owner: string) {
if (login === owner) return true
const {data: userOrgs} = await client.request('GET /users/{user}/orgs', {
Expand Down

0 comments on commit a162934

Please sign in to comment.