Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci(github): commit parity check based on similarity #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions .github/workflows/pr-commit-parity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,33 @@
- main
- dev

env:
NODEJS_VERSION: v18.18.2

jobs:
pr-commit-parity:
name: PR and Commit messages Parity
runs-on: ubuntu-22.04
env:
ACCEPTABLE_SIMILARITY_RATIO: 0.9
steps:
- uses: actions/[email protected]
- name: Use Node.js v18.18.2
uses: actions/[email protected]
- name: Install Indy SDK
run: >
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys CE7709D068DB5E88 \
&& sudo add-apt-repository "deb https://repo.sovrin.org/sdk/deb bionic stable" \
&& sudo apt-get update \
&& sudo apt-get install -y \
libindy \
libnullpay \
libvcx \
indy-cli \
&& sudo rm -f /etc/apt/sources.list.d/sovrin.list*
- name: Set up NodeJS ${{ env.NODEJS_VERSION }}
uses: actions/[email protected]
with:
node-version: v18.19.0
node-version: ${{ env.NODEJS_VERSION }}
- run: npm run configure

- name: Execute pr-commit-parity script
run: node tools/pr-commit-parity.js ${{ github.event.pull_request.url }}
run: node tools/pr-commit-parity.js ${{ github.event.pull_request.url }} $ACCEPTABLE_SIMILARITY_RATIO

Check failure on line 43 in .github/workflows/pr-commit-parity.yaml

View workflow job for this annotation

GitHub Actions / ActionLint / Lint_GitHub_Actions

shellcheck reported issue in this script: SC2086:info:1:69: Double quote to prevent globbing and word splitting
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
"sort-package-json": "1.57.0",
"source-map-loader": "4.0.1",
"stream-browserify": "3.0.0",
"string-similarity-js": "2.1.4",
"tap": "16.3.8",
"tape": "5.6.6",
"tape-promise": "4.0.0",
Expand Down
45 changes: 33 additions & 12 deletions tools/pr-commit-parity.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { stringSimilarity } from "string-similarity-js";

export async function fetchJsonFromUrl(url) {
const fetchResponse = await fetch(url);
return fetchResponse.json();
}

//regex expressions
// regex expressions
const PULL_REQ_REQUIREMENTS_REGEX = /\*\*Pull\sRequest\sRequirements(.|\n)*/gim;
const FIXES_OR_DEPENDS_REGEX = /(Fixes|Depends)(.|\n)*/gim;
const SIGNED_OFF_REGEX = /(")*Signed-off-by:(.|\s)*/gim;
const COMMIT_TITLE_REGEX = /.*\n/m;
const HYPHEN_REGEX = /(-)+/gm;
const BACKTICK_REGEX = /`+/gm;
const SPACE_REGEX = / +/gm;
const COMMIT_TO_BE_REVIEWED_REGEX = /("#*\s*Commit\sto\sbe\sreviewed)/gim;
const WHITESPACES_HARDCODED_REGEX = /(\r\n|\n|\r|\\r|\\n)/gm;
const WHITESPACES_HARDCODED_REGEX = /(\r\n|\\r)/gm;
const NEWLINE_HARDCODED_REGEX = /\\n/gm;

const args = process.argv.slice(2);

// The following 2 lines should be commented to test this script locally
const pullReqUrl = args[0];
const ACCEPTABLE_SIMILARITY_RATIO = parseFloat(args[1]);

// These following 2 lines should be un-commented to test this script locally
// const pullReqUrl = "https://api.github.com/repos/hyperledger/cactus/pulls/3338";
// const ACCEPTABLE_SIMILARITY_RATIO = 0.9;

const prMetadata = await fetchJsonFromUrl(pullReqUrl);
const prBodyRaw = JSON.stringify(prMetadata.body);
Expand All @@ -29,33 +39,44 @@ commitMessagesMetadata.forEach((commitMessageMetadata) => {
.replace(HYPHEN_REGEX, "")
.replace(BACKTICK_REGEX, "")
.replace(WHITESPACES_HARDCODED_REGEX, "")
.replace(FIXES_OR_DEPENDS_REGEX, "")
.replace(SPACE_REGEX, ""),
.replace(FIXES_OR_DEPENDS_REGEX, ""),
);
});

let prBodyStriped = prBodyRaw
.replace(PULL_REQ_REQUIREMENTS_REGEX, "")
.replace(FIXES_OR_DEPENDS_REGEX, "")
.replace(WHITESPACES_HARDCODED_REGEX, "")
.replace(WHITESPACES_HARDCODED_REGEX, "\n")
.replace(SIGNED_OFF_REGEX, "")
.replace(HYPHEN_REGEX, "")
.replace(BACKTICK_REGEX, "")
.replace(COMMIT_TO_BE_REVIEWED_REGEX, "")
.replace(SPACE_REGEX, "");
.replace(NEWLINE_HARDCODED_REGEX, "");

let PR_BODY_IN_COMMIT_MESSAGES = false;
let PR_COMMIT_PARITY = false;
for (let commitMessageListIndex in commitMessageList) {
let commitMessage = commitMessageList[commitMessageListIndex];
if (commitMessage == prBodyStriped) PR_BODY_IN_COMMIT_MESSAGES = true;
if (
stringSimilarity(commitMessage, prBodyStriped) >=
ACCEPTABLE_SIMILARITY_RATIO ||
stringSimilarity(
commitMessage.replace(COMMIT_TITLE_REGEX, ""),
prBodyStriped,
) >= ACCEPTABLE_SIMILARITY_RATIO
)
PR_COMMIT_PARITY = true;
}

if (!PR_BODY_IN_COMMIT_MESSAGES) {
if (!PR_COMMIT_PARITY) {
console.error(
"PR Body does not match any existing commit message\n" +
"Please make sure that the PR Body matches to minimum one of the commit messages\n" +
"PR message and commit message are not similar. A general solution for this is to have PR message exactly same as the commit message\n" +
"Please refer the following PR for reference: https://github.com/hyperledger/cacti/pull/3338\n" +
"And the commit message here: https://github.com/hyperledger/cacti/pull/3338/commits/47ebdec442d30fa48c8518b876c47c38097cf028\n",
"-----------------------------------------------\n\n",
"Commit Message List (ignore extra white spaces and new lines)\n" +
commitMessageList +
"\n----------------------------------------------\nRelevant Pr Description (ignore extra white spaces and new lines)\n" +
prBodyStriped,
);
process.exit(-1);
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11103,6 +11103,7 @@ __metadata:
sort-package-json: "npm:1.57.0"
source-map-loader: "npm:4.0.1"
stream-browserify: "npm:3.0.0"
string-similarity-js: "npm:2.1.4"
tap: "npm:16.3.8"
tape: "npm:5.6.6"
tape-promise: "npm:4.0.0"
Expand Down Expand Up @@ -49340,6 +49341,13 @@ __metadata:
languageName: node
linkType: hard

"string-similarity-js@npm:2.1.4":
version: 2.1.4
resolution: "string-similarity-js@npm:2.1.4"
checksum: 10/55063198f233aca8833d8429626d9fafdeb164e0fc6a66364a4d8f509df17d49f8a8a42e4096da60c53fe4586b1bace7098c0265da590fe4ec96c3a8ecb90f69
languageName: node
linkType: hard

"string-similarity@npm:^4.0.1":
version: 4.0.4
resolution: "string-similarity@npm:4.0.4"
Expand Down
Loading