-
Notifications
You must be signed in to change notification settings - Fork 2
Idea: Pre-Push Hook to Include Jira Title as Default Pull Request Title #1579
Description
Below is the output of ChatGPT when asked how we might accomplish having the default pull request title match the jira ticket title based on the git branch name.
Extra things to consider in the implementation:
- Filtering out just the branch title that corresponds with the jira ticket number ( eg - "UTOPIA-1400" from "UTOPIA-1400-fix" ).
- Case sensitivity in branch name.
- Storing secret for Jira API Key.
- Having access to Jira API - Will likely need to wait for Cloud Jira.
- Use fetch API instead of axios because of not introducing new dependencies.
Certainly! Husky is a popular tool used to enforce quality checks using hooks in Node.js projects. Using Husky alongside Node.js scripts, you can simplify the process of setting up hooks and writing the logic in JavaScript.
Here's how you can set up Husky to change the commit message based on the JIRA title before a push:
1. Install Husky
First, make sure you have husky installed in your project. If not, install it using npm:
npm install husky --save-dev2. Setup the Pre-Push Hook with Husky
Update your package.json to include the husky hooks:
{
"husky": {
"hooks": {
"pre-push": "node scripts/prePush.js"
}
}
}In the above configuration, we're specifying that Husky should run the scripts/prePush.js file before each push.
3. Create the prePush.js Script
Next, you'll create the scripts/prePush.js file. This script will handle the logic for checking the branch name, fetching the JIRA title, and creating a new commit.
const { execSync } = require('child_process');
const axios = require('axios');
const getBranchName = () => {
return execSync('git symbolic-ref --short HEAD').toString().trim();
};
const getJiraTitle = async (issueKey) => {
try {
const response = await axios.get(`https://yourjiraurl.com/rest/api/2/issue/${issueKey}`, {
headers: {
Authorization: 'Bearer YOUR_JIRA_TOKEN' // Adjust authentication method accordingly
}
});
return response.data.fields.summary;
} catch (error) {
console.error('Failed to fetch JIRA title:', error);
return null;
}
};
const addCommitWithJiraTitle = (title, issueKey) => {
execSync(`git commit --allow-empty -m "[${issueKey}] ${title}"`);
};
(async () => {
const branchName = getBranchName();
const jiraPattern = /^PROJ-[0-9]+$/; // Adjust the regex pattern
if (jiraPattern.test(branchName)) {
const title = await getJiraTitle(branchName);
if (title) {
addCommitWithJiraTitle(title, branchName);
}
}
})();Make sure to adjust the JIRA URL, authentication headers, and the pattern to match your specific needs. Also, you might want to install the axios library (npm install axios) if you haven't already, or use any other HTTP request library of your choice.
With this setup, every time you push, Husky will trigger the prePush.js script. The script will check the branch name, fetch the title from JIRA, and add an empty commit with the title of the JIRA ticket. This commit's message will then be used as the default PR title when you create a new PR.