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

make more flaxible to catch task name & use HUSKY_GIT_PARAMS if GIT_P… #4

Merged
merged 1 commit into from
Dec 28, 2018
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <jesse@dobbelae.re>",
"license": "MIT",
Expand Down