Skip to content

Instantly share code, notes, and snippets.

@marjamis
Last active September 3, 2020 09:56
Show Gist options
  • Save marjamis/2b0088ead7fcb6ea2ccfb196818b2be6 to your computer and use it in GitHub Desktop.
Save marjamis/2b0088ead7fcb6ea2ccfb196818b2be6 to your computer and use it in GitHub Desktop.
Github GraphQL bash script to loop through the results with a sample query.
#!/bin/bash -ex
CURL_COMMAND='curl -X POST https://api.github.com/graphql'
RAW_OUTPUT_FILE="$(tempfile -s ".query.output")"
MINIMUM_PLUS_1_COUNT=20
function API_call() {
if [ ! -v NEXT_PAGE ]; then
api_output=$($CURL_COMMAND -H "Authorization:bearer $GITHUB_TOKEN" -d @query)
else
newQuery=$(cat query | sed 's/first:100,/first:100, after:\\"'$NEXT_PAGE'\\", /' > query.temp)
api_output=$($CURL_COMMAND -H "Authorization:bearer $GITHUB_TOKEN" -d @query.temp)
rm query.temp
fi
echo $api_output >> $RAW_OUTPUT_FILE
if [ $(echo $api_output | jq -r ".data.repository.issues.pageInfo.hasNextPage") == "false" ] ; then
echo null
exit
fi
echo $api_output | jq -r ".data.repository.issues.edges[-1].cursor"
}
function paginateLoop() {
while [ "$NEXT_PAGE" != "null" ]; do
NEXT_PAGE=$(API_call $NEXT_PAGE)
done
}
paginateLoop
cat $RAW_OUTPUT_FILE
# Note: Need to name it thumbsUp for a key (no shortcut) to be able to move it up a level
# -s to combine multiple pages in the file into an array, as referenced with the .[] part below and then all things are in the one array rather than the same operations being applied to each array/page of data in the file
jq -rs '[.[].data.repository.issues.edges[].node | {url, title, createdAt, thumbsUp: .reactions.thumbsUp}] | map(select(.thumbsUp >= '$MINIMUM_PLUS_1_COUNT')) | sort_by(.thumbsUp) | reverse | .[0:10] | (map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' $RAW_OUTPUT_FILE | tee analysis.csv
rm $RAW_OUTPUT_FILE
{
"query": "query {
rateLimit {
cost
remaining
}
repository(owner:\"aws\", name:\"containers-roadmap\") {
issues(first:100, states:OPEN, labels:[\"EKS\"]) {
edges {
node {
id
createdAt
title
url
reactions(content:THUMBS_UP) {
thumbsUp: totalCount
}
}
cursor
}
pageInfo {
endCursor
hasNextPage
}
}
}
}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment