Repository navigation
Expand file tree
/
Copy pathapprove_pr.sh
More file actions
executable file
·79 lines (61 loc) · 1.99 KB
/
Copy pathapprove_pr.sh
File metadata and controls
executable file
·79 lines (61 loc) · 1.99 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
#!/usr/bin/env bash
#
# merge_pr.sh - Merge one or more GitHub PRs and delete their branches afterwards.
#
# Usage:
# ./merge_pr.sh <PR_NUMBER> [PR_NUMBER ...]
#
# Requires: GitHub CLI (`gh`) installed and authenticated (`gh auth login`).
set -euo pipefail
# ---- Configuration: edit these for your repo ----
ORG="OpenConext"
REPO="OpenConext-dashboard"
# ---------------------------------------------------
# --- Merge method: one of "merge", "squash", "rebase" ---
MERGE_METHOD="squash"
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <PR_NUMBER> [PR_NUMBER ...]" >&2
exit 1
fi
REPO_FULL="${ORG}/${REPO}"
if ! command -v gh &> /dev/null; then
echo "Error: GitHub CLI ('gh') is not installed or not in PATH." >&2
exit 1
fi
merge_pr() {
local pr_number="$1"
echo "Fetching info for PR #${pr_number} in ${REPO_FULL}..."
# Get PR state and branch name
local pr_state branch_name
pr_state=$(gh pr view "$pr_number" --repo "$REPO_FULL" --json state --jq '.state')
branch_name=$(gh pr view "$pr_number" --repo "$REPO_FULL" --json headRefName --jq '.headRefName')
if [[ -z "$branch_name" ]]; then
echo "Error: Could not determine branch name for PR #${pr_number}." >&2
return 1
fi
if [[ "$pr_state" != "OPEN" ]]; then
echo "Error: PR #${pr_number} is not open (current state: ${pr_state})." >&2
return 1
fi
echo "PR #${pr_number} found. Branch: ${branch_name}"
echo "Merging with method: ${MERGE_METHOD}..."
# Merge the PR and delete the branch (gh can do both in one call)
gh pr merge "$pr_number" \
--repo "$REPO_FULL" \
--"${MERGE_METHOD}" \
--delete-branch
echo "PR #${pr_number} merged successfully and branch '${branch_name}' deleted."
}
FAILED_PRS=()
for pr_number in "$@"; do
echo "=================================================="
if ! merge_pr "$pr_number"; then
FAILED_PRS+=("$pr_number")
fi
echo
done
if [[ ${#FAILED_PRS[@]} -gt 0 ]]; then
echo "Failed to merge PR(s): ${FAILED_PRS[*]}" >&2
exit 1
fi
echo "All PRs merged successfully."