From 0efae2cea64b16a7e7f46d0b7303b1f4c63f9370 Mon Sep 17 00:00:00 2001 From: Mark Stansberry Date: Thu, 27 Feb 2020 15:17:49 -0500 Subject: [PATCH 1/4] Add multi-page status results support --- bin/common.sh | 17 +++++++++++++++++ bin/in | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/bin/common.sh b/bin/common.sh index 32f0ee3..bd5e9b5 100644 --- a/bin/common.sh +++ b/bin/common.sh @@ -54,3 +54,20 @@ curlgh () { fi curl $skip_verify_arg -s -H "Authorization: token $source_access_token" $@ } + +curlgh_all_pages_status () { + results='' + page=1 + present='true' + while [ "$present" = "true" ]; do + current_results=$(curlgh "$@?page=$page" | jq .) + if [ "$(echo $current_results | jq .statuses)" != "[]" ]; then + results=$(jq -s '.[0] as $o1 | .[1] as $o2 | ($o1 + $o2) | .statuses = ($o1.statuses + $o2.statuses)' <(echo $results) <(echo $current_results)) + page=$(($page+1)) + else + present='false' + fi + done + + echo "$results" +} diff --git a/bin/in b/bin/in index b0d5e56..ae413f1 100755 --- a/bin/in +++ b/bin/in @@ -16,7 +16,7 @@ eval $( jq -r '{ # lookup # -curlgh "$source_endpoint/repos/$source_repository/commits/$version_commit/status?per_page=100" \ +curlgh_all_pages_status "$source_endpoint/repos/$source_repository/commits/$version_commit/status" \ | jq -c \ --arg status "$version_status" \ '{ From 23bf3723bd2c28dc12400afcd05e28eacdb380cb Mon Sep 17 00:00:00 2001 From: Dennis Wong Date: Thu, 27 Feb 2020 22:41:48 -0500 Subject: [PATCH 2/4] Identity function from jq should not change the output --- bin/common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/common.sh b/bin/common.sh index bd5e9b5..c65ea17 100644 --- a/bin/common.sh +++ b/bin/common.sh @@ -60,7 +60,7 @@ curlgh_all_pages_status () { page=1 present='true' while [ "$present" = "true" ]; do - current_results=$(curlgh "$@?page=$page" | jq .) + current_results=$(curlgh "$@?page=$page") if [ "$(echo $current_results | jq .statuses)" != "[]" ]; then results=$(jq -s '.[0] as $o1 | .[1] as $o2 | ($o1 + $o2) | .statuses = ($o1.statuses + $o2.statuses)' <(echo $results) <(echo $current_results)) page=$(($page+1)) From 320683ef5688025eaa42797329cc85d1eefb1b2c Mon Sep 17 00:00:00 2001 From: Dennis Wong Date: Thu, 27 Feb 2020 23:05:04 -0500 Subject: [PATCH 3/4] Alternative method for combining statuses array across multiple queries --- bin/common.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/common.sh b/bin/common.sh index c65ea17..c084b4c 100644 --- a/bin/common.sh +++ b/bin/common.sh @@ -61,8 +61,12 @@ curlgh_all_pages_status () { present='true' while [ "$present" = "true" ]; do current_results=$(curlgh "$@?page=$page") - if [ "$(echo $current_results | jq .statuses)" != "[]" ]; then - results=$(jq -s '.[0] as $o1 | .[1] as $o2 | ($o1 + $o2) | .statuses = ($o1.statuses + $o2.statuses)' <(echo $results) <(echo $current_results)) + + # If key "statuses" is not present, stop iterating loop + statuses=$(echo $current_results | jq -c '.statuses // []') + if [ "$statuses" != "[]" ]; then + # Identify "statuses" array in `current_results` and append it to "statuses" array in `results` + results=$(echo "$results" | jq --argjson s "$statuses" '.statuses += $s') page=$(($page+1)) else present='false' From 507e2b1e73cd2414d48f5db3cdff1c83f318af67 Mon Sep 17 00:00:00 2001 From: Dennis Wong Date: Thu, 27 Feb 2020 23:08:34 -0500 Subject: [PATCH 4/4] Save inital response so there is an array to append to in future iterations of loop --- bin/common.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bin/common.sh b/bin/common.sh index c084b4c..fb98a35 100644 --- a/bin/common.sh +++ b/bin/common.sh @@ -57,17 +57,25 @@ curlgh () { curlgh_all_pages_status () { results='' - page=1 + page=0 present='true' while [ "$present" = "true" ]; do + page=$(($page+1)) current_results=$(curlgh "$@?page=$page") + # Save the first query as the return value, in case the response + # body is not expected, the response body can still be returned + # and behave like curlgh would in the non-happy path. + if [ -z "$results" ]; then + results="$current_results" + continue + fi + # If key "statuses" is not present, stop iterating loop statuses=$(echo $current_results | jq -c '.statuses // []') if [ "$statuses" != "[]" ]; then # Identify "statuses" array in `current_results` and append it to "statuses" array in `results` results=$(echo "$results" | jq --argjson s "$statuses" '.statuses += $s') - page=$(($page+1)) else present='false' fi