Created
December 14, 2017 20:38
-
-
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)
This file contains hidden or 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
#' 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This uses https://github.com/ropensci/ghql to query the graph. Replacement of the 'after' cursor value is kind of rough but works.