Skip to content

Commit d41e64a

Browse files
ci(check-ci-skip): fix commitMessagesMetadata.forEach is not a function
Primary Changes ---------------- 1. Changed the method in getting the commit message from GitHub API to shell command to avoid the rate limits in calling the API. 2. Same goes for the author of commit message, we use shell command to fetch the username. Fixes hyperledger-cacti#3614 Signed-off-by: bado <[email protected]>
1 parent 50d19f8 commit d41e64a

File tree

2 files changed

+77
-76
lines changed

2 files changed

+77
-76
lines changed

.github/workflows/ci.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ env:
1717
jobs:
1818
ActionLint:
1919
needs: check-ci-skip
20+
if: needs.check-ci-skip.outputs.should_skip == 'false'
2021
uses: ./.github/workflows/actionlint.yaml
2122
DCI-Lint:
2223
name: DCI-Lint
2324
needs: check-ci-skip
25+
if: needs.check-ci-skip.outputs.should_skip == 'false'
2426
runs-on: ubuntu-22.04
2527
steps:
2628
- id: lint-git-repo
@@ -33,13 +35,16 @@ jobs:
3335

3436
check-ci-skip:
3537
runs-on: ubuntu-22.04
38+
outputs:
39+
should_skip: ${{ steps.check.outputs.should_skip }}
3640
steps:
3741
- uses: actions/[email protected]
3842
- name: Check CI Skip
39-
run: node tools/ci-skip-for-maintainers.js ${{ github.event.pull_request.url }} ${{ github.event.pull_request.user.login }}
43+
run: node tools/ci-skip-for-maintainers.js
4044

4145
check-coverage:
4246
needs: check-ci-skip
47+
if: needs.check-ci-skip.outputs.should_skip == 'false'
4348
outputs:
4449
run-coverage: ${{ steps.set-output.outputs.run-coverage }}
4550
runs-on: ubuntu-22.04
@@ -51,6 +56,7 @@ jobs:
5156

5257
compute_changed_packages:
5358
needs: check-ci-skip
59+
if: needs.check-ci-skip.outputs.should_skip == 'false'
5460
outputs:
5561
cmd-api-server-changed: ${{ steps.changes.outputs.cmd-api-server-changed }}
5662
plugin-ledger-connector-polkadot-changed: ${{ steps.changes.outputs.plugin-ledger-connector-polkadot-changed }}
@@ -187,6 +193,7 @@ jobs:
187193
188194
build-dev:
189195
needs: check-ci-skip
196+
if: needs.check-ci-skip.outputs.should_skip == 'false'
190197
continue-on-error: false
191198
env:
192199
DEV_BUILD_DISABLED: false

tools/ci-skip-for-maintainers.js

+69-75
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { readFileSync } from "fs";
2+
import { execSync } from "child_process";
23

3-
//A new tag exclusively for MAINTAINERS that allows skipping the CI check
4+
// Constants
45
const SKIP_CACTI = "skip-cacti-ci";
56
const MaintainersFile = "MAINTAINERS.md";
6-
//regular expression to get the maintainers in MAINTAINERS.md
77
const NAMES_REGEX = /\|\s*([A-Za-z\s]+)\s*/;
88
const LINKS_REGEX = /\|\s*\[([^\]]+)\]\[[^\]]+\]\s*/;
99
const TAGS_REGEX = /\|\s*([A-Za-z0-9_-]+|-)*\s*/;
@@ -12,90 +12,84 @@ const MAINTAINERS_REGEX = new RegExp(
1212
"g",
1313
);
1414

