Skip to content

Instantly share code, notes, and snippets.

@Machine-Maker
Last active July 31, 2022 04:55
Show Gist options
  • Save Machine-Maker/48cb1d0692a145baff7df2cd5f8df20d to your computer and use it in GitHub Desktop.
Save Machine-Maker/48cb1d0692a145baff7df2cd5f8df20d to your computer and use it in GitHub Desktop.
Script that tracks remotes and open PRs on GitHub
#!/bin/zsh
(
YELLOW="\e[33m"
LIGHT_YELLOW="\e[93m"
MAGENTA="\e[35m"
ENDCOLOR="\e[0m"
if git rev-parse --is-inside-git-dir > /dev/null 2>&1; then
echo "${YELLOW}Local Only Branches${ENDCOLOR}"
git branch --format "%(refname:short) %(upstream:trackshort)" | awk '{if (!$2) print "- \033[35m"$1"\033[0m";}' | cat
OWNER=`git remote get-url origin | sed -E 's/.*:([a-zA-Z-]+).*/\1/'`
# cannot use '{owner}' replacement because that isn't the origin branch
GH_BRANCH_PRS=$(gh api graphql -F name='{repo}' -F owner=$OWNER --paginate -q '.data.repository.refs.nodes[]' -f query='
query Branches($name: String!, $owner: String!, $endCursor: String) {
repository(owner: $owner, name: $name) {
refs(refPrefix: "refs/heads/", first: 100, after: $endCursor) {
totalCount
nodes {
name
associatedPullRequests(states: OPEN) {
totalCount
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}' | jq -s 'reduce .[] as $i ({}; .[$i.name] = $i.associatedPullRequests.totalCount)')
echo "${YELLOW}Branches without an open PR${ENDCOLOR}"
for branch in $(git branch -r --list "origin/*" --format="%(refname:lstrip=3)"); do
if [[ $branch == 'HEAD' || $branch == 'master' ]]; then
continue
fi
pr_count=$(echo "$GH_BRANCH_PRS" | jq ".\"$branch\"")
if [[ $pr_count -eq 0 ]]; then
git show-ref --verify --quiet "refs/heads/$branch"
if [[ $? -eq 0 ]]; then
extra="${LIGHT_YELLOW}(${YELLOW}L${LIGHT_YELLOW}/${YELLOW}R${LIGHT_YELLOW})"
else
extra="${LIGHT_YELLOW}( ${YELLOW}R${LIGHT_YELLOW} )"
fi
echo -e "- $extra $MAGENTA$branch$ENDCOLOR"
fi
done
else
echo "Not a git repository"
fi
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment