Skip to content

Instantly share code, notes, and snippets.

@phildier
Last active May 2, 2022 22:30
Show Gist options
  • Select an option

  • Save phildier/483625c41742f965ed430555f37febce to your computer and use it in GitHub Desktop.

Select an option

Save phildier/483625c41742f965ed430555f37febce to your computer and use it in GitHub Desktop.

List Open PRs

Lists a user's open pull requests on GitHub in a given GitHub organization with pretty formatting.

Installation and Usage

Requires the hub git wrapper to be installed: https://github.com/github/hub

Copy to somewhere in your $PATH and make executable:

cp list_open_prs.sh ~/bin/
chmod +x ~/bin/list_open_prs.sh

Example:

list_open_prs phildier AgencyPMG

You can edit the file and update the default username and org so you don't have to pass them in as parameters.

# set defaults
username=${1:-githubusername}
githuborg=${2:-githuborganization}
# change to
username=${1:-phildier}
githuborg=${2:-AgencyPMG}
#!/usr/bin/env bash
# vim:set ts=4 sw=4 expandtab:
# list a github user's open PRs with links and reviewers
# usage: list_open_prs [github username] [github organization]
# optionally, replace "githubusername" and "githuborganization" below with yours
# to set defaults
username=${1:-githubusername}
githuborg=${2:-githuborganization}
if ! which hub &>/dev/null
then
echo "hub is required, please install and try again"
echo "https://github.com/github/hub"
exit 1
fi
list_open_prs() {
GITHUB_USERNAME=$1
GITHUB_ORG=$2
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'
BLUE='\033[01;34m'
NONE='\033[0m'
# shellcheck disable=SC2016
hub api \
-t graphql \
-f q="is:open is:pr author:$GITHUB_USERNAME user:$GITHUB_ORG archived:false" \
-f query='query($q: String!, $n: Int = 30, $after: String) {search(query:$q, type: ISSUE, first: $n, after: $after) {edges {
node {
...on PullRequest{
title,
changedFiles,
additions,
deletions,
url,
timelineItems(first: 20, itemTypes: [PULL_REQUEST_REVIEW, PULL_REQUEST_REVIEW_THREAD, PULL_REQUEST_COMMIT_COMMENT_THREAD]) {
nodes {
... on PullRequestReview {
author {
login
}
state
}
}
}
}
}
}
}
}' \
| awk -v r="$RED" -v y="$YELLOW" -v g="$GREEN" -v b="$BLUE" -v n="$NONE" '
$1~/\.title/{printf "%s ", y"\nPR: "substr($0,index($0,$2))n; getline; printf "[%s files,", $2; getline; printf y" +%s, "n, $2; getline; print r"-"$2n"]"}
$1~/\.url$/{print b$2n}
$1~/.author.login$/{printf("- %s ",$2)}
$1~/\.state$/{
if($2=="APPROVED")
print "\t"g$2n
else
print "\t"r$2n
}'
}
list_open_prs "$username" "$githuborg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment