diff --git a/README.md b/README.md index 2245216..5854cda 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,20 @@ A Node.js git hook script to prefix commits automatically with the JIRA ticket, ``` 3. Configure scripts in `package.json`. The script expects his first argument to be the JIRA tag of the project. ``` - "commitmsg": "jira-smart-commit SPAN" + "commit-msg": "jira-smart-commit SPAN" ``` + or environment variables + + - TAG_MATCHER - regular expression + - TAG_MATCH_INDEX - match index + - DEBUG - if true will console log some data about branch, matches array etc + + example: if your branches have feature/SPAN-1234/some-description template + ``` + "commit-msg": "TAG_MATCHER=\"^[^/]+/(SPAN-[0-9]+)\" TAG_MATCH_INDEX=1 jira-smart-commit" + ``` + + 4. Do your git commits like usual. If the branch was prefixed with a JIRA tag, your commit message will get prefixed with the same tag. diff --git a/index.js b/index.js index 42c6623..8da10f3 100644 --- a/index.js +++ b/index.js @@ -12,8 +12,10 @@ const fs = require("fs"); * E.g My awesome commit -> TAG-123 My awesome commit */ -if (!process.argv[2]) { - console.error("Please run this script with the JIRA ticket prefix as CLI argument (e.g. node smart-commit-msg.js SPAN)"); +if (!process.argv[2] && !process.env.TAG_MATCHER ) { + console.error("Please run this script with the JIRA ticket prefix as CLI argument " + + "(e.g. node smart-commit-msg.js SPAN) or pass regular expression as envirement variable " + + "(e.g. TAG_MATCHER=\\\"SPAN-[0-9]+\\\" node smart-commit-msg.js)"); process.exit(1); } @@ -52,12 +54,16 @@ const fetchBranchNameFromGit = () => { */ const getIssueTagFromBranchName = (branchName) => { const matched = branchName.match(tagMatcher); - return matched && matched[0]; + const index = process.env.TAG_MATCH_INDEX ? Number(process.env.TAG_MATCH_INDEX) : 0; + if (process.env.DEBUG === 'true') { + console.log({branchName, tagMatcher, index, matched}); + } + return matched && matched[index]; }; const jiraTag = process.argv[2]; -const tagMatcher = new RegExp(`^${jiraTag}-\\d+`, "i"); -const commitMsgFile = process.env.GIT_PARAMS; +const tagMatcher = process.env.TAG_MATCHER ? new RegExp(process.env.TAG_MATCHER, "i") : new RegExp(`^${jiraTag}-\\d+`, "i"); +const commitMsgFile = process.env.GIT_PARAMS || process.env.HUSKY_GIT_PARAMS; const commitMsg = fs.readFileSync(commitMsgFile, { encoding: "utf-8" }); const branchName = getBranchName(); const issueTag = getIssueTagFromBranchName(branchName); diff --git a/index.spec.js b/index.spec.js index 1179c0e..015df21 100644 --- a/index.spec.js +++ b/index.spec.js @@ -8,6 +8,14 @@ describe('index.js', () => { expect(executeScriptMock("SPAN", "SPAN-1-proof-of-concept", "Initial commit✨")).to.equal("SPAN-1 Initial commit✨"); expect(executeScriptMock("PROJECT", "PROJECT-3-githook-test", "Add support for githooks")).to.equal("PROJECT-3 Add support for githooks"); expect(executeScriptMock("TAG", "TAG-5032-add-readme", "Add readme to project")).to.equal("TAG-5032 Add readme to project"); + + process.env.TAG_MATCHER = "^[^/]+/(SPAN-[0-9]+)"; + process.env.TAG_MATCH_INDEX = "1"; + process.env.DEBUG = "true"; + expect(executeScriptMock(undefined, "feature/SPAN-1234/init", "Initial commit")).to.equal('SPAN-1234 Initial commit'); + delete process.env.TAG_MATCHER; + delete process.env.TAG_MATCH_INDEX; + delete process.env.DEBUG; }); it('should not add a prefix again to my already prefixed commit message', () => { diff --git a/package.json b/package.json index 2e99dfb..566e564 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jira-smart-commit", - "version": "1.0.2", + "version": "1.0.3", "description": "A githook script that transforms commit messages to JIRA smart commits based on branch names", "author": "Jesse Dobbelaere ", "license": "MIT",