Skip to content

Instantly share code, notes, and snippets.

@buckelij
Created August 30, 2018 20:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buckelij/2c4d85901fcca2bf240d52a8822819f5 to your computer and use it in GitHub Desktop.
Save buckelij/2c4d85901fcca2bf240d52a8822819f5 to your computer and use it in GitHub Desktop.
paginate github scim data in bash/curl/jq
#!/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