Skip to content

Instantly share code, notes, and snippets.

@bartoszmajsak
Last active December 7, 2017 18:55
Show Gist options
  • Save bartoszmajsak/31e0ea6da797d80febcf9443fd2151ae to your computer and use it in GitHub Desktop.
Save bartoszmajsak/31e0ea6da797d80febcf9443fd2151ae to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "Options:"
echo "-r github repository name, together with user or organization, e.g. fabric8io/kubernetes-model"
echo "-t github token (https://github.com/settings/tokens)"
exit 0
;;
-r)
shift
if test $# -gt 0; then
REPO_NAME=$1
DIR=$(echo $1 | cut -d '/' -f 2)
fi
shift
;;
-t)
shift
if test $# -gt 0; then
TOKEN=$1
fi
shift
;;
*)
echo "$1 is not a recognized flag!"
exit -1
;;
esac
done
TOKEN_HEADER="Authorization:token ${TOKEN}"
NEXT_LINK="https://api.github.com/repos/${REPO_NAME}/branches"
while [ ! -z "${NEXT_LINK// }" ]; do
branches=$(http ${NEXT_LINK} "${TOKEN_HEADER}" | jq -c '[.[] | select(.name | startswith("versionUpdate")) | .name ]')
while read -r branch
do
http DELETE https://api.github.com/repos/${REPO_NAME}/git/refs/heads/${branch} "${TOKEN_HEADER}"
done < <(jq -cr '.[]' <<<"${branches}")
NEXT_LINK=$(http --print=h ${NEXT_LINK} "${TOKEN_HEADER}" | grep -Eo '<.*>; rel="next"' | cut -d';' -f1 | sed 's/<\(.*\)>/\1/p' -n)
done
@bartoszmajsak
Copy link
Author

bartoszmajsak commented Dec 7, 2017

Prerequisite

sudo dnf install httpie jq

GitHub token - read/write to org or repo

Execution

./clean-branches.sh -r fabric8-ui/fabric8-planner -t TOKEN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment