blob: b12d0bf1c671ce7dababd4458e3133d0c37dca94 [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 Armstrongd5e1b632024-03-25 12:18:00 -040031source "${libdir}/do/include.sh"
Joey Armstrong76f861a2024-03-13 16:01:24 -040032source "${libdir}/usage/include.sh"
Joey Armstrong76f861a2024-03-13 16:01:24 -040033
Joey Armstrongc5542fa2024-03-25 11:49:47 -040034## --------------------------------------------------------------------
35## --------------------------------------------------------------------
36function error()
37{
38 cat <<ERROR
39
40** -----------------------------------------------------------------------
41** IAM: ${FUNCNAME[1]}
42** ERROR: $@
43** -----------------------------------------------------------------------
44ERROR
Joey Armstrongd5e1b632024-03-25 12:18:00 -040045 echo
Joey Armstrongc5542fa2024-03-25 11:49:47 -040046 exit 1
47}
Joey Armstrongdc364a12024-02-29 15:14:11 -050048
49## -----------------------------------------------------------------------
50## -----------------------------------------------------------------------
Joey Armstrong76f861a2024-03-13 16:01:24 -040051# function join_by_orig()
52function join_by()
Joey Armstrongdc364a12024-02-29 15:14:11 -050053{
54 local d=${1-} f=${2-}; if shift 2; then printf %s "$f" "${@/#/$d}"; fi;
55}
56
57## -----------------------------------------------------------------------
Joey Armstrongdc364a12024-02-29 15:14:11 -050058## Intent: Iterate over --admin values and generate repositories
59## -----------------------------------------------------------------------
60function gen_admin_urls()
61{
62 local -n ref=$1 ; shift
63 local -n _repos=$1 ; shift
64 local -n _admins=$1 ; shift
65
66 local repo
67 for repo in "${_repos[@]}";
68 do
69 local attr
70 for admin in "${_admins[@]}";
71 do
72 # https://gerrit.opencord.org/admin/repos/voltha-go
73 case "$admin" in
74 branches)
75 ref+=("https://gerrit.opencord.org/admin/repos/${repo},branches")
76 ;;
77 tags)
78 ref+=("https://gerrit.opencord.org/admin/repos/${repo},tags")
79 ;;
80 *) error "Unknown --admin type [$admin]" ;;
81 esac
Joey Armstrong76f861a2024-03-13 16:01:24 -040082
Joey Armstrongdc364a12024-02-29 15:14:11 -050083 done # admins[@]
84 done # repos[@]
85 return
86}
Joey Armstrong76f861a2024-03-13 16:01:24 -040087
Joey Armstrongdc364a12024-02-29 15:14:11 -050088## -----------------------------------------------------------------------
89## Intent: Iterate over --admin values and generate repositories
90## -----------------------------------------------------------------------
91function gen_view_urls()
92{
Joey Armstrongd5e1b632024-03-25 12:18:00 -040093 # shellcheck disable=SC2178
Joey Armstrongdc364a12024-02-29 15:14:11 -050094 local -n ref=$1 ; shift
95 local -n _repos=$1 ; shift
96 local -n _views=$1 ; shift
97
98 local -a view_keys=("${!_views[@]}")
99 local stem0="https://gerrit.opencord.org/plugins/gitiles"
100
101 # https://gerrit.opencord.org/plugins/gitiles/ofagent-go
Joey Armstrong76f861a2024-03-13 16:01:24 -0400102
Joey Armstrongdc364a12024-02-29 15:14:11 -0500103 local repo
104 for repo in "${_repos[@]}";
105 do
Joey Armstrongdc364a12024-02-29 15:14:11 -0500106 local stem="${stem0}/$repo"
107 local view
108 for view in "${view_keys[@]}";
109 do
Joey Armstrongdc364a12024-02-29 15:14:11 -0500110 # https://gerrit.opencord.org/admin/repos/voltha-go
111 case "$view" in
112 # https://gerrit.opencord.org/plugins/gitiles/voltha-system-tests
113 search) continue ;;
114 default) ref+=("$stem") ;;
115 # repo*) ref+=("$stem/$repo") ;;
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400116 repo*) ref+=("$stem") ;;
117 master)
118 ref+=("$stem/+/refs/heads/master")
Joey Armstrongdc364a12024-02-29 15:14:11 -0500119 ;;
120
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400121 voltha-*)
122 echo "STEM: $stem"
123 if false; then
124 ref+=("$stem/+/refs/tags/${view}")
125 ref+=("$stem/+/refs/heads/${view}")
126 fi
127 # ref+=("$stem/+/refs/heads/${view}")
128 ref+=("$stem/+/refs/heads/${view}")
129 ;;
130
131 v*) ref+=("$stem/+/refs/tags/${view}") ;;
Joey Armstrongdc364a12024-02-29 15:14:11 -0500132 *) error "Unknown --view type [$view]" ;;
133 esac
134 done # views[@]
135 done # repos[@]
136
137 for r in "${ref[@]}";
138 do
139 echo "REPO: $r"
140 done
141
142 return
143}
Joey Armstrong76f861a2024-03-13 16:01:24 -0400144
Joey Armstrongdc364a12024-02-29 15:14:11 -0500145## -----------------------------------------------------------------------
146## -----------------------------------------------------------------------
147function get_urls()
148{
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400149 # shellcheck disable=SC2178
Joey Armstrongdc364a12024-02-29 15:14:11 -0500150 local -n ref="$1"; shift
151 local _srv="$1"; shift
152
153 local gerrit="https://${_srv}"
154 ref=(\
155 ['admin']="$gerrit/admin/repos"
156 ['base']="$gerrit"\
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400157 ['dashboard']="$gerrit/dashboard/self"\
158 )
Joey Armstrongdc364a12024-02-29 15:14:11 -0500159 return
160}
161
162## -----------------------------------------------------------------------
163## -----------------------------------------------------------------------
164function changeset()
165{
166 local _repo="$1"; shift
167 local _id="$1"; shift
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400168 # shellcheck disable=SC2178
Joey Armstrongdc364a12024-02-29 15:14:11 -0500169 local -n ref=$1; shift
170
171 local url="https://gerrit.opencord.org/c/${repo}/+/${id}"
172 ref+=("$url")
173 return
174}
175
176## -----------------------------------------------------------------------
177## -----------------------------------------------------------------------
178function browse()
179{
180 local -n _urls=$1; shift
Joey Armstrongdc364a12024-02-29 15:14:11 -0500181 "${BROWSER}" "${_urls[@]}" >/dev/null 2>/dev/null &
182 return
183}
184
185## -----------------------------------------------------------------------
186## -----------------------------------------------------------------------
187function access_url()
188{
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400189 # shellcheck disable=SC2178
Joey Armstrongdc364a12024-02-29 15:14:11 -0500190 local -n ref=$1; shift
191 local _repo="$1"; shift
192
193 declare -A data=()
194 get_urls data "$serv"
195
196 # declare -p data | tr '=' '\n'
197 # ref+=("https://gerrit.opencord.org/admin/repos/${_repo},access")
198 ref+=("${data['admin']}/${_repo},access")
199
200 # browse urls
201 return
202}
203
204## -----------------------------------------------------------------------
205## -----------------------------------------------------------------------
206function branch_url()
207{
208 local _repo="$1"; shift
209 local -a urls=()
210 urls+=("https://gerrit.opencord.org/admin/repos/${_repo},branches")
211 browse urls
212 return
213}
214
215## -----------------------------------------------------------------------
216## -----------------------------------------------------------------------
217function do_gerrit_dashboard()
218{
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400219 # shellcheck disable=SC2178
Joey Armstrongdc364a12024-02-29 15:14:11 -0500220 local -n ref=$1; shift
Joey Armstrong76f861a2024-03-13 16:01:24 -0400221
Joey Armstrongdc364a12024-02-29 15:14:11 -0500222 declare -A data=()
223 get_urls data "$serv"
224 ref+=("${data['dashboard']}")
225 return
226}
Joey Armstrongdc364a12024-02-29 15:14:11 -0500227
228# https://gerrit.opencord.org/q/status:open+-is:wip
229
230# repo=''
231declare -a urls=()
232
233##----------------##
234##---] MAIN [---##
235##----------------##
236[[ $# -eq 0 ]] && { set -- '--help'; }
237
238while [ $# -ne 0 ]; do
239 arg="$1"; shift
240 case "$arg" in
241 --help) usage; exit 0 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400242 --help-*)
243 case "$arg" in
244 --help-status) usage_verbose "$arg" ;;
245 *) usage ;;
246 esac
247 exit 0
248 ;;
Joey Armstrongdc364a12024-02-29 15:14:11 -0500249
250 ## Modes
Joey Armstrong49d2dce2024-04-25 11:22:09 -0400251 --age) # https://gerrit-review.googlesource.com/Documentation/user-search.html
252 arg="$1"; shift
253 add_filter_age "$arg"
254 ;;
255
256 ## FMT: 2006-01-02[ 15:04:05[.890][ -0700]];
257 --after|--since) # https://gerrit-review.googlesource.com/Documentation/user-search.html
258 arg="$1"; shift
259 add_filter_after "$arg"
260 ;;
261
262 ## FMT: 2006-01-02[ 15:04:05[.890][ -0700]];
263 --before) # https://gerrit-review.googlesource.com/Documentation/user-search.html
264 arg="$1"; shift
265 add_filter_before "$arg"
266 ;;
267
268 ## Modes
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400269 --debug)
270 # shellcheck disable=SC2034
Joey Armstrongdc364a12024-02-29 15:14:11 -0500271 declare -g -i argv_debug=1
272 declare -g -i debug=1
273 ;;
Joey Armstrong49d2dce2024-04-25 11:22:09 -0400274
Joey Armstrongdc364a12024-02-29 15:14:11 -0500275 --search) declare -g -i argv_search=1 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400276
277 --me)
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400278 # shellcheck disable=SC2034
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400279 declare -g -i argv_me=1
Joey Armstrong76f861a2024-03-13 16:01:24 -0400280 me+=("owner:${USER}@opennetworking.org")
281 ;;
282
283 --todo) source "$root/gerrit/todo.sh" ;;
284
Joey Armstrong4e36d662024-04-16 10:10:04 -0400285 --unmerged|--wip)
Joey Armstrong76f861a2024-03-13 16:01:24 -0400286 declare -a args=()
287 args=('--status' 'open')
288 [[ $# -gt 0 ]] && { args+=("$@"); }
289 set -- "${args[@]}"
Joey Armstrongdc364a12024-02-29 15:14:11 -0500290 ;;
291
292 --admin)
293 [[ ! -v admins ]] && { declare -a admins=(); }
294 arg="$1"; shift
295 case "$arg" in
296 br*) admins+=('branches') ;;
297 ta*) admins+=('tags') ;;
298 *) error "Unknown --attr value [$attr]" ;;
299 ## Also master and voltha-2.12
300 # https://gerrit.opencord.org/plugins/gitiles/voltha-go/+/refs/heads/voltha-2.12
301 esac
302 ;;
303
304 --all)
305 repo="$1"; shift
306 declare -a args=()
307 args+=('--repo' "$repo")
Joey Armstrong76f861a2024-03-13 16:01:24 -0400308 #
Joey Armstrongdc364a12024-02-29 15:14:11 -0500309 args+=('--admin' 'branches')
310 args+=('--admin' 'tags')
311
312 # args+=('--view') # post getopts expansion
313 # if @versions() else master
314 if [[ ${#versions[@]} -eq 0 ]]; then
315 versions+=('master')
316 fi
317
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400318 # args+=('--view' 'voltha-2.11')
Joey Armstrongdc364a12024-02-29 15:14:11 -0500319 args+=('--view' 'voltha-2.12')
320 set -- "${args[@]}" "$@"
321 ;;
322
Joey Armstrong76f861a2024-03-13 16:01:24 -0400323 --repo)
Joey Armstrongdc364a12024-02-29 15:14:11 -0500324 repo="$1"; shift
325 [[ ! -v repos ]] && { declare -a repos=(); }
326 repos+=("$repo")
327 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400328
Joey Armstrongdc364a12024-02-29 15:14:11 -0500329 --serv) serv="$1"; shift
Joey Armstrong76f861a2024-03-13 16:01:24 -0400330 case "$serv" in
Joey Armstrongdc364a12024-02-29 15:14:11 -0500331 *cord*) serv='gerrit.opencord.org' ;;
332 *onos*) serv='gerrit.onosproject.org' ;;
333 *) error "Detected invalid --serv $serv" ;;
334 esac
335 ;;
336
337 --access)
338 [[ ! -v repo ]] && { error "--repo is required"; }
339 access_url urls "$repo"
340 ;;
341
342 --branch)
343 [[ ! -v repo ]] && { error "--repo is required"; }
344 branch_url "$repo"
345 ;;
346
347 --crowd*)
348 # https://crowd.opennetworking.org/crowd/console/secure/group/browse.action?directoryId=163841&updateSuccessful=
349 declare https='https://crowd.opennetworking.org'
350 declare base_url="${https}/crowd/console/secure/group/browse.action"
351 case "$arg" in
352 --crowd-*)
353 # urls+=("${base_url}?directoryId=163841&updateSuccessful=")
354 urls+=("${base_url}")
355 ;;
356 *)
357 # https://crowd.opennetworking.org/crowd/console/secure/group/browse.action?search=%22VOLTHACore%22
358 [[ $# -eq 0 ]] && { error "--crowd requires an argument"; }
359 arg="$1"; shift
360 urls+=("${base_url}?search=%22${arg}%22")
361 ;;
362 esac
363 ;;
364
Joey Armstrong76f861a2024-03-13 16:01:24 -0400365 --*desk) error "Try --dashboard instead" ;;
Joey Armstrongdc364a12024-02-29 15:14:11 -0500366 --dash*) do_gerrit_dashboard urls ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400367
368 #
Joey Armstrongdc364a12024-02-29 15:14:11 -0500369 --group*)
370 case "$arg" in
371 --group*)
372 urls+=("https://${serv}/admin/groups")
373 ;;
374 *)
375 [[ $# -eq 0 ]] && { error "--group requires an argument"; }
376 arg="$1"; shift
377 urls+=("https://${serv}/admin/groups/q/filter:${arg}}")
378 ;;
379 esac
380 ;;
381
382 --id)
383 id="$1"; shift
384 [[ ${#repo} -eq 0 ]] && { error "--repo is required"; }
385 changeset "$repo" "$id" urls
386 ;;
387
388 --status)
Joey Armstrong76f861a2024-03-13 16:01:24 -0400389 arg="$1"; shift # open, closed, merged
390 case "$arg" in
391 closed|open|merged) add_filter_status "$arg";;
392 *) error "Detected invalid --status [$arg]" ;;
393 esac
Joey Armstrongdc364a12024-02-29 15:14:11 -0500394 ;;
395
396 --patch*)
397 repo="$1"; shift
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400398 urls+=("https://gerrit.opencord.org/q/${repo}+status:open")
Joey Armstrongdc364a12024-02-29 15:14:11 -0500399 # https://gerrit.opencord.org/q/voltha-openolt-adapter-go
400 ;;
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400401
402 --reviewer)
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400403 [[ $# -eq 0 ]] && { error '--reviewer requires an argument'; }
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400404 arg="$1"; shift
405 add_filter_reviewer "$arg"
406
407 declare -g -i argv_review=1
408 # Replace with: set -- '--review' "$@"
Joey Armstrongdc364a12024-02-29 15:14:11 -0500409 ;;
410
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400411 --review) declare -g -i argv_review=1 ;;
412
Joey Armstrongdc364a12024-02-29 15:14:11 -0500413 --ver*)
414 [[ $# -eq 0 ]] && { error "Usage: $arg [str]"; }
415 arg="$1"; shift
416 [[ ! -v versions ]] && { declare -a versions=(); }
417 versions+=("$arg")
418 ;;
419
420 # post getopt expansion to support --version x --version y
421 --view)
422 [[ $# -eq 0 ]] && { set -- '--default'; }
423 arg="$1"; shift
424 # [[ "${#views[@]}" -gt 0 ]]
425 [[ ! -v views ]] && { declare -A views=(); }
426 case "$arg" in
427
428 search*)
429 urls+=('https://gerrit.opencord.org/admin/repos')
430 ;;
431
432 repo) views['repository']=1 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400433
Joey Armstrongdc364a12024-02-29 15:14:11 -0500434 # https://gerrit.opencord.org/plugins/gitiles/voltha-go
435 master) views['master']=1 ;;
436 voltha*) views["$arg"]=1 ;;
437 v*) views["$arg"]=1 ;;
438 *)
439 # Missing latest tags
440 views['default']=1
441 views['master']=1
442 ;;
443 esac
444 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400445
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400446 -*)
447 echo "ERROR: Unknown argument [$arg]"
448 exit 1
449 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400450
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400451 [[:alnum:]]*)
Joey Armstrongdc364a12024-02-29 15:14:11 -0500452 declare -a what=()
453 # whats+=('patches')
454 whats+=('versions')
455
456 stem="https://gerrit.opencord.org/q/${arg}"
Joey Armstrong76f861a2024-03-13 16:01:24 -0400457
Joey Armstrongdc364a12024-02-29 15:14:11 -0500458 for what in "${whats[@]}";
459 do
460 case "$what" in
461 patches) urls+=("https://gerrit.opencord.org/q/${arg}") ;;
462 versions)
463 urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/heads/master/VERSION")
464 # urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/tags/v0.3.6/VERSION")
465 urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/heads/voltha-2.12/VERSION")
466 ;;
467 '??') urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}") ;;
468 *) urls+=("https://gerrit.opencord.org/q/${arg}") ;;
469 esac
470 done
471 esac
472done
473
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400474[[ -v argv_search ]] && { do_gerrit_search urls repos argv_status; }
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400475[[ -v admins ]] && { gen_admin_urls urls repos admins; }
Joey Armstrongc5542fa2024-03-25 11:49:47 -0400476[[ -v argv_review ]] && { do_gerrit_review urls repos argv_status; }
Joey Armstrongdc364a12024-02-29 15:14:11 -0500477
478## are --views working ?
479[[ "${#views[@]}" -gt 0 ]] && { gen_view_urls urls repos views; }
Joey Armstrongdc364a12024-02-29 15:14:11 -0500480
Joey Armstrong76f861a2024-03-13 16:01:24 -0400481if [[ ${#urls[@]} -eq 0 ]]; then
Joey Armstrongd5e1b632024-03-25 12:18:00 -0400482 # shellcheck disable=SC2034
Joey Armstrong76f861a2024-03-13 16:01:24 -0400483 declare -a show_all=() # show all for visiblity
484 do_gerrit_search urls repos show_all
485fi
486
Joey Armstrongdc364a12024-02-29 15:14:11 -0500487[[ -v debug ]] && { echo "$BROWSER" "${urls[@]}"; }
Joey Armstrong76f861a2024-03-13 16:01:24 -0400488
489if [[ ${#urls[@]} -gt 0 ]]; then
490 apply_filter_status urls
491 browse urls
492fi
Joey Armstrongdc364a12024-02-29 15:14:11 -0500493
494## -----------------------------------------------------------------------
495## [TODO]
496## Query patches by repo:voltha-lib-go
497## ssh gerrit.opencord.org gerrit query --patch-sets projects:voltha-lib-go | grep -i description
498# ssh gerrit.opencord.org gerrit query --patch-sets 'projects:voltha-lib-go AND status:open limit:2' |
499
500# Show URL and Commit message
501# ssh gerrit.opencord.org gerrit query --format=JSON --patch-sets 'projects:voltha-lib-go AND status:open limit:2' | jq '.url,.commitMessage'
502## -----------------------------------------------------------------------
503
504# [EOF]
Joey Armstrong76f861a2024-03-13 16:01:24 -0400505
506
507# 1) gerrit.sh --search --me --wip
508# query> owner:joey@opennetworking.org status:open
509
510# --search --me
511# args+=("owner:${USER}@opennetworking.org")
512
513# --wip)
514# argv_status+=('status:open')
515# ;;
516
Joey Armstrong76f861a2024-03-13 16:01:24 -0400517# [EOF]