Skip to content

Instantly share code, notes, and snippets.

@berndverst
Created August 26, 2022 21:01
Show Gist options
  • Save berndverst/8691bad50a8bcfb293c9cd3e9027c64c to your computer and use it in GitHub Desktop.
Save berndverst/8691bad50a8bcfb293c9cd3e9027c64c to your computer and use it in GitHub Desktop.
GitHub CLI helper for OSS maintainers to update/fixup PRs from contributors

GitHub (GH) CLI helper script to update Pull Requests (PRs) from contributors.

As a maintainer of an OSS project I constantly find myself having to fix up incoming PRs. I enjoy using the GitHub CLI, but unfortunately this CLI does not provide an easy way to identify the remote fork the PR originated from.

As a result my git remote -v lists a lot of remotes that I had to manually add after viewing the PR in my browser to obtain the remote clone URL. This is not sustainable. Git allows pushing to a remote URL directly, so there is no need to define the remote before hand.

This script defines a utility functions that makes it easy for gh GitHub CLI users to update contributions.

gh pr checkout 2009
# make some changes to the PR and commit them

updatePR 2009

You will see something like the following:

updatePR 2009
Pushing to git@github.com:berndverst/components-contrib.git HEAD:master
Are you sure? Type [y] to continue: y

Everything up-to-date
#!/bin/bash
updatePR () {
destination=$(gh pr view $1 --json headRepositoryOwner,headRepository,headRefName | jq -r '"git@github.com:" + .headRepositoryOwner.login + "/" + .headRepository.name + ".git HEAD:" + .headRefName')
echo "Pushing to ${destination}"
read confirm"?Are you sure? Type [y] to continue: "
echo
if [[ $confirm =~ ^[Yy]$ ]]
then
$(echo "git push ${destination}")
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment