Skip to content

Commit 0269352

Browse files
committed
commitlint.config: add rule
Add default-revert-message rule.
1 parent db4c07b commit 0269352

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

commitlint.config.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ module.exports = {
5151
// disabled because most of the time it doesn't work, due to https://github.com/conventional-changelog/commitlint/issues/3404
5252
// and anyway we were using this rule only as a warning, not an error (because a scope is not required, e.g. when too broad)
5353
"type-empty": [RuleConfigSeverity.Disabled, "never"],
54+
"default-revert-message": [RuleConfigSeverity.Error, "never"],
5455
},
56+
57+
// Commitlint automatically ignores some kinds of commits like Revert commit messages.
58+
// We need to set this value to false to apply our rules on these messages.
59+
defaultIgnores: false,
5560
plugins: [
5661
// TODO (ideas for more rules):
57-
// * Detect reverts which have not been elaborated.
5862
// * Reject some stupid obvious words: change, update, modify (if first word after colon, error; otherwise warning).
5963
// * Think of how to reject this shitty commit message: https://github.com/nblockchain/NOnion/pull/34/commits/9ffcb373a1147ed1c729e8aca4ffd30467255594
6064
// * Workflow: detect if wip commit in a branch not named "wip/*" or whose name contains "squashed".
@@ -142,6 +146,30 @@ module.exports = {
142146
return Plugins.properIssueRefs(rawStr);
143147
},
144148

149+
"default-revert-message": (
150+
{
151+
header,
152+
body,
153+
}: {
154+
header: any;
155+
body: any;
156+
},
157+
when: string
158+
) => {
159+
Helpers.assertWhen(when);
160+
161+
let bodyStr = Helpers.convertAnyToString(body, "body");
162+
let headerStr = Helpers.assertNotNull(
163+
Helpers.convertAnyToString(header, "header"),
164+
notNullStringErrorMessage("header")
165+
);
166+
return Plugins.defaultRevertMessage(
167+
headerStr,
168+
bodyStr,
169+
when
170+
);
171+
},
172+
145173
"title-uppercase": ({ header }: { header: any }) => {
146174
let headerStr = Helpers.assertNotNull(
147175
Helpers.convertAnyToString(header, "header"),

commitlint/helpers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export abstract class Helpers {
3737
return text as string;
3838
}
3939

40+
public static assertWhen(when: string) {
41+
if (when !== "never" && when !== "always") {
42+
throw new Error(
43+
'Variable "when" should be either "never" or "always"'
44+
);
45+
}
46+
}
47+
4048
public static assertCharacter(letter: string) {
4149
if (letter.length !== 1) {
4250
throw Error("This function expects a character as input");

commitlint/plugins.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,44 @@ export abstract class Plugins {
278278
];
279279
}
280280

281+
public static defaultRevertMessage(
282+
headerStr: string,
283+
bodyStr: string | null,
284+
when: string
285+
) {
286+
let offence = false;
287+
let isRevertCommitMessage = headerStr.toLowerCase().includes("revert");
288+
289+
const negated = when === "never";
290+
291+
if (isRevertCommitMessage) {
292+
let isDefaultRevertHeader =
293+
headerStr.match(/^[Rr]evert ".+"$/) !== null;
294+
295+
if (isDefaultRevertHeader) {
296+
if (bodyStr !== null) {
297+
let lines = bodyStr.split("\n");
298+
offence =
299+
lines.length == 1 &&
300+
// 40 is the length of git commit hash.
301+
lines[0].match(/^This reverts commit [^ ]{40}\.$/) !==
302+
null;
303+
} else {
304+
offence = true;
305+
}
306+
}
307+
308+
offence = negated ? offence : !offence;
309+
}
310+
return [
311+
!offence,
312+
(negated
313+
? `Please explain why you're reverting.`
314+
: `Please don't change the default revert commit message.`) +
315+
Helpers.errMessageSuffix,
316+
];
317+
}
318+
281319
public static titleUppercase(headerStr: string) {
282320
let firstWord = headerStr.split(" ")[0];
283321
let offence =

0 commit comments

Comments
 (0)