-
Notifications
You must be signed in to change notification settings - Fork 1
Add Bash and PowerShell scripts for monitoring GitHub API rate limits #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,184 @@ | ||||||||||||||||||||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||||||||||||||||||||
| # gh-ratelimit - GitHub API rate-limit monitor for Bash and POSIX-style shells. | ||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||
| # What it does: | ||||||||||||||||||||||||||||||||
| # Queries GitHub's /rate_limit endpoint through GitHub CLI and renders the | ||||||||||||||||||||||||||||||||
| # current API buckets sorted by pressure so the tightest buckets appear first. | ||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||
| # Requirements: | ||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+8
|
||||||||||||||||||||||||||||||||
| # - gh (GitHub CLI) | ||||||||||||||||||||||||||||||||
| # - jq | ||||||||||||||||||||||||||||||||
| # - an authenticated gh session | ||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||
| # Setup: | ||||||||||||||||||||||||||||||||
| # macOS: brew install gh jq | ||||||||||||||||||||||||||||||||
| # Ubuntu: sudo apt-get install gh jq | ||||||||||||||||||||||||||||||||
| # Then authenticate once: | ||||||||||||||||||||||||||||||||
| # gh auth login | ||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||
| # Notes: | ||||||||||||||||||||||||||||||||
| # - This script uses gh for both API access and auth context. | ||||||||||||||||||||||||||||||||
| # - Bash version requires jq for parsing and pretty-printing JSON. | ||||||||||||||||||||||||||||||||
| # - GitHub's /rate_limit endpoint is exempt from rate limiting, so polling it is safe. | ||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||
| # Usage: | ||||||||||||||||||||||||||||||||
| # gh-ratelimit | ||||||||||||||||||||||||||||||||
| # gh-ratelimit -w | ||||||||||||||||||||||||||||||||
| # gh-ratelimit -w -i 5 | ||||||||||||||||||||||||||||||||
| # gh-ratelimit -j | ||||||||||||||||||||||||||||||||
| # gh-ratelimit -q | ||||||||||||||||||||||||||||||||
| # gh-ratelimit -h | ||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||
| # Options: | ||||||||||||||||||||||||||||||||
| # -w, --watch Refresh continuously. | ||||||||||||||||||||||||||||||||
| # -i, --interval Watch refresh interval in seconds. Default: 10. | ||||||||||||||||||||||||||||||||
| # -j, --json Print the raw GitHub API response as formatted JSON. | ||||||||||||||||||||||||||||||||
| # -q, --quiet Only show buckets that are not full and have a non-zero limit. | ||||||||||||||||||||||||||||||||
| # -h, --help Show this help text. | ||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||
| # Examples: | ||||||||||||||||||||||||||||||||
| # gh-ratelimit # one-time snapshot | ||||||||||||||||||||||||||||||||
| # gh-ratelimit -q # only show buckets under pressure | ||||||||||||||||||||||||||||||||
| # gh-ratelimit -w -i 60 # refresh every 60 seconds | ||||||||||||||||||||||||||||||||
| # gh-ratelimit -j | jq . # inspect raw payload | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| set -euo pipefail | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| WATCH=0 | ||||||||||||||||||||||||||||||||
| INTERVAL=10 | ||||||||||||||||||||||||||||||||
| JSON=0 | ||||||||||||||||||||||||||||||||
| QUIET=0 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| show_usage() { | ||||||||||||||||||||||||||||||||
| awk 'NR == 1 { next } /^#/ { sub(/^# ?/, ""); print; next } { exit }' "$0" | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| while [[ $# -gt 0 ]]; do | ||||||||||||||||||||||||||||||||
| case "$1" in | ||||||||||||||||||||||||||||||||
| -w|--watch) WATCH=1 ;; | ||||||||||||||||||||||||||||||||
| -i|--interval) INTERVAL="${2:-10}"; shift ;; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| -i|--interval) INTERVAL="${2:-10}"; shift ;; | |
| -i|--interval) | |
| if [[ -z "${2:-}" || "${2:-}" == -* ]]; then | |
| echo "error: -i|--interval requires a positive integer argument" >&2 | |
| show_usage >&2 | |
| exit 2 | |
| fi | |
| if ! [[ "$2" =~ ^[1-9][0-9]*$ ]]; then | |
| echo "error: invalid interval '$2'; expected a positive integer" >&2 | |
| show_usage >&2 | |
| exit 2 | |
| fi | |
| INTERVAL="$2" | |
| shift | |
| ;; |
Copilot
AI
Apr 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Host parsing from gh auth status is using awk ... {print $5}, which (with the common output Logged in to github.com as <user> (...)) will print as instead of the hostname. Adjust the parsing to reliably extract the hostname (e.g., the token after to), or use a less brittle approach than fixed field numbers.
| host=$(gh auth status 2>&1 | awk -F'[ ()]+' '/Logged in to/{print $5; exit}' || echo 'github.com') | |
| host=$(gh auth status 2>&1 | awk -F'[ ()]+' '/Logged in to/ { for (i = 1; i < NF; i++) if ($i == "to") { print $(i + 1); exit } }' || echo 'github.com') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The header comment claims this script works with "Bash and POSIX-style shells", but the implementation uses Bash-specific features (
#!/usr/bin/env bash,[[ ... ]],local, and process substitution< <(...)). Either update the description to state it requires Bash, or refactor to be POSIX-sh compatible.