Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/github.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion R/install-remote.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-github.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
47 changes: 47 additions & 0 deletions tests/testthat/test-install-remote.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Loading