Skip to content

Instantly share code, notes, and snippets.

@vvscode
Created January 17, 2020 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvscode/b7bad44be81412243305b8c75aa5bfb4 to your computer and use it in GitHub Desktop.
Save vvscode/b7bad44be81412243305b8c75aa5bfb4 to your computer and use it in GitHub Desktop.
commit-message.js
/**
* It takes commit message and append a branch name below
*/
const fs = require('fs');
const childProcess = require('child_process');
const commitMessageFile = process.argv[2];
/**
* feature/foobar-RN-52/FOO-123/BAR-456 => BAR-456
* feature/fooBAR-123 => BAR-123
* feature/foobBAR-123-some-test => BAR-123
* BAR-123-some-test => BAR-123
*/
const extractTicketName = str => (str.match(/[A-Z]+-\d+/g) || []).pop();
const commitMessage = fs.readFileSync(commitMessageFile).toString();
const currentGitBranch = childProcess.execSync('git rev-parse --abbrev-ref HEAD').toString();
const jiraTicketFromBranch = extractTicketName(currentGitBranch);
if (jiraTicketFromBranch) {
const editedCommitMessage = `${commitMessage}\n\n${jiraTicketFromBranch}`;
fs.writeFileSync(commitMessageFile, editedCommitMessage);
}
@vvscode
Copy link
Author

vvscode commented Jan 17, 2020

package.json:

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "pre-push": "yarn lint",
      "commit-msg": "node scripts/commit-message.js $HUSKY_GIT_PARAMS"
    }
  }

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