Skip to content

Commit

Permalink
support GH_TOKEN and GITHUB_TOKEN for auth (#1937)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Jul 9, 2024
1 parent 0f0ad9d commit 37f5b31
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Suggests: BiocManager, cli, covr, cpp11, devtools, gitcreds, jsonlite, jsonvalid
miniUI, packrat, pak, R6, remotes, reticulate, rmarkdown, rstudioapi, shiny, testthat,
uuid, waldo, yaml, webfakes
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
Config/Needs/website: tidyverse/tidytemplate
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# renv (development version)

* `renv` now supports setting of GitHub authentication credentials via
any of `GITHUB_TOKEN`, `GITHUB_PAT`, and `GH_TOKEN`. (#1937)

* `renv` now also passes any custom headers available to
`utils::available.packages()` during bootstrap. (#1942)

Expand Down
18 changes: 13 additions & 5 deletions R/bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -387,23 +387,31 @@ renv_bootstrap_download_tarball <- function(version) {

}

renv_bootstrap_github_token <- function() {
for (envvar in c("GITHUB_TOKEN", "GITHUB_PAT", "GH_TOKEN")) {
envval <- Sys.getenv(envvar, unset = NA)
if (!is.na(envval))
return(envval)
}
}

renv_bootstrap_download_github <- function(version) {

enabled <- Sys.getenv("RENV_BOOTSTRAP_FROM_GITHUB", unset = "TRUE")
if (!identical(enabled, "TRUE"))
return(FALSE)

# prepare download options
pat <- Sys.getenv("GITHUB_PAT")
if (nzchar(Sys.which("curl")) && nzchar(pat)) {
token <- renv_bootstrap_github_token()
if (nzchar(Sys.which("curl")) && nzchar(token)) {
fmt <- "--location --fail --header \"Authorization: token %s\""
extra <- sprintf(fmt, pat)
extra <- sprintf(fmt, token)
saved <- options("download.file.method", "download.file.extra")
options(download.file.method = "curl", download.file.extra = extra)
on.exit(do.call(base::options, saved), add = TRUE)
} else if (nzchar(Sys.which("wget")) && nzchar(pat)) {
} else if (nzchar(Sys.which("wget")) && nzchar(token)) {
fmt <- "--header=\"Authorization: token %s\""
extra <- sprintf(fmt, pat)
extra <- sprintf(fmt, token)
saved <- options("download.file.method", "download.file.extra")
options(download.file.method = "wget", download.file.extra = extra)
on.exit(do.call(base::options, saved), add = TRUE)
Expand Down
16 changes: 8 additions & 8 deletions R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -529,20 +529,20 @@ renv_download_auth_bitbucket <- function() {

renv_download_auth_github <- function(url) {

pat <- renv_download_auth_github_pat(url)
if (is.null(pat))
token <- renv_download_auth_github_token(url)
if (is.null(token))
return(character())

c("Authorization" = paste("token", pat))
c("Authorization" = paste("token", token))

}

renv_download_auth_github_pat <- function(url) {
renv_download_auth_github_token <- function(url) {

# check for an existing PAT
pat <- Sys.getenv("GITHUB_PAT", unset = NA)
if (!is.na(pat))
return(pat)
# check for an existing token from environment variable
token <- renv_bootstrap_github_token()
if (length(token))
return(token)

# if gitcreds is available, try to use it
gitcreds <-
Expand Down
1 change: 1 addition & 0 deletions R/github.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

16 changes: 0 additions & 16 deletions R/update.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,6 @@ renv_update_find_git_impl <- function(record) {

renv_update_find_github <- function(records) {

# check for GITHUB_PAT
if (!renv_envvar_exists("GITHUB_PAT")) {

msg <- paste(
"GITHUB_PAT is unset. Updates may fail due to GitHub's API rate limit.",
"",
"To increase your GitHub API rate limit:",
"- Use `usethis::create_github_token()` to create a Personal Access Token (PAT).",
"- Use `usethis::edit_r_environ()` and add the token as `GITHUB_PAT`.",
sep = "\n"
)

warning(msg, call. = FALSE)

}

names(records) <- map_chr(records, `[[`, "Package")
results <- renv_parallel_exec(records, function(record) {
catch(renv_update_find_github_impl(record))
Expand Down
18 changes: 13 additions & 5 deletions inst/resources/activate.R
Original file line number Diff line number Diff line change
Expand Up @@ -486,23 +486,31 @@ local({

}

renv_bootstrap_github_token <- function() {
for (envvar in c("GITHUB_TOKEN", "GITHUB_PAT", "GH_TOKEN")) {
envval <- Sys.getenv(envvar, unset = NA)
if (!is.na(envval))
return(envval)
}
}

renv_bootstrap_download_github <- function(version) {

enabled <- Sys.getenv("RENV_BOOTSTRAP_FROM_GITHUB", unset = "TRUE")
if (!identical(enabled, "TRUE"))
return(FALSE)

# prepare download options
pat <- Sys.getenv("GITHUB_PAT")
if (nzchar(Sys.which("curl")) && nzchar(pat)) {
token <- renv_bootstrap_github_token()
if (nzchar(Sys.which("curl")) && nzchar(token)) {
fmt <- "--location --fail --header \"Authorization: token %s\""
extra <- sprintf(fmt, pat)
extra <- sprintf(fmt, token)
saved <- options("download.file.method", "download.file.extra")
options(download.file.method = "curl", download.file.extra = extra)
on.exit(do.call(base::options, saved), add = TRUE)
} else if (nzchar(Sys.which("wget")) && nzchar(pat)) {
} else if (nzchar(Sys.which("wget")) && nzchar(token)) {
fmt <- "--header=\"Authorization: token %s\""
extra <- sprintf(fmt, pat)
extra <- sprintf(fmt, token)
saved <- options("download.file.method", "download.file.extra")
options(download.file.method = "wget", download.file.extra = extra)
on.exit(do.call(base::options, saved), add = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion man/paths.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/testthat/helper-skip.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

skip_if_no_github_auth <- function() {
skip_if_not(renv_envvar_exists("GITHUB_PAT"), "GITHUB_PAT is not set")
token <- renv_bootstrap_github_token()
skip_if(empty(token), "GITHUB_PAT is not set")
}

skip_if_no_python <- function() {
Expand Down

0 comments on commit 37f5b31

Please sign in to comment.