Skip to content

Commit

Permalink
test issue2679 skip ci
Browse files Browse the repository at this point in the history
  • Loading branch information
zondervancalvez committed Sep 16, 2024
1 parent 7b32791 commit eadd3e8
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 1 deletion.
13 changes: 12 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
- name: Get the output response
run: echo "${{ steps.lint-git-repo.outputs.lint-git-repo-response }}"

override-skip-ci:
uses: ./.github/workflows/skip-ci-check.yaml

check-coverage:
outputs:
run-coverage: ${{ steps.set-output.outputs.run-coverage }}
Expand Down Expand Up @@ -2662,6 +2665,7 @@ jobs:
ignore-unfixed: false
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'

name: Cactus_CI
'on':
pull_request:
Expand All @@ -2672,4 +2676,11 @@ name: Cactus_CI
push:
branches:
- main
- dev
- dev
workflow_dispatch:
inputs:
run_ci:
description: 'Force run CI, overriding skip-ci in commit messages'
required: true
default: false
type: boolean
23 changes: 23 additions & 0 deletions .github/workflows/skip-ci-check.yaml
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

Check failure on line 13 in .github/workflows/skip-ci-check.yaml

View workflow job for this annotation

GitHub Actions / ActionLint / Lint_GitHub_Actions

the runner of "actions/checkout@v2" action is too old to run on GitHub Actions. update the action's version to fix this issue

- 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 }}
135 changes: 135 additions & 0 deletions tools/skip-ci.js
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();

0 comments on commit eadd3e8

Please sign in to comment.