Skip to content

Commit fc8a9b3

Browse files
committed
commitlint.config: add rule
Add default-revert-message rule.
1 parent 853e9c5 commit fc8a9b3

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

commitlint.config.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ module.exports = {
5050
// disabled because most of the time it doesn't work, due to https://github.com/conventional-changelog/commitlint/issues/3404
5151
// 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)
5252
"type-empty": [RuleConfigSeverity.Disabled, "never"],
53+
"default-revert-message": [RuleConfigSeverity.Error, "never"],
5354
},
55+
56+
// Commitlint automatically ignores some kinds of commits like Revert commit messages.
57+
// We need to set this value to false to apply our rules on these messages.
58+
defaultIgnores: false,
5459
plugins: [
5560
// TODO (ideas for more rules):
5661
// * 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).
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".
@@ -139,6 +143,32 @@ module.exports = {
139143
return Plugins.properIssueRefs(rawStr);
140144
},
141145

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

commitlint/plugins.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ test("default-revert-message1", () => {
659659
let defaultRevertMessage1 = runCommitLintOnMsg(
660660
commitMsgWithoutDefaultRevertMessage
661661
);
662+
662663
expect(defaultRevertMessage1.status).not.toBe(0);
663664
});
664665

@@ -670,6 +671,7 @@ test("default-revert-message2", () => {
670671
let defaultRevertMessage2 = runCommitLintOnMsg(
671672
commitMsgWithDefaultRevertMessage
672673
);
674+
673675
expect(defaultRevertMessage2.status).toBe(0);
674676
});
675677

commitlint/plugins.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,44 @@ export abstract class Plugins {
262262
];
263263
}
264264

265+
public static defaultRevertMessage(
266+
headerStr: string,
267+
bodyStr: string | null,
268+
when = "never"
269+
) {
270+
let offence = false;
271+
let isRevertCommitMessage = headerStr.toLowerCase().includes("revert");
272+
console.log("----");
273+
console.log("headerStr: " + headerStr);
274+
console.log("bodyStr:" + bodyStr);
275+
console.log("----");
276+
if (isRevertCommitMessage) {
277+
let isDefaultRevertHeader =
278+
headerStr.match(/^[Rr]evert ".+"$/) !== null;
279+
280+
if (isDefaultRevertHeader) {
281+
if (bodyStr !== null) {
282+
let lines = bodyStr.split("\n");
283+
offence =
284+
// 40 is the length of git commit hash in the following regex pattern.
285+
lines.length == 1 &&
286+
lines[0].match(/^This reverts commit [^ ]{40}\.$/) !==
287+
null;
288+
} else {
289+
offence = true;
290+
}
291+
}
292+
293+
const negated = when === "never";
294+
offence = negated ? offence : !offence;
295+
}
296+
console.log("offence:" + offence);
297+
return [
298+
!offence,
299+
`Please explain why you're reverting` + Helpers.errMessageSuffix,
300+
];
301+
}
302+
265303
public static titleUppercase(headerStr: string) {
266304
let firstWord = headerStr.split(" ")[0];
267305
let offence =

0 commit comments

Comments
 (0)