-
Notifications
You must be signed in to change notification settings - Fork 7
Add token instructions + support for GITHUB_PAT and GITHUB_TOKEN #22
Conversation
use GITHUB_TOKEN or GITHUB_PAT if GITHUB_GRAPHQL_TOKEN is not found
This looks great! And such a great PR description! 👏 To add testing, we'd need to rely on mocking. I wasn't able to find a resource for a how to mock a function depending on its arguments (https://github.com/r-lib/whoami/blob/master/tests/testthat/test-gh-username.R#L25 felt hacky even if it works and was merged 😂). I wonder whether it'd be overkill to define 3 helpers: get_github_token <- function(...){
Sys.getenv("GITHUB_TOKEN", ...)
}
get_github_path <- function(){
Sys.getenv("GITHUB_PAT")
} and mock their behavior to see what |
Two other notes
|
What do you think of this test for context("test-get_token")
test_that("check that correct token is retrieved", {
mock_sysgetenv <- function(gh_graphql, gh_token, gh_pat) {
function(x, unset = ""){
token <- switch(x,
"GITHUB_GRAPHQL_TOKEN" = gh_graphql,
"GITHUB_TOKEN" = gh_token,
"GITHUB_PAT" = gh_pat)
if(token != "") return(token)
return(unset)
}
}
with_mock(
Sys.getenv = mock_sysgetenv("aa", "bb", "cc"),
expect_equal(get_token(), "aa")
)
with_mock(
Sys.getenv = mock_sysgetenv("", "bb", "cc"),
expect_equal(get_token(), "bb")
)
with_mock(
Sys.getenv = mock_sysgetenv("", "", "cc"),
expect_equal(get_token(), "cc")
)
with_mock(
Sys.getenv = mock_sysgetenv("", "", ""),
expect_error(get_token())
)
}) |
wow I might only have written one function factory! It looks great! |
Ok, I added the test and added myself as a contributor. Couldn't figure out how to use |
Thank you! Will review next week. |
Oops I snoozed the reminder too much! On it now! |
Thanks again! |
Woot! |
In this PR, I made the following changes:
get_tokens()
to retrieve tokens in the following order of precedence: 1. GITHUB_GRAPHQL_TOKEN 2. GITHUB_TOKEN 3. GITHUB_PATiterate()
to stop and return error message given in responsesIt's probably a good idea to add a test, but I'm not sure what the best way is of doing that.
Relevant issues/code that came up in my discussion with @maelle :
#6
ropensci/ghql#11
https://github.com/r-lib/whoami/blob/master/R/whoami.R#L279
https://github.com/r-lib/gh/blob/bed43d354789d2e4dde75db0327da1d46358f953/R/gh_request.R#L143