jackmarketon
1/11/2018 - 4:34 PM

Add Branch Name tag to commit msg

Using husky you can use this to add a Jira tag in format TAG-234 from the branch name to a commit message.

Please set the JIRA_TAG constant however, call this by installing husky and setting a commitmsg: "node ./scripts/commit-msg.js" in the package.json

#!/usr/bin/env node

const fs = require('fs');

const JIRA_TAG = 'TAG';

/* If message title:
 * * Doesn't start with square brackets []
 * * Doesn't start with Merge branch
 * * Doesn't start with Merge pull request
 * * Doesn't start with #
 * and
 * branch name starts with ${JIRA_TAG}-XXX (e.g. TAG-123-branch-description)
 * then prepend the issue tag to the commit message
 *
 * My awesome commit -> [TAG-123] My awesome commit
 */

const startsWithBraces = (str) => str.match(/^\[[^\]]/);
const startsWithMergeBranch = (str) => 0 === str.indexOf('Merge branch');
const startsWithMergePR = (str) => 0 === str.indexOf('Merge pull request');
const startsWithHash = (str) => 0 === str.indexOf('#');
const isInvalidMessage = (str) =>
    !startsWithBraces(str) &&
    !startsWithMergeBranch(str) &&
    !startsWithMergePR(str) &&
    !startsWithHash(str);
const tagMatcher = new RegExp(`^${JIRA_TAG}-\\d+`, 'i');
const getIssueTagFromBranchName = (str) => {
    const matched = str.match(tagMatcher);
    return matched && matched[0];
};

const messageFile = process.env.GIT_PARAMS;
const message = fs.readFileSync(messageFile, { encoding: 'utf-8' });
const messageTitle = message.split('\n')[0];
const branchName = require('child_process')
    .execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' })
    .split('\n')[0];
const issueTag = getIssueTagFromBranchName(branchName);

if (issueTag && isInvalidMessage(messageTitle)) {
    // Apply the issue tag to message title
    const messageLines = message.split('\n');
    messageLines[0] = `[${issueTag.toUpperCase()}] ${messageTitle}`;
    fs.writeFileSync(
        messageFile,
        messageLines.join('\n'), { encoding: 'utf-8' });
    console.log(`New message title: ${messageLines[0]}`);
}