conventional-changelog / commitlint

📓 Lint commit messages
https://commitlint.js.org
MIT License
16.65k stars 892 forks source link

Ability append / prepend a prefix or a dynamic text on commit message #2020

Closed TanKucukhas closed 4 years ago

TanKucukhas commented 4 years ago

I have multiple projects with different Jira numbers. So in my project, I only want the user to commit a message following @commitlint/config-conventional rules.

I could not find anything on documentation that can help my case, is this possible using the setup below?

Expected Behavior

fix: bla bla bla should pass but actual commit should be[ABCD-10] fix: bla bla bla bla bla bla should fail [ABCD-10] bla bla bla should fail [ABCD-10 fix: bla bla bla should fail

Current Behavior

fix: bla bla bla passes but I'm unable to append my prefix before the commit pushes

Affected packages

Possible Solution

A way to prepend, append fixed or dynamic text

Steps to Reproduce (for bugs)

see below setup

```js "husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }, ``` ```js // commitlint.config.js module.exports = { extends: ["@commitlint/config-conventional"], }; ```

Context

Your Environment

Executable Version
commitlint --version 9.1.1
git --version 2.21.0
node --version v10.15.0
escapedcat commented 4 years ago

Hi, would you mind checking if this is a duplicate of #1009 or #1016? Thanks

TanKucukhas commented 4 years ago

Thanks @escapedcat , I've seen these. My case is a little different I don't want user to input the id. Instead, I want to append fixed text to the commit automatically.

escapedcat commented 4 years ago

I see. Uhm, not to question your usecase but to understand it a bit better. Where would should the ticket-number come from?
Could it be enough to add a rule that checks if a ticket number is added? In that case a commit message might look like this though:

fix: bla bla bla [ABCD-10]

Would that be an issue? If yes, why?

TanKucukhas commented 4 years ago

I might have a unique use case. We have a couple of different projects and each project needs to have their own JIRA ticket, and not per commit. So for each project, I needed to append the same JIRA ID for all of the commits coming from the same project. Doing that, developers won't need to do manual work of adding the same ticket id all the time. Meanwhile, we wanted to still have some rules in place to commit a message, so that you can't push a commit without a type and subject. I have used @commitlint/config-conventional for the job.

I am actually able to do this with the help of a shell script.

package.json

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS && ./commit-hook.sh ${HUSKY_GIT_PARAMS} 'JIRA-10'"
    }
  },

commit-hook.sh

#!/bin/sh
## grab original message from .git/COMMIT_EDITMSG
ORIGINAL_COMMIT=$(cat "../../$1")
## grab Jira ID from second parameter of ./commit-hook.sh ${HUSKY_GIT_PARAMS} 'JIRA-#'"
JIRA_ID="$2"
## add Jira ID to the beginning and replace .git/COMMIT_EDITMSG
echo "$JIRA_ID $ORIGINAL_COMMIT" > "../../$1"

since the shell script runs after commitlint it does not get checked and you can do push afterward without problem

for instance: if you commit "fix: message" passes the validation but when you do push, you'll see Jira ticket prepended automatically to your commit message JIRA:10 fix: message

escapedcat commented 4 years ago

Thanks, that helped! Not sure if this should be part of the scope of commitlint.
So if the script works for you we might be good here?

TanKucukhas commented 4 years ago

@escapedcat thanks for taking the time. You are right, it is more likely scope for Husky or even Githooks. We can close.