Skip to content

Instantly share code, notes, and snippets.

@jpgrace
Forked from sebald/prepare-commit-msg
Created January 29, 2017 19:11
Show Gist options
  • Save jpgrace/b100f07e3467534a4c26d6ae045d9118 to your computer and use it in GitHub Desktop.
Save jpgrace/b100f07e3467534a4c26d6ae045d9118 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 branch', (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)||[])[1];
// 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