Skip to content

Commit

Permalink
ci(github): commit parity to use PR title in check
Browse files Browse the repository at this point in the history
    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 <jagpreetsinghsasan@accenture.com>
  • Loading branch information
jagpreetsinghsasan committed Sep 16, 2024
1 parent 3172fc6 commit b4f43ad
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions tools/pr-commit-parity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down

0 comments on commit b4f43ad

Please sign in to comment.