Skip to content

Instantly share code, notes, and snippets.

@KyMidd

KyMidd/asdf.sh Secret

Created April 12, 2024 19:44
Show Gist options
  • Select an option

  • Save KyMidd/dbcc29364956a19e666f8fe1f43e8ff0 to your computer and use it in GitHub Desktop.

Select an option

Save KyMidd/dbcc29364956a19e666f8fe1f43e8ff0 to your computer and use it in GitHub Desktop.
###
### Functions
###
# Hold until rate-limit success
hold_until_rate_limit_success() {
# Loop forever
while true; do
# Any call to AWS returns rate limits in the response headers
API_RATE_LIMIT_UNITS_REMAINING=$(curl -sv \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$gh_org/ActionPRValidate_AnyJobRun/autolinks 2>&1 1>/dev/null \
| grep -E '< x-ratelimit-remaining' \
| cut -d ' ' -f 3 \
| xargs \
| tr -d '\r')
# If API rate-limiting is hit, sleep for 1 minute
# Rounded parenthesis are used to trigger arithmetic expansion, which compares more than the first numeric digit (bash is weird)
if (( "$API_RATE_LIMIT_UNITS_REMAINING" < 100 )); then
echo "ℹ️ We have less than 100 GitHub API rate-limit tokens left ($API_RATE_LIMIT_UNITS_REMAINING), sleeping for 1 minute"
sleep 60
# If API rate-limiting shows remaining units, break out of loop and function
else
echo "💥 Rate limit checked, we have "$API_RATE_LIMIT_UNITS_REMAINING" core tokens remaining so we are continuing"
break
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment