blob: 3463960029495beb11495952af886a19a6c94e74 [file] [log] [blame]
Joey Armstrongdc364a12024-02-29 15:14:11 -05001#!/bin/bash
2## -----------------------------------------------------------------------
3## Intent: Construct gerrit server queries and URLs based on arguments
4## -----------------------------------------------------------------------
5
6{ # loader
7 declare pgm=''
8 pgm="$(realpath --canonicalize-existing "$0")"
Joey Armstrong76f861a2024-03-13 16:01:24 -04009 readonly pgm
Joey Armstrongdc364a12024-02-29 15:14:11 -050010
Joey Armstrong76f861a2024-03-13 16:01:24 -040011 declare libdir="${pgm%/*}"
12 libdir="${libdir%/*}/gerrit"
13 readonly libdir
14
15 declare root=''
16 root="${pgm%%/gerrit/bin/gerrit.sh}"
17 source "$root/lf/onf-common/common.sh" '--common-args-begin--'
Joey Armstrongdc364a12024-02-29 15:14:11 -050018}
19
20##-------------------##
21##---] GLOBALS [---##
22##-------------------##
23declare -g serv='gerrit.opencord.org'
Joey Armstrong76f861a2024-03-13 16:01:24 -040024declare -x -g BROWSER="${BROWSER:-/usr/local/bin/firefox}"
25declare -g -a me=()
26
27##--------------------##
28##---] INCLUDES [---##
29##--------------------##
Joey Armstrongc5542fa2024-03-25 11:49:47 -040030source "${libdir}/filters/include.sh"
Joey Armstrong76f861a2024-03-13 16:01:24 -040031source "${libdir}/usage/include.sh"
Joey Armstrong76f861a2024-03-13 16:01:24 -040032
Joey Armstrongc5542fa2024-03-25 11:49:47 -040033## --------------------------------------------------------------------
34## --------------------------------------------------------------------
35function error()
36{
37 cat <<ERROR
38
39** -----------------------------------------------------------------------
40** IAM: ${FUNCNAME[1]}
41** ERROR: $@
42** -----------------------------------------------------------------------
43ERROR
44 echo
45 exit 1
46}
Joey Armstrongdc364a12024-02-29 15:14:11 -050047
48## -----------------------------------------------------------------------
49## -----------------------------------------------------------------------
Joey Armstrong76f861a2024-03-13 16:01:24 -040050# function join_by_orig()
51function join_by()
Joey Armstrongdc364a12024-02-29 15:14:11 -050052{
53 local d=${1-} f=${2-}; if shift 2; then printf %s "$f" "${@/#/$d}"; fi;
54}
55
56## -----------------------------------------------------------------------
57## Usage: $0 --repo voltha-go --search --wip
58## -----------------------------------------------------------------------
59function do_gerrit_search()
60{
61 local -n ref_urls=$1 ; shift
62 local -n ref_repos=$1 ; shift
63 local -n ref_status=$1 ; shift
64
Joey Armstrong76f861a2024-03-13 16:01:24 -040065 ref_urls=()
66
Joey Armstrongdc364a12024-02-29 15:14:11 -050067 # local stem='https://gerrit.opencord.org/q/project:voltha-go+status:open'
68 local stem='https://gerrit.opencord.org/q/'
69
Joey Armstrong76f861a2024-03-13 16:01:24 -040070 local -a common=()
71 [[ -v me ]] && { common+=("${me[@]}"); }
72 common+=("${ref_status[@]}")
73
Joey Armstrongdc364a12024-02-29 15:14:11 -050074 local repo
75 for repo in "${ref_repos[@]}";
Joey Armstrong76f861a2024-03-13 16:01:24 -040076 do
77 local -a args=()
Joey Armstrongdc364a12024-02-29 15:14:11 -050078 args+=("project:${repo}")
Joey Armstrong76f861a2024-03-13 16:01:24 -040079 args+=("${common[@]}")
Joey Armstrongdc364a12024-02-29 15:14:11 -050080
81 local url="$stem"
82 url+="$(join_by '+' "${args[@]}")"
83 ref_urls+=("$url")
84 done
85
Joey Armstrong76f861a2024-03-13 16:01:24 -040086 if [[ ${#ref_urls[@]} -eq 0 ]]; then
87 local -a args=()
88 args=("${common[@]}")
Joey Armstrongdc364a12024-02-29 15:14:11 -050089
Joey Armstrong76f861a2024-03-13 16:01:24 -040090 local url="$stem"
91 url+="$(join_by '+' "${args[@]}")"
92 ref_urls+=("$url")
93 fi
94
Joey Armstrongdc364a12024-02-29 15:14:11 -050095 # --debug) declare -g -i debug=1 ;;
96 # --search) declare -g -i search=1 ;;
97 # --wip) declare -g -i status_is_open=1 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -040098
99 return
Joey Armstrongdc364a12024-02-29 15:14:11 -0500100}
101
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400102## -----------------------------------------------------------------------
103## Usage: $0 --status open --reviewer [u]
104## -----------------------------------------------------------------------
105function do_gerrit_review()
106{
107 local -n ref_urls=$1 ; shift
108 local -n ref_repos=$1 ; shift
109 local -n ref_status=$1 ; shift
Joey Armstrongdc364a12024-02-29 15:14:11 -0500110
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400111 ref_urls=()
112
113 # local stem='https://gerrit.opencord.org/q/project:voltha-go+status:open'
114 local stem='https://gerrit.opencord.org/q/'
115
116 local -a common=()
117 [[ -v me ]] && { common+=('reviewer:self') }
118
119 for reviewer in "${argv_reviewer[@]}"; do
120 common+=("reviewer:${reviewer}")
121 done
122
123 local repo
124 for repo in "${ref_repos[@]}";
125 do
126 local -a args=()
127 args+=("project:${repo}")
128 args+=("${common[@]}")
129
130 local url="$stem"
131 url+="$(join_by '+' "${args[@]}")"
132 ref_urls+=("$url")
133 done
134
135 ## If not repo specific then query all
136 if [[ ${#ref_urls[@]} -eq 0 ]]; then
137 local -a args=()
138 args=("${common[@]}")
139
140 local url="$stem"
141 url+="$(join_by '+' "${args[@]}")"
142 ref_urls+=("$url")
143 fi
144
145 # --debug) declare -g -i debug=1 ;;
146 # --search) declare -g -i search=1 ;;
147 # --wip) declare -g -i status_is_open=1 ;;
148
149 return
150}
151
Joey Armstrongdc364a12024-02-29 15:14:11 -0500152## -----------------------------------------------------------------------
153## Intent: Iterate over --admin values and generate repositories
154## -----------------------------------------------------------------------
155function gen_admin_urls()
156{
157 local -n ref=$1 ; shift
158 local -n _repos=$1 ; shift
159 local -n _admins=$1 ; shift
160
161 local repo
162 for repo in "${_repos[@]}";
163 do
164 local attr
165 for admin in "${_admins[@]}";
166 do
167 # https://gerrit.opencord.org/admin/repos/voltha-go
168 case "$admin" in
169 branches)
170 ref+=("https://gerrit.opencord.org/admin/repos/${repo},branches")
171 ;;
172 tags)
173 ref+=("https://gerrit.opencord.org/admin/repos/${repo},tags")
174 ;;
175 *) error "Unknown --admin type [$admin]" ;;
176 esac
Joey Armstrong76f861a2024-03-13 16:01:24 -0400177
Joey Armstrongdc364a12024-02-29 15:14:11 -0500178 done # admins[@]
179 done # repos[@]
180 return
181}
Joey Armstrong76f861a2024-03-13 16:01:24 -0400182
Joey Armstrongdc364a12024-02-29 15:14:11 -0500183## -----------------------------------------------------------------------
184## Intent: Iterate over --admin values and generate repositories
185## -----------------------------------------------------------------------
186function gen_view_urls()
187{
188 local -n ref=$1 ; shift
189 local -n _repos=$1 ; shift
190 local -n _views=$1 ; shift
191
192 local -a view_keys=("${!_views[@]}")
193 local stem0="https://gerrit.opencord.org/plugins/gitiles"
194
195 # https://gerrit.opencord.org/plugins/gitiles/ofagent-go
Joey Armstrong76f861a2024-03-13 16:01:24 -0400196
Joey Armstrongdc364a12024-02-29 15:14:11 -0500197 local repo
198 for repo in "${_repos[@]}";
199 do
Joey Armstrongdc364a12024-02-29 15:14:11 -0500200 local stem="${stem0}/$repo"
201 local view
202 for view in "${view_keys[@]}";
203 do
Joey Armstrongdc364a12024-02-29 15:14:11 -0500204 # https://gerrit.opencord.org/admin/repos/voltha-go
205 case "$view" in
206 # https://gerrit.opencord.org/plugins/gitiles/voltha-system-tests
207 search) continue ;;
208 default) ref+=("$stem") ;;
209 # repo*) ref+=("$stem/$repo") ;;
210 repo*) ref+=("$stem") ;;
211 master)
212 ref+=("$stem/+/refs/heads/master")
213 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400214
Joey Armstrongdc364a12024-02-29 15:14:11 -0500215 voltha-*)
216 echo "STEM: $stem"
217 if false; then
Joey Armstrong76f861a2024-03-13 16:01:24 -0400218 ref+=("$stem/+/refs/tags/${view}")
Joey Armstrongdc364a12024-02-29 15:14:11 -0500219 ref+=("$stem/+/refs/heads/${view}")
220 fi
221 # ref+=("$stem/+/refs/heads/${view}")
222 ref+=("$stem/+/refs/heads/${view}")
223 ;;
224
225 v*) ref+=("$stem/+/refs/tags/${view}") ;;
226 *) error "Unknown --view type [$view]" ;;
227 esac
228 done # views[@]
229 done # repos[@]
230
231 for r in "${ref[@]}";
232 do
233 echo "REPO: $r"
234 done
235
236 return
237}
Joey Armstrong76f861a2024-03-13 16:01:24 -0400238
Joey Armstrongdc364a12024-02-29 15:14:11 -0500239## -----------------------------------------------------------------------
240## -----------------------------------------------------------------------
241function get_urls()
242{
243 local -n ref="$1"; shift
244 local _srv="$1"; shift
245
246 local gerrit="https://${_srv}"
247 ref=(\
248 ['admin']="$gerrit/admin/repos"
249 ['base']="$gerrit"\
250 ['dashboard']="$gerrit/dashboard/self"\
251 )
252 return
253}
254
255## -----------------------------------------------------------------------
256## -----------------------------------------------------------------------
257function changeset()
258{
259 local _repo="$1"; shift
260 local _id="$1"; shift
261 local -n ref=$1; shift
262
263 local url="https://gerrit.opencord.org/c/${repo}/+/${id}"
264 ref+=("$url")
265 return
266}
267
268## -----------------------------------------------------------------------
269## -----------------------------------------------------------------------
270function browse()
271{
272 local -n _urls=$1; shift
Joey Armstrongdc364a12024-02-29 15:14:11 -0500273 "${BROWSER}" "${_urls[@]}" >/dev/null 2>/dev/null &
274 return
275}
276
277## -----------------------------------------------------------------------
278## -----------------------------------------------------------------------
279function access_url()
280{
281 local -n ref=$1; shift
282 local _repo="$1"; shift
283
284 declare -A data=()
285 get_urls data "$serv"
286
287 # declare -p data | tr '=' '\n'
288 # ref+=("https://gerrit.opencord.org/admin/repos/${_repo},access")
289 ref+=("${data['admin']}/${_repo},access")
290
291 # browse urls
292 return
293}
294
295## -----------------------------------------------------------------------
296## -----------------------------------------------------------------------
297function branch_url()
298{
299 local _repo="$1"; shift
300 local -a urls=()
301 urls+=("https://gerrit.opencord.org/admin/repos/${_repo},branches")
302 browse urls
303 return
304}
305
306## -----------------------------------------------------------------------
307## -----------------------------------------------------------------------
308function do_gerrit_dashboard()
309{
310 local -n ref=$1; shift
Joey Armstrong76f861a2024-03-13 16:01:24 -0400311
Joey Armstrongdc364a12024-02-29 15:14:11 -0500312 declare -A data=()
313 get_urls data "$serv"
314 ref+=("${data['dashboard']}")
315 return
316}
Joey Armstrongdc364a12024-02-29 15:14:11 -0500317
318# https://gerrit.opencord.org/q/status:open+-is:wip
319
320# repo=''
321declare -a urls=()
322
323##----------------##
324##---] MAIN [---##
325##----------------##
326[[ $# -eq 0 ]] && { set -- '--help'; }
327
328while [ $# -ne 0 ]; do
329 arg="$1"; shift
330 case "$arg" in
331 --help) usage; exit 0 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400332 --help-*)
333 case "$arg" in
334 --help-status) usage_verbose "$arg" ;;
335 *) usage ;;
336 esac
337 exit 0
338 ;;
Joey Armstrongdc364a12024-02-29 15:14:11 -0500339
340 ## Modes
341 --debug)
342 declare -g -i argv_debug=1
343 declare -g -i debug=1
344 ;;
345 --search) declare -g -i argv_search=1 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400346
347 --me)
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400348 declare -g -i argv_me=1
Joey Armstrong76f861a2024-03-13 16:01:24 -0400349 me+=("owner:${USER}@opennetworking.org")
350 ;;
351
352 --todo) source "$root/gerrit/todo.sh" ;;
353
Joey Armstrongdc364a12024-02-29 15:14:11 -0500354 --wip)
Joey Armstrong76f861a2024-03-13 16:01:24 -0400355 declare -a args=()
356 args=('--status' 'open')
357 [[ $# -gt 0 ]] && { args+=("$@"); }
358 set -- "${args[@]}"
Joey Armstrongdc364a12024-02-29 15:14:11 -0500359 ;;
360
361 --admin)
362 [[ ! -v admins ]] && { declare -a admins=(); }
363 arg="$1"; shift
364 case "$arg" in
365 br*) admins+=('branches') ;;
366 ta*) admins+=('tags') ;;
367 *) error "Unknown --attr value [$attr]" ;;
368 ## Also master and voltha-2.12
369 # https://gerrit.opencord.org/plugins/gitiles/voltha-go/+/refs/heads/voltha-2.12
370 esac
371 ;;
372
373 --all)
374 repo="$1"; shift
375 declare -a args=()
376 args+=('--repo' "$repo")
Joey Armstrong76f861a2024-03-13 16:01:24 -0400377 #
Joey Armstrongdc364a12024-02-29 15:14:11 -0500378 args+=('--admin' 'branches')
379 args+=('--admin' 'tags')
380
381 # args+=('--view') # post getopts expansion
382 # if @versions() else master
383 if [[ ${#versions[@]} -eq 0 ]]; then
384 versions+=('master')
385 fi
386
387# args+=('--view' 'voltha-2.11')
388 args+=('--view' 'voltha-2.12')
389 set -- "${args[@]}" "$@"
390 ;;
391
Joey Armstrong76f861a2024-03-13 16:01:24 -0400392 --repo)
Joey Armstrongdc364a12024-02-29 15:14:11 -0500393 repo="$1"; shift
394 [[ ! -v repos ]] && { declare -a repos=(); }
395 repos+=("$repo")
396 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400397
Joey Armstrongdc364a12024-02-29 15:14:11 -0500398 --serv) serv="$1"; shift
Joey Armstrong76f861a2024-03-13 16:01:24 -0400399 case "$serv" in
Joey Armstrongdc364a12024-02-29 15:14:11 -0500400 *cord*) serv='gerrit.opencord.org' ;;
401 *onos*) serv='gerrit.onosproject.org' ;;
402 *) error "Detected invalid --serv $serv" ;;
403 esac
404 ;;
405
406 --access)
407 [[ ! -v repo ]] && { error "--repo is required"; }
408 access_url urls "$repo"
409 ;;
410
411 --branch)
412 [[ ! -v repo ]] && { error "--repo is required"; }
413 branch_url "$repo"
414 ;;
415
416 --crowd*)
417 # https://crowd.opennetworking.org/crowd/console/secure/group/browse.action?directoryId=163841&updateSuccessful=
418 declare https='https://crowd.opennetworking.org'
419 declare base_url="${https}/crowd/console/secure/group/browse.action"
420 case "$arg" in
421 --crowd-*)
422 # urls+=("${base_url}?directoryId=163841&updateSuccessful=")
423 urls+=("${base_url}")
424 ;;
425 *)
426 # https://crowd.opennetworking.org/crowd/console/secure/group/browse.action?search=%22VOLTHACore%22
427 [[ $# -eq 0 ]] && { error "--crowd requires an argument"; }
428 arg="$1"; shift
429 urls+=("${base_url}?search=%22${arg}%22")
430 ;;
431 esac
432 ;;
433
Joey Armstrong76f861a2024-03-13 16:01:24 -0400434 --*desk) error "Try --dashboard instead" ;;
Joey Armstrongdc364a12024-02-29 15:14:11 -0500435 --dash*) do_gerrit_dashboard urls ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400436
437 #
Joey Armstrongdc364a12024-02-29 15:14:11 -0500438 --group*)
439 case "$arg" in
440 --group*)
441 urls+=("https://${serv}/admin/groups")
442 ;;
443 *)
444 [[ $# -eq 0 ]] && { error "--group requires an argument"; }
445 arg="$1"; shift
446 urls+=("https://${serv}/admin/groups/q/filter:${arg}}")
447 ;;
448 esac
449 ;;
450
451 --id)
452 id="$1"; shift
453 [[ ${#repo} -eq 0 ]] && { error "--repo is required"; }
454 changeset "$repo" "$id" urls
455 ;;
456
457 --status)
Joey Armstrong76f861a2024-03-13 16:01:24 -0400458 arg="$1"; shift # open, closed, merged
459 case "$arg" in
460 closed|open|merged) add_filter_status "$arg";;
461 *) error "Detected invalid --status [$arg]" ;;
462 esac
Joey Armstrongdc364a12024-02-29 15:14:11 -0500463 ;;
464
465 --patch*)
466 repo="$1"; shift
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400467 urls+=("https://gerrit.opencord.org/q/${repo}+status:open")
Joey Armstrongdc364a12024-02-29 15:14:11 -0500468 # https://gerrit.opencord.org/q/voltha-openolt-adapter-go
469 ;;
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400470
471 --reviewer)
472 arg="$1"; shift
473 add_filter_reviewer "$arg"
474
475 declare -g -i argv_review=1
476 # Replace with: set -- '--review' "$@"
Joey Armstrongdc364a12024-02-29 15:14:11 -0500477 ;;
478
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400479 --review) declare -g -i argv_review=1 ;;
480
Joey Armstrongdc364a12024-02-29 15:14:11 -0500481 --ver*)
482 [[ $# -eq 0 ]] && { error "Usage: $arg [str]"; }
483 arg="$1"; shift
484 [[ ! -v versions ]] && { declare -a versions=(); }
485 versions+=("$arg")
486 ;;
487
488 # post getopt expansion to support --version x --version y
489 --view)
490 [[ $# -eq 0 ]] && { set -- '--default'; }
491 arg="$1"; shift
492 # [[ "${#views[@]}" -gt 0 ]]
493 [[ ! -v views ]] && { declare -A views=(); }
494 case "$arg" in
495
496 search*)
497 urls+=('https://gerrit.opencord.org/admin/repos')
498 ;;
499
500 repo) views['repository']=1 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400501
Joey Armstrongdc364a12024-02-29 15:14:11 -0500502 # https://gerrit.opencord.org/plugins/gitiles/voltha-go
503 master) views['master']=1 ;;
504 voltha*) views["$arg"]=1 ;;
505 v*) views["$arg"]=1 ;;
506 *)
507 # Missing latest tags
508 views['default']=1
509 views['master']=1
510 ;;
511 esac
512 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400513
514 -*)
Joey Armstrongdc364a12024-02-29 15:14:11 -0500515 echo "ERROR: Unknown argument [$arg]"
516 exit 1
517 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400518
Joey Armstrongdc364a12024-02-29 15:14:11 -0500519 [[:alnum:]]*)
520 declare -a what=()
521 # whats+=('patches')
522 whats+=('versions')
523
524 stem="https://gerrit.opencord.org/q/${arg}"
Joey Armstrong76f861a2024-03-13 16:01:24 -0400525
Joey Armstrongdc364a12024-02-29 15:14:11 -0500526 for what in "${whats[@]}";
527 do
528 case "$what" in
529 patches) urls+=("https://gerrit.opencord.org/q/${arg}") ;;
530 versions)
531 urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/heads/master/VERSION")
532 # urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/tags/v0.3.6/VERSION")
533 urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/heads/voltha-2.12/VERSION")
534 ;;
535 '??') urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}") ;;
536 *) urls+=("https://gerrit.opencord.org/q/${arg}") ;;
537 esac
538 done
539 esac
540done
541
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400542[[ -v argv_search ]] && { do_gerrit_search urls repos argv_status; }
543[[ -v admins ]] && { gen_admin_urls urls repos admins; }
544
545# -----------------------------------------------------------------------
546# Generalize:
547# o --review function like a simple search filter.
548# o piece missing is defining query URL stem= when needed.
549# -----------------------------------------------------------------------
550[[ -v argv_review ]] && { do_gerrit_review urls repos argv_status; }
Joey Armstrongdc364a12024-02-29 15:14:11 -0500551
552## are --views working ?
553[[ "${#views[@]}" -gt 0 ]] && { gen_view_urls urls repos views; }
Joey Armstrongdc364a12024-02-29 15:14:11 -0500554
Joey Armstrong76f861a2024-03-13 16:01:24 -0400555if [[ ${#urls[@]} -eq 0 ]]; then
556 declare -a show_all=() # show all for visiblity
557 do_gerrit_search urls repos show_all
558fi
559
560
Joey Armstrongdc364a12024-02-29 15:14:11 -0500561[[ -v debug ]] && { echo "$BROWSER" "${urls[@]}"; }
Joey Armstrong76f861a2024-03-13 16:01:24 -0400562
563if [[ ${#urls[@]} -gt 0 ]]; then
564 apply_filter_status urls
565 browse urls
566fi
Joey Armstrongdc364a12024-02-29 15:14:11 -0500567
568## -----------------------------------------------------------------------
569## [TODO]
570## Query patches by repo:voltha-lib-go
571## ssh gerrit.opencord.org gerrit query --patch-sets projects:voltha-lib-go | grep -i description
572# ssh gerrit.opencord.org gerrit query --patch-sets 'projects:voltha-lib-go AND status:open limit:2' |
573
574# Show URL and Commit message
575# ssh gerrit.opencord.org gerrit query --format=JSON --patch-sets 'projects:voltha-lib-go AND status:open limit:2' | jq '.url,.commitMessage'
576## -----------------------------------------------------------------------
577
578# [EOF]
Joey Armstrong76f861a2024-03-13 16:01:24 -0400579
580
581# 1) gerrit.sh --search --me --wip
582# query> owner:joey@opennetworking.org status:open
583
584# --search --me
585# args+=("owner:${USER}@opennetworking.org")
586
587# --wip)
588# argv_status+=('status:open')
589# ;;
590
591
592# [EOF]