Skip to content

Commit

Permalink
Merge pull request #1 from jessedobbelaere/hotfix/duplicate-prefixes
Browse files Browse the repository at this point in the history
Fix duplicate prefixes being added
  • Loading branch information
jessedobbelaere authored Oct 13, 2017
2 parents b0f89b3 + 0975480 commit abf9d5c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ const jiraTag = process.argv[2];
const tagMatcher = new RegExp(`^${jiraTag}-\\d+`, "i");
const commitMsgFile = process.env.GIT_PARAMS;
const commitMsg = fs.readFileSync(commitMsgFile, { encoding: "utf-8" });
const commitMsgTitle = commitMsg.split("\n")[0];
const branchName = getBranchName();
const issueTag = getIssueTagFromBranchName(branchName);
const rawCommitMsgTitle = commitMsg.split("\n")[0];

if (issueTag && isInvalidMessage(commitMsgTitle)) {
if (issueTag && isInvalidMessage(rawCommitMsgTitle)) {
// Add the JIRA issue tag to commit message title
const ticketTag = issueTag.toUpperCase();
const commitMsgTitle = rawCommitMsgTitle.replace(ticketTag, "").trim();
const messageLines = commitMsg.split("\n");
messageLines[0] = `${issueTag.toUpperCase()} ${commitMsgTitle}`;
messageLines[0] = `${ticketTag} ${commitMsgTitle}`;
fs.writeFileSync(commitMsgFile, messageLines.join("\n"), { encoding: "utf-8" });
} else {
fs.writeFileSync(commitMsgFile, commitMsg, { encoding: "utf-8" });
Expand Down
6 changes: 6 additions & 0 deletions index.test.js → index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ describe('index.js', () => {
expect(executeScriptMock("TAG", "TAG-5032-add-readme", "Add readme to project")).to.equal("TAG-5032 Add readme to project");
});

it('should not add a prefix again to my already prefixed commit message', () => {
expect(executeScriptMock("SPAN", "SPAN-1-proof-of-concept", "SPAN-1 Initial commit✨")).to.equal("SPAN-1 Initial commit✨");
expect(executeScriptMock("PROJECT", "PROJECT-3-githook-test", "PROJECT-3 Add support for githooks")).to.equal("PROJECT-3 Add support for githooks");
expect(executeScriptMock("TAG", "TAG-5032-add-readme", "TAG-5032 Add readme to project")).to.equal("TAG-5032 Add readme to project");
});

it('should not add a prefix in merge pull requests', () => {
const commitMergeMessage = "Merge branch 'develop' of github.com:jessedobbelaere/jira-smart-commit into bugfixes";
expect(executeScriptMock("SPAN", "SPAN-1-proof-of-concept", commitMergeMessage)).to.equal(commitMergeMessage);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "jira-smart-commit",
"version": "1.0.0",
"version": "1.0.1",
"description": "A githook script that transforms commit messages to JIRA smart commits based on branch names",
"author": "Jesse Dobbelaere <jesse@dobbelae.re>",
"license": "MIT",
"main": "index.js",
"scripts": {
"test": "mocha index.test",
"test": "mocha index.spec.js",
"release": "with-package git commit -am pkg.version && with-package git tag pkg.version && git push && npm publish && git push --tags"
},
"engines": {
Expand Down

0 comments on commit abf9d5c

Please sign in to comment.