Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit fe62328

Browse files
committed
use gitcreds for managing github PAT
1 parent 61f2003 commit fe62328

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Imports:
3535
assertthat,
3636
knitr,
3737
rmarkdown,
38-
dplyr
38+
dplyr,
39+
gitcreds
3940
Suggests:
4041
git2r,
4142
testthat

R/gist_auth.R

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#' Authorize with GitHub.
22
#'
3-
#' This function is run automatically to allow gistr to access your GitHub
3+
#' This function is run automatically to allow gistr to access your GitHub
44
#' account.
55
#'
66
#' There are two ways to authorise gistr to work with your GitHub account:
7-
#'
7+
#'
88
#' - Generate a personal access token with the gist scope selected, and set it
99
#' as the `GITHUB_PAT` environment variable per session using `Sys.setenv`
1010
#' or across sessions by adding it to your `.Renviron` file or similar.
@@ -16,7 +16,7 @@
1616
#' Using `GITHUB_PAT` is recommended.
1717
#'
1818
#' @export
19-
#' @param app An [httr::oauth_app()] for GitHub. The default uses an
19+
#' @param app An [httr::oauth_app()] for GitHub. The default uses an
2020
#' application `gistr_oauth` created by Scott Chamberlain.
2121
#' @param reauth (logical) Force re-authorization?
2222
#' @return a named list, with a single slot for `Authorization`, with a single
@@ -27,24 +27,33 @@
2727
#' }
2828

2929
gist_auth <- function(app = gistr_app, reauth = FALSE) {
30-
30+
31+
#if there is a token cached, use that
3132
if (exists("auth_config", envir = cache) && !reauth) {
32-
return(auth_header(cache$auth_config$auth_token$credentials$access_token))
33+
return(auth_header(cache$auth_config$auth_token))
3334
}
34-
pat <- Sys.getenv("GITHUB_PAT", "")
35-
if (!identical(pat, "")) {
36-
auth_config <- list(auth_token=list(credentials=list(access_token=pat)))
37-
} else if (!interactive()) {
35+
#if nothing cached, use gitcreds. gitcreds will retrieve PAT stored as an
36+
#GITHUB_PAT environment variable or one set with gitcreds::gitcreds_set()
37+
#TODO use tryCatch here to customize error message to be more helpful
38+
# cli::cli_alert_warning("Please set {.field GITHUB_PAT} in your {.file .Renviron} or use the {.pkg gitcreds}
39+
creds <- try(gitcreds::gitcreds_get())
40+
if (!inherits(creds, "try-error")) {
41+
token <- creds$password
42+
} else if (interactive()) { #if no gitcreds, try interactive authentication
43+
# try oauth direct
44+
endpt <- httr::oauth_endpoints("github")
45+
auth <- httr::oauth2.0_token(endpt, app, scope = "gist", cache = !reauth)
46+
token <- auth$credentials$access_token
47+
} else {
3848
stop("In non-interactive environments, please set GITHUB_PAT env to a GitHub",
3949
" access token (https://help.github.com/articles/creating-an-access-token-for-command-line-use)",
4050
call. = FALSE)
41-
} else {
42-
endpt <- httr::oauth_endpoints("github")
43-
token <- httr::oauth2.0_token(endpt, app, scope = "gist", cache = !reauth)
44-
auth_config <- httr::config(token = token)
4551
}
52+
53+
#cache auth config
54+
auth_config <- httr::config(token = token)
4655
cache$auth_config <- auth_config
47-
auth_header(auth_config$auth_token$credentials$access_token)
56+
return(auth_header(auth_config$auth_token))
4857
}
4958

5059
auth_header <- function(x) list(Authorization = paste0("token ", x))

0 commit comments

Comments
 (0)