Skip to content

Commit abf9d5c

Browse files
Merge pull request #1 from jessedobbelaere/hotfix/duplicate-prefixes
Fix duplicate prefixes being added
2 parents b0f89b3 + 0975480 commit abf9d5c

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ const jiraTag = process.argv[2];
5959
const tagMatcher = new RegExp(`^${jiraTag}-\\d+`, "i");
6060
const commitMsgFile = process.env.GIT_PARAMS;
6161
const commitMsg = fs.readFileSync(commitMsgFile, { encoding: "utf-8" });
62-
const commitMsgTitle = commitMsg.split("\n")[0];
6362
const branchName = getBranchName();
6463
const issueTag = getIssueTagFromBranchName(branchName);
64+
const rawCommitMsgTitle = commitMsg.split("\n")[0];
6565

66-
if (issueTag && isInvalidMessage(commitMsgTitle)) {
66+
if (issueTag && isInvalidMessage(rawCommitMsgTitle)) {
6767
// Add the JIRA issue tag to commit message title
68+
const ticketTag = issueTag.toUpperCase();
69+
const commitMsgTitle = rawCommitMsgTitle.replace(ticketTag, "").trim();
6870
const messageLines = commitMsg.split("\n");
69-
messageLines[0] = `${issueTag.toUpperCase()} ${commitMsgTitle}`;
71+
messageLines[0] = `${ticketTag} ${commitMsgTitle}`;
7072
fs.writeFileSync(commitMsgFile, messageLines.join("\n"), { encoding: "utf-8" });
7173
} else {
7274
fs.writeFileSync(commitMsgFile, commitMsg, { encoding: "utf-8" });

index.test.js renamed to index.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ describe('index.js', () => {
1010
expect(executeScriptMock("TAG", "TAG-5032-add-readme", "Add readme to project")).to.equal("TAG-5032 Add readme to project");
1111
});
1212

13+
it('should not add a prefix again to my already prefixed commit message', () => {
14+
expect(executeScriptMock("SPAN", "SPAN-1-proof-of-concept", "SPAN-1 Initial commit✨")).to.equal("SPAN-1 Initial commit✨");
15+
expect(executeScriptMock("PROJECT", "PROJECT-3-githook-test", "PROJECT-3 Add support for githooks")).to.equal("PROJECT-3 Add support for githooks");
16+
expect(executeScriptMock("TAG", "TAG-5032-add-readme", "TAG-5032 Add readme to project")).to.equal("TAG-5032 Add readme to project");
17+
});
18+
1319
it('should not add a prefix in merge pull requests', () => {
1420
const commitMergeMessage = "Merge branch 'develop' of github.com:jessedobbelaere/jira-smart-commit into bugfixes";
1521
expect(executeScriptMock("SPAN", "SPAN-1-proof-of-concept", commitMergeMessage)).to.equal(commitMergeMessage);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "jira-smart-commit",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A githook script that transforms commit messages to JIRA smart commits based on branch names",
55
"author": "Jesse Dobbelaere <[email protected]>",
66
"license": "MIT",
77
"main": "index.js",
88
"scripts": {
9-
"test": "mocha index.test",
9+
"test": "mocha index.spec.js",
1010
"release": "with-package git commit -am pkg.version && with-package git tag pkg.version && git push && npm publish && git push --tags"
1111
},
1212
"engines": {

0 commit comments

Comments
 (0)