Skip to content

Automating pull request creation with Jira info from the command line. πŸ‘¨πŸ»β€πŸ’»βž‘οΈπŸŒβœ…

License

Notifications You must be signed in to change notification settings

wesleyscholl/github-jira-pr-script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

69 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

github-jira-pr-script - Automating pull request creation with Jira projects from the command line πŸ‘¨πŸ»β€πŸ’»βž‘οΈπŸŒβœ…

Manually creating a GitHub pull request is a time-consuming task. Save time using this shell script.

Note

There are now two versions of this script:

  • pr-script.sh pulls JIRA ticket information and populates a pre-existing pull request template, PR summary provided by Gemini AI.
  • public-pr-script.sh creates a new pull request using the branch name, git diff, and git commits with hashes, PR summary provided by Gemini AI. I created this version for general use, not specific to Jira.

Table of Contents

What this script automates:

Name Description
Git Commit Push Pushing local commits to remote branch with git push.
PR Creation Clicking create a pull request from the feature branch in Github.
Target Merge Branch Selecting the target branch to merge changes into.
PR Title Copying the story name of the Jira ticket as the title of the pull request.
Jira Ticket Link Adding the link of the Jira ticket to the pull request description.
Jira Ticket Description Adding a description and acceptance criteria from the Jira ticket to the pull request.
PR Reviewers Adding the reviewers of the pull request.
PR Assignee Assigning the pull request owner as the assignee to the pull request.
Jira Ticket Comments Adding all Jira comments from the associated ticket.
Jira Ticket Subtasks Adding all Jira subtasks and subtask statuses, checked checkbox if completed.
Jira Team, Sprint, Goal, Components & Epic Adding a section with the team name, sprint, and epic.
Jira Steps to Reproduce Adding steps to reproduce if they exist, otherwise N/A.
GitHub Commit Hashes Adding the updates section, a list of all commits with hashes and commit descriptions.
GitHub Diff Adding a code changes section with the git diff of all code modifications.
Jira Ticket Screenshots Adding screenshots for UI modifications or functionality improvements. (Optional - 3 Maximum)
GitHub PR Labels Adding labels to categorize pull requests as a Story, Bug, QA, UAT, etc. Also adds team labels - ex. Automation, NSS_Automation (Optional)

Requirements

Important

For the public script (public-pr-script.sh), there are fewer requirements: GitHub CLI (hub), GitHub Token (PAT - set in .bash_profile or .zshrc), Gemini AI API Key (set in .bash_profile or .zshrc), GitHub reviewers (Code reviewer GitHub usernames) and GitHub assignee (your GitHub username) in .bash_profile or .zshrc (*reviwers and assignee are both optional).

Name Description Link, Location, or Command
hub GitHub CLI - Adds additional GitHub commands to access the API from the CLI. hub
jq Command-line JSON processor - Parsing the response data from the Jira API. jq
GitHub Personal Access Token - PAT Personal access tokens can be used instead of a password for Git over HTTPS, or can be used to authenticate to the API over Basic Authentication. * Note * repo permissions are required for this script to function properly. Create A GitHub Personal Access Token
Jira API Token API tokens can authenticate scripts or other process with Atlassian cloud products. Create a JIRA Token
<your_pr_script>.sh Shell file that automates creating pull requests using Jira ticket data, images, and other details. Static location on your local computer - touch pr_script.sh
PULL_REQUEST_TEMPLATE A pull request template to be used if a template does not exist in the repo. Static location on your local computer - PULL_REQUEST_TEMPLATE in the same folder as pr_script.sh
Environment Variables Environment variables that are stored in the terminal/bash/zsh configuration. Includes: <pr_script>.sh path alias, jira_url, jira_access_token, github_author and github_reviewers ~/.bash_profile or ~/.zshrc - open with open ~/.bash_profile or open ~/.zshrc - then load with source ~/.bash_profile or source ~/.zshrc
Git Bash *Required for Windows Git Bash provides a UNIX command line emulator for windows which can be used to run Git, shell commands, and much more. Download Git Bash

Mac Installation


Mac Instructions

1. Install hub.
brew install hub
Screenshot 2023-11-08 at 12 36 56 PM
2. Install jq.
brew install jq

jjjqqq

4. Create A GitHub Personal Access Token with repo permissions. Copy this token and keep for use later.
Screenshot 2023-11-08 at 2 23 56 PM
5. Configure the GitHub hub configuration:
git config --global hub.protocol https
git config --global hub.user [your github username]
git config --global hub.token [your github personal access token]
Screenshot 2023-11-08 at 12 36 37 PM
6. Create a JIRA Token. Also copy this token and keep for use later.
Screenshot 2023-11-08 at 1 00 27 PM Screenshot 2023-11-08 at 1 01 07 PM
7. Create a local pr_script.sh file - Run the following command: touch pr_script.sh.
touch pr_script.sh
Screenshot 2023-11-08 at 12 49 44 PM
8. Copy and paste the pull request script template to your pr_script.sh file, then save the file.
Screenshot 2023-11-08 at 12 50 28 PM
9. Open your terminal/bash/zsh configuration, open ~/.bash_profile or open ~/.zshrc.
open ~/.bash_profile

or

open ~/.zshrc
Screenshot 2023-11-08 at 1 02 09 PM
10. Fill in the following bash configuration values:
alias pr='<pr_script_path>.sh'
export jira_url=https://totalwine.atlassian.net/
export jira_access_token=<Totalwine_email>:<Jiratoken>
export github_author=<github_username>
export github_reviewers=<reviewers_github_usernames,seperated,by,commas>

Example:

alias pr='/Users/wscholl/pr_script.sh'
export jira_url=https://totalwine.atlassian.net/
export jira_access_token=wscholl@totalwine.com:AT***...***0A
export github_author=wesleyscholl
export github_reviewers=naimish1083,KevinArce98,kareewongsagul
11. Load the file with the source command:
source ~/.bash_profile

or

source ~/.zshrc
12. You can now execute the script to automate your pull request, the target branch is optional and defaults to develop.
pr <target_branch>



Windows Installation


Windows Instructions

1. Scoop is required to install hub, if it's not installed on your machine open a powershell window and run:

> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
> irm get.scoop.sh | iex
Screenshot 2023-11-09 at 12 06 55 PM

2. Then install hub:

> scoop install hub

3. Use scoop to install jq:

> scoop install jq
Screenshot 2023-11-09 at 12 10 34 PM

4. Create A GitHub Personal Access Token with repo permissions. Copy this token and keep for use later.

Screenshot 2023-11-08 at 2 23 56 PM

5. Install Git Bash from the Software Center App:

Click Search on the Microsoft Toolbar and type in software center.

Screenshot 2023-11-10 at 1 29 12 PM

Open the application and click on Git Bash to install.

Screenshot 2023-11-10 at 1 27 36 PM

6. Copy the pr_script.sh file from the github-jira-pr-script repo or create a new file in your C:/Users/<your_username>/ folder:

Powershell:

> New-Item -Type File -Path pr_script.sh

Or Git Bash.

touch pr_script.sh

7. Open a git bash window and set the hub configuration using the following commands:

Ensure you update <your_github_username> with your github username and <your_github_personal_access_token> with your github personal access token.

git config --global hub.protocol https
git config --global hub.user <your_github_username>
git config --global hub.token <your_github_personal_access_token>
Screenshot 2023-11-09 at 12 23 07 PM

8. Create a JIRA Token

Create a JIRA Token and copy for use later.

Screenshot 2023-11-08 at 1 00 27 PM Screenshot 2023-11-08 at 1 01 07 PM

9. Create and open your bash configuration, ~/.bash_profile.

In the same directory:

touch .bash_profile

Then:

notepad .bash_profile

And complete the following configuration:

alias pr='sh C:/Users/<your_windows_username>/pr_script.sh'
export jira_url=https://totalwine.atlassian.net/
export jira_access_token=<Totalwine_email>:<Jiratoken>
export github_author=<github_username>
export github_reviewers=<reviewers_github_usernames,seperated,by,commas>

Example:

alias pr='sh C:/Users/WScholl/pr_script.sh'
export jira_url=https://totalwine.atlassian.net/
export jira_access_token=wscholl@totalwine.com:AT***...***0A
export github_author=wesleyscholl
export github_reviewers=naimish1083,KevinArce98,kareewongsagul

*Make sure you copy the full Jira token string into the .bash_profile configuration.

10. Load the .bash_profile configuration with the source command:

source ~/.bash_profile

11. You can now execute the script to automate your pull request creation. Target branch is optional, defaults to develop.

pr <target_branch>

*Only run the pr command from a Total Wine Labs git repository feature branch, otherwise it will fail.



Before submitting an actual Pull Request

The pull request function has been commented out to ensure that the pull request looks good before creating a live Pull Request. The script will output text similar to the example below.

Example Script Pull Request Output:

CRS-41964 NSS - 2.1 | Disable "Free State" data field - Story

# [**CRS-41964**](https://totalwine.atlassian.net/browse/CRS-41964) NSS - 2.1 | Disable "Free State" data field - Story


### Description
As a user,I want the "Free Goods State" data field to be disabled and  set to "No" to simplify data entryas filling this field is not required.
##
#### Comments
- Finished development, wrote unit test to ensure input is disabled.!Screenshot 2023-11-08 at 2.09.51 PM.png|width=391,height=368!
##
#### Subtasks
- [x] Development - Done
- [ ] QA Validation - Code Review
- [ ] PO Approval - Open
##
> Team Pluto
> 23-23 - Team Pluto
> NSS - 2.1 | Post MVP
##
## Steps to Reproduce
- N/A

## Updates
* list of updates
7c1e712 CRS-41964 - Updated input value according to acceptance criteria
41b99c4 CRS-41964 - Free Goods State input now disabled, added unit test to test input is disabled

## Code Changes
* list of code changes

+diff --git a/src/components/SupplyChain/StoreContainer.test.tsx b/src/components/SupplyChain/StoreContainer.test.tsx
+index 437e1b4..1983103 100644
+--- a/src/components/SupplyChain/StoreContainer.test.tsx
++++ b/src/components/SupplyChain/StoreContainer.test.tsx
+@@ -185,4 +185,12 @@ describe('StoreContainer', () => {
+ 
+     expect(setSupplyChainData).toHaveBeenCalled();
+   });
++
++  it('should disable "Free Goods State" input when freeGoodsStateInput prop is true', () => {
++    const wrapper = shallow(<StoreContainer {...props} freeGoodsStateInput />);
++    const freeGoodsStateInput = wrapper.find(
++      'Select[name="nss-store-supply-chain-free-goods-select"]'
++    );
++    expect(freeGoodsStateInput.prop('disabled')).toBe(true);
++  });
+ });
+diff --git a/src/components/SupplyChain/StoreContainer.tsx b/src/components/SupplyChain/StoreContainer.tsx
+index 63955cd..2c2b358 100644
+--- a/src/components/SupplyChain/StoreContainer.tsx
++++ b/src/co


## Screen Shots
![Screen Shot](https://api.media.atlassian.com/file/f6bef5ab-a4b5-4e28-ab32-2a197b719c42/binary?token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI5NTI3ZWFkMC1mMDFjLTQ3NjMtYTdlNC04Y2UxNzk1ZWM0NTIiLCJhY2Nlc3MiOnsidXJuOmZpbGVzdG9yZTpmaWxlOmY2YmVmNWFiLWE0YjUtNGUyOC1hYjMyLTJhMTk3YjcxOWM0MiI6WyJyZWFkIl19LCJleHAiOjE2OTk0NzIwODcsIm5iZiI6MTY5OTQ3MTQ4N30.2gBkYLD_8Y-_pEsraxRH69bnxe0E4WlB3J25wE32k70&client=9527ead0-f01c-4763-a7e4-8ce1795ec452&dl=true&name=Screenshot+2023-11-08+at+2.09.51+PM.png)

When you are ready to submit the Pull Request

Uncomment these lines at the bottom of the pr_script.sh and run pr <branch>.

# if [ -z "$label" ]; then
# 	hub pull-request -b $base_branch -F PR_MESSAGE --no-edit -o -r $reviewers -a $assign
# else
# 	hub pull-request -b $base_branch -F PR_MESSAGE --no-edit -o -r $reviewers -a $assign -l $label
# fi

*Once the script completes, it will open the newly created PR in the default web browser. It should look like the following example.

Example Created Pull Request

Screenshot 2023-11-08 at 2 19 30 PM Screenshot 2023-11-08 at 3 44 28 PM

Troubleshooting

fatal: not a git repository (or any of the parent directories): .git

Screenshot 2023-11-10 at 1 56 17 PM

This error message displays when the pr script is run from a non .git repository folder. Run the pr command from the feature branch within a Total Wine Labs repository.

command not found: pr_script.sh

If you see this message, the environment variables haven't been loaded or the alias syntax is incorrect.

Screenshot 2023-11-08 at 2 37 28 PM
  • Update your alias to the following syntax: alias pr='/Users/wscholl/pr_script.sh' Update the path if your pr_script.sh file is in a different location.
  • Load the bash/zsh/terminal configuration environment variables with the following command:
source ~/.bash_profile

or

source ~/.zshrc

Forbidden HTTP 403 - Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization.

This means that the GitHub personal access token (PAT) needs authorization to access the TotalWineLabs organization.

Screenshot 2023-11-08 at 2 23 56 PM
  • When the token has been properly authorized, you should see "Deauthorize" under single sign-on organizations next to TotalWineLabs.
Screenshot 2023-11-08 at 2 18 12 PM

Permission Denied Error

If you receive this error, execute one of the following commands:

chmod +x pr

or

chmod +x path of the script.sh

Terminal/bash/zsh config file not found

Use the following commands to create the file:

touch ~/.bash_profile

or

touch ~/.zshrc

After you’ve created the file, open it with open ~/.bash_profile or open ~/.zshrc and add the environment variables to it.

Cannot execute: required file not found error

If you see this message, the .bash_profile alias is not configured properly.

bash: C:/Users/<your_windows_username>/pr_script.sh: cannot execute: required file not found

Modify this line:

alias pr='C:/Users/WScholl/pr_script.sh'

to

alias pr='sh C:/Users/WScholl/pr_script.sh'

Windows requires the sh command to be specified in the alias.