From 7c678a2b3ccc2bf131edb1816989bffc75c42e54 Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 20 Oct 2022 16:58:06 +0330 Subject: [PATCH 1/4] commitlint/plugins.test: add failing tests Add tests for reject-obvious-words rule. --- commitlint/plugins.test.ts | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/commitlint/plugins.test.ts b/commitlint/plugins.test.ts index 15af4d52..74d10a31 100644 --- a/commitlint/plugins.test.ts +++ b/commitlint/plugins.test.ts @@ -846,6 +846,68 @@ test("proper-issue-refs5", () => { expect(properIssueRefs5.status).not.toBe(0); }); +test("reject-obvious-words1", () => { + let commitMsgWithObviousWordAfterColon = "foo: change"; + let rejectObviousWords1 = runCommitLintOnMsg( + commitMsgWithObviousWordAfterColon + ); + expect(rejectObviousWords1.status).not.toBe(0); +}); + +test("reject-obvious-words2", () => { + let commitMsgWithObviousWordAfterColon = "foo: change bla bla"; + let rejectObviousWords2 = runCommitLintOnMsg( + commitMsgWithObviousWordAfterColon + ); + expect(rejectObviousWords2.status).not.toBe(0); +}); + +test("reject-obvious-words3", () => { + let commitMsgWithObviousWordAfterColon = "foo: change bla bla\n\nBla blah."; + let rejectObviousWords3 = runCommitLintOnMsg( + commitMsgWithObviousWordAfterColon + ); + expect(rejectObviousWords3.status).not.toBe(0); +}); + +test("reject-obvious-words4", () => { + let commitMsgWithoutObviousWordAfterColon = "foo: bla bla bla"; + let rejectObviousWords4 = runCommitLintOnMsg( + commitMsgWithoutObviousWordAfterColon + ); + expect(rejectObviousWords4.status).toBe(0); +}); + +test("reject-obvious-words5", () => { + // In general using `update` in commit message is a bad practice but there is + // exception in cases that the area refers to a file which is only modified when + // it needs updating and there's no need for further explanation of why it needs + // updating such as: + // README: update or Backend/servers.json: update + + let commitMsgWithUpdateWordAfterColon = "foo: update"; + let rejectObviousWords5 = runCommitLintOnMsg( + commitMsgWithUpdateWordAfterColon + ); + expect(rejectObviousWords5.status).toBe(0); +}); + +test("reject-obvious-words6", () => { + let commitMsgWithUpdateWordAfterColon = "foo: update bla bla"; + let rejectObviousWords6 = runCommitLintOnMsg( + commitMsgWithUpdateWordAfterColon + ); + expect(rejectObviousWords6.status).not.toBe(0); +}); + +test("reject-obvious-words7", () => { + let commitMsgWithUpdateWordAfterColon = "foo: update bla bla\n\nBla blah."; + let rejectObviousWords7 = runCommitLintOnMsg( + commitMsgWithUpdateWordAfterColon + ); + expect(rejectObviousWords7.status).toBe(0); +}); + test("subject-lowercase1", () => { let commitMsgWithUppercaseAfterColon = "foo: Bar baz"; let subjectLowerCase1 = runCommitLintOnMsg( From c8a9391b3d388d081ddfe4249bca2ffef1262c7a Mon Sep 17 00:00:00 2001 From: realmarv Date: Tue, 14 Feb 2023 13:17:49 +0330 Subject: [PATCH 2/4] commitlint: add new rule Add a rule to reject obvious words in the commit title. --- commitlint.config.ts | 16 ++++++++++++++++ commitlint/plugins.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/commitlint.config.ts b/commitlint.config.ts index a708f127..7e413fb4 100644 --- a/commitlint.config.ts +++ b/commitlint.config.ts @@ -51,6 +51,7 @@ module.exports = { // disabled because most of the time it doesn't work, due to https://github.com/conventional-changelog/commitlint/issues/3404 // 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) "type-empty": [RuleConfigSeverity.Disabled, "never"], + "reject-obvious-words": [RuleConfigSeverity.Error, "always"], }, plugins: [ // TODO (ideas for more rules): @@ -81,6 +82,21 @@ module.exports = { return Plugins.commitHashAlone(rawStr); }, + "reject-obvious-words": ({ + header, + body, + }: { + header: any; + body: any; + }) => { + let headerStr = Helpers.convertAnyToString( + header, + "header" + ); + let bodyStr = Helpers.convertAnyToString(body, "header"); + return Plugins.rejectObviousWords(headerStr, bodyStr); + }, + "empty-wip": ({ header }: { header: any }) => { let headerStr = Helpers.assertNotNull( Helpers.convertAnyToString(header, "header"), diff --git a/commitlint/plugins.ts b/commitlint/plugins.ts index 03a47233..46610e27 100644 --- a/commitlint/plugins.ts +++ b/commitlint/plugins.ts @@ -1,5 +1,6 @@ import { abbr } from "./abbreviations"; import { Helpers } from "./helpers"; +const obviousWords = ["change", "update", "modify"]; export abstract class Plugins { public static bodyProse(rawStr: string) { @@ -278,6 +279,35 @@ export abstract class Plugins { ]; } + public static rejectObviousWords( + headerStr: string | null, + bodyStr: string | null + ) { + let offence = false; + let firstWordInTitle = ""; + + if (headerStr !== null) { + let colonFirstIndex = headerStr.indexOf(":"); + let titleStartIndex = Math.max(0, colonFirstIndex + 1); + let title = headerStr + .substring(titleStartIndex, headerStr.length) + .trim(); + let titleWords = title.split(" "); + firstWordInTitle = titleWords[0]; + + if (firstWordInTitle === "update") { + offence = titleWords.length > 1 && bodyStr === null; + } else { + offence = obviousWords.includes(firstWordInTitle); + } + } + + return [ + !offence, + `Please don't use obvious words such as ${firstWordInTitle} in the commit title`, + ]; + } + public static titleUppercase(headerStr: string) { let firstWord = headerStr.split(" ")[0]; let offence = From a5aedf5490bcef7bf5278e9125f65e2b075bd091 Mon Sep 17 00:00:00 2001 From: realmarv Date: Sun, 26 Mar 2023 11:34:22 +0330 Subject: [PATCH 3/4] commitlint/plugins.test: add failing test --- commitlint/plugins.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/commitlint/plugins.test.ts b/commitlint/plugins.test.ts index 74d10a31..3b7b6b06 100644 --- a/commitlint/plugins.test.ts +++ b/commitlint/plugins.test.ts @@ -908,6 +908,15 @@ test("reject-obvious-words7", () => { expect(rejectObviousWords7.status).toBe(0); }); +test("reject-obvious-words8", () => { + let commitMsgWithoutObviousWordAfterColon = + "scripts/detectNotUsingGitPush1by1.fsx: fix"; + let rejectObviousWords8 = runCommitLintOnMsg( + commitMsgWithoutObviousWordAfterColon + ); + expect(rejectObviousWords8.status).not.toBe(0); +}); + test("subject-lowercase1", () => { let commitMsgWithUppercaseAfterColon = "foo: Bar baz"; let subjectLowerCase1 = runCommitLintOnMsg( From c9eb3a4e5e72919ae648560ae735c4c623f6158b Mon Sep 17 00:00:00 2001 From: realmarv Date: Sun, 26 Mar 2023 11:54:36 +0330 Subject: [PATCH 4/4] commitlint/plugins: fix the rule If the subject contains just the word "fix", and there's no commit msg body, this rule should complain. --- commitlint/plugins.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/commitlint/plugins.ts b/commitlint/plugins.ts index 46610e27..c13fd1e8 100644 --- a/commitlint/plugins.ts +++ b/commitlint/plugins.ts @@ -297,6 +297,8 @@ export abstract class Plugins { if (firstWordInTitle === "update") { offence = titleWords.length > 1 && bodyStr === null; + } else if (firstWordInTitle === "fix") { + offence = bodyStr === null; } else { offence = obviousWords.includes(firstWordInTitle); }