How to create pull request on GitHub using bash shell and gh command line tool for coding review

Aniello Musella - Jul 10 '23 - - Dev Community

In this post I show how to create a pull request on GitHub to ask a coding review to another coder (e.g. a supervisor, a team leader or just another coder). To do that, I'll use the bash shell and the GitHub CLI.

What do you need?

The script

You can download the script here.

#!/bin/bash

coder="animusna" # THE CODER NAME WILL RECEIVE THE CODING REVIEW REQUEST
branch="develop" # THE ORIGIN BRANCH ON WHICH YOU'LL MAKE THE PULL REQUEST 

if [ $# -eq 0 ]
  then
    echo "***No arguments supplied"
    exit 1
fi

if [ -z "$1" ]
  then
    echo "***Pull request title not supplied"
    exit 1
fi


if [ -z "$2" ]
  then
    echo "***Pull request message not supplied"
    exit 1
fi

currentBranch=`git rev-parse --abbrev-ref HEAD`

echo -e "\nChecking login status Github cli"

gh auth status

echo -e "\nPushing you branch to origin..."

git push origin $currentBranch

echo -e "\nCreating pull request for branch \"$branch\" asking to \"$coder\" to review...\n"
gh pr create --title "$1" --body "$2" -r $coder -B $branch


echo -e "\n\nbye ;)"
Enter fullscreen mode Exit fullscreen mode

The script requires to be configured:

  • The GitHub account name of the coder will receive the coding review request.
  • The branch on which you'll open the pull request.

These parameters are hard coded, but you can change the script to receive these values via shell parameters.

Before to run the script

You'll need to install the GitHub command line tool following these instructions.

After the setup, you must authenticate typing the following command:

animus@devto:~$gh auth login
Enter fullscreen mode Exit fullscreen mode

Follow the instructions and complete the authentication process.

gh authetnication wizard

How to run the script

To run the script, you must provide the following parameters:

  • Pull request title
  • Pull request message
animus@devto:~$./ps.sh 'smart feature' 'check this out...'
Enter fullscreen mode Exit fullscreen mode

Running the script, you'll see something like shown in the image below.

Image description

After the execution of the script, the reviewer will receive a notification about the request just created.

Image description

Image description

Conclusions

The script described in this post is very simple, and it shows just one of many potential that the GitHub CLI could offer in a team to collaborate and to automate operations on GitHub repositories.

. . . . . . . . . . . . . . .
Terabox Video Player