Skip to content

Instantly share code, notes, and snippets.

@sebald
Last active April 24, 2017 11:22
Show Gist options
  • Save sebald/6f9cc8be436e73ecc121777fb12d8ffc to your computer and use it in GitHub Desktop.
Save sebald/6f9cc8be436e73ecc121777fb12d8ffc to your computer and use it in GitHub Desktop.
Append Jira issue number to every commit
#!/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);
});
});
});
@sebald
Copy link
Author

sebald commented Jan 27, 2017

Make sure it is executable -> chmod u+x .git/hooks/prepare-commit-msg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment