diff --git a/R/github.R b/R/github.R index 294b917e..65e099f3 100644 --- a/R/github.R +++ b/R/github.R @@ -68,7 +68,7 @@ github_commit <- function(username, repo, ref = "HEAD", #' #' @keywords internal #' @noRd -github_pat <- function(quiet = TRUE) { +github_pat <- function(quiet = TRUE, host = "api.github.com") { env_var_aliases <- c( "GITHUB_PAT", @@ -89,7 +89,7 @@ github_pat <- function(quiet = TRUE) { } pat <- tryCatch( - gitcreds_get()$password, + gitcreds_get(url = download_url(host))$password, error = function(e) "" ) if (nzchar(pat)) { diff --git a/R/install-remote.R b/R/install-remote.R index 792a7e1d..b57059cf 100644 --- a/R/install-remote.R +++ b/R/install-remote.R @@ -241,7 +241,7 @@ package2remote <- function(name, lib = .libPaths(), repos = getOption("repos"), username = x$RemoteUsername, ref = x$RemoteRef, sha = x$RemoteSha, - auth_token = github_pat()), + auth_token = github_pat(host = x$RemoteHost)), gitlab = remote("gitlab", host = x$RemoteHost, repo = x$RemoteRepo, diff --git a/tests/testthat/test-github.R b/tests/testthat/test-github.R index b0c916af..95fbf380 100644 --- a/tests/testthat/test-github.R +++ b/tests/testthat/test-github.R @@ -30,6 +30,17 @@ test_that("github_pat", { expect_true(nzchar(github_pat())) }) +test_that("github_pat uses host-specific PAT env var", { + withr::local_envvar(c( + GITHUB_PAT = NA, + GITHUB_TOKEN = NA, + CI = NA, + GITHUB_PAT_GITHUB_EXAMPLE_COM = "host-pat" + )) + + expect_equal(github_pat(host = "github.example.com/api/v3"), "host-pat") +}) + test_that("github_commit", { skip_on_cran() skip_if_offline() diff --git a/tests/testthat/test-install-remote.R b/tests/testthat/test-install-remote.R index 323f8fa6..3dff4e6a 100644 --- a/tests/testthat/test-install-remote.R +++ b/tests/testthat/test-install-remote.R @@ -45,3 +45,50 @@ test_that("package2remotes looks for the DESCRIPTION in .libPaths", { expect_equal(package2remote("noremotes")$sha, NA_character_) }) + +test_that("package2remote() resolves host-specific github PATs", { + withr::local_envvar(c( + GITHUB_PAT = NA, + GITHUB_TOKEN = NA, + GITHUB_PAT_GITHUB_COM = "pat-github-com", + GITHUB_PAT_GITHUB_EXAMPLE_COM = "pat-github-example" + )) + + lib <- tempfile() + dir.create(lib) + on.exit(unlink(lib, recursive = TRUE), add = TRUE) + + dir.create(file.path(lib, "pkg1")) + writeLines(c( + "Package: pkg1", + "Version: 1.0.0", + "RemoteType: github", + "RemoteHost: api.github.com", + "RemotePackage: pkg1", + "RemoteRepo: pkg1", + "RemoteUsername: repo", + "RemoteRef: HEAD", + "RemoteSha: abc123" + ), file.path(lib, "pkg1", "DESCRIPTION")) + + dir.create(file.path(lib, "pkg2")) + writeLines(c( + "Package: pkg2", + "Version: 1.0.0", + "RemoteType: github", + "RemoteHost: github.example.com/api/v3", + "RemotePackage: pkg2", + "RemoteRepo: pkg2", + "RemoteUsername: repo", + "RemoteRef: HEAD", + "RemoteSha: def456" + ), file.path(lib, "pkg2", "DESCRIPTION")) + + remote1 <- package2remote("pkg1", lib = lib) + remote2 <- package2remote("pkg2", lib = lib) + + expect_equal(remote1$host, "api.github.com") + expect_equal(remote2$host, "github.example.com/api/v3") + expect_equal(remote1$auth_token, "pat-github-com") + expect_equal(remote2$auth_token, "pat-github-example") +})