commitlint icon indicating copy to clipboard operation
commitlint copied to clipboard

cz commitlint CLI question customization for custom rules.

Open gsmith-mythical opened this issue 3 years ago • 8 comments

I would like to add the ability to add a CLI prompt for a custom rule. (e.g. ticket number included in commit message)

Expected Behavior

Would like for a way to add new custom option to add ticket number to the CLI prompt. feat(button): added theme settings [TICKET-123] commitlint.config.js

module.exports = {
    extends: ['@commitlint/config-conventional'],
    plugins: [
        {
          rules: {
            ticket: ({ subject }) => {
              const ticketRegex = /(\w+-{1}\d+)/;
              return [
                ticketRegex.test(subject),
                "Your subject should include the ticket, for example PRJ-300.",
              ];
            },
          },
        },
      ],
    rules: {
      ticket: [2, "always"],
    },
    parserPreset: './parser-presets.js',
    prompt: {
        questions: {
          // ...
          subject: {
            description: 'Write a short, imperative tense description of the change',
          },
          ticket: {
            description: 'Provide a Ticket Number',
          },
          body: {
            description: 'Provide a longer description of the change',
          },
          // ...
        },
      }
}


parser-preset.js

module.exports = {
    parserOpts: {
      headerPattern: /^(\w+)\(\w+\):[ ]{1}(\w{1}.+\w{1})[ ]{1}\[(\w+-{1}\d+)\]$/,
      headerCorrespondence: ['type', 'scope', 'subject', 'ticket'],
    },
  };

Current Behavior

Currently ignores my new rule / custom CLI prompt. I can't find any documentation that directly walks through this process of making a custom prompt with a custom rule. You can override current rules and change language. But unable to have question use new custom made rules. If this isn't something that can be / is supported that's perfectly fine! I'm just curious if it's currently possible, and if it is where I may be tripping up.

Affected packages

  • [X] cli
  • [X] cz-commitlint
Executable Version
commitlint --version 12.1.4
git --version 2.32.0
node --version 14.17.1

gsmith-mythical avatar Jul 21 '21 22:07 gsmith-mythical

I had the same issue. With the help of this comment I was able to fix it by adding a rule for each header correspondence. Otherwise it was just being ignored.

So I ended up with something similar to this:

rules: {
  "type-empty": [2, "never"],
  "scope-empty": [2, "never"],
  "subject-empty": [2, "never"],
  "ticket": [2, "always"],
}

Hope it helps!

chrwlk avatar Oct 13 '21 09:10 chrwlk

Could you share full commitlint.config.js ? Thanks!

antonpatsev avatar Jun 08 '22 14:06 antonpatsev

I searched and found this nice tutorial. I believe this may work with some tweaks

@antonpatsev @gsmith-mythical

tomavic avatar Jun 18 '22 08:06 tomavic

I searched and found this nice tutorial

Which tutorial?

escapedcat avatar Jun 18 '22 09:06 escapedcat

@escapedcat 😂 I forgot to paste it . Here it is

I find it too much configurations which didn't work for me

The second one here seems a bit interesting as it uses emojis too with no problems.

tomavic avatar Jun 18 '22 09:06 tomavic

I also added a pre-comit-msg hook which was not mentioned in the above tutorial but was mentioned in other tutuorials. The hook itself has some issues if it's used in a wrong way. But luckily it works for me now.

"husky": "^8.0.0",
"@commitlint/cli": "^17.0.2",
"commitizen": "^4.2.4",
"commitlint-config-gitmoji": "^2.2.5",
"cz-customizable": "^6.3.0",

My prepare-commit-msg hook

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

exec < /dev/tty && node_modules/.bin/cz --hook || true

🛠⚒ WARNING 🛠⚒

This will work just fine on linux based systems. For Windows there will be a hateful behaviour you will experience. for more information please check https://github.com/commitizen/cz-cli/issues/627#issuecomment-530609506 Here I had to include node_modules/.bin/cz instead of npx cz. I didn't like the idea of adding extra npm script like this

"cm": "cz"
// or
"commit": "cz"

My pre-commit hook

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"

My cz-config.js

module.exports = {
  types: [
    { value: ':sparkles: feat', name: '✨ feat:\tAdding a new feature' },
    { value: ':bug: fix', name: '🐛 fix:\tFixing a bug' },
    { value: ':memo: docs', name: '📝 docs:\tAdd or update documentation' },
    {
      value: ':lipstick: style',
      name: '💄 style:\tAdd or update styles, ui or ux',
    },
    {
      value: ':recycle: refactor',
      name: '♻️  refactor:\tCode change that neither fixes a bug nor adds a feature',
    },
    {
      value: ':zap: perf',
      name: '⚡️ perf:\tCode change that improves performance',
    },
    {
      value: ':white_check_mark: test',
      name: '✅ test:\tAdding tests cases',
    },
    {
      value: ':truck: chore',
      name: '🚚 chore:\tChanges to the build process or auxiliary tools\n\t\tand libraries such as documentation generation',
    },
    { value: ':rewind: revert', name: '⏪️ revert:\tRevert to a commit' },
    { value: ':construction: wip', name: '🚧 wip:\tWork in progress' },
    {
      value: ':construction_worker: build',
      name: '👷 build:\tAdd or update regards to build process',
    },
    {
      value: ':green_heart: ci',
      name: '💚 ci:\tAdd or update regards to build process',
    },
  ],

  scopes: [
    { name: 'ui' },
    { name: 'android' },
    { name: 'ios' },
    { name: 'home' },
    { name: 'planner' },
    { name: 'settings' },
  ],

  scopeOverrides: {
    fix: [{ name: 'merge' }, { name: 'style' }, { name: 'test' }, { name: 'hotfix' }],
  },

  allowCustomScopes: true,
  allowBreakingChanges: ['feat', 'fix'],
  // skip any questions you want
  skipQuestions: ['body'],
  subjectLimit: 100,
};

My commitlint.config.js

module.exports = {
  extends: ['gitmoji'],
  rules: {
    'header-max-length': [0, 'always', 100],
  },
};

My Package.json config part

  "config": {
    "commitizen": {
      "path": "cz-customizable"
    }
  },

tomavic avatar Jun 18 '22 10:06 tomavic

I wanna make something similar like this feat: TEAMCVSPB-(any number) - (message). I created regexp like this /^(\w*): (TEAMCVSPB-\d+) ([a-zA-Z])+$/g, but it can't parse it I got this in my log

{
  type: null,
  ticket: null,
  subject: null,
  merge: null,
  header: 'test: TEAMCVSPB-6972 - test',
  body: null,
  footer: null,
  notes: [],
  references: [],
  mentions: [],
  revert: null,
  raw: 'test: TEAMCVSPB-6972 - test\n\n'
} 

my config looks like this

  parserPreset: {
    parserOpts: {
      headerPattern: /^(\w*): (TEAMCVSPB-\d+) ([a-zA-Z])+$/g,
      headerCorrespondence: ["type", "ticket", "subject"],
    },
  },
  rules: {
    "type-empty": [2, "never"],
    "ticket": [2, "always"],
    "subject-empty": [2, "never"],
  },

What I'm doing wrong guys please help me ? @chrwlk @tomavic @gsmith-mythical

stevenKirill avatar Dec 15 '23 12:12 stevenKirill

I'm not able to have a prompt for a custom rule. (e.g. ticket number included in commit message).

commitlint-cli: 18.4.3 git --version 2.39.3 node --version 18

webmatrixxxl avatar Apr 18 '24 12:04 webmatrixxxl