blob: 4900ab18bc2de58cce450ff482bc5bdd44a9d33b [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##--------------------##
30source "${libdir}/filters/status.sh"
31source "${libdir}/usage/include.sh"
32# source "${libdir}/usage/main.sh"
33
Joey Armstrongdc364a12024-02-29 15:14:11 -050034
35## -----------------------------------------------------------------------
36## -----------------------------------------------------------------------
Joey Armstrong76f861a2024-03-13 16:01:24 -040037# function join_by_orig()
38function join_by()
Joey Armstrongdc364a12024-02-29 15:14:11 -050039{
40 local d=${1-} f=${2-}; if shift 2; then printf %s "$f" "${@/#/$d}"; fi;
41}
42
43## -----------------------------------------------------------------------
44## Usage: $0 --repo voltha-go --search --wip
45## -----------------------------------------------------------------------
46function do_gerrit_search()
47{
48 local -n ref_urls=$1 ; shift
49 local -n ref_repos=$1 ; shift
50 local -n ref_status=$1 ; shift
51
Joey Armstrong76f861a2024-03-13 16:01:24 -040052 ref_urls=()
53
Joey Armstrongdc364a12024-02-29 15:14:11 -050054 # local stem='https://gerrit.opencord.org/q/project:voltha-go+status:open'
55 local stem='https://gerrit.opencord.org/q/'
56
Joey Armstrong76f861a2024-03-13 16:01:24 -040057 local -a common=()
58 [[ -v me ]] && { common+=("${me[@]}"); }
59 common+=("${ref_status[@]}")
60
Joey Armstrongdc364a12024-02-29 15:14:11 -050061 local repo
62 for repo in "${ref_repos[@]}";
Joey Armstrong76f861a2024-03-13 16:01:24 -040063 do
64 local -a args=()
Joey Armstrongdc364a12024-02-29 15:14:11 -050065 args+=("project:${repo}")
Joey Armstrong76f861a2024-03-13 16:01:24 -040066 args+=("${common[@]}")
Joey Armstrongdc364a12024-02-29 15:14:11 -050067
68 local url="$stem"
69 url+="$(join_by '+' "${args[@]}")"
70 ref_urls+=("$url")
71 done
72
Joey Armstrong76f861a2024-03-13 16:01:24 -040073 if [[ ${#ref_urls[@]} -eq 0 ]]; then
74 local -a args=()
75 args=("${common[@]}")
Joey Armstrongdc364a12024-02-29 15:14:11 -050076
Joey Armstrong76f861a2024-03-13 16:01:24 -040077 local url="$stem"
78 url+="$(join_by '+' "${args[@]}")"
79 ref_urls+=("$url")
80 fi
81
Joey Armstrongdc364a12024-02-29 15:14:11 -050082 # --debug) declare -g -i debug=1 ;;
83 # --search) declare -g -i search=1 ;;
84 # --wip) declare -g -i status_is_open=1 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -040085
86 return
Joey Armstrongdc364a12024-02-29 15:14:11 -050087}
88
89
90## -----------------------------------------------------------------------
91## Intent: Iterate over --admin values and generate repositories
92## -----------------------------------------------------------------------
93function gen_admin_urls()
94{
95 local -n ref=$1 ; shift
96 local -n _repos=$1 ; shift
97 local -n _admins=$1 ; shift
98
99 local repo
100 for repo in "${_repos[@]}";
101 do
102 local attr
103 for admin in "${_admins[@]}";
104 do
105 # https://gerrit.opencord.org/admin/repos/voltha-go
106 case "$admin" in
107 branches)
108 ref+=("https://gerrit.opencord.org/admin/repos/${repo},branches")
109 ;;
110 tags)
111 ref+=("https://gerrit.opencord.org/admin/repos/${repo},tags")
112 ;;
113 *) error "Unknown --admin type [$admin]" ;;
114 esac
Joey Armstrong76f861a2024-03-13 16:01:24 -0400115
Joey Armstrongdc364a12024-02-29 15:14:11 -0500116 done # admins[@]
117 done # repos[@]
118 return
119}
Joey Armstrong76f861a2024-03-13 16:01:24 -0400120
Joey Armstrongdc364a12024-02-29 15:14:11 -0500121## -----------------------------------------------------------------------
122## Intent: Iterate over --admin values and generate repositories
123## -----------------------------------------------------------------------
124function gen_view_urls()
125{
126 local -n ref=$1 ; shift
127 local -n _repos=$1 ; shift
128 local -n _views=$1 ; shift
129
130 local -a view_keys=("${!_views[@]}")
131 local stem0="https://gerrit.opencord.org/plugins/gitiles"
132
133 # https://gerrit.opencord.org/plugins/gitiles/ofagent-go
Joey Armstrong76f861a2024-03-13 16:01:24 -0400134
Joey Armstrongdc364a12024-02-29 15:14:11 -0500135 local repo
136 for repo in "${_repos[@]}";
137 do
Joey Armstrongdc364a12024-02-29 15:14:11 -0500138 local stem="${stem0}/$repo"
139 local view
140 for view in "${view_keys[@]}";
141 do
Joey Armstrongdc364a12024-02-29 15:14:11 -0500142 # https://gerrit.opencord.org/admin/repos/voltha-go
143 case "$view" in
144 # https://gerrit.opencord.org/plugins/gitiles/voltha-system-tests
145 search) continue ;;
146 default) ref+=("$stem") ;;
147 # repo*) ref+=("$stem/$repo") ;;
148 repo*) ref+=("$stem") ;;
149 master)
150 ref+=("$stem/+/refs/heads/master")
151 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400152
Joey Armstrongdc364a12024-02-29 15:14:11 -0500153 voltha-*)
154 echo "STEM: $stem"
155 if false; then
Joey Armstrong76f861a2024-03-13 16:01:24 -0400156 ref+=("$stem/+/refs/tags/${view}")
Joey Armstrongdc364a12024-02-29 15:14:11 -0500157 ref+=("$stem/+/refs/heads/${view}")
158 fi
159 # ref+=("$stem/+/refs/heads/${view}")
160 ref+=("$stem/+/refs/heads/${view}")
161 ;;
162
163 v*) ref+=("$stem/+/refs/tags/${view}") ;;
164 *) error "Unknown --view type [$view]" ;;
165 esac
166 done # views[@]
167 done # repos[@]
168
169 for r in "${ref[@]}";
170 do
171 echo "REPO: $r"
172 done
173
174 return
175}
Joey Armstrong76f861a2024-03-13 16:01:24 -0400176
Joey Armstrongdc364a12024-02-29 15:14:11 -0500177## -----------------------------------------------------------------------
178## -----------------------------------------------------------------------
179function get_urls()
180{
181 local -n ref="$1"; shift
182 local _srv="$1"; shift
183
184 local gerrit="https://${_srv}"
185 ref=(\
186 ['admin']="$gerrit/admin/repos"
187 ['base']="$gerrit"\
188 ['dashboard']="$gerrit/dashboard/self"\
189 )
190 return
191}
192
193## -----------------------------------------------------------------------
194## -----------------------------------------------------------------------
195function changeset()
196{
197 local _repo="$1"; shift
198 local _id="$1"; shift
199 local -n ref=$1; shift
200
201 local url="https://gerrit.opencord.org/c/${repo}/+/${id}"
202 ref+=("$url")
203 return
204}
205
206## -----------------------------------------------------------------------
207## -----------------------------------------------------------------------
208function browse()
209{
210 local -n _urls=$1; shift
Joey Armstrongdc364a12024-02-29 15:14:11 -0500211 "${BROWSER}" "${_urls[@]}" >/dev/null 2>/dev/null &
212 return
213}
214
215## -----------------------------------------------------------------------
216## -----------------------------------------------------------------------
217function access_url()
218{
219 local -n ref=$1; shift
220 local _repo="$1"; shift
221
222 declare -A data=()
223 get_urls data "$serv"
224
225 # declare -p data | tr '=' '\n'
226 # ref+=("https://gerrit.opencord.org/admin/repos/${_repo},access")
227 ref+=("${data['admin']}/${_repo},access")
228
229 # browse urls
230 return
231}
232
233## -----------------------------------------------------------------------
234## -----------------------------------------------------------------------
235function branch_url()
236{
237 local _repo="$1"; shift
238 local -a urls=()
239 urls+=("https://gerrit.opencord.org/admin/repos/${_repo},branches")
240 browse urls
241 return
242}
243
244## -----------------------------------------------------------------------
245## -----------------------------------------------------------------------
246function do_gerrit_dashboard()
247{
248 local -n ref=$1; shift
Joey Armstrong76f861a2024-03-13 16:01:24 -0400249
Joey Armstrongdc364a12024-02-29 15:14:11 -0500250 declare -A data=()
251 get_urls data "$serv"
252 ref+=("${data['dashboard']}")
253 return
254}
Joey Armstrongdc364a12024-02-29 15:14:11 -0500255
256# https://gerrit.opencord.org/q/status:open+-is:wip
257
258# repo=''
259declare -a urls=()
260
261##----------------##
262##---] MAIN [---##
263##----------------##
264[[ $# -eq 0 ]] && { set -- '--help'; }
265
266while [ $# -ne 0 ]; do
267 arg="$1"; shift
268 case "$arg" in
269 --help) usage; exit 0 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400270 --help-*)
271 case "$arg" in
272 --help-status) usage_verbose "$arg" ;;
273 *) usage ;;
274 esac
275 exit 0
276 ;;
Joey Armstrongdc364a12024-02-29 15:14:11 -0500277
278 ## Modes
279 --debug)
280 declare -g -i argv_debug=1
281 declare -g -i debug=1
282 ;;
283 --search) declare -g -i argv_search=1 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400284
285 --me)
286 # declare -g -i argv_me=1
287 me+=("owner:${USER}@opennetworking.org")
288 ;;
289
290 --todo) source "$root/gerrit/todo.sh" ;;
291
Joey Armstrongdc364a12024-02-29 15:14:11 -0500292 --wip)
Joey Armstrong76f861a2024-03-13 16:01:24 -0400293 declare -a args=()
294 args=('--status' 'open')
295 [[ $# -gt 0 ]] && { args+=("$@"); }
296 set -- "${args[@]}"
Joey Armstrongdc364a12024-02-29 15:14:11 -0500297 ;;
298
299 --admin)
300 [[ ! -v admins ]] && { declare -a admins=(); }
301 arg="$1"; shift
302 case "$arg" in
303 br*) admins+=('branches') ;;
304 ta*) admins+=('tags') ;;
305 *) error "Unknown --attr value [$attr]" ;;
306 ## Also master and voltha-2.12
307 # https://gerrit.opencord.org/plugins/gitiles/voltha-go/+/refs/heads/voltha-2.12
308 esac
309 ;;
310
311 --all)
312 repo="$1"; shift
313 declare -a args=()
314 args+=('--repo' "$repo")
Joey Armstrong76f861a2024-03-13 16:01:24 -0400315 #
Joey Armstrongdc364a12024-02-29 15:14:11 -0500316 args+=('--admin' 'branches')
317 args+=('--admin' 'tags')
318
319 # args+=('--view') # post getopts expansion
320 # if @versions() else master
321 if [[ ${#versions[@]} -eq 0 ]]; then
322 versions+=('master')
323 fi
324
325# args+=('--view' 'voltha-2.11')
326 args+=('--view' 'voltha-2.12')
327 set -- "${args[@]}" "$@"
328 ;;
329
Joey Armstrong76f861a2024-03-13 16:01:24 -0400330 --repo)
Joey Armstrongdc364a12024-02-29 15:14:11 -0500331 repo="$1"; shift
332 [[ ! -v repos ]] && { declare -a repos=(); }
333 repos+=("$repo")
334 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400335
Joey Armstrongdc364a12024-02-29 15:14:11 -0500336 --serv) serv="$1"; shift
Joey Armstrong76f861a2024-03-13 16:01:24 -0400337 case "$serv" in
Joey Armstrongdc364a12024-02-29 15:14:11 -0500338 *cord*) serv='gerrit.opencord.org' ;;
339 *onos*) serv='gerrit.onosproject.org' ;;
340 *) error "Detected invalid --serv $serv" ;;
341 esac
342 ;;
343
344 --access)
345 [[ ! -v repo ]] && { error "--repo is required"; }
346 access_url urls "$repo"
347 ;;
348
349 --branch)
350 [[ ! -v repo ]] && { error "--repo is required"; }
351 branch_url "$repo"
352 ;;
353
354 --crowd*)
355 # https://crowd.opennetworking.org/crowd/console/secure/group/browse.action?directoryId=163841&updateSuccessful=
356 declare https='https://crowd.opennetworking.org'
357 declare base_url="${https}/crowd/console/secure/group/browse.action"
358 case "$arg" in
359 --crowd-*)
360 # urls+=("${base_url}?directoryId=163841&updateSuccessful=")
361 urls+=("${base_url}")
362 ;;
363 *)
364 # https://crowd.opennetworking.org/crowd/console/secure/group/browse.action?search=%22VOLTHACore%22
365 [[ $# -eq 0 ]] && { error "--crowd requires an argument"; }
366 arg="$1"; shift
367 urls+=("${base_url}?search=%22${arg}%22")
368 ;;
369 esac
370 ;;
371
Joey Armstrong76f861a2024-03-13 16:01:24 -0400372 --*desk) error "Try --dashboard instead" ;;
Joey Armstrongdc364a12024-02-29 15:14:11 -0500373 --dash*) do_gerrit_dashboard urls ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400374
375 #
Joey Armstrongdc364a12024-02-29 15:14:11 -0500376 --group*)
377 case "$arg" in
378 --group*)
379 urls+=("https://${serv}/admin/groups")
380 ;;
381 *)
382 [[ $# -eq 0 ]] && { error "--group requires an argument"; }
383 arg="$1"; shift
384 urls+=("https://${serv}/admin/groups/q/filter:${arg}}")
385 ;;
386 esac
387 ;;
388
389 --id)
390 id="$1"; shift
391 [[ ${#repo} -eq 0 ]] && { error "--repo is required"; }
392 changeset "$repo" "$id" urls
393 ;;
394
395 --status)
Joey Armstrong76f861a2024-03-13 16:01:24 -0400396 arg="$1"; shift # open, closed, merged
397 case "$arg" in
398 closed|open|merged) add_filter_status "$arg";;
399 *) error "Detected invalid --status [$arg]" ;;
400 esac
Joey Armstrongdc364a12024-02-29 15:14:11 -0500401 ;;
402
403 --patch*)
404 repo="$1"; shift
405 urls+=("https://gerrit.opencord.org/q/${repo}+stats:open")
406 # https://gerrit.opencord.org/q/voltha-openolt-adapter-go
407 ;;
408 --review)
409 urls+=('https://gerrit.opencord.org/c/aether-ci-management/+/34900')
410 ;;
411
412 --ver*)
413 [[ $# -eq 0 ]] && { error "Usage: $arg [str]"; }
414 arg="$1"; shift
415 [[ ! -v versions ]] && { declare -a versions=(); }
416 versions+=("$arg")
417 ;;
418
419 # post getopt expansion to support --version x --version y
420 --view)
421 [[ $# -eq 0 ]] && { set -- '--default'; }
422 arg="$1"; shift
423 # [[ "${#views[@]}" -gt 0 ]]
424 [[ ! -v views ]] && { declare -A views=(); }
425 case "$arg" in
426
427 search*)
428 urls+=('https://gerrit.opencord.org/admin/repos')
429 ;;
430
431 repo) views['repository']=1 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400432
Joey Armstrongdc364a12024-02-29 15:14:11 -0500433 # https://gerrit.opencord.org/plugins/gitiles/voltha-go
434 master) views['master']=1 ;;
435 voltha*) views["$arg"]=1 ;;
436 v*) views["$arg"]=1 ;;
437 *)
438 # Missing latest tags
439 views['default']=1
440 views['master']=1
441 ;;
442 esac
443 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400444
445 -*)
Joey Armstrongdc364a12024-02-29 15:14:11 -0500446 echo "ERROR: Unknown argument [$arg]"
447 exit 1
448 ;;
Joey Armstrong76f861a2024-03-13 16:01:24 -0400449
Joey Armstrongdc364a12024-02-29 15:14:11 -0500450 [[:alnum:]]*)
451 declare -a what=()
452 # whats+=('patches')
453 whats+=('versions')
454
455 stem="https://gerrit.opencord.org/q/${arg}"
Joey Armstrong76f861a2024-03-13 16:01:24 -0400456
Joey Armstrongdc364a12024-02-29 15:14:11 -0500457 for what in "${whats[@]}";
458 do
459 case "$what" in
460 patches) urls+=("https://gerrit.opencord.org/q/${arg}") ;;
461 versions)
462 urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/heads/master/VERSION")
463 # urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/tags/v0.3.6/VERSION")
464 urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/heads/voltha-2.12/VERSION")
465 ;;
466 '??') urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}") ;;
467 *) urls+=("https://gerrit.opencord.org/q/${arg}") ;;
468 esac
469 done
470 esac
471done
472
473[[ -v argv_search ]] && { do_gerrit_search urls repos argv_status; }
Joey Armstrongdc364a12024-02-29 15:14:11 -0500474[[ -v admins ]] && { gen_admin_urls urls repos admins; }
475
476## are --views working ?
477[[ "${#views[@]}" -gt 0 ]] && { gen_view_urls urls repos views; }
478# [[ ! -v views ]] && { echo 'called'; gen_view_urls urls repos views; }
479
Joey Armstrong76f861a2024-03-13 16:01:24 -0400480if [[ ${#urls[@]} -eq 0 ]]; then
481 declare -a show_all=() # show all for visiblity
482 do_gerrit_search urls repos show_all
483fi
484
485
Joey Armstrongdc364a12024-02-29 15:14:11 -0500486[[ -v debug ]] && { echo "$BROWSER" "${urls[@]}"; }
Joey Armstrong76f861a2024-03-13 16:01:24 -0400487
488if [[ ${#urls[@]} -gt 0 ]]; then
489 apply_filter_status urls
490 browse urls
491fi
Joey Armstrongdc364a12024-02-29 15:14:11 -0500492
493## -----------------------------------------------------------------------
494## [TODO]
495## Query patches by repo:voltha-lib-go
496## ssh gerrit.opencord.org gerrit query --patch-sets projects:voltha-lib-go | grep -i description
497# ssh gerrit.opencord.org gerrit query --patch-sets 'projects:voltha-lib-go AND status:open limit:2' |
498
499# Show URL and Commit message
500# ssh gerrit.opencord.org gerrit query --format=JSON --patch-sets 'projects:voltha-lib-go AND status:open limit:2' | jq '.url,.commitMessage'
501## -----------------------------------------------------------------------
502
503# [EOF]
Joey Armstrong76f861a2024-03-13 16:01:24 -0400504
505
506# 1) gerrit.sh --search --me --wip
507# query> owner:joey@opennetworking.org status:open
508
509# --search --me
510# args+=("owner:${USER}@opennetworking.org")
511
512# --wip)
513# argv_status+=('status:open')
514# ;;
515
516
517# [EOF]