3

I am using Husky to set my git hooks, and am trying to change to default format of the header expected by Commitlint:

type(scope?): subject

I am specifically trying to have this formatting:

:gitmoji:? [scope] subject

With :gitmoji: one of Gitmoji's emoji and being optional, with square brackets around the scope (and not optional) instead of the parentheses, and without the : to separate the type + the scope from the subject. Also I'd like the scope to have a formatting kind of like TCKT-666 (to refer a Jira's ticket, for example),

Right now, I've been trying a lot of things using the parserPreset, parserOpts, headerPattern and headerCorrespondence properties from commitlint.config.js, but I encountered several issues:

  • the headerPattern regex seems to be totally ignored, and all the errors I get only come from the rules I set within commitlint.config.js - so I cannot set a specific formatting for my scope (although commitlint-plugin-function-rules might help with that)
  • I have absolutely no idea how to remove the need for the : after the type, or how to replace parentheses by square brackets around the scope
Thanh-Quy Nguyen
  • 2,995
  • 7
  • 30
  • 46

1 Answers1

2

This should work for :gitmoji:? [scope] subject

module.exports = {
  parserPreset: {
    parserOpts: {
      headerPattern: /^(?:(:\w+:)\s)?\[(\w+)\] (.+)/,
      headerCorrespondence: ["type", "scope", "subject"],
    },
  },
  plugins: [
    {
      rules: {
        "header-match-team-pattern": (parsed) => {
          const { type, scope, subject } = parsed;
          if (type === null && scope === null && subject === null) {
            return [
              false,
              "header must be in format ':gitmoji:? [scope] subject'",
            ];
          }
          return [true, ""];
        },
        "gitmoji-type-enum": (parsed, _when, expectedValue) => {
          const { type } = parsed;
          if (type && !expectedValue.includes(type)) {
            return [
              false,
              `type must be one of ${expectedValue}
    see https://gitmoji.dev`,
            ];
          }
          return [true, ""];
        },
      },
    },
  ],
  rules: {
    // "type-empty": [2, "never"],
    "header-match-team-pattern": [2, "always"],
    "gitmoji-type-enum": [2, "always", [":bug:", ":sparkle:"]], // custom rule defined in plugins
    // "subject-case": [2, "always", "sentence-case"],
  },
};

Looks like it's required to have a custom rule like header-match-team-pattern that makes sure that RegExp matched.

strdr4605
  • 3,796
  • 2
  • 16
  • 26
  • 1
    Thank you! I'll try that as soon as I'm done with my current task ^^ – Thanh-Quy Nguyen Jan 06 '22 at 08:04
  • 1
    I haven't been able to use the `explained-type-enum` rule, and I don't find any corresponding rule or plugin in https://commitlint.js.org/#/reference-rules or on npm. Is there a package you used to make that work? – Thanh-Quy Nguyen Jan 06 '22 at 18:04
  • 1
    I assume the custom rule `explained-type-enum` is the one that should be definable in the plugins? (I didn't find anything related to that in the docs) Also I tried several things such as `git commit -m ":yahahalalala: [ Something else entirely"` and `git commit -m "[ Something else entirely"` and they were not blocked by commitlint for some reason... – Thanh-Quy Nguyen Jan 06 '22 at 18:42
  • After a little research, I found that your solution may not work because of an issue with headerPattern that is yet to be resolved: https://github.com/conventional-changelog/commitlint/issues/607 – Thanh-Quy Nguyen Jan 07 '22 at 09:06
  • 2
    I updated the answer, see if it's working for your use cases! – strdr4605 Jan 10 '22 at 07:49
  • 2
    It's totally working now, thanks a lot! – Thanh-Quy Nguyen Jan 16 '22 at 16:55
  • Does it support typescript? Can you include kind of JIRA tickets format? – SalahAdDin Jun 30 '22 at 22:44