| 1 |
name: Validate pull request |
| 2 |
|
| 3 |
on: |
| 4 |
# pull_request_target is needed so the GITHUB_TOKEN has pull-requests: write |
| 5 |
# permission for PRs from forks. This is safe because the workflow never |
| 6 |
# checks out or executes any code from the PR; it only reads commit metadata |
| 7 |
# via the GitHub API and posts reviews. |
| 8 |
pull_request_target: # zizmor: ignore[dangerous-triggers] |
| 9 |
types: [opened, synchronize, edited] |
| 10 |
|
| 11 |
permissions: |
| 12 |
contents: read |
| 13 |
pull-requests: write |
| 14 |
|
| 15 |
jobs: |
| 16 |
check-pr: |
| 17 |
name: Commit subject lines |
| 18 |
runs-on: ubuntu-latest |
| 19 |
steps: |
| 20 |
- name: Run validation script |
| 21 |
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 |
| 22 |
with: |
| 23 |
github-token: ${{ secrets.GITHUB_TOKEN }} |
| 24 |
script: | |
| 25 |
const pullRequest = { |
| 26 |
pull_number: context.issue.number, |
| 27 |
owner: context.repo.owner, |
| 28 |
repo: context.repo.repo, |
| 29 |
}; |
| 30 |
|
| 31 |
const { data: commits } = await github.rest.pulls.listCommits(pullRequest); |
| 32 |
const { data: reviews } = await github.rest.pulls.listReviews(pullRequest); |
| 33 |
|
| 34 |
// Get latest review by `github-actions[bot]`. |
| 35 |
const githubActionsBot = 41898282; |
| 36 |
const latestReview = reviews |
| 37 |
.filter(r => r.user.id === githubActionsBot) |
| 38 |
.at(-1); |
| 39 |
|
| 40 |
// Ensure commit subject lines are in the form `area or areas: description`. |
| 41 |
const subjectLineFormat = /^.+:\s.+$/; |
| 42 |
const badCommits = commits |
| 43 |
.map(c => [c.sha.slice(0, 8), c.commit.message.split('\n')[0]]) |
| 44 |
.filter(([_, s]) => !subjectLineFormat.test(s)) |
| 45 |
.map(([sha, title]) => `- ${sha}: ${title}`); |
| 46 |
|
| 47 |
if (badCommits.length > 0) { |
| 48 |
// One or more commits are bad. Make sure changes are requested. |
| 49 |
const body = 'The following commits do not follow our format for subject lines:\n\n' |
| 50 |
+ `${badCommits.join('\n')}\n\nCommits should have a subject line following the ` |
| 51 |
+ 'format `<topic>: <description>`. Please review the [commit guidelines]' |
| 52 |
+ '(https://jj-vcs.github.io/jj/prerelease/contributing/#commit-guidelines) for ' |
| 53 |
+ 'more information.'; |
| 54 |
|
| 55 |
if (latestReview?.state !== 'CHANGES_REQUESTED') { |
| 56 |
await github.rest.pulls.createReview({ |
| 57 |
event: 'REQUEST_CHANGES', |
| 58 |
...pullRequest, |
| 59 |
body, |
| 60 |
}); |
| 61 |
} |
| 62 |
else { |
| 63 |
await github.rest.pulls.updateReview({ |
| 64 |
review_id: latestReview.id, |
| 65 |
...pullRequest, |
| 66 |
body, |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
await core.setFailed('One or more commits were not formatted correctly.'); |
| 71 |
} |
| 72 |
else { |
| 73 |
// All commits are formatted correctly. Dismiss any change requests. |
| 74 |
if (latestReview && latestReview.state === 'CHANGES_REQUESTED') { |
| 75 |
await github.rest.pulls.dismissReview({ |
| 76 |
message: 'All commits are now correctly formatted. Thank you for your contribution!', |
| 77 |
review_id: latestReview.id, |
| 78 |
...pullRequest, |
| 79 |
}); |
| 80 |
} |
| 81 |
|
| 82 |
console.log('All commits were correctly formatted.'); |
| 83 |
} |