Allow authenticated Github use in changelog script and improve error messages

This commit is contained in:
Katrina Verey
2023-02-01 15:39:38 -05:00
parent 95edcc0681
commit 0c34cf4192

View File

@@ -27,6 +27,13 @@ if [[ -z "${1-}" ]] || [[ -z "${2-}" ]]; then
exit 1
fi
if [[ -z "${GITHUB_USERNAME-}" ]] || [[ -z "${GITHUB_TOKEN-}" ]]; then
echo "WARNING: Please set GITHUB_USERNAME and GITHUB_TOKEN to avoid GitHub API rate limits."
github_auth_string=""
else
github_auth_string="-u ${GITHUB_USERNAME}:${GITHUB_TOKEN}"
fi
module=$1
fullTag=$2
changeLogFile="${3:-}"
@@ -49,10 +56,21 @@ results=""
for((i=0; i < ${#commits[@]}; i+=batchSize))
do
commitList=$(IFS="+"; echo "${commits[@]:i:batchSize}" | sed 's/ /+/g')
if newResults=$(curl -sSL "https://api.github.com/search/issues?q=$commitList+repo%3Akubernetes-sigs%2Fkustomize" | jq -r '[ .items[] | { number, title } ]'); then
if ! newResultsRaw=$(curl -sSL "https://api.github.com/search/issues?q=$commitList+repo%3Akubernetes-sigs%2Fkustomize+is:pull-request" "${github_auth_string}"); then
echo "Failed to fetch results for commits (exit code $?): $commitList"
exit 1
fi
if [[ "${newResultsRaw}" == *"API rate limit exceeded"* ]]; then
echo "GitHub API rate limit exceeded. Please set GITHUB_USERNAME and GITHUB_TOKEN to avoid this."
exit 1
fi
if [[ "${newResultsRaw}" == *"\"items\":"* ]] ; then
newResults=$(echo "$newResultsRaw" | jq -r '[ .items[] | { number, title } ]')
results=$(echo "$results" "$newResults" | jq -s '.[0] + .[1] | unique')
else
echo "Failed to fetch results for commits (exit code $?): $commitList"
echo "Request for commits $commitList returned invalid results"
exit 1
fi
done