blob: adabf93285a7e5d69393331e5f1ae1345e3ca267 [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")"
9 root="${pgm%/*}" # path -= /bin/
10 root="${root%/*}" # path is parent
11 source "$root/lf/onf-common/common.sh" '--common-args-begin--'
12
13 # lib_root="${pgm/.sh}" gerrit.sh => gerrit/
14}
15
16##-------------------##
17##---] GLOBALS [---##
18##-------------------##
19declare -g serv='gerrit.opencord.org'
20declare -x -g BROWSER="${BROWSER:-opera}"
21
22## -----------------------------------------------------------------------
23## -----------------------------------------------------------------------
24function error_orig()
25{
26 echo "${FUNCNAME[1]} ERROR: $*"
27 exit 1
28}
29
30## -----------------------------------------------------------------------
31## -----------------------------------------------------------------------
32function join_by_orig()
33{
34 local d=${1-} f=${2-}; if shift 2; then printf %s "$f" "${@/#/$d}"; fi;
35}
36
37## -----------------------------------------------------------------------
38## Usage: $0 --repo voltha-go --search --wip
39## -----------------------------------------------------------------------
40function do_gerrit_search()
41{
42 local -n ref_urls=$1 ; shift
43 local -n ref_repos=$1 ; shift
44 local -n ref_status=$1 ; shift
45
46 # local stem='https://gerrit.opencord.org/q/project:voltha-go+status:open'
47 local stem='https://gerrit.opencord.org/q/'
48
49 local repo
50 for repo in "${ref_repos[@]}";
51 do
52 local -a args=()
53 args+=("project:${repo}")
54
55 if [[ -v argv_me ]]; then
56 args+=("owner:${USER}@opennetworking.org")
57 fi
58
59 local status
60 for status in "${ref_status[@]}";
61 do
62 args+=("$status")
63 done
64
65 local url="$stem"
66 url+="$(join_by '+' "${args[@]}")"
67 ref_urls+=("$url")
68 done
69
70
71
72 # --debug) declare -g -i debug=1 ;;
73 # --search) declare -g -i search=1 ;;
74 # --wip) declare -g -i status_is_open=1 ;;
75}
76
77
78## -----------------------------------------------------------------------
79## Intent: Iterate over --admin values and generate repositories
80## -----------------------------------------------------------------------
81function gen_admin_urls()
82{
83 local -n ref=$1 ; shift
84 local -n _repos=$1 ; shift
85 local -n _admins=$1 ; shift
86
87 local repo
88 for repo in "${_repos[@]}";
89 do
90 local attr
91 for admin in "${_admins[@]}";
92 do
93 # https://gerrit.opencord.org/admin/repos/voltha-go
94 case "$admin" in
95 branches)
96 ref+=("https://gerrit.opencord.org/admin/repos/${repo},branches")
97 ;;
98 tags)
99 ref+=("https://gerrit.opencord.org/admin/repos/${repo},tags")
100 ;;
101 *) error "Unknown --admin type [$admin]" ;;
102 esac
103
104 done # admins[@]
105 done # repos[@]
106 return
107}
108
109## -----------------------------------------------------------------------
110## Intent: Iterate over --admin values and generate repositories
111## -----------------------------------------------------------------------
112function gen_view_urls()
113{
114 local -n ref=$1 ; shift
115 local -n _repos=$1 ; shift
116 local -n _views=$1 ; shift
117
118 local -a view_keys=("${!_views[@]}")
119 local stem0="https://gerrit.opencord.org/plugins/gitiles"
120
121 # https://gerrit.opencord.org/plugins/gitiles/ofagent-go
122
123 local repo
124 for repo in "${_repos[@]}";
125 do
126 declare -p repo
127 local stem="${stem0}/$repo"
128 local view
129 for view in "${view_keys[@]}";
130 do
131 declare -p view
132 # https://gerrit.opencord.org/admin/repos/voltha-go
133 case "$view" in
134 # https://gerrit.opencord.org/plugins/gitiles/voltha-system-tests
135 search) continue ;;
136 default) ref+=("$stem") ;;
137 # repo*) ref+=("$stem/$repo") ;;
138 repo*) ref+=("$stem") ;;
139 master)
140 ref+=("$stem/+/refs/heads/master")
141 ;;
142
143 voltha-*)
144 echo "STEM: $stem"
145 if false; then
146 ref+=("$stem/+/refs/tags/${view}")
147 ref+=("$stem/+/refs/heads/${view}")
148 fi
149 # ref+=("$stem/+/refs/heads/${view}")
150 ref+=("$stem/+/refs/heads/${view}")
151 ;;
152
153 v*) ref+=("$stem/+/refs/tags/${view}") ;;
154 *) error "Unknown --view type [$view]" ;;
155 esac
156 done # views[@]
157 done # repos[@]
158
159 for r in "${ref[@]}";
160 do
161 echo "REPO: $r"
162 done
163
164 return
165}
166
167## -----------------------------------------------------------------------
168## -----------------------------------------------------------------------
169function get_urls()
170{
171 local -n ref="$1"; shift
172 local _srv="$1"; shift
173
174 local gerrit="https://${_srv}"
175 ref=(\
176 ['admin']="$gerrit/admin/repos"
177 ['base']="$gerrit"\
178 ['dashboard']="$gerrit/dashboard/self"\
179 )
180 return
181}
182
183## -----------------------------------------------------------------------
184## -----------------------------------------------------------------------
185function changeset()
186{
187 local _repo="$1"; shift
188 local _id="$1"; shift
189 local -n ref=$1; shift
190
191 local url="https://gerrit.opencord.org/c/${repo}/+/${id}"
192 ref+=("$url")
193 return
194}
195
196## -----------------------------------------------------------------------
197## -----------------------------------------------------------------------
198function browse()
199{
200 local -n _urls=$1; shift
201
202 # declare -p _urls
203 # echo "${_urls[@]}"
204 "${BROWSER}" "${_urls[@]}" >/dev/null 2>/dev/null &
205 return
206}
207
208## -----------------------------------------------------------------------
209## -----------------------------------------------------------------------
210function access_url()
211{
212 local -n ref=$1; shift
213 local _repo="$1"; shift
214
215 declare -A data=()
216 get_urls data "$serv"
217
218 # declare -p data | tr '=' '\n'
219 # ref+=("https://gerrit.opencord.org/admin/repos/${_repo},access")
220 ref+=("${data['admin']}/${_repo},access")
221
222 # browse urls
223 return
224}
225
226## -----------------------------------------------------------------------
227## -----------------------------------------------------------------------
228function branch_url()
229{
230 local _repo="$1"; shift
231 local -a urls=()
232 urls+=("https://gerrit.opencord.org/admin/repos/${_repo},branches")
233 browse urls
234 return
235}
236
237## -----------------------------------------------------------------------
238## -----------------------------------------------------------------------
239function do_gerrit_dashboard()
240{
241 local -n ref=$1; shift
242
243 declare -A data=()
244 get_urls data "$serv"
245 ref+=("${data['dashboard']}")
246 return
247}
248
249## -----------------------------------------------------------------------
250## -----------------------------------------------------------------------
251function usage()
252{
253 cat <<EOH
254Usage: $0
255 --all r Load all metadata for a repository
256 --reviews Load pending code review requests
257
258 --access Display gerrit ACLs
259 --dash(boad) Display my patch dashboard
260 --branch Admin branch URL
261
262 --crowd View crowd access roles for group
263 --crowd-all Group search (crowd)
264
265 --groups [g] View access roles for group.
266 --groups-all Group search (gerrit)
267
268 --repo Repository name
269 --serv Gerrit server name
270
271 --me Query for my patches
272 --wip
273 --search
274
275 --admins a
276 a=branches View repository branches
277 a=tags View repository tags
278
279 --patch [repo] Search for repository patches
280 --version v Artifact versions(s) to act on
281 v=master Mainline development
282 v=voltha-2.12 Artifact branch from a release
283 v=v2.12.0 Artifact tag derived from a release
284 v=v3.5.4 Artifact tag derived from VERSION file
285
286 --view v View a code repository:
287 repo View top level gerrit repository page (w/branch & tag)
288 search Gerrit repo search page
289 master View a branch
290 voltha-2.12 View a branch
291 v2.3.3 View a tag
292
293[FILTERS]
294 --status s Filter query by open/merge/closed status
295 s=open (is:open)
296 s=merged
297
298 --debug
299 --help
300
301% gerrit.sh --repo voltha-go --admin tags --admin branches
302
303## Search for umerged patches in repo:voltha-go
304% gerrit.sh --repo voltha-go --search --wip
305EOH
306 return
307}
308
309#
310
311# https://gerrit.opencord.org/q/status:open+-is:wip
312
313# repo=''
314declare -a urls=()
315
316##----------------##
317##---] MAIN [---##
318##----------------##
319[[ $# -eq 0 ]] && { set -- '--help'; }
320
321while [ $# -ne 0 ]; do
322 arg="$1"; shift
323 case "$arg" in
324 --help) usage; exit 0 ;;
325
326 ## Modes
327 --debug)
328 declare -g -i argv_debug=1
329 declare -g -i debug=1
330 ;;
331 --search) declare -g -i argv_search=1 ;;
332 --me) declare -g -i argv_me=1 ;;
333
334 --wip)
335 [[ ! -v argv_status ]] && { declare -g -a argv_status=(); }
336 argv_status+=('status:open')
337 ;;
338
339 --admin)
340 [[ ! -v admins ]] && { declare -a admins=(); }
341 arg="$1"; shift
342 case "$arg" in
343 br*) admins+=('branches') ;;
344 ta*) admins+=('tags') ;;
345 *) error "Unknown --attr value [$attr]" ;;
346 ## Also master and voltha-2.12
347 # https://gerrit.opencord.org/plugins/gitiles/voltha-go/+/refs/heads/voltha-2.12
348 esac
349 ;;
350
351 --all)
352 repo="$1"; shift
353 declare -a args=()
354 args+=('--repo' "$repo")
355 #
356 args+=('--admin' 'branches')
357 args+=('--admin' 'tags')
358
359 # args+=('--view') # post getopts expansion
360 # if @versions() else master
361 if [[ ${#versions[@]} -eq 0 ]]; then
362 versions+=('master')
363 fi
364
365# args+=('--view' 'voltha-2.11')
366 args+=('--view' 'voltha-2.12')
367 set -- "${args[@]}" "$@"
368 ;;
369
370 --repo)
371 repo="$1"; shift
372 [[ ! -v repos ]] && { declare -a repos=(); }
373 repos+=("$repo")
374 ;;
375
376 --serv) serv="$1"; shift
377 case "$serv" in
378 *cord*) serv='gerrit.opencord.org' ;;
379 *onos*) serv='gerrit.onosproject.org' ;;
380 *) error "Detected invalid --serv $serv" ;;
381 esac
382 ;;
383
384 --access)
385 [[ ! -v repo ]] && { error "--repo is required"; }
386 access_url urls "$repo"
387 ;;
388
389 --branch)
390 [[ ! -v repo ]] && { error "--repo is required"; }
391 branch_url "$repo"
392 ;;
393
394 --crowd*)
395 # https://crowd.opennetworking.org/crowd/console/secure/group/browse.action?directoryId=163841&updateSuccessful=
396 declare https='https://crowd.opennetworking.org'
397 declare base_url="${https}/crowd/console/secure/group/browse.action"
398 case "$arg" in
399 --crowd-*)
400 # urls+=("${base_url}?directoryId=163841&updateSuccessful=")
401 urls+=("${base_url}")
402 ;;
403 *)
404 # https://crowd.opennetworking.org/crowd/console/secure/group/browse.action?search=%22VOLTHACore%22
405 [[ $# -eq 0 ]] && { error "--crowd requires an argument"; }
406 arg="$1"; shift
407 urls+=("${base_url}?search=%22${arg}%22")
408 ;;
409 esac
410 ;;
411
412 --dash*) do_gerrit_dashboard urls ;;
413
414 #
415 --group*)
416 case "$arg" in
417 --group*)
418 urls+=("https://${serv}/admin/groups")
419 ;;
420 *)
421 [[ $# -eq 0 ]] && { error "--group requires an argument"; }
422 arg="$1"; shift
423 urls+=("https://${serv}/admin/groups/q/filter:${arg}}")
424 ;;
425 esac
426 ;;
427
428 --id)
429 id="$1"; shift
430 [[ ${#repo} -eq 0 ]] && { error "--repo is required"; }
431 changeset "$repo" "$id" urls
432 ;;
433
434 --status)
435 status="$1"; shift # open, closed, merged
436 urls[-1]+="+status:${status}"
437 ;;
438
439 --patch*)
440 repo="$1"; shift
441 urls+=("https://gerrit.opencord.org/q/${repo}+stats:open")
442 # https://gerrit.opencord.org/q/voltha-openolt-adapter-go
443 ;;
444 --review)
445 urls+=('https://gerrit.opencord.org/c/aether-ci-management/+/34900')
446 ;;
447
448 --ver*)
449 [[ $# -eq 0 ]] && { error "Usage: $arg [str]"; }
450 arg="$1"; shift
451 [[ ! -v versions ]] && { declare -a versions=(); }
452 versions+=("$arg")
453 ;;
454
455 # post getopt expansion to support --version x --version y
456 --view)
457 [[ $# -eq 0 ]] && { set -- '--default'; }
458 arg="$1"; shift
459 # [[ "${#views[@]}" -gt 0 ]]
460 [[ ! -v views ]] && { declare -A views=(); }
461 case "$arg" in
462
463 search*)
464 urls+=('https://gerrit.opencord.org/admin/repos')
465 ;;
466
467 repo) views['repository']=1 ;;
468
469 # https://gerrit.opencord.org/plugins/gitiles/voltha-go
470 master) views['master']=1 ;;
471 voltha*) views["$arg"]=1 ;;
472 v*) views["$arg"]=1 ;;
473 *)
474 # Missing latest tags
475 views['default']=1
476 views['master']=1
477 ;;
478 esac
479 ;;
480
481 -*)
482 echo "ERROR: Unknown argument [$arg]"
483 exit 1
484 ;;
485
486 [[:alnum:]]*)
487 declare -a what=()
488 # whats+=('patches')
489 whats+=('versions')
490
491 stem="https://gerrit.opencord.org/q/${arg}"
492
493 for what in "${whats[@]}";
494 do
495 case "$what" in
496 patches) urls+=("https://gerrit.opencord.org/q/${arg}") ;;
497 versions)
498 urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/heads/master/VERSION")
499 # urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/tags/v0.3.6/VERSION")
500 urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}/+/refs/heads/voltha-2.12/VERSION")
501 ;;
502 '??') urls+=("https://gerrit.opencord.org/plugins/gitiles/${arg}") ;;
503 *) urls+=("https://gerrit.opencord.org/q/${arg}") ;;
504 esac
505 done
506 esac
507done
508
509[[ -v argv_search ]] && { do_gerrit_search urls repos argv_status; }
510
511[[ -v admins ]] && { gen_admin_urls urls repos admins; }
512
513## are --views working ?
514[[ "${#views[@]}" -gt 0 ]] && { gen_view_urls urls repos views; }
515# [[ ! -v views ]] && { echo 'called'; gen_view_urls urls repos views; }
516
517[[ -v debug ]] && { echo "$BROWSER" "${urls[@]}"; }
518# "$BROWSER" "${urls[@]}" >/dev/null 2>/dev/null
519[[ -v argv_debug ]] && { set -x; }
520"$BROWSER" "${urls[@]}"
521[[ -v argv_debug ]] && { set +x; }
522
523## -----------------------------------------------------------------------
524## [TODO]
525## Query patches by repo:voltha-lib-go
526## ssh gerrit.opencord.org gerrit query --patch-sets projects:voltha-lib-go | grep -i description
527# ssh gerrit.opencord.org gerrit query --patch-sets 'projects:voltha-lib-go AND status:open limit:2' |
528
529# Show URL and Commit message
530# ssh gerrit.opencord.org gerrit query --format=JSON --patch-sets 'projects:voltha-lib-go AND status:open limit:2' | jq '.url,.commitMessage'
531## -----------------------------------------------------------------------
532
533# [EOF]