2023-02-21 02:56:09 -05:00
|
|
|
/**
|
2023-03-08 05:00:04 -05:00
|
|
|
* Check if commit messages are sufficiently descriptive (not too short).
|
2023-02-21 02:56:09 -05:00
|
|
|
*
|
2023-03-08 05:00:04 -05:00
|
|
|
* Search for commit messages that appear to be automatically generated by Gitlab or temporary messages and report them.
|
2023-02-21 02:56:09 -05:00
|
|
|
*
|
|
|
|
* @dangerjs WARN
|
|
|
|
*/
|
2023-03-08 05:00:04 -05:00
|
|
|
|
2023-03-30 05:16:39 -04:00
|
|
|
module.exports = async function () {
|
2023-03-08 05:00:04 -05:00
|
|
|
const mrCommits = danger.gitlab.commits;
|
|
|
|
|
|
|
|
const detectRegexes = [
|
|
|
|
/^Rebased.*onto.*/i, // Automatically generated message by Gitlab
|
|
|
|
/^Fast-forwarded.*to.*/i, // Automatically generated message by Gitlab
|
|
|
|
/^Applied suggestion to.*/i, // Automatically generated message by Gitlab
|
|
|
|
/^Suggestion applied for line.*/i, // Automatically generated message by Gitlab
|
|
|
|
/^Opened merge request/i, // Automatically generated message by Gitlab
|
|
|
|
/^Merged.*/i, // Automatically generated message by Gitlab
|
|
|
|
/^Opened issue #\d+:.*/i, // Automatically generated message by Gitlab
|
|
|
|
/^Closed issue #\d+:.*/i, // Automatically generated message by Gitlab
|
|
|
|
/^Tagged.*as.*/i, // Automatically generated message by Gitlab
|
|
|
|
/^Deleted tag.*/i, // Automatically generated message by Gitlab
|
|
|
|
/^WIP.*/i, // Message starts with prefix "WIP"
|
|
|
|
/^Cleaned.*/i, // Message starts "Cleaned"
|
|
|
|
/clean ?up/i, // Message contains "clean up"
|
|
|
|
/^[^A-Za-z0-9\s].*/, // Message starts with special characters
|
|
|
|
];
|
2023-02-21 02:56:09 -05:00
|
|
|
|
2023-03-08 05:00:04 -05:00
|
|
|
// Search for the messages in each commit
|
|
|
|
let partMessages = [];
|
|
|
|
for (const commit of mrCommits) {
|
|
|
|
const commitMessage = commit.message;
|
|
|
|
const commitMessageTitle = commit.title;
|
|
|
|
|
|
|
|
// Check if the commit message contains any Jira ticket references
|
|
|
|
const jiraTicketRegex = /[A-Z0-9]+-[0-9]+/g;
|
|
|
|
const jiraTicketMatches = commitMessage.match(jiraTicketRegex);
|
|
|
|
if (jiraTicketMatches) {
|
|
|
|
const jiraTicketNames = jiraTicketMatches.join(", ");
|
|
|
|
partMessages.push(
|
|
|
|
`- the commit message \`${commitMessageTitle}\` probably contains Jira ticket reference (\`${jiraTicketNames}\`). Please remove Jira tickets from commit messages.`
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-21 02:56:09 -05:00
|
|
|
|
2023-03-08 05:00:04 -05:00
|
|
|
// Check if the commit message matches any regex from "detectRegexes"
|
|
|
|
if (detectRegexes.some((regex) => commitMessage.match(regex))) {
|
|
|
|
partMessages.push(
|
|
|
|
`- the commit message \`${commitMessageTitle}\` appears to be a temporary or automatically generated message`
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the commit message is not too short
|
|
|
|
const shortCommitMessageThreshold = 20; // commit message is considered too short below this number of characters
|
2023-02-21 02:56:09 -05:00
|
|
|
if (commitMessage.length < shortCommitMessageThreshold) {
|
2023-03-08 05:00:04 -05:00
|
|
|
partMessages.push(
|
|
|
|
`- the commit message \`${commitMessageTitle}\` may not be sufficiently descriptive`
|
|
|
|
);
|
2023-02-21 02:56:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 05:16:39 -04:00
|
|
|
// Create report
|
|
|
|
if (partMessages.length) {
|
|
|
|
partMessages.sort();
|
|
|
|
let dangerMessage = `\nSome issues found for the commit messages in this MR:\n${partMessages.join(
|
|
|
|
"\n"
|
|
|
|
)}
|
2023-03-08 05:00:04 -05:00
|
|
|
\nPlease consider updating these commit messages. It is recommended to follow this [commit messages guide](https://gitlab.espressif.cn:6688/espressif/esp-idf/-/wikis/dev-proc/Commit-messages)`;
|
2023-03-30 05:16:39 -04:00
|
|
|
|
|
|
|
// Create AI generated suggestion for git commit message based of gitDiff and current commit messages
|
|
|
|
const AImessageSuggestion =
|
|
|
|
await require("./aiGenerateGitMessage.js")();
|
|
|
|
dangerMessage += AImessageSuggestion;
|
|
|
|
|
|
|
|
warn(dangerMessage);
|
|
|
|
}
|
2023-02-21 02:56:09 -05:00
|
|
|
};
|