15-
const main = async () => {
16-
const markdownContent = readFileSync(MaintainersFile, "utf-8");
15+
const getMaintainersFileContent = () => readFileSync(MaintainersFile, "utf-8");
1716

18-
const args = process.argv.slice(2);
19-
const pullReqUrl = args[0];
20-
const committerLogin = args[1];
17+
// Function to get the latest commit message author
18+
const getCommitterLogin = () => {
19+
const authorBuffer = execSync("git log -1 | grep Author | cut -d' ' -f2");
20+
const authorString = authorBuffer.toString();
21+
const authorStringTrim = authorString.trim();
22+
return authorStringTrim;
23+
};
2124

22-
//Uncomment these lines and change it for local machine testing purposes:
23-
//const pullReqUrl = "https://api.github.com/repos/<username>/cactus/pulls/<number>";
24-
//const committerLogin = "<username>";
25+
// Function to get the latest commit message
26+
const getLatestCommitMessage = () => {
27+
const commitMsgBuffer = execSync("git log -1 --pretty=%B");
28+
const commitMsgString = commitMsgBuffer.toString();
29+
const commitMsgTrim = commitMsgString.trim();
30+
return commitMsgTrim;
31+
};
2532

26-
const fetchJsonFromUrl = async (url) => {
27-
const fetchResponse = await fetch(url);
28-
return fetchResponse.json();
29-
};
33+
// Function to check if SKIP_CACTI tag is in the commit message
34+
const checkSkipCI = (commitMessage) => {
35+
if (commitMessage.includes(SKIP_CACTI)) {
36+
console.log("Skip requested in commit message.");
37+
return true;
38+
}
39+
console.log("No skip request found.");
40+
return false;
41+
};
3042

31-
let commitMessageList = [];
32-
const commitMessagesMetadata = await fetchJsonFromUrl(
33-
pullReqUrl + "/commits",
43+
// Function to extract maintainers from the MAINTAINERS.md file content
44+
const extractMaintainers = (maintainerMetaData) => {
45+
let match;
46+
const maintainers = [];
47+
while ((match = MAINTAINERS_REGEX.exec(maintainerMetaData)) !== null) {
48+
const github = match[2];
49+
maintainers.push(github);
50+
}
51+
return maintainers;
52+
};
53+
54+
// Function to check if committer is an active maintainer
55+
const checkCommitterIsMaintainer = (committerLogin, activeMaintainers) => {
56+
if (activeMaintainers.includes(committerLogin)) {
57+
console.log("The author of this PR is an active maintainer.");
58+
return true;
59+
}
60+
console.log(
61+
"CI will not be skipped. \nThe author of this PR is not an active maintainer.\nPlease refer to https://github.com/hyperledger/cacti/blob/main/MAINTAINERS.md for the list of active maintainers.",
3462
);
63+
return false;
64+
};
3565

36-
commitMessagesMetadata.forEach((commitMessageMetadata) => {
37-
// get commit message body
38-
commitMessageList.push(commitMessageMetadata["commit"]["message"]);
39-
});
66+
// Main function to determine whether to skip CI or proceed
67+
const main = async () => {
68+
const markdownContent = getMaintainersFileContent();
69+
const committerLogin = getCommitterLogin();
70+
const commitMessage = getLatestCommitMessage();
4071

41-
// Check if skip-ci is found in commit message
42-
const checkSkipCI = () => {
43-
for (let commitMessageListIndex in commitMessageList) {
44-
let commitMessage = commitMessageList[commitMessageListIndex];
45-
if (commitMessage.includes(SKIP_CACTI)) {
46-
console.log("Skip requested in commit message.");
47-
return true;
48-
} else {
49-
console.log("No skip request found.");
50-
}
51-
return false;
52-
}
53-
};
72+
const shouldSkipCI = checkSkipCI(commitMessage);
73+
if (!shouldSkipCI) {
74+
console.log("No skip requested. Proceeding with CI.");
75+
process.exit(0);
76+
}
5477

55-
// Function to extract active maintainers
56-
const extractMaintainers = (maintainerMetaData) => {
57-
let match;
58-
const maintainers = [];
59-
while ((match = MAINTAINERS_REGEX.exec(maintainerMetaData)) !== null) {
60-
const github = match[2];
61-
maintainers.push(github);
62-
}
63-
return maintainers;
64-
};
65-
// Get the maintainers
6678
const activeMaintainers = extractMaintainers(markdownContent);
67-
activeMaintainers.forEach((maintainers) => {
68-
maintainers;
69-
});
70-
71-
// Check if committer is a trusted maintainer
72-
const checkCommitterIsMaintainer = () => {
73-
if (activeMaintainers.includes(committerLogin)) {
74-
console.log("The author of this PR is an active maintainer.");
75-
return true;
76-
} else {
77-
console.log(
78-
"CI will not be skipped. \nThe author of this PR is not an active maintainer.\nPlease refer to https://github.com/hyperledger/cacti/blob/main/MAINTAINERS.md for the list of active maintainers.",
79-
);
80-
return false;
81-
}
82-
};
83-
84-
// Main logic
85-
86-
const shouldSkipCI = checkSkipCI();
79+
const isMaintainer = checkCommitterIsMaintainer(
80+
committerLogin,
81+
activeMaintainers,
82+
);
8783

88-
if (shouldSkipCI) {
89-
const isMaintainer = checkCommitterIsMaintainer();
90-
if (isMaintainer) {
91-
console.log(
92-
"Exit with an error code so as to pause the dependent workflows. CI skipped as per request.",
93-
);
94-
process.exit(1); // Exit successfully to skip CI
95-
}
84+
if (isMaintainer) {
85+
console.log(
86+
"CI skipped as per request. Exit with an error to PAUSE dependent workflows.",
87+
);
88+
console.log("::set-output name=should_skip::true");
89+
process.exit(0);
9690
} else {
97-
console.log("No skip requested. Proceeding with CI.");
98-
process.exit(0); // Exit successfully to run CI
91+
console.log("::set-output name=should_skip::false");
92+
process.exit(0);
9993
}
10094
};
10195

0 commit comments

Comments
 (0)