Skip to content

Instantly share code, notes, and snippets.

@gdbassett
Created December 14, 2017 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdbassett/db0975bcbaf46c69e77983d6acbdf6f8 to your computer and use it in GitHub Desktop.
Save gdbassett/db0975bcbaf46c69e77983d6acbdf6f8 to your computer and use it in GitHub Desktop.
A function demonstrating tokenized pagination of graphQL for R, (querying the github API to get vz-risk/VCDB issue IDs to compare to a verisr object)
#' A function to find github issues which may have been closed w/o being added
#'
#' NOTE: Many github issues have 'na' for their issue #, leading to false positives
#' NOTE: Requires ghql package, currently only at https://github.com/ropensci/ghql
#' `devtools::install_github("ropensci/ghql")`
#'
#' @param veris a verisr dataframe
#' @param gh_token a github user token for api access
#' @return a list of github issue numbers not in veris
#' @export
crosswalk_github_issues <- function(veris, gh_token) {
# initialize API
cli$exec(qry$queries$myquery)
cli <- ghql::GraphqlClient$new(
url = "https://api.github.com/graphql",
headers = httr::add_headers(Authorization = paste0("token ", gh_token))
)
cli$load_schema()
# iterator to stop while loop
qry <- ghql::Query$new()
qry$query('initialIssueCursor', '{
repository(owner: "vz-risk", name: "VCDB") {
issues(first: 100, states: CLOSED) {
totalCount,
edges {
node {
number
}
},
pageInfo {
startCursor
endCursor
hasNextPage
}
}
}
}')
getIssues <- '{
repository(owner:"vz-risk", name:"VCDB") {
issues (first:100, after:\"%s\", states:CLOSED) {
totalCount,
edges {
node {
number
}
},
pageInfo {
endCursor,
hasNextPage
}
}
}
}'
qry$query('getissues', getIssues)
q <- cli$exec(qry$queries$initialIssueCursor)
cursor <- q$data$repository$issues$pageInfo$endCursor
issues <- q$data$repository$issues$edges$node$number
while (q$data$repository$issues$pageInfo$hasNextPage) {
# message(paste0("At offset: ", cursor)) # DEBUG
qry$queries$getissues$query <- sprintf(getIssues, cursor)
q <- cli$exec(qry$queries$getissues)
cursor <- q$data$repository$issues$pageInfo$endCursor
issues <- c(issues, q$data$repository$issues$edges$node$number)
}
setdiff(issues, veris$plus.github)
}
@gdbassett
Copy link
Author

This uses https://github.com/ropensci/ghql to query the graph. Replacement of the 'after' cursor value is kind of rough but works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment