forked from CJohnsonADAH/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-gitlab-do
executable file
·652 lines (521 loc) · 24.5 KB
/
adpn-gitlab-do
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#!/bin/bash
#
# adpn-gitlab-do: script to handle Gitlab API commands
#
# @version 2021.0802
SCRIPTPATH="$(which "$0")"
SCRIPTPATH="$( readlink --canonicalize "${SCRIPTPATH}" )"
SCRIPTDIR="$(dirname "${SCRIPTPATH}")"
source "${SCRIPTDIR}/adpn-define-aliases"
__USAGE__="Usage: ${SCRIPT} [--version] [--help] <CMD> [<ARGS>]"
__HELP__="[${SCRIPT}] Try '${SCRIPT} help' for more information."
__DOC__="""${__USAGE__}
--version Display the version of the script
--help Display these usage notes
The most commonly used ${SCRIPT} commands are:
list List data about projects that you are a member of
issue Retrieve information about a Gitlab Issue thread
Exit codes:
0 = success (successful operation and expected result)
1-254 = error in executing command
255 = command not supported
"""
##########################################################################################
### COMMAND LINE: loop through switches ##################################################
##########################################################################################
declare -a _ARGV ; _ARGV=("$0")
declare -a _CMDLINE ; _CMDLINE=("$0")
declare -A _PARAM=()
declare -a SWITCHFILES ; SWITCHFILES=()
if [[ -r "${CONFFILE}" ]] ; then
SWITCHFILES+=(${CONFFILE})
fi
CMDLINETXT=$(mktemp)
until [[ "$#" -lt 1 ]] ; do
_CMDLINE+=("$1")
printf "%s\n" "$1" >> "${CMDLINETXT}"
shift
done
SWITCHFILES+=(${CMDLINETXT})
adpn_command_line "${SWITCHFILES[@]}"
rm "${CMDLINETXT}"
##########################################################################################
## COMMUNICATIONS: check for information to access encrypted persistent data #############
##########################################################################################
export ADPN_STASH_FILE
export ADPN_STASH_KEY
ADPN_STASH_CLOSE=""
# stash open: this will output some variable settings
if [[ -z "${ADPN_STASH_FILE}" || -z "${ADPN_STASH_KEY}" ]] ; then
eval $( "${ADPN_STASH}" open --quiet --if-needed ${DDBG} )
fi
##########################################################################################
### SCRIPT: DETERMINE COMMAND, THEN EXECUTE PIPELINE ####################################
##########################################################################################
EXITCODE=0
CMD="${_ARGV[1]}"
adpn_set_display_settings # V, VV, Q, QQ, DBG, DBGLEVEL, DDBG, SCRIPT_CMD_NAME / @see adpn-define-aliases
declare -a CONTEXT_SW=( "--context=${SCRIPT_CMD_NAME}" )
[[ -n "${DDBG}" ]] && CONTEXT_SW+=( "${DDBG}" )
[[ -n "${_SWITCHES[dry-run]}" ]] && CONTEXT_SW+=( "${_SWITCHES[dry-run]}" )
[[ -n "${_SWITCHES[token]}" ]] && CONTEXT_SW+=( "${_SWITCHES[token]}" )
adpn_debug 100,adpn,gitlab,"${SCRIPT}","${CMD}" "${LINENO}" "entered, cmd=%s, cmdline='%s'" "${CMD}" "$( join_by "', '" "${_CMDLINE[@]:2}" )"
adpn_debug 400,adpn,gitlab,"${SCRIPT}",stash "${LINENO}" "ADPN_STASH_FILE='%s'" "${ADPN_STASH_FILE}"
adpn_script_handle_version_or_help
declare -A _NO_RECURSE=( [token]="token|parse|help|version" )
if [[ "${CMD}" =~ ^(${_NO_RECURSE[token]}) ]] ; then
# avoid infinite recursion, double-execution, unwanted prompts & errors, etc.
[[ -n "${_SWITCHES[token]}" ]] && PRIVATE_TOKEN="${_SWITCHES[token]}"
else
# Retrieve Gitlab token for ADPN script access to Gitlab
# This may produce an interactive prompt for the master password on a kdbx file
PRIVATE_TOKEN=$( "${ME}" token "${CONTEXT_SW[@]}" )
ADG_CODE="$?"
# We should only have to retrieve this once, at the first request for the token.
# The encrypted stash file can pass it down to any child calls, and up to any parent calls.
if [[ -n "${PRIVATE_TOKEN}" ]] ; then
export PRIVATE_TOKEN
elif [[ "${ADG_CODE}" -gt 0 ]] ; then
EXITCODE="${ADG_CODE}"
adpn_notice "${LINENO}" "REQUIRED: Gitlab API access token."
fi
fi
if [[ "${EXITCODE}" -eq 0 ]] ; then
case "${CMD}" in
""|"list"|"project")
# @method adpn gitlab list
# Display a list of available projects.
#
# Usage: adpn gitlab list
#
# @version 2021.0331
if [[ -z "${CMD}" ]] ; then
CMD="list"
fi
declare -a ADG_GET_ARGV=( "/projects?membership=1" )
[[ -z "${_SWITCHES[no-fail]}" ]] && ADG_GET_ARGV+=( "--fail" )
STASH_CACHE="gitlab/api/get-list"
OUT_JSON=$( adpn_parameter_from ".${STASH_CACHE}<(adpn stash)" )
if [[ -z "${OUT_JSON}" ]] ; then
OUT_JSON="$( "${ME}" get "${ADG_GET_ARGV[@]}" "${CONTEXT_SW[@]}" )" ; EXITCODE="$?"
else
adpn_debug 20,adpn,gitlab,"${SCRIPT}" "${LINENO}" "Retrieved JSON response from HTTP cache (%d chars)" $( printf "%s" "${OUT_JSON}" | wc -c )
fi
if [[ "${EXITCODE}" -gt 0 ]] ; then
adpn_notice "${LINENO}" "Gitlab HTTP API request (GET %s) failed, exit code=%d" "${ADG_GET_ARGV[0]}" "${EXITCODE}"
elif [[ -n "${_ARGV[*]:2}" ]] ; then
PATH_REGEX=$( join_by "|" "${_ARGV[@]:2}" )
REGEX="\bpath_with_namespace\s+(${PATH_REGEX})\b$"
adpn_debug 01,adpn,gitlab,"${SCRIPT}" "${LINENO}" "%s " "${SCRIPT}" get "/projects?membership=1" "|" adpn-json.py --parse --key=id --key=path_with_namespace "|" grep -E "${REGEX}"
printf "%s" "${OUT_JSON}" | adpn-json.py --parse --key=id --key=path_with_namespace | grep -E "${REGEX}"
else
printf "%s" "${OUT_JSON}" | adpn-json.py --parse --key=id --key=path_with_namespace
fi
if [[ "${EXITCODE}" -eq 0 && -n "${OUT_JSON}" ]] ; then
"${ADPN}" stash post "${STASH_CACHE}" "${OUT_JSON}" "${CONTEXT_SW[@]}"
fi
EXITCODE="$?"
;;
"opened")
PROJECT="$( adpn_parameter_from "--project" ".gitlab/repository" )"
STATE="$( adpn_parameter_from "--state" "opened" )"
SORT="$( adpn_parameter_from "--sort" "desc" )"
ORDER_BY="$( adpn_parameter_from "--order_by" "updated_at" )"
LABELS="$( adpn_parameter_from "--labels" "Content Ingest" )"
declare -a ADG_ISSUE_ARGV=("--key=iid")
[[ -n "${PROJECT}" ]] && ADG_ISSUE_ARGV+=( "--project=${PROJECT}" )
[[ -n "${LABELS}" ]] && ADG_ISSUE_ARGV+=( "--labels=${LABELS}" )
[[ -n "${STATE}" ]] && ADG_ISSUE_ARGV+=( "--state=${STATE}" )
[[ -n "${ORDER_BY}" ]] && ADG_ISSUE_ARGV+=( "--order_by=${ORDER_BY}" )
[[ -n "${SORT}" ]] && ADG_ISSUE_ARGV+=( "--sort=${SORT}" )
ADG_ISSUE_ARGV+=( "${CONTEXT_SW[@]}" )
IID="$( "${ME}" issue "${ADG_ISSUE_ARGV[@]}" | head -n 1 )"
[[ -n "${IID}" ]] && printf "%s#%d" "${PROJECT}" "${IID}" || EXITCODE=1
;;
"issue")
METHOD=get
declare -a ADPN_GITLAB_SW=("--fail")
if [[ -n "${_PARAM[post]}" ]] ; then
METHOD=post
[[ -n "${_PARAM[dry-run]}" ]] && ADPN_GITLAB_SW+=("--dry-run")
[[ -n "${_PARAM[title]}" ]] && ADPN_GITLAB_SW+=("--data-title=${_PARAM[title]}")
[[ -n "${_PARAM[description]}" ]] && ADPN_GITLAB_SW+=("--data-description=${_PARAM[description]}")
[[ -n "${_PARAM[labels]}" ]] && ADPN_GITLAB_SW+=("--data-labels=${_PARAM[labels]}")
fi
[[ -n "${_PARAM[pipe]}" ]] && ADPN_GITLAB_SW+=("--pipe=${_PARAM[pipe]}")
[[ -n "${_PARAM[token]}" ]] && ADPN_GITLAB_SW+=( "--token=${_PARAM[token]}" )
API_PATH="/issues"
[[ -n "${_PARAM[project]}" ]] && PROJECT="${_PARAM[project]}" || PROJECT="${_ARGV[2]}"
ISSUE_ASPECT=$( printf "%s" "${PROJECT}" | cut --field=3 --delimiter='#' --only-delimited )
ISSUE_ID=$( printf "%s" "${PROJECT}" | cut --field=2 --delimiter='#' --only-delimited )
PROJECT=$( printf "%s" "${PROJECT}" | cut --field=1 --delimiter='#' )
if [[ -n "${PROJECT}" ]] ; then
PROJECT_ID=$( "${ME}" project "${PROJECT}" "${CONTEXT_SW[@]}" | cut --field=2 )
fi
if [[ -n "${_ARGV[3]}" ]] ; then
ISSUE_ID="${_ARGV[3]}"
fi
declare -a ADPN_JSON_SW=()
[[ -n "${PROJECT_ID}" ]] && API_PATH=$( printf "/projects/%d%s" "${PROJECT_ID}" "${API_PATH}" )
[[ -n "${ISSUE_ID}" ]] && API_PATH=$( printf "%s/%s" "${API_PATH}" "${ISSUE_ID}" )
[[ -n "${ISSUE_ASPECT}" ]] && API_PATH=$( printf "%s/%s" "${API_PATH}" "${ISSUE_ASPECT}" )
SEP="?"
[[ -n "${_PARAM[labels]}" ]] && LABELS="$( rawurlencode "${_PARAM[labels]}" )" && API_PATH="$( printf "%s%s%s=%s" "${API_PATH}" "${SEP}" "labels" "${LABELS}" )" && SEP="&"
[[ -n "${_PARAM[state]}" ]] && STATE="$( rawurlencode "${_PARAM[state]}" )" && API_PATH="$( printf "%s%s%s=%s" "${API_PATH}" "${SEP}" "state" "${STATE}" )" && SEP="&"
[[ -n "${_PARAM[order_by]}" ]] && ORDER_BY="$( rawurlencode "${_PARAM[order_by]}" )" && API_PATH="$( printf "%s%s%s=%s" "${API_PATH}" "${SEP}" "order_by" "${ORDER_BY}" )" && SEP="&"
[[ -n "${_PARAM[sort]}" ]] && SORT="$( rawurlencode "${_PARAM[sort]}" )" && API_PATH="$( printf "%s%s%s=%s" "${API_PATH}" "${SEP}" "sort" "${SORT}" )" && SEP="&"
[[ -n "${_PARAM[assigneee]}" ]] && ASSIGNEE="$( rawurlencode "${_PARAM[assignee]}" )" && API_PATH="$( printf "%s%s%s=%s" "${API_PATH}" "${SEP}" "assignee_username" "${ASSIGNEE}" ) )" && SEP="&"
[[ -n "${ISSUE_ID}" ]] && ISSUE_STR="$( printf "ISSUE #%d -- " "${ISSUE_ID}" )" || ISSUE_STR=""
adpn_debug 10,adpn,gitlab,http,"${SCRIPT}","${CMD}" "${LINENO}" "%sGET %s (%s)" "${ISSUE_STR}" "${API_PATH}" "$( declare -p _ARGV )"
if [[ -n "${_SWITCHES[key]}" ]] ; then
for SW in "${!_SWITCHES[@]}" ; do
if [[ "${SW}" =~ ^key([#][0-9]+)?$ ]] ; then
ADPN_JSON_SW+=( "${_SWITCHES[$SW]}" )
fi
done
fi
if [[ -n "${_PARAM[output]}" ]] ; then
ADPN_JSON_SW+=( $(printf "%s--output=%s" "" "${_PARAM[output]}" ) )
elif [[ -z "${_PARAM[key]}" ]] ; then
ADPN_JSON_SW+=( "--output=urlencode" )
fi
F_JSON_OUT=$(mktemp)
if [[ -n "${_PARAM[resource]}" ]] ; then
printf "%s\n" "$( adpn-json.py --output=application/json --key="resource" --value="${API_PATH}" )" "$( adpn-json.py --output=application/json --key="request" --value="${METHOD}" )" | adpn-json.py --cascade --output=application/json > "${F_JSON_OUT}"
elif [[ -n "${_PARAM[pipe]}" ]] ; then
printf "%s" "$( cat - )" | "${ME}" "${METHOD}" "${ADPN_GITLAB_SW[@]}" "${API_PATH}" ${V} ${DBG} > "${F_JSON_OUT}"
else
"${ME}" "${METHOD}" "${ADPN_GITLAB_SW[@]}" "${API_PATH}" ${V} ${DBG} > "${F_JSON_OUT}"
fi
EXITCODE="$?"
if [[ "${_PARAM[output]}" =~ ^(application/)?json$ ]] ; then
cat "${F_JSON_OUT}"
elif [[ "${EXITCODE}" -eq 0 ]] ; then
cat "${F_JSON_OUT}" | adpn-json.py --parse "${ADPN_JSON_SW[@]}"
EXITCODE="$?"
else
adpn_notice "${LINENO}" "Error from HTTP request (exit code=%d)." "${EXITCODE}"
cat "${F_JSON_OUT}" 1>&2
fi
rm "${F_JSON_OUT}"
;;
"token")
PRIVATE_TOKEN=$( adpn_parameter_from "--token" ".gitlab/token<(adpn stash)" ) ; EXITCODE="$?"
if [[ -n "${PRIVATE_TOKEN}" ]] ; then
adpn_debug 500,adpn,gitlab,token "${LINENO}" "Private Token: %s" "$( obscure_password "${PRIVATE_TOKEN}" )"
printf "%s" "${PRIVATE_TOKEN}"
else
PRIVATE_TOKEN=$( adpn_parameter_from ".gitlab/token" "keepass:///~/.adpn/adpnet.kdbx?title=Gitlab+Token" ) ; EXITCODE="$?"
adpn_debug 500,adpn,gitlab,token "${LINENO}" "Source for Private Token: %s" "${PRIVATE_TOKEN}"
if [[ "${EXITCODE}" -eq 0 && -n "${PRIVATE_TOKEN}" ]] ; then
PRIVATE_TOKEN=$( adpn_getpassword_from "${PRIVATE_TOKEN}" ); EXITCODE="$?"
adpn_debug 500,adpn,gitlab,token "${LINENO}" "Stashing gitlab/token for future reference..."
[[ "${EXITCODE}" -eq 0 ]] && { "${ADPN}" stash post "gitlab/token" "${PRIVATE_TOKEN}" "${CONTEXT_SW[@]}" ; EXITCODE="$?" ; }
[[ "${EXITCODE}" -eq 0 ]] && printf "%s" "${PRIVATE_TOKEN}"
else
[[ "${EXITCODE}" -eq 0 ]] && EXITCODE=1
fi
fi
;;
"packet")
F_GL_ISSUE=$(mktemp)
ERR_DATA=""
ISH="${_ARGV[2]}"
[[ "${ISH}" =~ ^gitlab:(.*)$ ]] && ISH="${BASH_REMATCH[1]}"
declare -a PACKET_KEYS=( "--key=id" "--key=title" "--key=description" "--key=web_url" "--key=project_id" "--key=iid" )
adpn_debug 01,adpn,gitlab,packet "${LINENO}" "%s " "${SCRIPT}" issue "${ISH}" "${_ARGV[@]:3}" "${PACKET_KEYS[@]}" --output="multipart/form-data;table" "${CONTEXT_SW[@]}" ">" "${F_GL_ISSUE}"
"${ME}" issue "${ISH}" "${_ARGV[@]:3}" "${PACKET_KEYS[@]}" --output="multipart/form-data;table" "${CONTEXT_SW[@]}" > "${F_GL_ISSUE}" ; EXITCODE="$?"
RE_PACKET="${ADPN_JSON_PACKET_REGEX}"
if [[ "${EXITCODE}" -eq 0 ]] ; then
ALL_GREPS=1 # assume failure until we find a match
WEB_URL=- # assume empty until we find a match
# 1. Scan for the JSON packet, if any, in issue.description
push_input_loop
while read -r LINE ; do # < "${F_GL_ISSUE}"
adpn_debug 450,adpn,gitlab,packet "${LINENO}" "Data:\t%s" "${LINE}"
declare -A A_DATA=()
N="$( printf "%s\n" "${LINE}" | sed -E 's/\t/\n/g' | wc -l )"
push_input_loop $'\n'
for I in $( seq 1 2 $N ) ; do
S_FIELD="$( printf "%s\n" "${LINE}" | cut --field="${I}" )"
CUT_FIELD="$(( I+1 ))"
S_ENC_VALUE="$( printf "%s\n" "${LINE}" | cut --field="${CUT_FIELD}" )"
A_DATA["${S_FIELD}"]="$( urldecode "${S_ENC_VALUE}" )"
done
pop_input_loop
ISSUE_ID="${A_DATA[id]}"
ISH_TITLE="${A_DATA[title]}"
ISH_BODY="${A_DATA[description]}"
WEB_URL="${A_DATA[web_url]}"
PROJECT_ID="${A_DATA[project_id]}"
ISSUE_IID="${A_DATA[iid]}"
API_RESOURCE=$( printf "/projects/%d/issues/%d" "${PROJECT_ID}" "${ISSUE_IID}" ) ; EXITCODE="$?"
if [[ "${EXITCODE}" -eq 0 ]] ; then
GREP="$( printf "%s" "${ISH_BODY}" | grep -E "${RE_PACKET}" )"
GREPEXIT="$?"
if [[ -n "${GREP}" ]] ; then
adpn_debug 50,adpn,gitlab,packet "${LINENO}" "PACKET FOUND (issue.description): '%s'" "${GREP}"
adpn_write_json_packets_from "${WEB_URL}" "${API_RESOURCE}" "${GREP}"
fi
# if not found, retain some data for possible error message
if [[ "${GREPEXIT}" -ge 0 ]] ; then
ERR_DATA=$( printf "%s [title: '%s']" "${ERR_DATA}" "${ISH_TITLE}" )
fi
# if found, flag this as OK
if [[ "${GREPEXIT}" -lt "${ALL_GREPS}" ]] ; then
ALL_GREPS="${GREPEXIT}"
fi
else
adpn_notice "${LINENO}" "ERROR: Could not find a valid PROJECT_ID and ISSUE_IID."
fi
done < "${F_GL_ISSUE}"
pop_input_loop
# 2. Scan for the JSON packet, if any, in issue/notes.body
if [[ "${EXITCODE}" -eq 0 ]] ; then
ISHN="${ISH}#notes?sort=asc&order_by=created_at"
adpn_debug 01,adpn,gitlab,packet "${LINENO}" "%s " "${SCRIPT}" "issue" "${ISHN}" "${_ARGV[@]:3}" --key=id --key=body --output="multipart/form-data;table" "${CONTEXT_SW[@]}" ">" "${F_GL_ISSUE}"
"${ME}" issue "${ISHN}" "${_ARGV[@]:3}" --key=id --key=body --output="multipart/form-data;table" "${CONTEXT_SW[@]}" > "${F_GL_ISSUE}"
EXITCODE="$?"
if [[ "${EXITCODE}" -eq 0 ]] ; then
push_input_loop
cat "${F_GL_ISSUE}" | while read -r LINE ; do
NOTE_ID=$( echo "${LINE}" | cut --field=2 )
ENCBODY=$( echo "${LINE}" | cut --field=4 )
BODY=$( urldecode "${ENCBODY}" )
GREP=$( printf "%s" "${BODY}" | grep -E "${RE_PACKET}" )
GREPEXIT="$?"
if [[ -n "${GREP}" ]] ; then
adpn_debug 50,adpn,gitlab,packet "${LINENO}" "PACKET FOUND (issue/notes[%s].body): '%s'" "${NOTE_ID}" "${GREP}"
adpn_write_json_packets_from "${WEB_URL}" "${API_RESOURCE}" "${GREP}"
else
adpn_debug 100,adpn,gitlab,packet "${LINENO}" "no packet found (issue/notes[%s].body)" "${NOTE_ID}"
fi
# if found, flag this as OK
if [[ "${GREPEXIT}" -lt "${ALL_GREPS}" ]] ; then
ALL_GREPS="${GREPEXIT}"
fi
done
pop_input_loop
EXITCODE="${ALL_GREPS}"
else
adpn_debug 01,adpn,gitlab,packet "${LINENO}" "gitlab issue/notes request FAILED: %d" "${EXITCODE}"
fi
fi
fi
if [[ "${EXITCODE}" -gt 0 ]] ; then
if [[ -z "${QQ}" ]] ; then
adpn_notice "${LINENO}" "No JSON packet found for issue %s%s" "${ISH}" "${ERR_DATA}"
fi
fi
rm -f "${F_GL_ISSUE}"
;;
"get")
OBJECT="${_ARGV[2]}"
declare -a ADG_ARGV=( "${CONTEXT_SW[@]}" "${_SWITCHES[@]}" "--" "${_ARGV[@]:3}" )
if [[ "${OBJECT}" == "packet" ]] ; then
# Treat this as a request for a JSON packet encoded within a given Gitlab Issue
"${ME}" packet "${ADG_ARGV[@]}" ; EXITCODE="$?"
elif [[ "${OBJECT}" == "issue" ]] ; then
# Treat this as a request for a Gitlab Issue object from the Gitlab API / refs <https://docs.gitlab.com/ee/api/issues.html>
"${ME}" issue "${ADG_ARGV[@]}" ; EXITCODE="$?"
elif [[ "${OBJECT}" == "project" ]] ; then
# Treat this as a request for a Gitlab Project object from the Gitlab API / refs <https://docs.gitlab.com/ee/api/projects.html>
"${ME}" project "${ADG_ARGV[@]}" ; EXITCODE="$?"
elif [[ "${OBJECT}" == "token" ]] ; then
# Treat this as a request for the Gitlab private access token as stored in adpnet.json or the encrypted KeePass database
"${ME}" token "${ADG_ARGV[@]}" ; EXITCODE="$?"
elif [[ "${OBJECT}" == "labels" ]] ; then
ISH_JSON="$( "${ME}" issue --output=application/json "${ADG_ARGV[@]}" )" ; EXITCODE="$?"
[[ "${EXITCODE}" -eq 0 ]] && printf "%s" "${ISH_JSON}" | adpn-json.py --key="labels"
else
# Treat this as one or more direct requests to the Gitlab API / refs <https://docs.gitlab.com/ee/api/README.html>
if [[ -z "${PRIVATE_TOKEN}" ]] ; then
adpn_notice "WARNING: Use of the Gitlab API requires an access token, and you don't seem to have one configured. See <https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html>"
else
API_URL=$( adpnprop "gitlab/base" )
if [[ -n "${_PARAM[url]}" ]] ; then
API_URL="${_PARAM[url]}"
fi
declare -a CURL_SW=()
CURL_SW+=( "--silent" )
CURL_SW+=( "--request" "GET" )
if [[ -n "${_PARAM[fail]}" ]] ; then
CURL_SW+=( "--fail" )
fi
if [[ -n "${PRIVATE_TOKEN}" ]] ; then
CURL_SW+=( "--header" )
CURL_SW+=( "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" )
fi
for SLUG in "${_ARGV[@]:2}" ; do
URL=$( printf "${API_URL}" "${SLUG#/}" )
adpn_debug 200,adpn,gitlab,http,get "${LINENO}" "%s " "HTTP>>>" curl "${CURL_SW[@]}" "${URL}"
curl "${CURL_SW[@]}" "${URL}"
CURLEXIT="$?"
if [[ "${CURLEXIT}" -ge "${EXITCODE}" ]] ; then
EXITCODE="${CURLEXIT}"
fi
done
fi
fi
;;
"post")
OBJECT="${_ARGV[2]}"
if [[ -z "${PRIVATE_TOKEN}" ]] ; then
adpn_notice "WARNING: Use of the Gitlab API requires an access token, and you don't seem to have one configured. See <https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html>"
EXITCODE=1
elif [[ "${OBJECT}" == "note" ]] ; then
if [[ -n "${_PARAM[no-body]}" ]] ; then
NOTE_BODY=""
elif [[ -n "${_PARAM[body]}" ]] ; then
NOTE_BODY="${_PARAM[body]}"
else
NOTE_BODY="$( cat - )"
fi
[[ -n "${_PARAM[issue]}" ]] && ISSUE="${_PARAM[issue]}" || ISSUE="${_ARGV[3]}"
# confidential=true|false, created_at=<iso8601 date/time>
GITLAB_RESOURCE=""
REGEX_ISSUE='/*projects/[0-9]+/+issues/+[0-9]+/*'
if [[ -n "${ISSUE}" ]] ; then
if [[ "${ISSUE}" =~ ^${REGEX_ISSUE}$ ]] ; then
GITLAB_RESOURCE="${ISSUE}"
else
GITLAB_RESOURCE="$( "${ME}" issue "${ISSUE}" "${CONTEXT_SW[@]}" --resource | grep -E '^resource\s+' | cut --field=2 )"
fi
fi
if [[ "${GITLAB_RESOURCE}" =~ ^${REGEX_ISSUE}$ ]] ; then
GITLAB_RESOURCE_ISSUE=$( printf '%s' "${GITLAB_RESOURCE}" )
GITLAB_RESOURCE=$( printf "%s/%s" "${GITLAB_RESOURCE}" "notes" )
declare -a ADGP_SW=( "${ME}" post "${GITLAB_RESOURCE}" "${CONTEXT_SW[@]}" )
ADGP_SW+=( "--pipe=body" )
DBG_BODY="$( printf "%s" "${NOTE_BODY}" | head -c 10 )"
[[ "${#BODY_TEXT}" -gt "${#DBG_BODY}" ]] && DBG_BODY+="..."
adpn_debug 25,adpn,gitlab,http,post,issue "${LINENO}" "printf '%%s' '%s' | %s" "${DBG_BODY}" "${ADGP_SW[*]}"
declare -A TMP_OUT=( [post_json]="$( mktemp )" )
if [[ -z "${_PARAM[no-body]}" && "${#NOTE_BODY}" -gt 0 ]] ; then
printf "%s" "${NOTE_BODY}" | "${ADGP_SW[@]}" | adpn-json.py --output=application/json --prolog --epilog >> "${TMP_OUT[post_json]}"
EXITCODE="$?"
fi
if [[ "${EXITCODE}" -eq 0 ]] ; then
declare -a _LABEL_SW=()
[[ -n "${_PARAM[remove_labels]}" ]] && _LABEL_SW+=( "$( printf -- '--data-remove_labels=%s' "${_PARAM[remove_labels]}" )" )
[[ -n "${_PARAM[add_labels]}" ]] && _LABEL_SW+=( "$( printf -- '--data-add_labels=%s' "${_PARAM[add_labels]}" )" )
if [[ "${#_LABEL_SW[@]}" -gt 0 ]] ; then
declare -a _CMD_LABELS=( "${ME}" post "${_CONTEXT_SW[@]}" "--verb=PUT" "${GITLAB_RESOURCE_ISSUE}" "${_LABEL_SW[@]}" )
adpn_debug 25,adpn,gitlab,http,post,issue "${LINENO}" "%s" "${_CMD_LABELS[*]}"
"${_CMD_LABELS[@]}" | adpn-json.py --output=application/json --into="issue" --prolog --epilog >> "${TMP_OUT[post_json]}"
EXITCODE="$?"
fi
fi
cat "${TMP_OUT[post_json]}" | adpn-json.py --cascade --output=application/json
for FILE in "${TMP_OUT[@]}" ; do
rm "${FILE}"
done
else
adpn_notice "${LINENO}" "REQUIRED: You must provide an identifier for a Gitlab Issue to post a note; try using --issue=<REFERENCE>"
[[ -n "${GITLAB_RESOURCE}" ]] && adpn_notice "${LINENO}" "Invalid Format: '%s' does not match /%s/" "${GITLAB_RESOURCE}" "${REGEX_ISSUE}"
fi
else
API_URL=$( adpnprop "gitlab/base" )
if [[ -n "${_PARAM[url]}" ]] ; then
API_URL="${_PARAM[url]}"
fi
HTTP_VERB=$( adpn_parameter_from '--verb' '--request' '\POST' )
declare -a CURL_SW=()
CURL_SW+=( "--silent" )
CURL_SW+=( "--request" "${HTTP_VERB}" )
if [[ -n "${_PARAM[fail]}" ]] ; then
CURL_SW+=( "--fail" )
fi
if [[ -n "${PRIVATE_TOKEN}" ]] ; then
CURL_SW+=( "--header" )
CURL_SW+=( "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" )
fi
for KEY in "${!_PARAM[@]}" ; do
case "${KEY}" in
"token"|"context"|"verbose"|"quiet"|"debug"|"dry-run")
;;
"verb"|"request"|"fail")
;;
"pipe")
PIPED_TEXT="$( cat - )"
PIPED_PAIR="$( printf "%s=%s" "${_PARAM[$KEY]}" "${PIPED_TEXT}" )"
CURL_SW+=( '--data-urlencode' "${PIPED_PAIR}")
;;
*)
FIELD="${KEY}"
if [[ "${KEY}" =~ ^data-(.*) ]] ; then
FIELD="${BASH_REMATCH[1]}"
fi
CURL_SW+=( "--data-urlencode" "${FIELD}=${_PARAM[$KEY]}" )
;;
esac
done
for SLUG in "${_ARGV[@]:2}" ; do
URL=$( printf "${API_URL}" "${SLUG#/}" )
if [[ -n "${DBG}" ]] ; then
adpn_debug 30,adpn,gitlab,http,post,curl "${LINENO}" "%s " curl "${CURL_SW[@]}" "${URL}"
fi
if [[ -z "${_PARAM[dry-run]}" ]] ; then
curl "${CURL_SW[@]}" "${URL}"
fi
CURLEXIT="$?"
if [[ "${CURLEXIT}" -ge "${EXITCODE}" ]] ; then
EXITCODE="${CURLEXIT}"
fi
done
fi
;;
"parse")
GITLAB_ISSUE=""
for PACKET_SOURCE in "${_ARGV[@]:2}" ; do
if [[ "${PACKET_SOURCE}" =~ ^(!|@|\^)((/?[A-Za-z0-9].*)?)$ ]] ; then
declare -a ADPN_GITLAB_OPENED_SW=( "opened" )
[[ -n "${_SWITCHES[token]}" ]] && ADPN_GITLAB_OPENED_SW+=( "${_SWITCHES[token]}" )
[[ -n "${BASH_REMATCH[2]}" ]] && ADPN_GITLAB_OPENED_LABEL="$( adpn_opened_get_labels "${BASH_REMATCH[2]}" )" || ADPN_GITLAB_OPENED_LABEL=""
[[ -n "${ADPN_GITLAB_OPENED_LABEL}" ]] && ADPN_GITLAB_OPENED_SW+=( "$(printf -- "--labels=Content Ingest,%s" "${ADPN_GITLAB_OPENED_LABEL}" )" )
ADPN_GITLAB_OPENED_SW+=( "${CONTEXT_SW[@]}" )
GITLAB_ISSUE="$( "${ME}" "${ADPN_GITLAB_OPENED_SW[@]}" )"
elif [[ "${PACKET_SOURCE}" =~ ^gitlab:(.*)$ ]] ; then
GITLAB_ISSUE="${BASH_REMATCH[1]}"
elif [[ "${PACKET_SOURCE}" =~ ^(([A-Za-z0-9/]|-)+)[#]([0-9]+)$ ]] ; then
GITLAB_ISSUE="${BASH_REMATCH[0]}"
elif [[ "${PACKET_SOURCE}" =~ ^[#]?([0-9]+)$ ]] ; then
DEFAULT_REPOSITORY="$( adpnprop gitlab/repository )"
GITLAB_ISSUE="$( printf "%s#%d" "${DEFAULT_REPOSITORY}" "${BASH_REMATCH[1]}" )"
elif [[ "${PACKET_SOURCE}" =~ ^((https?[:])?//(www[.])?gitlab[.]com/(([a-zA-Z0-9/]|-)+)(/-)(/issues/([0-9]+)))$ ]] ; then
GITLAB_ISSUE="$( printf "%s#%d" "${BASH_REMATCH[4]}" "${BASH_REMATCH[8]}" )"
fi
if [[ -n "${GITLAB_ISSUE}" ]] ; then
if [[ "${_PARAM[output]}" == "tsv" ]] ; then
PROJECT_ID="$( printf "%s" "${GITLAB_ISSUE}" | cut --field=1 --delimiter="#" )"
ISSUE_ID="$( printf "%s" "${GITLAB_ISSUE}" | cut --field=2 --delimiter "#" --only-delimited )"
printf "%s\t%s\n" "${PROJECT_ID}" "${ISSUE_ID}"
else
printf "%s" "${GITLAB_ISSUE}"
fi
else
if [[ "${DBGLEVEL}" -ge 2 ]] ; then
printf "[%s] Unable to recognize and parse a Gitlab issue ID from: '%s'\n" "${SCRIPT} ${CMD}" "${PACKET_SOURCE}"
EXITCODE=1
fi
fi
done
;;
"version"|"help")
EXITCODE=0
;;
*)
adpn_notice "'${CMD}' command not understood."
echo "${__HELP__}" 1>&2
EXITCODE=255
;;
esac
fi
##########################################################################################
### CLEANUP: remove temporary files, set exit code, etc. #################################
##########################################################################################
if [[ -n "${ADPN_STASH_CLOSE}" ]]; then
eval $( "${ADPN_STASH}" close --if-needed="${ADPN_STASH_CLOSE}" --quiet "${CONTEXT_SW[@]}" )
fi
adpn_debug 100,adpn,"gitlab","${SCRIPT}","${CMD}" "${LINENO}" "exited, cmd=%s, code=%d" "${CMD}" "${EXITCODE}"
exit ${EXITCODE}