Skip to content

Instantly share code, notes, and snippets.

@mislav
Last active January 24, 2024 23:38
Show Gist options
  • Save mislav/63c8e9f130020fe66b0947916022614d to your computer and use it in GitHub Desktop.
Save mislav/63c8e9f130020fe66b0947916022614d to your computer and use it in GitHub Desktop.
hub api example of how to fetch all comments over GraphQL
comments() {
local issue="${1?}"
shift 1
paginate hub api --cache 3600 graphql -F issue="$issue" "$@" -f query='
query ($owner: String = "{owner}", $repo: String = "{repo}", $issue: Int!, $per_page: Int = 30, $after: String) {
repository(owner: $owner, name: $repo) {
issueOrPullRequest(number: $issue) {
... on Issue {
comments(first: $per_page, after: $after) {
...Comments
}
}
... on PullRequest {
comments(first: $per_page, after: $after) {
...Comments
}
}
}
}
}
fragment Comments on IssueCommentConnection {
edges {
node {
author {
login
}
body
}
}
pageInfo {
hasNextPage
endCursor
}
}
'
}
# Automatically fetch all pages of the provided GraphQL query
paginate() {
local output cursor
output="$("$@")"
if [[ $output == \{* ]]; then
cursor="$(jq -r '..|.pageInfo?|select(.hasNextPage)|.endCursor' <<<"$output")"
else
cursor="$(awk '/\.hasNextPage/ { has_next=$2 } /\.endCursor/ { if (has_next=="true") print $2 }' <<<"$output")"
fi
printf "%s\n" "$output"
[ -z "$cursor" ] || paginate "$@" -f after="$cursor"
}
# Example usage: fetch all comments for issue/PR #1705 of the current repository and output them as text
comments 1705 -F per_page=50 | \
jq -r '.data.repository.issueOrPullRequest.comments.edges[].node | "@\(.author.login): \(.body)\n\n"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment