Skip to content

Create Github pull requests with script

Why create pull requests directly from a npm script?

Creating pull requests is a important part of the development lifecycle of a project. Most of the time we will create a new branch from main or a specific environment like production, make some changes and then create a pull request to merge the new code to the original branch. The problem is most of the time we don't have specific format or a standard way to create the title and description. We can solve this problem by creating our own script, also the time needed to create a pull request is decreased.

Creating new project

First we need to create a new node project

mkdir node-pr-script
cd node-pr-script
npm init -y

Creating script

Now create a new folder in the base of the project called scripts, inside create a new file called

printf "Creating new pull request from current branch"
printf "Enter the title of the new pull request\n"
read title
printf "Enter the description of the new pull request\n"
read description

printf "Creating Pull Request...\n"
gh pr create -B main -t "[$title]" -b "$description"

This new script asks for two variables title and description, and then we use GitHub cli to create a new pull request from the current branch to main, this is just an example, but we can ask for more information or set a specific format for the title or message

Adding script to package.json

Now we are going to open our package.json file and add a new script to execute our github-pr script

  "name": "pr-script",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "pr": "sh ./scripts/"
  "keywords": [],
  "author": "",
  "license": "ISC"

Execute script

Now open your terminal login to your GitHub account using the cli and then execute the following command to create a new pull request

npm run pr