-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathmerge-pull-request.sh
More file actions
executable file
·127 lines (103 loc) · 3.76 KB
/
Copy pathmerge-pull-request.sh
File metadata and controls
executable file
·127 lines (103 loc) · 3.76 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env bash
# Repository-local entry point for the vendored GitHub pull-request merge tool.
#
# The wrapped tool intentionally remains interactive for merge inspection, signing, and pushing.
# This wrapper only validates Torrust Tracker's non-destructive preconditions and fixes the
# upstream repository and target branch. See .github/skills/dev/git-workflow/merge-pull-request/SKILL.md.
set -euo pipefail
readonly EXPECTED_REPOSITORY="torrust/torrust-tracker"
readonly TARGET_BRANCH="develop"
SCRIPT_DIRECTORY="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIRECTORY
readonly VENDORED_TOOL="${SCRIPT_DIRECTORY}/github-merge.py"
print_usage() {
cat >&2 <<'EOF'
Usage: ./contrib/dev-tools/git/merge-pull-request.sh [--dry-run] PULL_REQUEST
Validate the local maintainer merge-workflow prerequisites, then invoke the vendored merge tool
for torrust/torrust-tracker targeting develop.
Options:
--dry-run Validate only. Do not access GitHub, create temporary branches, merge, sign, or push.
-h, --help Show this help.
EOF
}
require_clean_working_tree() {
if [[ -n "$(git status --porcelain)" ]]; then
echo "ERROR: Working tree is not clean; preserve or stash unrelated work before merging." >&2
exit 1
fi
}
require_repository_configuration() {
local repository
repository=$(git config --get githubmerge.repository || true)
if [[ "${repository}" != "${EXPECTED_REPOSITORY}" ]]; then
if [[ -z "${repository}" ]]; then
echo "ERROR: githubmerge.repository is not configured; run 'git config githubmerge.repository ${EXPECTED_REPOSITORY}'." >&2
else
echo "ERROR: githubmerge.repository is '${repository}'; run 'git config githubmerge.repository ${EXPECTED_REPOSITORY}'." >&2
fi
exit 1
fi
}
require_target_branch() {
local current_branch
current_branch=$(git branch --show-current)
if [[ "${current_branch}" != "${TARGET_BRANCH}" ]]; then
echo "ERROR: Run this workflow from the '${TARGET_BRANCH}' branch; current branch is '${current_branch:-detached HEAD}'." >&2
exit 1
fi
}
require_signing_key() {
local signing_key
signing_key=$(git config --get user.signingkey || true)
if [[ -z "${signing_key}" ]]; then
echo "ERROR: user.signingkey is not configured; run 'git config --global user.signingkey <gpg-key-id>'." >&2
exit 1
fi
}
require_vendored_tool() {
if [[ ! -f "${VENDORED_TOOL}" || ! -r "${VENDORED_TOOL}" ]]; then
echo "ERROR: Vendored merge tool is unavailable: '${VENDORED_TOOL}'." >&2
exit 1
fi
}
require_python() {
if ! command -v python3 >/dev/null 2>&1; then
echo "ERROR: python3 is required to run the vendored merge tool; install Python 3 and retry." >&2
exit 1
fi
}
main() {
local dry_run=false
case "${1:-}" in
--dry-run)
dry_run=true
shift
;;
-h|--help)
print_usage
exit 0
;;
esac
if [[ $# -ne 1 || ! "${1}" =~ ^[1-9][0-9]*$ ]]; then
echo "ERROR: PULL_REQUEST must be a positive integer." >&2
print_usage
exit 2
fi
local pull_request=$1
if ! git rev-parse --show-toplevel >/dev/null 2>&1; then
echo "ERROR: Run this command inside a Git working tree." >&2
exit 1
fi
require_clean_working_tree
require_repository_configuration
require_target_branch
require_signing_key
if [[ "${dry_run}" == true ]]; then
printf 'Dry-run preflight passed for %s PR %s targeting %s.\n' "${EXPECTED_REPOSITORY}" "${pull_request}" "${TARGET_BRANCH}"
exit 0
fi
require_vendored_tool
require_python
exec python3 "${VENDORED_TOOL}" "${pull_request}" "${TARGET_BRANCH}"
}
main "$@"