# shellcheck shell=bash #
# vim: ft=sh:ai:ts=8:sw=8:noet
set -eufCo pipefail
export SHELLOPTS
IFS=$'\t\n'
function _urlencode() {
# echoes url encoded input back
local _tmp="${1}"
_tmp="${_tmp// /%20}" # space
_tmp="${_tmp//-/%2d}" # -
_tmp="${_tmp//./%2e}" # .
_tmp="${_tmp//\//%2f}" # /
echo "${_tmp}"
}
export -f _urlencode
function _gitlab_paginate() {
# handles gitlab pagination, returns joined result back
# TODO: function name
# TODO: move this into generator
if [[ "unset" == "${GITLAB_TOKEN:-unset}" ]]; then
# TODO: make sure its _env_ var
>&2 echo "Error: need GITLAB_TOKEN env var"
return 42
fi
# TODO: move this into generator
if [[ 1 -ne $# ]]; then
>&2 echo "Error: need URL"
fi
local url="${1}"
if [[ "${1}" != *"per_page="* ]]; then
if [[ "${1}" = *"?"* ]]; then
url="${1}&per_page=${GITLAB_PER_PAGE:-100}"
else
url="${1}?per_page=${GITLAB_PER_PAGE:-100}"
fi
fi
# TODO: move this into generator
while read -r cmd; do
command -v "${cmd}" >/dev/null 2>/dev/null || { >&2 echo "Error: command '${cmd}' required!" ; }
done <<-CMD
curl
jq
CMD
local pages
pages="$(2>/dev/null curl -qsSLI -H "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "${url}" \
| awk -F ',' '/^[Ll]ink: / { gsub(/.*&page=/,"");gsub(/[&>].*/,"");print}')"
if [[ "xx" == "x${pages}x" ]]; then
>&2 echo "Got empty responce for pages, assuming no pagination and setting pages=1"
pages=1
elif [[ ! "${pages}" =~ ^[0-9]+$ ]]; then
>&2 echo "Got non-number '${pages}' as pages, exiting"
return 42
fi
for p in $(seq 1 "${pages}"); do
curl -qsSL -H "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "${url}&page=${p}"
done | jq -n 'inputs' | jq -ns 'inputs | add'
}
export -f _gitlab_paginate
#
source <(curl -qsSL https://gitlab.sh)
in scripts. handy af. strict. WIP. Commit: f9a72d86