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

Fix duplicate prefixes being added #1

Merged
merged 1 commit into from
Oct 13, 2017
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
Fix duplicate prefixes being added
  • Loading branch information
jessedobbelaere committed Oct 13, 2017
commit 0975480e7226babc7b65dc9318e7f4cfad14c9df
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