Skip to content

Commit 6688205

Browse files
committed
pass host to github_pat
1 parent 6c988cc commit 6688205

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

R/github.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ github_commit <- function(username, repo, ref = "HEAD",
6868
#'
6969
#' @keywords internal
7070
#' @noRd
71-
github_pat <- function(quiet = TRUE) {
71+
github_pat <- function(quiet = TRUE, host = "api.github.com") {
7272

7373
env_var_aliases <- c(
7474
"GITHUB_PAT",
@@ -89,7 +89,7 @@ github_pat <- function(quiet = TRUE) {
8989
}
9090

9191
pat <- tryCatch(
92-
gitcreds_get()$password,
92+
gitcreds_get(url = download_url(host))$password,
9393
error = function(e) ""
9494
)
9595
if (nzchar(pat)) {

R/install-remote.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ package2remote <- function(name, lib = .libPaths(), repos = getOption("repos"),
241241
username = x$RemoteUsername,
242242
ref = x$RemoteRef,
243243
sha = x$RemoteSha,
244-
auth_token = github_pat()),
244+
auth_token = github_pat(host = x$RemoteHost)),
245245
gitlab = remote("gitlab",
246246
host = x$RemoteHost,
247247
repo = x$RemoteRepo,

tests/testthat/test-github.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ test_that("github_pat", {
3030
expect_true(nzchar(github_pat()))
3131
})
3232

33+
test_that("github_pat uses host-specific PAT env var", {
34+
withr::local_envvar(c(
35+
GITHUB_PAT = NA,
36+
GITHUB_TOKEN = NA,
37+
CI = NA,
38+
GITHUB_PAT_GITHUB_EXAMPLE_COM = "host-pat"
39+
))
40+
41+
expect_equal(github_pat(host = "github.example.com/api/v3"), "host-pat")
42+
})
43+
3344
test_that("github_commit", {
3445
skip_on_cran()
3546
skip_if_offline()

tests/testthat/test-install-remote.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,50 @@ test_that("package2remotes looks for the DESCRIPTION in .libPaths", {
4545

4646
expect_equal(package2remote("noremotes")$sha, NA_character_)
4747
})
48+
49+
test_that("package2remote() resolves host-specific github PATs", {
50+
withr::local_envvar(c(
51+
GITHUB_PAT = NA,
52+
GITHUB_TOKEN = NA,
53+
GITHUB_PAT_GITHUB_COM = "pat-github-com",
54+
GITHUB_PAT_GITHUB_EXAMPLE_COM = "pat-github-example"
55+
))
56+
57+
lib <- tempfile()
58+
dir.create(lib)
59+
on.exit(unlink(lib, recursive = TRUE), add = TRUE)
60+
61+
dir.create(file.path(lib, "pkg1"))
62+
writeLines(c(
63+
"Package: pkg1",
64+
"Version: 1.0.0",
65+
"RemoteType: github",
66+
"RemoteHost: api.github.com",
67+
"RemotePackage: pkg1",
68+
"RemoteRepo: pkg1",
69+
"RemoteUsername: repo",
70+
"RemoteRef: HEAD",
71+
"RemoteSha: abc123"
72+
), file.path(lib, "pkg1", "DESCRIPTION"))
73+
74+
dir.create(file.path(lib, "pkg2"))
75+
writeLines(c(
76+
"Package: pkg2",
77+
"Version: 1.0.0",
78+
"RemoteType: github",
79+
"RemoteHost: github.example.com/api/v3",
80+
"RemotePackage: pkg2",
81+
"RemoteRepo: pkg2",
82+
"RemoteUsername: repo",
83+
"RemoteRef: HEAD",
84+
"RemoteSha: def456"
85+
), file.path(lib, "pkg2", "DESCRIPTION"))
86+
87+
remote1 <- package2remote("pkg1", lib = lib)
88+
remote2 <- package2remote("pkg2", lib = lib)
89+
90+
expect_equal(remote1$host, "api.github.com")
91+
expect_equal(remote2$host, "github.example.com/api/v3")
92+
expect_equal(remote1$auth_token, "pat-github-com")
93+
expect_equal(remote2$auth_token, "pat-github-example")
94+
})

0 commit comments

Comments
 (0)