-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-squash-merge
executable file
·106 lines (84 loc) · 2.49 KB
/
gh-squash-merge
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
#!/usr/bin/env bash
set -eo pipefail
WORK_FILE=$(mktemp --tmpdir squash-merge-commit-msg.XXXXXX)
cleanup() {
if [[ -n "${WORK_FILE:-}" ]]; then
rm -f "$WORK_FILE"
fi
}
usage() {
cat <<EOF
Usage: gh squash-merge [<number> | <url> | <branch>] [flags]
Issues a squash merge on the PR.
Without an argument, the pull request that belongs to the current branch
is used.
Arguments
<number> The PR number to squash merge
<url> The PR URL to squash merge
<branch> The branch of PR URL to squash merge
Flags
--admin Use administrator privileges to merge a pull request that does not meet requirement
--auto Automatically merge only after necessary requirements are met
-R, --repo [HOST/]OWNER/REPO Select another repository using the [HOST/]OWNER/REPO format
Notes:
- The title of the commit will be based on GitHub repository settings
- The PR body contains a section similar to the following which lets us autogenerate
squash merge commit message
- If PR body does not contain a section similar an empty body will be set
<!-- SQUASH_MERGE_START -->
- this forms the body of the commit message and could be multiple lines
but generally we tend towards
- doc: conventional commit messages
<!-- SQUASH_MERGE_END -->
EXAMPLES
gh squash-merge
gh squash-merge 811
gh squash-merge --repo quotidian-ennui/gh-squash-merge 811
gh squash-merge https://github.com/quotidian-ennui/gh-squash-merge/pull/811
gh squash-merge feature/update
EOF
exit 1
}
trap cleanup 1 2 15
additional_args=()
ARGS=$(getopt --options 'R::h' --longoptions 'repo:,help,admin,auto' -- "${@}")
eval "set -- ${ARGS}"
while true; do
case "${1}" in
--h | --help)
usage
;;
--R | --repo)
repo="${2}"
shift 2
;;
--admin | --auto)
additional_args+=("${1}")
shift
;;
--)
shift
break
;;
*)
usage
;;
esac
done
remaining_args=("${1}")
if [ -n "${repo:-}" ] && [ -z "${remaining_args:-}" ]; then
echo "argument required when using the --repo flag"
usage
fi
details_args=()
if [ -n "${repo:-}" ]; then
details_args+=('--repo')
details_args+=("${repo}")
fi
if [ -n "${remaining_args:-}" ]; then
details_args+=("${remaining_args}")
fi
body=$(gh pr view --json body $"${details_args[@]}" --jq '.body')
echo "$body" | awk '/SQUASH_MERGE_START/,/SQUASH_MERGE_END/' | { grep -v "SQUASH_MERGE" || test $? = 1; } >"$WORK_FILE"
gh pr merge $"${details_args[@]}" --delete-branch --squash --body-file "$WORK_FILE" $"${additional_args[@]}"
cleanup