Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add JIRA ticket number to commit messages #3285

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
node .husky/commit-msg.js $1
59 changes: 59 additions & 0 deletions .husky/commit-msg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import { execSync } from "node:child_process";

const ticketNumberRegex = /LF-\d+/;
const commitMessageFilePath = process.argv[2];

const git = {
getCurrentBranchName: () =>
execSync("git rev-parse --abbrev-ref HEAD").toString().trim(),
readCommitMsg: () => fs.readFileSync(commitMessageFilePath, "utf8"),
writeCommitMsg: (msg) => fs.writeFileSync(commitMessageFilePath, msg),
};

function processCommitMsg() {
try {
const branchName = git.getCurrentBranchName();
const match = branchName.match(ticketNumberRegex);

// Branch name doesn't contain ticket number
if (!match) {
console.warn(
"JIRA ticket number not found in branch name. Proceeding without modification."
);
return;
}

const commitMessage = git.readCommitMsg();

const filteredCommitMessage = commitMessage
.split("\n")
.filter((line) => !line.trim().startsWith("#"))
.join("\n")
.trim();

if (filteredCommitMessage === "") return;

// Check if commit already contains ticket number
if (filteredCommitMessage.match(ticketNumberRegex)) {
console.log(
"Commit message already contains a JIRA ticket number. Proceeding without modification."
);
return;
}

// Modify commit message
git.writeCommitMsg(match[0] + " " + filteredCommitMessage);
} catch (error) {
console.error("Error processing commit message:", error);
}
}

// Call the hook only when this file is executed directly by node.
// This is needed to ensure proper execution of tests.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
processCommitMsg();
}

export { git, processCommitMsg };
4 changes: 2 additions & 2 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
'*.{md,yml}': 'prettier --write'
export default {
"*.{md,yml,json,js}": "prettier --write",
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
"lint": "lerna run lint",
"start": "lerna run start",
"bootstrap": "lerna bootstrap",
"prepare": "husky install",
"prepare": "husky install && chmod u+x .husky/*",
"ngrok": "ngrok start --config=./ngrok/ngrok.yml --all",
"ngrok:setup": "node ./ngrok/ngrok-setup.js",
"ngrok:api": "ngrok start --config=./ngrok/ngrok.yml api",
"ngrok:webapp": "ngrok start --config=./ngrok/ngrok.yml webapp"
},
"type": "module",
"jest": {
"testPathIgnorePatterns": [
"<rootDir>[/\\\\](node_modules|packages/webapp/scripts)[/\\\\]"
Expand Down
94 changes: 94 additions & 0 deletions tests/prepare-commit-msg.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { test, describe, mock } from "node:test";
import assert from "node:assert/strict";
import { processCommitMsg, git } from "../.husky/commit-msg.js";

const setupMocks = ({ commitMsg, branchName }) => {
mock.method(git, "getCurrentBranchName", () => branchName);
mock.method(git, "readCommitMsg", () => commitMsg);
return mock.method(git, "writeCommitMsg", () => {});
};

describe("prepare-commit-msg - adding JIRA ticket number to commit messages", () => {
test("should automatically add ticket number when missing", () => {
const mockWriteCommitMsg = setupMocks({
branchName: `LF-1212-test-branch`,
commitMsg: "test commit",
});

processCommitMsg();

// function to modifiy commit msg should be called called once
assert.equal(mockWriteCommitMsg.mock.callCount(), 1);

// function to modifiy commit msg should be called with modified commit message
assert.equal(
mockWriteCommitMsg.mock.calls[0].arguments[0],
"LF-1212 test commit"
);
});

test("should filter out comments starting with #", () => {
const mockWriteCommitMsg = setupMocks({
branchName: `LF-1212-test-branch`,
commitMsg: `test commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch LF-1212-test-branch
#
`,
});

processCommitMsg();

assert.equal(mockWriteCommitMsg.mock.callCount(), 1);
assert.equal(
mockWriteCommitMsg.mock.calls[0].arguments[0],
"LF-1212 test commit"
);
});

test("should not modify commit message if it is empty", (t) => {
const mockWriteCommitMsg = setupMocks({
branchName: `LF-1212-test-branch`,
commitMsg: "",
});

processCommitMsg();

assert.equal(mockWriteCommitMsg.mock.callCount(), 0);
});

test("should not modify commit message if it already has ticket number in front", (t) => {
const mockWriteCommitMsg = setupMocks({
branchName: `LF-1212-test-branch`,
commitMsg: "LF-1212 test commit",
});

processCommitMsg();

assert.equal(mockWriteCommitMsg.mock.callCount(), 0);
});

test("should not modify commit message if it already has ticket number inside", () => {
const mockWriteCommitMsg = setupMocks({
branchName: `LF-1212-test-branch`,
commitMsg: "test LF-1212 commit",
});

processCommitMsg();

assert.equal(mockWriteCommitMsg.mock.callCount(), 0);
});

test("should not modify commit message if branch name does not contain ticket number", () => {
const mockWriteCommitMsg = setupMocks({
branchName: `test-branch`,
commitMsg: "test commit",
});

processCommitMsg();

assert.equal(mockWriteCommitMsg.mock.callCount(), 0);
});
});
Loading