-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-mr
executable file
·251 lines (237 loc) · 5.48 KB
/
git-mr
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env bash
set -euo pipefail
if ! apiurl="$(git config --get gitlab.url)/api/v4"; then
echo "You forgot to set gitlab.url" >&2
exit 1
fi
if ! slug="$(git config --get gitlab.slug)"; then
echo "You forgot to set gitlab.slug" >&2
exit 1
fi
if ! token="$(git config --get gitlab.token)"; then
echo "You forgot to set gitlab.token" >&2
echo "The token needs the 'api' scope only" >&2
exit 1
fi
branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
declare -A uidCache
# Urlencode a string
urlencode() {
# https://stackoverflow.com/a/7506695
echo -ne "${1}" | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g'
}
# Run curl with auth
curl() {
command curl -s -H "Authorization: Bearer ${token}" "${@}"
}
# Fetch the UID of a user by name
fetchUid() {
# Fetch from the cache if cached
if [[ -v uidCache["$1"] ]]; then
echo "${uidCache[\"$1\"]}"
return
fi
uid="$(curl "${apiurl}/users/?username=$(urlencode "${1}")" | jq '.[].id')"
if [ -z "${uid}" ]; then
echo "User ${1} does not exist" >&2
exit 1
fi
uidCache["${1}"]="${uid}"
echo "${uid}"
}
# Show details if desired
paddedTitle() {
tput bold
printf %-13s "${1}: "
tput sgr0
}
show() {
json="${1}"
{
paddedTitle ID
wip="$(jq '.[].work_in_progress' <<< "${json}")"
if [ "${wip}" = true ]; then
wip=" $(tput setaf 3)(WIP)$(tput sgr0)"
else
wip=
fi
echo "!$(jq '.[].iid' <<< "${json}")${wip} - $(jq -r '.[].web_url' <<< "${json}")"
}
{
paddedTitle Branch
jq -r '.[].source_branch' <<< "${json}"
}
{
paddedTitle Automerge
automerge="$(jq '.[].merge_when_pipeline_succeeds' <<< "${json}")"
if [ "${automerge}" = true ]; then
echo "$(tput setaf 2)yes$(tput sgr0)"
else
echo "$(tput setaf 1)no$(tput sgr0)"
fi
}
{
paddedTitle "Last update"
jq -r '.[].updated_at' <<< "${json}"
}
{
paddedTitle "Assignees"
jq -r '[.[].assignees | .[] | .username] | join(", ")' <<< "${json}"
}
{
paddedTitle "Reviewers"
jq -r '[.[].reviewers | .[] | .username] | join(", ")' <<< "${json}"
}
{
paddedTitle Title
jq -r '.[].title' <<< "${json}"
}
{
paddedTitle Description
echo
jq -r '.[].description' <<< "${json}"
}
}
# Parse arguments
labels=()
assignees=()
reviewers=()
ready=0
draft=0
needsUpdate=0
show=0
usage() {
echo "Usage: git mr [-h] [-d/-R] [-s] [-t title] [-b body] [-l label].. [-a assignee].. [-r reviewer].."
exit "${1}"
}
while getopts "hdRst:b:l:a:r:" o; do
case "${o}" in
t)
title="${OPTARG}"
needsUpdate=1
;;
b)
body="${OPTARG}"
needsUpdate=1
;;
l)
labels+=("${OPTARG}")
needsUpdate=1
;;
a)
assignees+=("${OPTARG}")
needsUpdate=1
;;
r)
reviewers+=("${OPTARG}")
needsUpdate=1
;;
d)
draft=1
needsUpdate=1
;;
R)
ready=1
needsUpdate=1
;;
s)
show=1
;;
h)
usage 0
;;
*)
usage 1
;;
esac
done
if [ "${draft}" = 1 ] && [ "${ready}" = 1 ]; then
echo "A MR cannot be both a draft and ready" >&2
exit 1
fi
# Format params
if [ "${#labels[@]}" != 0 ]; then
labelsParam='&labels='
first=1
for label in "${labels[@]}"; do
if [ "${first}" = 1 ]; then
first=0
else
labelsParam="${labelsParam},"
fi
labelsParam="${labelsParam}$(urlencode "${label}")"
done
else
labelsParam=
fi
if [ "${#assignees[@]}" != 0 ]; then
assigneesParam='&assignee_ids='
first=1
for username in "${assignees[@]}"; do
if [ "${first}" = 1 ]; then
first=0
else
assigneesParam="${assigneesParam},"
fi
uid="$(fetchUid "${username}")"
assigneesParam="${assigneesParam}${uid}"
done
else
assigneesParam=
fi
if [ "${#reviewers[@]}" != 0 ]; then
reviewersParam='&reviewer_ids='
first=1
for username in "${reviewers[@]}"; do
if [ "${first}" = 1 ]; then
first=0
else
reviewersParam="${reviewersParam},"
fi
uid="$(fetchUid "${username}")"
reviewersParam="${reviewersParam}${uid}"
done
else
reviewersParam=
fi
# Find existing MR
json="$(curl "${apiurl}/projects/$(urlencode "${slug}")/merge_requests?state=opened&source_branch=$(urlencode "${branch}")&target_branch=master")"
webUrl="$(jq -r '.[].web_url' <<< "${json}")"
if [ -n "${webUrl}" ]; then
# Maybe update the MR
if [ "${needsUpdate}" = 1 ]; then
id="$(jq -r '.[].iid' <<< "${json}")"
oldTitle="$(jq -r '.[].title' <<< "${json}")"
echo "Updating !${id}" >&2
if [ -n "${title:-}" ]; then
if [ "${draft}" = 1 ]; then
title="Draft: ${title}"
fi
titleParam="&title=$(urlencode "${title}")"
else
if [ "${draft}" = 1 ]; then
titleParam="&title=$(urlencode "Draft: ${oldTitle}")"
elif [ "${ready}" = 1 ]; then
titleParam="&title=$(urlencode "${oldTitle#"Draft: "}")"
else
titleParam=
fi
fi
if [ -n "${body:-}" ]; then
bodyParam="&description=$(urlencode "${body}")"
else
bodyParam=
fi
curl -XPUT "$(sed 's/&/?/' <<< "${apiurl}/projects/$(urlencode "${slug}")/merge_requests/${id}${titleParam}${bodyParam}${labelsParam}${assigneesParam}${reviewersParam}")" >/dev/null
fi
show "$(curl "${apiurl}/projects/$(urlencode "${slug}")/merge_requests?state=opened&source_branch=$(urlencode "${branch}")&target_branch=master")"
exit 0
fi
# Create new MR
[ -z "${title:-}" ] && title="$(git log -1 --pretty=%s)"
[ -z "${body:-}" ] && body="$(git log -1 --pretty=%b)"
if [ "${draft}" = 1 ]; then
title="Draft: ${title}"
fi
json="$(curl -XPOST "${apiurl}/projects/$(urlencode "${slug}")/merge_requests?source_branch=$(urlencode "${branch}")&target_branch=master&remove_source_branch=true&title=$(urlencode "${title}")&description=$(urlencode "${body}")${labelsParam}${assigneesParam}${reviewersParam}")"
show "[${json}]"