-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcleanup-pr-branch
executable file
·100 lines (83 loc) · 2.8 KB
/
cleanup-pr-branch
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
92
93
94
95
96
97
98
99
100
#!/bin/bash
set -e
set -o pipefail
if [[ -n "$TOKEN" ]]; then
GITHUB_TOKEN=$TOKEN
fi
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi
URI=https://api.github.com
API_VERSION=v3
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json"
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
# Github Actions uses either status code 0 for success or any other code for failure.
# Docs: https://help.github.com/en/articles/virtual-environments-for-github-actions#exit-codes-and-statuses
NO_BRANCH_DELETED_EXIT_CODE=${NO_BRANCH_DELETED_EXIT_CODE:-0}
main(){
action=$(jq --raw-output .action "$GITHUB_EVENT_PATH")
merged=$(jq --raw-output .pull_request.merged "$GITHUB_EVENT_PATH")
echo "DEBUG -> action: $action merged: $merged"
if [[ "$action" != "closed" ]] || [[ "$merged" != "true" ]]; then
exit "$NO_BRANCH_DELETED_EXIT_CODE"
fi
# delete the branch.
ref=$(jq --raw-output .pull_request.head.ref "$GITHUB_EVENT_PATH")
owner=$(jq --raw-output .pull_request.head.repo.owner.login "$GITHUB_EVENT_PATH")
repo=$(jq --raw-output .pull_request.head.repo.name "$GITHUB_EVENT_PATH")
default_branch=$(
curl -XGET -fsSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
"${URI}/repos/${owner}/${repo}" | jq .default_branch
)
if [[ "$ref" == "$default_branch" ]]; then
# Never delete the default branch.
echo "Will not delete default branch (${default_branch}) for ${owner}/${repo}, exiting."
exit 0
fi
is_protected=$(
curl -XGET -fsSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
"${URI}/repos/${owner}/${repo}/branches/${ref}" | jq .protected
)
if [[ "$is_protected" == "true" ]]; then
# Never delete protected branches
echo "Will not delete protected branch (${ref}) for ${owner}/${repo}, exiting."
exit 0
fi
pulls_with_ref_as_base=$(
curl -XGET -fsSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
"${URI}/repos/${owner}/${repo}/pulls?state=open&base=${ref}"
)
has_pulls_with_ref_as_base=$(echo "$pulls_with_ref_as_base" | jq 'has(0)')
if [[ "$has_pulls_with_ref_as_base" != false ]]; then
# Do not delete if the branch is a base branch of another pull request
pr=$(echo "$pulls_with_ref_as_base" | jq '.[0].number')
echo "${ref} is the base branch of PR #${pr} for ${owner}/${repo}, exiting."
exit 0
fi
echo "Deleting branch ref $ref for owner ${owner}/${repo}..."
response=$(
curl -XDELETE -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
--output /dev/null \
--write-out "%{http_code}" \
"${URI}/repos/${owner}/${repo}/git/refs/heads/${ref}"
)
if [[ ${response} -eq 422 ]]; then
echo "The branch is already gone!"
elif [[ ${response} -eq 204 ]]; then
echo "Branch delete success!"
else
echo "Something unexpected happened!"
exit "$NO_BRANCH_DELETED_EXIT_CODE"
fi
exit 0
}
main "$@"