Created
August 30, 2018 20:45
-
-
Save buckelij/2c4d85901fcca2bf240d52a8822819f5 to your computer and use it in GitHub Desktop.
paginate github scim data in bash/curl/jq
This file contains 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 | |
# requires `bash, `jq` and `curl` | |
# set your credentials and org name below | |
# | |
# This script paginates the GraphQL API to get all users. It does not check for rate limits | |
# but sleeps 10 seconds after every call to try to avoid hitting a rate limit. | |
CREDENTIALS=USERNAME:TOKEN | |
ORG=YOUR-ORG | |
CURSOR=null | |
function getPage { | |
curl -s -X POST -u $CREDENTIALS -H "Content-Type: application/json" -d '{ "query": "{ organization(login: \"'$ORG'\") { samlIdentityProvider { externalIdentities(first: 100, after: '$1') { pageInfo { endCursor startCursor hasNextPage } edges { cursor node { samlIdentity { nameId } scimIdentity { username } user { login } } } } } } }" }' https://api.github.com/graphql | |
} | |
output="" | |
while true; do | |
page=$(getPage "$CURSOR") | |
output=$(echo "$output" "$page") | |
hasNextPage=$(echo "$page" | jq .data.organization.samlIdentityProvider.externalIdentities.pageInfo.hasNextPage) | |
if [ "$hasNextPage" = "true" ]; then | |
CURSOR='\"'$(echo "$page" | jq -r .data.organization.samlIdentityProvider.externalIdentities.pageInfo.endCursor)'\"' | |
else | |
break | |
fi | |
sleep 10 | |
done | |
echo "$output" | jq . -C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment