forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b32791
commit eadd3e8
Showing
3 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Skip CI Checker | ||
env: | ||
NODEJS_VERSION: v18.18.2 | ||
on: | ||
workflow_call: | ||
|
||
jobs: | ||
ci-check: | ||
runs-on: ubuntu-22.04 | ||
if: "!contains('dependabot[bot]', github.event.pull_request.user.login)" | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Node.js | ||
uses: actions/[email protected] | ||
with: | ||
node-version: ${{ env.NODEJS_VERSION }} | ||
|
||
- name: Check skip CI logic | ||
run: node tools/skip-ci.js ${{ github.event.pull_request.url }} | ||
env: | ||
INPUT_RUN_CI: ${{ github.event.inputs.run_ci }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Maintainers allowed to use the skip-ci tag | ||
const MAINTAINERS = [ | ||
"izuru0", | ||
"outSH", | ||
"petermetz", | ||
"takeutak", | ||
"jagpreetsinghsasan", | ||
"VRamakrishna", | ||
"sandeepnRES", | ||
"RafaelAPB", | ||
"zondervancalvez", | ||
]; | ||
const SKIP_CMDS = [ | ||
"[skip ci]", | ||
"[ci skip]", | ||
"[no ci]", | ||
"[skip actions]", | ||
"[actions skip]", | ||
"skip ci", | ||
"skip-ci", | ||
]; | ||
|
||
//regular expression | ||
const FIXES_OR_DEPENDS_REGEX = /(Fixes|Depends)(.|\n)*/gim; | ||
const SIGNED_OFF_REGEX = /(")*Signed-off-by:(.|\s)*/gim; | ||
const HYPHEN_REGEX = /(-)+/gm; | ||
const BACKTICK_REGEX = /`+/gm; | ||
const WHITESPACES_HARDCODED_REGEX = /(\r\n|\\r)/gm; | ||
|
||
// Check if manual override is set from workflow_dispatch input | ||
const checkOverride = () => { | ||
const override = process.env.INPUT_RUN_CI; // This gets the input from the workflow_dispatch event | ||
if (override && override.toLowerCase()) { | ||
console.log( | ||
"Manual override triggered. CI will run regardless of skip-ci.", | ||
); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
const args = process.argv.slice(2); | ||
const pullReqUrl = args[0]; | ||
//const pullReqUrl = "https://api.github.com/repos/zondervancalvez/cactus/pulls/7"; | ||
|
||
export async function fetchJsonFromUrl(url) { | ||
console.log("URL:" + url); | ||
const fetchResponse = await fetch(url); | ||
return fetchResponse.json(); | ||
} | ||
|
||
let commitMessageList = []; | ||
let committerMaintainerList = []; | ||
|
||
const commitMessagesMetadata = await fetchJsonFromUrl(pullReqUrl + "/commits"); | ||
console.log(commitMessagesMetadata); | ||
|
||
commitMessagesMetadata.forEach((commitMessageMetadata) => { | ||
// get commit message body | ||
commitMessageList.push( | ||
commitMessageMetadata["commit"]["message"] | ||
.replace(SIGNED_OFF_REGEX, "") | ||
.replace(HYPHEN_REGEX, "") | ||
.replace(BACKTICK_REGEX, "") | ||
.replace(WHITESPACES_HARDCODED_REGEX, "") | ||
.replace(FIXES_OR_DEPENDS_REGEX, ""), | ||
); | ||
|
||
// get committer | ||
committerMaintainerList.push( | ||
commitMessageMetadata["committer"]["login"] | ||
.replace(SIGNED_OFF_REGEX, "") | ||
.replace(HYPHEN_REGEX, "") | ||
.replace(BACKTICK_REGEX, "") | ||
.replace(WHITESPACES_HARDCODED_REGEX, "") | ||
.replace(FIXES_OR_DEPENDS_REGEX, ""), | ||
); | ||
}); | ||
|
||
// Check if skip-ci is found in commit message | ||
const checkSkipCI = () => { | ||
for (let commitMessageListIndex in commitMessageList) { | ||
let commitMessage = commitMessageList[commitMessageListIndex]; | ||
if (commitMessage.toLowerCase().indexOf(SKIP_CMDS)) { | ||
console.log("Skip requested in commit message."); | ||
return true; | ||
} else { | ||
console.log("No skip request found."); | ||
return false; | ||
} | ||
} | ||
}; | ||
|
||
// Check if committer is a trusted maintainer | ||
const checkCommitterIsMaintainer = () => { | ||
for (let committerMaintainerListIndex in committerMaintainerList) { | ||
let committer = committerMaintainerList[committerMaintainerListIndex]; | ||
if (MAINTAINERS.includes(committer)) { | ||
console.log("Committer is a trusted maintainer, skipping tests."); | ||
return true; | ||
} else { | ||
console.error("Untrusted committer, failing the job."); | ||
return false; | ||
} | ||
} | ||
}; | ||
|
||
// Main logic | ||
const main = () => { | ||
const override = checkOverride(); | ||
|
||
if (!override) { | ||
const shouldSkipCI = checkSkipCI(); | ||
|
||
if (shouldSkipCI) { | ||
const isMaintainer = checkCommitterIsMaintainer(); | ||
if (isMaintainer) { | ||
console.log("CI skipped as per request."); | ||
process.exit(0); // Exit successfully to skip CI | ||
} else { | ||
console.error("Committer is not trusted. CI will not be skipped."); | ||
process.exit(1); // Fail the pipeline | ||
} | ||
} else { | ||
console.log("No skip requested. Proceeding with CI."); | ||
process.exit(0); // Exit successfully to run CI | ||
} | ||
} else { | ||
console.log("Manual override set. Running CI."); | ||
process.exit(0); // Exit successfully to run CI | ||
} | ||
}; | ||
|
||
// Run the main function | ||
main(); |