Skip to content

Commit af22bdc

Browse files
committed
fix(index.js): Use async await when listing commits for a pull request and add better error logs
1 parent c1e0287 commit af22bdc

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ jobs:
88
runs-on: ubuntu-latest
99
steps:
1010
- name: Clean commit step
11-
uses: ad1992/[email protected].0
11+
uses: ad1992/[email protected].10
1212
with:
1313
repo-token: ${{ secrets.GITHUB_TOKEN }}

dist/index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5300,7 +5300,7 @@ module.exports = function(fn) {
53005300
/***/ 557:
53015301
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
53025302

5303-
const { getInput, setFailed, debug } = __webpack_require__(674);
5303+
const { getInput, setFailed, debug, error } = __webpack_require__(674);
53045304
const { GitHub, context } = __webpack_require__(205);
53055305

53065306
const { BAD_KEYWORDS, MIN_COMMIT_MESSAGE_LENGTH } = __webpack_require__(481);
@@ -5319,29 +5319,37 @@ function filterCommit(commit) {
53195319

53205320
async function verifyCommits(repoToken) {
53215321
const client = new GitHub(repoToken);
5322-
const { data: commits } = client.pulls.listCommits({ owner: context.repo.repo, repo: context.repo.repo, pull_number: context.issue.number})
5323-
debug(`There are ${commits.length} commits`);
5322+
const { data: commits } = await client.pulls.listCommits({ owner: context.repo.owner, repo: context.repo.repo, pull_number: context.issue.number})
5323+
debug(`There are ${commits.length} commits in this pr`);
53245324

5325-
let errors = [];
5325+
let badCommits = 0;
5326+
let isCommitBad = false;
53265327
for (const commit of commits) {
53275328
const { message } = commit.commit;
5328-
debug(message);
53295329

53305330
if (message.length < MIN_COMMIT_MESSAGE_LENGTH) {
5331-
errors.push(`${message} has less than ${MIN_COMMIT_MESSAGE_LENGTH} characters`);
5331+
error(`commit message \"${message}\" has less than ${MIN_COMMIT_MESSAGE_LENGTH} characters`);
5332+
isCommitBad = true;
53325333
}
53335334

53345335
if (message[0] !== message[0].toUpperCase()) {
5335-
errors.push(`${message} should have first letter in upper case`);
5336+
error(`commit message \"${message}\" should have first letter in upper case`);
5337+
isCommitBad = true;
53365338
}
53375339

53385340
const badKeywords = filterCommit(message);
53395341
if (badKeywords.length) {
5340-
errors.push(`${message} contains ${badKeywords.join()}`);
5342+
error(`commit message \"${message}\" contains ${badKeywords.join()}`);
5343+
isCommitBad = true;
5344+
}
5345+
5346+
if(isCommitBad) {
5347+
badCommits++;
5348+
isCommitBad = false;
53415349
}
53425350
}
5343-
if (errors.length) {
5344-
throw Error(errors);
5351+
if (badCommits) {
5352+
throw Error(`${badCommits} have been encountered. Please fix the above errors`);
53455353
}
53465354
}
53475355
async function main() {

index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getInput, setFailed, debug } = require('@actions/core');
1+
const { getInput, setFailed, debug, error } = require('@actions/core');
22
const { GitHub, context } = require('@actions/github');
33

44
const { BAD_KEYWORDS, MIN_COMMIT_MESSAGE_LENGTH } = require('./constants');
@@ -17,29 +17,37 @@ function filterCommit(commit) {
1717

1818
async function verifyCommits(repoToken) {
1919
const client = new GitHub(repoToken);
20-
const { data: commits } = client.pulls.listCommits({ owner: context.repo.repo, repo: context.repo.repo, pull_number: context.issue.number})
21-
debug(`There are ${commits.length} commits`);
20+
const { data: commits } = await client.pulls.listCommits({ owner: context.repo.owner, repo: context.repo.repo, pull_number: context.issue.number})
21+
debug(`There are ${commits.length} commits in this pr`);
2222

23-
let errors = [];
23+
let badCommits = 0;
24+
let isCommitBad = false;
2425
for (const commit of commits) {
2526
const { message } = commit.commit;
26-
debug(message);
2727

2828
if (message.length < MIN_COMMIT_MESSAGE_LENGTH) {
29-
errors.push(`${message} has less than ${MIN_COMMIT_MESSAGE_LENGTH} characters`);
29+
error(`commit message \"${message}\" has less than ${MIN_COMMIT_MESSAGE_LENGTH} characters`);
30+
isCommitBad = true;
3031
}
3132

3233
if (message[0] !== message[0].toUpperCase()) {
33-
errors.push(`${message} should have first letter in upper case`);
34+
error(`commit message \"${message}\" should have first letter in upper case`);
35+
isCommitBad = true;
3436
}
3537

3638
const badKeywords = filterCommit(message);
3739
if (badKeywords.length) {
38-
errors.push(`${message} contains ${badKeywords.join()}`);
40+
error(`commit message \"${message}\" contains ${badKeywords.join()}`);
41+
isCommitBad = true;
42+
}
43+
44+
if(isCommitBad) {
45+
badCommits++;
46+
isCommitBad = false;
3947
}
4048
}
41-
if (errors.length) {
42-
throw Error(errors);
49+
if (badCommits) {
50+
throw Error(`${badCommits} have been encountered. Please fix the above errors`);
4351
}
4452
}
4553
async function main() {

0 commit comments

Comments
 (0)