Last active
April 12, 2024 10:29
-
-
Save axdotl/9bf9317639101f6f68121c9878b85284 to your computer and use it in GitHub Desktop.
Shell Script to get rate limit status for a Github App installation
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
#!/bin/bash | |
set -o pipefail | |
app_id=$1 # App ID as first argument | |
installation_id=$2 # Installation ID as second argument | |
pem=$( cat $3 ) # file path of the private key as third argument | |
now=$(date +%s) | |
iat=$((${now} - 60)) # Issues 60 seconds in the past | |
exp=$((${now} + 600)) # Expires 10 minutes in the future | |
b64enc() { openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'; } | |
header_json='{ | |
"typ":"JWT", | |
"alg":"RS256" | |
}' | |
# Header encode | |
header=$( echo -n "${header_json}" | b64enc ) | |
payload_json='{ | |
"iat":'"${iat}"', | |
"exp":'"${exp}"', | |
"iss":'"${app_id}"' | |
}' | |
# Payload encode | |
payload=$( echo -n "${payload_json}" | b64enc ) | |
# Signature | |
header_payload="${header}"."${payload}" | |
signature=$( | |
openssl dgst -sha256 -sign <(echo -n "${pem}") \ | |
<(echo -n "${header_payload}") | b64enc | |
) | |
# Create JWT | |
JWT="${header_payload}"."${signature}" | |
printf '%s\n' "JWT: $JWT" | |
# Get the installation access token | |
TOKEN=$(curl --request POST \ | |
--url "https://api.github.com/app/installations/${installation_id}/access_tokens" \ | |
--header "Accept: application/vnd.github+json" \ | |
--header "Authorization: Bearer $JWT" \ | |
--header "X-GitHub-Api-Version: 2022-11-28" | jq -r .token) | |
printf '%s\n' "TOKEN: $TOKEN" | |
# Get the rate limit information | |
curl -L \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer $TOKEN" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
https://api.github.com/rate_limit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment