forked from beatlabs/delete-old-branches-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-old-branches
executable file
·91 lines (77 loc) · 3.18 KB
/
delete-old-branches
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
90
91
#!/bin/bash
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}
EXCLUDE_BRANCH_REGEX=${INPUT_EXTRA_PROTECTED_BRANCH_REGEX:-^$}
EXCLUDE_TAG_REGEX=${INPUT_EXTRA_PROTECTED_TAG_REGEX:-^$}
branch_protected() {
local br=${1}
protected=$(curl -X GET -s -H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${BASE_URI}/repos/${REPO}/branches/${br}" | jq -r .protected)
# If we got null then something else happened (like no access error etc) so
# we can't determine the status for the branch
case ${protected} in
null) echo "Unable to determine status for branch: ${br}"; return 0 ;;
true) return 0 ;;
*) return 1 ;;
esac
}
extra_branch_or_tag_protected() {
local br=${1} ref="${2}"
if [[ "${ref}" == "branch" ]]; then
echo "${br}" | grep -qE "${EXCLUDE_BRANCH_REGEX}"
elif [[ "${ref}" == "tag" ]]; then
echo "${br}" | grep -qE "${EXCLUDE_TAG_REGEX}"
fi
return $?
}
delete_branch_or_tag() {
local br=${1} ref="${2}"
echo "Deleting: ${br}"
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}")
case ${status} in
204) ;;
*) echo "Deletion of branch ${br} failed with http_status=${status}"
echo "===== Dumping curl call ====="
[[ -f debug.log ]] && cat debug.log
;;
esac
else
echo "dry-run mode. Nothing changed!"
fi
}
main() {
# fetch history etc
git fetch --prune --unshallow --tags
for br in $(git ls-remote -q --heads --refs | sed "s@^.*heads/@@"); do
if [[ -z "$(git log --oneline -1 --since="${DATE}" origin/"${br}")" ]]; then
branch_protected "${br}" && echo "branch: ${br} is likely protected. Won't delete it" && continue
extra_branch_or_tag_protected "${br}" "branch" && echo "branch: ${br} is explicitly protected and won't be deleted" && continue
delete_branch_or_tag "${br}" "heads"
fi
done
if [[ "${DELETE_TAGS}" == true ]]; then
local tag_counter=1
for br in $(git ls-remote -q --tags --refs | sed "s@^.*tags/@@" | sort -rn); do
if [[ -z "$(git log --oneline -1 --since="${DATE}" "${br}")" ]]; then
if [[ ${tag_counter} -gt ${MINIMUM_TAGS} ]]; then
extra_branch_or_tag_protected "${br}" "tag" && echo "tag: ${br} is explicitely proetected and won't be deleted" && continue
delete_branch_or_tag "${br}" "tags"
else
echo "Not deleting tag ${br} due to minimum tag requirement(min: ${MINIMUM_TAGS})"
((tag_counter+=1))
fi
fi
done
fi
}
main "$@"