Implement Husky in your project

Tameem Rafay
2 min readAug 1, 2023

Most of the time in our project we pushed the code with some random commit message but for the best practice we should write the Jira ticket number in each commit so that whenever we want to find which files I have changed for this ticket, it can be tracked easily.

So here I will tell you how to integrate the husky into your project. So that in the future it will restrict you to write the ticket no in your commit message. The main advantage of this implementation is to show the transparency to your client like which code is referenced against which tick.

In this example, I will show you how to integrate it into the Javascript code. First, you have to install the required packages in your project and then you can write some script using some regex that will force you to push your commit with the required pattern

In our current Jira project we have the ticket number like this NNDPF-[ticket_no]. So, I want to push the commit that have this pattern

type: [ticket number] commit message

Example: → fix: [NNDPF-1021] fixed the customer sign up api

fix: This will be the prefix of the commit that will elaborate the type of commit. It might be fix/feat/style/refactor

[NNDPF-1021]: This will the ticket number of Jira. That must be included in your commit message.

fixed the customer sign up api: This is the actual commit message that elaborates what actually you have performed in this change

Here is the implementation of the code using Husky

npm install --save-dev @commitlint/{cli,config-conventional}
npm install husky --save-dev
npx husky install
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit ${1}'

Create a new file on the root of the project with the following file name commitlint.config.js and copy the below code in it

const typeEnum = [
"feat", // implement new feature
"fix", // some quick fix
"style",
"refactor",
"test" // for test cases
];

module.exports = {
extends: ["@commitlint/config-conventional"],
rules: {
"subject-case": [2, "always", "sentence-case"],
"subject-empty": [2, "never"],
"type-empty": [2, "never"],
"type-enum": [2, "always", typeEnum],
},
plugins: [
{
rules: {
"type-enum": ({ type, subject }) => {
if (typeEnum?.includes(type) && /^\[NNDPF-\d+\] /.test(subject)) {
return [true];
}
return [
false,
!/^\[NNDPF-\d+\] /.test(subject)
? `Commit message should start with <type>: '[NNDPF-<number>] '.`
: !typeEnum?.includes(type) &&
`Type should be 'feat',
'fix',
'style',
'refactor',
'test',`,
];
},
},
},
],
};

So, on Jira it will look like.

--

--