-
Notifications
You must be signed in to change notification settings - Fork 4
/
prune_preview_branches.sh
executable file
·70 lines (58 loc) · 2.16 KB
/
prune_preview_branches.sh
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
#!/usr/bin/env bash
set -e
check_prerequisites() {
# Verify if jq is installed and GITHUB_TOKEN is set.
which jq &>/dev/null || (echo "Error: jq is required but not installed. You can download and install jq from <https://stedolan.github.io/jq/download/>." && exit 1)
test -n "$GITHUB_TOKEN" || (echo "Error: GITHUB_TOKEN (repo scope) is required but not set." && exit 1)
}
is_pr_merged() {
# Verify if a PR is merged.
local repo_owner="$1" repo_name="$2" pr_number="$3"
local api_url="https://api.github.com/repos/${repo_owner}/${repo_name}/pulls/${pr_number}"
local is_merged
is_merged=$(curl -fsSL -H "Authorization: token $GITHUB_TOKEN" "$api_url" | jq -r '.merged_at')
[[ -n "$is_merged" && "$is_merged" != "null" ]]
}
process_branch() {
# If the branch starts with 'preview', it checks if the associated pull request is merged.
# If it is merged, the branch is added to a list of branches to be pruned. Otherwise, the branch is skipped.
local branch="$1"
if [[ $branch == preview* ]]; then
IFS='/' read -r _ repo_owner repo_name pr_number <<< "$branch"
if is_pr_merged "$repo_owner" "$repo_name" "$pr_number"; then
echo "$branch: This preview PR is merged, will be pruned."
PRUNE_BRANCHES+=("$branch")
else
echo "$branch: This preview PR is not merged, skipping."
fi
fi
}
delete_branch() {
# Delete a branch based on the specified mode (local or remote).
local branch="$1" mode="$2"
echo "Deleting branch: $branch"
case "$mode" in
local) git branch -D "$branch" ;;
remote) git push origin --delete "$branch" ;;
*) echo "No action specified for branch deletion." ;;
esac
}
# Get the directory of this script.
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
cd "$SCRIPT_DIR"
check_prerequisites
# Check if arguments are passed.
if [ "$#" -eq 0 ]; then
set +o posix
mapfile -t BRANCHES < <(git branch --list | sed 's/^[*[:space:]]*//')
else
BRANCHES=("$@")
fi
PRUNE_BRANCHES=()
for branch in "${BRANCHES[@]}"; do
process_branch "$branch"
done
# Delete branches in the prune list.
for branch in "${PRUNE_BRANCHES[@]}"; do
delete_branch "$branch" "${DELETE_BRANCHES:-}"
done