-
Notifications
You must be signed in to change notification settings - Fork 11
/
pr-merge
executable file
·134 lines (118 loc) · 3.43 KB
/
pr-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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
#
# This script expects:
#
# 0. jq commandline JSON processor, see https://stedolan.github.io/jq
#
# 1. .netrc with configuration as follows:
# machine api.github.com
# login UID
# password ACCESS_TOKEN
#
# 2. Remote URLs in your jboss-eap clone using git protocol instead of https, ie. something like:
# origin [email protected]:istudens/jboss-eap7.git (fetch)
# origin [email protected]:istudens/jboss-eap7.git (push)
# upstream [email protected]:jbossas/jboss-eap7.git (fetch)
# upstream [email protected]:jbossas/jboss-eap7.git (push)
#
# 3. In case of remotes like above, the first parameter should be 'upstream' and the second parameter should be a number of PR being merged.
#
usage() {
local script_name=$(basename "${0}")
echo 1>&2 "Usage: ${script_name} <remote> <pr>"
echo 1>&2 'Ex:'
echo 1>&2 "${script_name} upstream 3"
}
isNetrcPresent() {
local netrc_file=${1:-"${HOME}/.netrc"}
if [ ! -e "${netrc_file}" -o ! -r "${netrc_file}" ] ; then
echo "Missing configuration file ${netrc_file} - required by CuRL to authentificate against Github."
exit 4
fi
}
isJqInstalled() {
local jq_url='https://stedolan.github.io/jq'
which jq > /dev/null
if [ "${?}" != 0 ]; then
echo "No jq commandline install - please install JQ before using this script: ${jq_url}"
fi
}
isHubInstalled() {
which hub &> /dev/null
if [ "${?}" != 0 ]; then
echo "The hub commandline tool is not installed. Using curl."
false
fi
}
apiCommand() {
if [ isHubInstalled ]; then
hub api "${1}"
else
curl -L -s -n "${1}"
fi
}
sanity_check() {
isJqInstalled
if [ ! isHubInstalled ]; then
isNetrcPresent
fi
}
sanity_check
set -eo pipefail
if [ ${#} != 2 ]; then
echo 1>&2 "Usage: $0 <remote> <pr>"
exit 1
fi
readonly BRANCH=$(git rev-parse --abbrev-ref HEAD)
readonly REMOTE=${1}
readonly PR_ID=${2}
if [ -z "${REMOTE}" ]; then
echo "No REMOTE provided."
usage
exit 2
fi
if [ -z "${PR_ID}" ]; then
echo "No PR provided."
usage
exit 3
fi
echo "Merging ${PR_ID} from ${REMOTE} onto ${BRANCH}"
readonly FETCH_URL=$(git remote -v | grep "^${REMOTE}[ ]*" | grep 'fetch' | cut -f2 | cut -d" " -f1)
if [ -z "${FETCH_URL}" ]; then
usage
echo "Unable to retrieve remote fetch URL from ${REMOTE}."
echo ''
echo 'Available remotes in your repository are:'
git remote -v
exit 4
fi
# replace all slashes with colons to keep delimiter consistent
readonly URL_PARSE=$(echo "${FETCH_URL}" | sed "s/\//:/g")
readonly REPO_PART="${URL_PARSE##*:}"
readonly REPO=$(echo "${REPO_PART}" | sed "s/.git//")
readonly URL_LEFT="${URL_PARSE:0:$((${#URL_PARSE} - ${#REPO_PART} - 1))}"
readonly OWNER="${URL_LEFT##*:}"
if [ -z "${OWNER}" -o -z "${REPO}" ]; then
echo "Unable to parse owner and repo from ${FETCH_URL}"
exit 4
fi
readonly PR_GITHUB_API_URL="https://api.github.com/repos/${OWNER}/${REPO}/pulls/${PR_ID}"
readonly PULL=$(apiCommand "${PR_GITHUB_API_URL}")
set -e
echo -n "Retrieving PR information from ${PR_GITHUB_API_URL}... "
if [ $(echo ${PULL} | jq .status) == '"404"' ]; then
echo "Failed."
echo "GitHub response:"
echo "${PULL}"
exit 2
fi
echo 'Done.'
set +e
readonly MSG="Merge pull request #${PR_ID} from $(echo ${PULL} | jq -r .head.label) $(echo $PULL | jq -r .title)"
echo "Git commit message will be: ${MSG}"
if [ -z "${NO_STOP_BEFORE_MERGE}" ] ; then
echo -n "Continue..." && read _
fi
set -ex
git fetch "${REMOTE}" "pull/${PR_ID}/head"
git merge --no-ff -m "${MSG}" 'FETCH_HEAD'