-
-
Notifications
You must be signed in to change notification settings - Fork 0
89 lines (77 loc) · 2.6 KB
/
Copy pathbranch-cleanup.yml
File metadata and controls
89 lines (77 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
#
# Branch Cleanup — manually delete stale branches server-side.
#
# Runs on GitHub's runners using GITHUB_TOKEN, deleting refs via the REST API
# directly. This works where a proxied `git push --delete` is blocked. Refuses
# to touch keep-listed branches and defaults to a dry run for safety.
name: Branch Cleanup
on:
workflow_dispatch:
inputs:
branches:
description: "Space-separated branch names to delete"
required: true
type: string
dry_run:
description: "If true, only report what would be deleted"
required: false
type: boolean
default: true
permissions:
contents: write
concurrency:
group: branch-cleanup
cancel-in-progress: false
jobs:
cleanup:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Delete branches
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INPUT_BRANCHES: ${{ inputs.branches }}
INPUT_DRY_RUN: ${{ inputs.dry_run }}
run: |
set -euo pipefail
# Branches this tool must never delete.
KEEP="main cicd/codeql-cron-monthly estate-standardization-20260607"
is_kept() {
local b="$1" k
for k in $KEEP; do
[ "$b" = "$k" ] && return 0
done
return 1
}
echo "Repository: ${GITHUB_REPOSITORY}"
echo "Dry run: ${INPUT_DRY_RUN}"
echo "Requested: ${INPUT_BRANCHES}"
echo
deleted=0; absent=0; skipped=0
for b in ${INPUT_BRANCHES}; do
if is_kept "$b"; then
echo "SKIP (protected): $b"
skipped=$((skipped + 1))
continue
fi
if [ "${INPUT_DRY_RUN}" = "true" ]; then
echo "DRY-RUN (would delete): $b"
continue
fi
# Idempotent: an already-absent ref counts as success.
if gh api -X DELETE "repos/${GITHUB_REPOSITORY}/git/refs/heads/${b}" --silent 2>/tmp/gh_err; then
echo "DELETED: $b"
deleted=$((deleted + 1))
elif grep -qiE 'Reference does not exist|Not Found|HTTP 404|HTTP 422' /tmp/gh_err; then
echo "ABSENT (already gone): $b"
absent=$((absent + 1))
else
echo "ERROR deleting $b:"
cat /tmp/gh_err
exit 1
fi
done
echo
echo "Summary: deleted=${deleted} absent=${absent} skipped=${skipped}"