Skip to content

Commit

Permalink
commitlint.config: add proper-revert-message rule
Browse files Browse the repository at this point in the history
  • Loading branch information
tehraninasab committed Feb 8, 2023
1 parent 6e95392 commit cfe2d56
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
23 changes: 23 additions & 0 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ module.exports = {
"too-many-spaces": [RuleConfigSeverity.Error, "always"],
"commit-hash-alone": [RuleConfigSeverity.Error, "always"],
"title-uppercase": [RuleConfigSeverity.Error, "always"],
"proper-revert-message": [RuleConfigSeverity.Error, "always"],
},

// Commitlint automatically ignores some kinds of commits like Revert commit messages.
// We need to set this value to false to apply our rules on these messages.
defaultIgnores: false,
plugins: [
// TODO (ideas for more rules):
// * Detect if paragraphs in body have been cropped too shortly (less than 64 chars), and suggest same auto-wrap command that body-soft-max-line-length suggests, since it unwraps and wraps (both).
Expand Down Expand Up @@ -118,6 +123,24 @@ module.exports = {
return Plugins.properIssueRefs(rawStr);
},

"proper-revert-message": ({
header,
body,
}: {
header: any;
body: any;
}) => {
let bodyStr = Helpers.convertAnyToString(
body,
"body"
).trim();
let headerStr = Helpers.convertAnyToString(
header,
"header"
).trim();
return Plugins.properRevertMessage(headerStr, bodyStr);
},

"title-uppercase": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
header,
Expand Down
8 changes: 1 addition & 7 deletions commitlint/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ export abstract class Helpers {
): string {
if (potentialString === null || potentialString === undefined) {
// otherwise, String(null) might give us the stupid string "null"
throw new Error(
"Unexpected " +
paramName +
"===null or " +
paramName +
"===undefined happened"
);
return "";
}
return String(potentialString);
}
Expand Down
18 changes: 18 additions & 0 deletions commitlint/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,24 @@ export abstract class Plugins {
];
}

public static properRevertMessage(headerStr: string, bodyStr: string) {
let offence = false;

if (headerStr.match(/^[Rr]evert ".+"$/) !== null) {
// does msg have a body?
if (bodyStr !== "") {
let lines = bodyStr.split("\n");
offence =
lines[0].match(/^This reverts commit [^ ]{40}\.$/) !==
null && lines.length == 1;
} else {
offence = true;
}
}

return [!offence, `Please explain why you're reverting`];
}

public static titleUppercase(headerStr: string) {
let firstWord = headerStr.split(" ")[0];
let offence =
Expand Down

0 comments on commit cfe2d56

Please sign in to comment.