Skip to content

Commit 6bd4eac

Browse files
committed
Rewrite the PR commit linter job in CI
The action this was using previously is gone now.
1 parent 57049fe commit 6bd4eac

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

.github/scripts/lint-commits.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,44 +55,44 @@ const checkers = [{
5555
},
5656
}];
5757

58-
async function readJsonFromStdin() {
59-
const chunks = [];
60-
for await (const chunk of process.stdin) {
61-
chunks.push(chunk);
62-
}
63-
64-
return Buffer.concat(chunks).toString();
65-
}
66-
6758
function runChecker({ check, error }) {
6859
return check(this.message) ? [] : [error];
6960
}
7061

71-
async function main() {
72-
const commits = JSON.parse(await readJsonFromStdin());
73-
74-
let exitCode = 0;
62+
function checkCommits(commits) {
63+
let success = true;
7564
for (const { commit, sha } of commits) {
7665
const errors = checkers.flatMap(runChecker, commit);
7766
if (errors.length === 0) {
7867
continue;
7968
}
8069

81-
if (exitCode !== 0) {
70+
if (!success) {
8271
console.log("");
8372
}
84-
exitCode = 1;
73+
success = false;
8574

8675
console.log("Commit %s failed these checks:", sha);
8776
for (const error of errors) {
8877
console.log(" %s", error);
8978
}
9079
}
9180

92-
return exitCode;
81+
return success;
82+
}
83+
84+
async function main(github, core, context) {
85+
const { repository, pull_request } = context.payload;
86+
const options = github.rest.pulls.listCommits.endpoint.merge({
87+
owner: repository.owner.login,
88+
repo: repository.name,
89+
pull_number: pull_request.number,
90+
});
91+
const commits = await github.paginate(options);
92+
const isErrorFree = checkCommits(commits);
93+
if (!isErrorFree) {
94+
core.setFailed("Please check the logs for errors in commit messages.");
95+
}
9396
}
9497

95-
main().catch((error) => {
96-
console.error(error);
97-
return 1;
98-
}).then((code) => { process.exitCode = code; });
98+
module.exports = { main };

.github/workflows/lint-commits.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ jobs:
1212
runs-on: ubuntu-latest
1313

1414
steps:
15-
- name: Get PR commits
16-
id: commits
17-
uses: IdanHo/get-pr-commits@d94b66d146a31ef91e54a2597dee4fb523157232
18-
with: { token: "${{ github.token }}" }
15+
- uses: actions/checkout@v2
1916

2017
- name: Check commits
21-
run: |
22-
node .github/scripts/lint-commits.js << 'JSON'
23-
${{ steps.commits.outputs.commits }}
24-
JSON
18+
uses: actions/github-script@v5
19+
with:
20+
script: await require(".github/scripts/lint-commits.js")
21+
.main(github, core, context)

0 commit comments

Comments
 (0)