Skip to content

Commit 850a1c2

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 7af9983 commit 850a1c2

File tree

2 files changed

+68
-76
lines changed

2 files changed

+68
-76
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
steps:
3737
- uses: actions/[email protected]
3838
- name: Check CI Skip
39-
run: node tools/ci-skip-for-maintainers.js ${{ github.event.pull_request.url }} ${{ github.event.pull_request.user.login }}
39+
run: node tools/ci-skip-for-maintainers.js ${{ github.event.pull_request.user.login }}
4040

4141
check-coverage:
4242
needs: check-ci-skip

tools/ci-skip-for-maintainers.js

+67-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,82 @@ 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+
process.exit(1);
9689
} else {
97-
console.log("No skip requested. Proceeding with CI.");
98-
process.exit(0); // Exit successfully to run CI
90+
process.exit(0);
9991
}
10092
};
10193

0 commit comments

Comments
 (0)