Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add a whoami function (user, token, scopes) * documentation * unrelated typos * roxygenize * better pointer to help * link to gh_whoami() help from gh help * test gh_whoami() * Announce mocking status when recording * Add additional classes to returned error objects (#50) This allows you to catch a github_error or the exact status code directly. * Record tests (sort of, see below) in test-error.R Note: there's only one new recording because the second test matches against first recording. It just happens to pass in a happy coincidence. * Improve and record tests of gh_whoami() * Make GITHUB_TOKEN be non-NULL on travis and appveyor * Don't run any example re: gh_whoami() * Use if() {} else {} when forming Authorization header * Use gh() instead of low-level fxns in gh_whoami()
- Loading branch information
1 parent
23308b6
commit 951031e
Showing
18 changed files
with
226 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#' Info on current GitHub user and token | ||
#' | ||
#' Reports wallet name, GitHub login, and GitHub URL for the current | ||
#' authenticated user, the first few and last characters of the token, and the | ||
#' associated scopes. | ||
#' | ||
#' Get a personal access token for the GitHub API from | ||
#' \url{https://github.com/settings/tokens} and select the scopes necessary for | ||
#' your planned tasks. The \code{repo} scope, for example, is one many are | ||
#' likely to need. The token itself is a string of 40 letters and digits. You | ||
#' can store it any way you like and provide explicitly via the \code{.token} | ||
#' argument to \code{\link{gh}()}. | ||
#' | ||
#' However, many prefer to define an environment variable \code{GITHUB_PAT} (or | ||
#' \code{GITHUB_TOKEN}) with this value in their \code{.Renviron} file. Add a | ||
#' line that looks like this, substituting your PAT: | ||
#' | ||
#' \preformatted{ | ||
#' GITHUB_PAT=8c70fd8419398999c9ac5bacf3192882193cadf2 | ||
#' } | ||
#' | ||
#' Put a line break at the end! If you’re using an editor that shows line | ||
#' numbers, there should be (at least) two lines, where the second one is empty. | ||
#' Restart R for this to take effect. Call \code{gh_whoami()} to confirm | ||
#' success. | ||
#' | ||
#' To get complete information on the authenticated user, call | ||
#' \code{gh("/user")}. | ||
#' | ||
#' For token management via API (versus the browser), use the | ||
#' \href{https://developer.github.com/v3/oauth_authorizations/}{OAuth | ||
#' Authorizations API}. This API requires Basic Authentication using your | ||
#' username and password, not tokens, and is outside the scope of the \code{gh} | ||
#' package. | ||
#' | ||
#' @inheritParams gh | ||
#' | ||
#' @return A \code{gh_response} object, which is also a \code{list}. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' gh_whoami() | ||
#' | ||
#' ## explicit token + use with GitHub Enterprise | ||
#' gh_whoami(.token = "8c70fd8419398999c9ac5bacf3192882193cadf2", | ||
#' .api_url = "https://github.foobar.edu/api/v3") | ||
#' } | ||
gh_whoami <- function(.token = NULL, .api_url = NULL, .send_headers = NULL) { | ||
.token <- .token %||% gh_token() | ||
if (isTRUE(.token == "")) { | ||
message("No personal access token (PAT) available.\n", | ||
"Obtain a PAT from here:\n", | ||
"https://github.com/settings/tokens\n", | ||
"For more on what to do with the PAT, see ?gh_whoami.") | ||
return(invisible(NULL)) | ||
} | ||
res <- gh(endpoint = "/user", .token = .token, | ||
.api_url = .api_url, .send_headers = .send_headers) | ||
scopes <- attr(res, "response")[["x-oauth-scopes"]] | ||
res <- res[c("name", "login", "html_url")] | ||
res$scopes <- scopes | ||
res$token <- hide_middle(.token) | ||
## 'gh_response' class has to be restored | ||
class(res) <- c("gh_response", "list") | ||
res | ||
} | ||
|
||
hide_middle <- function(x, n = 4) { | ||
paste0(substr(x, start = 1, stop = n), | ||
"...", | ||
substr(x, start = nchar(x) - n + 1, stop = nchar(x))) | ||
} |
This file contains 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
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions
1
tests/testthat/httrmock/keys/objects/1b3bdfc20da2394badadab615ceed82c
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
b9a62b3016e67bbab29cda2f3ec3457b |
1 change: 1 addition & 0 deletions
1
tests/testthat/httrmock/keys/objects/ebbdcbead1d79635fc42a966994ee813
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
99a06080989bb5e278f39cf8c9035a26 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
context("whoami") | ||
|
||
test_that("whoami works in presence of PAT", { | ||
## being explicit re token because GITHUB_PAT > GITHUB_TOKEN in gh_token() | ||
## don't want developer's GITHUB_PAT to override gh-testing's GITHUB_TOKEN | ||
res <- gh_whoami(Sys.getenv("GITHUB_TOKEN")) | ||
expect_s3_class(res, "gh_response") | ||
expect_identical(res[["login"]], "gh-testing") | ||
expect_match(res[["scopes"]], "\\brepo\\b") | ||
expect_match(res[["scopes"]], "\\buser\\b") | ||
}) | ||
|
||
test_that("whoami works in absence of PAT", { | ||
expect_message(res <- gh_whoami(.token = ""), | ||
"No personal access token \\(PAT\\) available.") | ||
expect_null(res) | ||
}) | ||
|
||
test_that("whoami errors with bad PAT", { | ||
skip("re-activate when request matching sorted out (gaborcsardi/httrmock#3)") | ||
|
||
e <- tryCatch(gh_whoami(.token = NA), error = identity) | ||
expect_s3_class(e, "github_error") | ||
expect_s3_class(e, "http_error_401") | ||
|
||
e <- tryCatch(gh_whoami(.token = "blah"), error = identity) | ||
expect_s3_class(e, "github_error") | ||
expect_s3_class(e, "http_error_401") | ||
}) |