Created
June 7, 2022 02:05
-
-
Save sbassett29/d44f0a8f5ebfcd80d4c4af6aa5910c9b to your computer and use it in GitHub Desktop.
Returns (complete) count of rows from gerrit api searches (gerrit.website/r/changes/?q=some-search-string)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
################################################################################ | |
# Author: [email protected] | |
# License: Apache 2 <https://opensource.org/licenses/Apache-2.0> | |
# Description: | |
# A way to get (complete) row counts for gerrit searches via its api | |
################################################################################ | |
set -euo pipefail | |
# validate arguments | |
if ([ -z ${1+x} ]); then | |
printf "One argument required: {gerrit search url}. Exiting.\n" | |
exit 1 | |
fi | |
# set variables | |
readonly gerrit_search_url="$1" | |
readonly gerrit_max_results=500 | |
# Description: Check binary dependencies | |
# Arguments: List of binaries from args | |
# Output: Error message and exit | |
function check_binaries() { | |
bins="$@" | |
for bin in "${bins[@]}"; do | |
if [[ -z $(command -v $bin) ]]; then | |
printf "Dependency '$bin' does not appear to be installed.\n" | |
printf "Exiting for now...\n" | |
exit 1 | |
fi | |
done | |
} | |
# Description: run curl against gerrit api searches and get count | |
# Arguments: $1 = gerrit api search (without start/end) | |
# Outputs: row count total | |
function get_total_row_count() { | |
local total_count=0 | |
local next=1 | |
initial_req_data=$(curl -s "$gerrit_search_url") | |
initial_count=$(echo "$initial_req_data" | sed 1d | jq '. | length') | |
more_results=$(echo "$initial_req_data" | sed '1d' | jq '. | last._more_changes') | |
total_count=$(($total_count + $initial_count)) | |
while [ "$more_results" = "true" ] | |
do | |
next=$(($next + $gerrit_max_results)) | |
next_req_data=$(curl -s "$gerrit_search_url""&S=""$next") | |
next_count=$(echo "$next_req_data" | sed 1d | jq '. | length') | |
more_results=$(echo "$next_req_data" | sed '1d' | jq '. | last._more_changes') | |
total_count=$(($total_count + $next_count)) | |
done | |
echo "$total_count" | |
} | |
# Description: Main | |
# Arguments: Arguments provided to the script | |
# Output: None | |
function main() { | |
# set bin dependencies | |
local bins=("curl" "sed" "jq") | |
check_binaries "${bins[@]}" | |
get_total_row_count | |
} | |
# Call main() | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment