Last active
April 24, 2017 11:22
-
-
Save sebald/6f9cc8be436e73ecc121777fb12d8ffc to your computer and use it in GitHub Desktop.
Append Jira issue number to every commit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const { exec } = require('child_process'); | |
const { readFile, writeFile } = require('fs'); | |
const { EOL } = require('os'); | |
const CURRENT_BRANCH_JIRA_ISSUE_EXP = /[a-zA-Z]+[-][0-9]+/; | |
const messageFile = process.argv[2]; | |
/** | |
* We need the message file to append. | |
* If we can not find it -> abort. | |
*/ | |
if(!/COMMIT_EDITMSG/i.test(messageFile)) { | |
return process.exit(0); | |
} | |
exec('git rev-parse --abbrev-ref HEAD', (err, stdout) => { | |
// Command will fail if there are no commits yet. | |
if (err) { return process.exit(0); } | |
// Extract Jira issue number from current branch. | |
const issue = (CURRENT_BRANCH_JIRA_ISSUE_EXP.exec(stdout)||[])[0]; | |
// Could not find an issue number. | |
if (!issue) { return process.exit(0); } | |
// Read commit message file | |
readFile(messageFile, (err, data) => { | |
if (err) { return process.exit(0) }; | |
// Append issue number to file content and write back. | |
writeFile(messageFile, `${data}${EOL}${EOL}Issue: ${issue.toUpperCase()}`, () => { | |
process.exit(0); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure it is executable ->
chmod u+x .git/hooks/prepare-commit-msg