Last active
May 1, 2019 15:20
-
-
Save mmieluch/ddd0a40f6d761f7b68cb01e835cbaea1 to your computer and use it in GitHub Desktop.
Prepend commit message with JIRA issue ID
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 fs = require('fs') | |
const { execSync } = require('child_process') | |
const msgFilePath = process.argv[2] | |
if (typeof msgFilePath !== 'string' && !msgFilePath.length) { | |
console.error('Commit message file not passed in from Git!') | |
process.exit(1) | |
} | |
const buffer = execSync(`git rev-parse --abbrev-ref HEAD`) | |
const currentBranch = buffer.toString().trim() | |
const re = /^(feature|hotfix|release|fix)(\/|-)(\w{2,4})-(\d+)$/ | |
if (!re.test(currentBranch)) { | |
// Unknown branch name format. Nothing to do here. | |
process.exit(0) | |
} | |
const matches = currentBranch.match(re) | |
if (!matches) { | |
process.exit(0) | |
} | |
const prefix = matches[3].toUpperCase() | |
const issueNumber = matches[4] | |
const issueID = `${prefix}-${issueNumber}` | |
const currentContent = fs.readFileSync(msgFilePath).toString() | |
// It's most likely an amended commit and is already prefixed with this issue ID. | |
// No need to append it again. | |
if (currentContent.startsWith(issueID)) { | |
process.exit(0) | |
} | |
fs.writeFileSync(msgFilePath, issueID + ': ' + currentContent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment