From b64ac67d2fe50e01bf83f9b0d911a2937e16d1ea Mon Sep 17 00:00:00 2001 From: jagpreetsinghsasan Date: Mon, 16 Sep 2024 06:08:44 +0000 Subject: [PATCH] ci(github): commit parity to use PR title in check Primary Changes --------------- 1. Updated the script to include the check for releases 2. Fixed certain regex and added a new regex for issue/PR references. This is done because the issue numbers tagged in PR message or commit messages are sometime resolved directly and sometimes parsed with the orgname. 3. With the new regex in 2), we can now safely check for parity while including the fixes/depends line, further loosing the parity check, thus reducing false-positives Fixes #3526 Signed-off-by: jagpreetsinghsasan --- tools/pr-commit-parity.js | 42 +++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/tools/pr-commit-parity.js b/tools/pr-commit-parity.js index 0d073d9b9b..a6b6252019 100644 --- a/tools/pr-commit-parity.js +++ b/tools/pr-commit-parity.js @@ -51,12 +51,12 @@ export async function fetchJsonFromUrl(url) { // 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 COMMIT_TITLE_REGEX = /^.*$/m; const HYPHEN_REGEX = /(-)+/gm; const BACKTICK_REGEX = /`+/gm; const COMMIT_TO_BE_REVIEWED_REGEX = /("#*\s*Commit\sto\sbe\sreviewed)/gim; +const HYPERLEDGER_REFERENCE_REGEX = /hyperledger#/gm; const WHITESPACES_HARDCODED_REGEX = /(\r\n|\\r)/gm; const NEWLINE_HARDCODED_REGEX = /\\n/gm; @@ -82,31 +82,53 @@ commitMessagesMetadata.forEach((commitMessageMetadata) => { .replace(SIGNED_OFF_REGEX, "") .replace(HYPHEN_REGEX, "") .replace(BACKTICK_REGEX, "") + .replace(HYPERLEDGER_REFERENCE_REGEX, "#") .replace(WHITESPACES_HARDCODED_REGEX, "") - .replace(FIXES_OR_DEPENDS_REGEX, ""), + .trim(), ); }); let prBodyStriped = prBodyRaw .replace(PULL_REQ_REQUIREMENTS_REGEX, "") - .replace(FIXES_OR_DEPENDS_REGEX, "") .replace(WHITESPACES_HARDCODED_REGEX, "\n") .replace(SIGNED_OFF_REGEX, "") .replace(HYPHEN_REGEX, "") .replace(BACKTICK_REGEX, "") + .replace(HYPERLEDGER_REFERENCE_REGEX, "#") .replace(COMMIT_TO_BE_REVIEWED_REGEX, "") - .replace(NEWLINE_HARDCODED_REGEX, ""); + .replace(NEWLINE_HARDCODED_REGEX, "") + .trim(); let PR_COMMIT_PARITY = false; for (let commitMessageListIndex in commitMessageList) { let commitMessage = commitMessageList[commitMessageListIndex]; + let commitMessageSubject = commitMessage.match(COMMIT_TITLE_REGEX)[0]; + /* + * This condition checks for (A && (B || C)) is true, where, + * A) pr title is similar to the commit subject + * B) pr body is similar to the entire commit message + * C) pr body is similar to the commit message excluding commit subject + */ if ( - stringSimilarity(commitMessage, prBodyStriped) >= + stringSimilarity(commitMessageSubject, prMetadata.title) >= + ACCEPTABLE_SIMILARITY_RATIO && + (stringSimilarity(commitMessage, prBodyStriped) >= ACCEPTABLE_SIMILARITY_RATIO || - stringSimilarity( - commitMessage.replace(COMMIT_TITLE_REGEX, ""), - prBodyStriped, - ) >= ACCEPTABLE_SIMILARITY_RATIO + stringSimilarity( + commitMessage.replace(COMMIT_TITLE_REGEX, ""), + prBodyStriped, + ) >= ACCEPTABLE_SIMILARITY_RATIO) + ) + PR_COMMIT_PARITY = true; + /* + * This condition checks for (A && B) is true, where, + * A) pr title is similar to the commit subject + * B) pr body is empty (in case of releases) + */ + if ( + stringSimilarity(commitMessageSubject, prMetadata.title) >= + ACCEPTABLE_SIMILARITY_RATIO && + prBodyStriped === "" ) PR_COMMIT_PARITY = true; }