question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

A way to prepend, append fixed or dynamic text

Steps to Reproduce (for bugs)

see below setup

  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },
// 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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
TanKucukhascommented, Jul 28, 2020

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

1reaction
TanKucukhascommented, Jul 25, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Commit message templates - GitLab Docs
GitLab uses commit templates to create default messages for specific types of commits. These templates encourage commit messages to follow a particular ...
Read more >
How to add Git's branch name to the commit message?
Here is my commit-msg script as an example: #!/bin/sh # # Automatically adds branch name and branch description to every commit message. #...
Read more >
Prepending Your Git Commit Messages with User Story IDs
It's always a good practice to prepend your commit messages with something meaningful. For example, it is often common when working with project...
Read more >
Change default title for pull request commits to not include PR id
Please provide a means to set a default commit message using static text and special variables which would provide dynamic values, such as...
Read more >
CommitPrefix - IntelliJ IDEs Plugin
Helps you to insert the task number from current branch to the beginning of the commit message. Usage sample: Full Usage Documentation. Email....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found