Skip to content

Commit

Permalink
commitlint.config: add default-revert-message rule
Browse files Browse the repository at this point in the history
  • Loading branch information
tehraninasab committed Feb 13, 2023
1 parent ac427b8 commit e9a1695
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
31 changes: 25 additions & 6 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ module.exports = {
"too-many-spaces": [RuleConfigSeverity.Error, "always"],
"commit-hash-alone": [RuleConfigSeverity.Error, "always"],
"title-uppercase": [RuleConfigSeverity.Error, "always"],
"default-revert-message": [RuleConfigSeverity.Error, "never"],
},

// 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).
// * Detect reverts which have not been elaborated.
// * Reject some stupid obvious words: change, update, modify (if first word after colon, error; otherwise warning).
// * Think of how to reject this shitty commit message: https://github.com/nblockchain/NOnion/pull/34/commits/9ffcb373a1147ed1c729e8aca4ffd30467255594
// * Title should not have dot at the end.
Expand All @@ -59,7 +63,7 @@ module.exports = {
{
rules: {
"body-prose": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
let rawStr = Helpers.convertAnyToString(raw, "raw");
return Plugins.bodyProse(rawStr);
},

Expand Down Expand Up @@ -92,12 +96,12 @@ module.exports = {
},

"footer-notes-misplacement": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
let rawStr = Helpers.convertAnyToString(raw, "raw");
return Plugins.footerNotesMisplacement(rawStr);
},

"footer-references-existence": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
let rawStr = Helpers.convertAnyToString(raw, "raw");
return Plugins.footerReferencesExistence(rawStr);
},

Expand All @@ -114,10 +118,25 @@ module.exports = {
},

"proper-issue-refs": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
let rawStr = Helpers.convertAnyToString(raw, "raw");
return Plugins.properIssueRefs(rawStr);
},

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

"title-uppercase": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
header,
Expand Down Expand Up @@ -169,7 +188,7 @@ module.exports = {
_: any,
maxLineLength: number
) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
let rawStr = Helpers.convertAnyToString(raw, "raw");
return Plugins.bodySoftMaxLineLength(rawStr, maxLineLength);
},

Expand Down
10 changes: 2 additions & 8 deletions commitlint/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ export abstract class Helpers {
public static convertAnyToString(
potentialString: any,
paramName: string
): string {
): any {
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 null;
}
return String(potentialString);
}
Expand Down
22 changes: 22 additions & 0 deletions commitlint/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,28 @@ export abstract class Plugins {
];
}

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

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

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

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

0 comments on commit e9a1695

Please sign in to comment.