Skip to content

Commit

Permalink
fix: retrieve all branches through pagination (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikaro authored Oct 18, 2023
1 parent 3aac108 commit 3a54bdf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM alpine:latest
LABEL maintainer="[email protected]"

RUN apk add --no-cache bash ca-certificates curl git jq
RUN apk add --no-cache bash ca-certificates git github-cli

COPY delete-old-branches /usr/bin/delete-old-branches

Expand Down
18 changes: 8 additions & 10 deletions delete-old-branches
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ set -eo pipefail
[[ -n ${INPUT_REPO_TOKEN} ]] || { echo "Please set the REPO_TOKEN input"; exit 1; }
[[ -n ${INPUT_DATE} ]] || { echo "Please specify a suitable date input for branch filtering"; exit 1; }

BASE_URI="https://api.github.com"
REPO="${GITHUB_REPOSITORY}"
DATE=${INPUT_DATE}
GITHUB_TOKEN=${INPUT_REPO_TOKEN}
DRY_RUN=${INPUT_DRY_RUN:-true}
DELETE_TAGS=${INPUT_DELETE_TAGS:-false}
MINIMUM_TAGS=${INPUT_MINIMUM_TAGS:-0}
Expand All @@ -17,6 +15,9 @@ EXCLUDE_BRANCH_REGEX=${INPUT_EXTRA_PROTECTED_BRANCH_REGEX:-^$}
EXCLUDE_TAG_REGEX=${INPUT_EXTRA_PROTECTED_TAG_REGEX:-^$}
EXCLUDE_OPEN_PR_BRANCHES=${INPUT_EXCLUDE_OPEN_PR_BRANCHES:-true}

# used for GitHub CLI authentication
export GITHUB_TOKEN=${INPUT_REPO_TOKEN}

echo "was_dry_run=${DRY_RUN}" >> $GITHUB_OUTPUT
deleted_branches=()

Expand All @@ -38,8 +39,7 @@ default_branch_protected() {
branch_protected() {
local br=${1}

protected=$(curl -X GET -s -H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${BASE_URI}/repos/${REPO}/branches/${br}" | jq -r .protected)
protected=$(gh api "repos/${REPO}/branches/${br}" --jq '.protected')

# If we got null then something else happened (like no access error etc) so
# we can't determine the status for the branch
Expand Down Expand Up @@ -68,8 +68,7 @@ is_pr_open_on_branch() {
fi

local br=${1}
open_prs_branches=$(curl -X GET -s -H "Authorization: token ${GITHUB_TOKEN}" \
"${BASE_URI}/repos/${REPO}/pulls" | jq '.[].head.ref' | tr -d '"')
open_prs_branches=$(gh api "repos/${REPO}/pulls" --jq '.[].head.ref' --paginate)

for pr_br in ${open_prs_branches}; do
if [[ "${pr_br}" == "${br}" ]]; then
Expand All @@ -87,13 +86,12 @@ delete_branch_or_tag() {
echo "Deleting: ${br}"
echo "To recreate run: git branch '${br}' '${sha}'"
if [[ "${DRY_RUN}" == false ]]; then
status=$(curl -I -X DELETE -o debug.log -s -H "Authorization: Bearer ${GITHUB_TOKEN}" \
-w "%{http_code}" "${BASE_URI}/repos/${REPO}/git/refs/${ref}/${br}")
status=$(gh api --method DELETE "repos/${REPO}/git/refs/${ref}/${br}" &> debug.log && echo ok || echo failed)

case ${status} in
204) ;;
ok) ;;
*) echo "Deletion of branch ${br} failed with http_status=${status}"
echo "===== Dumping curl call ====="
echo "===== Dumping log ====="
[[ -f debug.log ]] && cat debug.log
;;
esac
Expand Down

0 comments on commit 3a54bdf

Please sign in to comment.