Skip to content

Commit 3582529

Browse files
committed
add gh_wrapper to fallback for 401 calls
This allows users with bad credentials (they expired or were misconfigured in the first place) to install pandoc using the limited API without credentials. This will fix cderv#30
1 parent ee86163 commit 3582529

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# pandoc (development version)
22

3+
- pandoc installation will no longer immediately fail for users with bad
4+
GitHub credentials
5+
36
# pandoc 0.2.0
47

58
- input and output path containing short path version using `~` now works (thanks, @olivroy, #31)

R/pandoc-install.R

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ pandoc_install_nightly <- function(n_last = 1L) {
149149
version <- "nightly"
150150
install_dir <- pandoc_home(version)
151151
rlang::inform(c(i = "Retrieving last available nightly informations..."))
152-
runs <- gh::gh("/repos/jgm/pandoc/actions/workflows/nightly.yml/runs",
152+
runs <- gh_wrapper("/repos/jgm/pandoc/actions/workflows/nightly.yml/runs",
153153
.limit = max(15L, n_last + 5)
154154
)
155155
runs <- keep(runs$workflow_runs, ~ .x$conclusion == "success")
156156
ind <- min(length(runs), n_last)
157157
artifacts_url <- runs[[ind]][["artifacts_url"]]
158158
head_sha <- runs[[ind]][["head_sha"]]
159-
artifacts <- gh::gh(artifacts_url)
159+
artifacts <- gh_wrapper(artifacts_url)
160160
artifact_url <- keep(artifacts$artifacts, ~ .x$name == bundle_name)[[1]][["archive_download_url"]]
161161
if (fs::dir_exists(install_dir)) {
162162
current_version <- pandoc_nightly_version()
@@ -171,7 +171,7 @@ pandoc_install_nightly <- function(n_last = 1L) {
171171
bundle_name <- fs::path_ext_set(head_sha, "zip")
172172
rlang::inform(c(i = "Installing last available nightly..."))
173173
tmp_file <- with_download_cache("nightly", bundle_name, {
174-
gh::gh(artifact_url, .destfile = bundle_name)
174+
gh_wrapper(artifact_url, .destfile = bundle_name)
175175
})
176176

177177
utils::unzip(tmp_file, exdir = install_dir, junkpaths = TRUE)
@@ -293,7 +293,7 @@ pandoc_releases <- function() {
293293
fetch_gh_releases <- function(limit = Inf) {
294294
gh_required()
295295
rlang::inform(c(i = "Fetching Pandoc releases info from github..."))
296-
gh::gh(
296+
gh_wrapper(
297297
"GET /repos/:owner/:repo/releases",
298298
owner = "jgm",
299299
repo = "pandoc",
@@ -337,6 +337,23 @@ pandoc_os <- function() {
337337
)
338338
}
339339

340+
# fallback for bad token errors. See
341+
# https://github.com/cderv/pandoc/issues/30#issuecomment-1691712036
342+
# for details
343+
gh_wrapper <- function(...) {
344+
tryCatch(gh::gh(...),
345+
http_error_401 = function(e) {
346+
rlang::inform(c(
347+
"!" = "Bad GitHub credentials found",
348+
i = "Attempting to install without credentials..."))
349+
# if we get a 401 error, the user has a bad token and we should try with
350+
# an empty token
351+
e$call$.token <- ""
352+
eval(e$call)
353+
}
354+
)
355+
}
356+
340357
# nocov start
341358
gh_required <- function() {
342359
rlang::check_installed(

0 commit comments

Comments
 (0)