From 54d192bda4ee600355b77d86e6e51d03c742a946 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Mon, 4 Nov 2024 17:30:25 -0800 Subject: [PATCH] rename & export .GETWauthThenNonAuth, .getGitCredsToken --- DESCRIPTION | 4 ++-- NEWS.md | 3 +++ R/Require-helpers.R | 31 ++++++++++++++++++++++--------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a1d8a948..c23fd97b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,8 +15,8 @@ Description: A single key function, 'Require' that makes rerun-tolerant URL: https://Require.predictiveecology.org, https://github.com/PredictiveEcology/Require -Date: 2024-10-23 -Version: 1.0.1.9002 +Date: 2024-11-04 +Version: 1.0.1.9003 Authors@R: c( person(given = "Eliot J B", family = "McIntire", diff --git a/NEWS.md b/NEWS.md index 8e15158b..e5ec67fb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ version 1.0.2 ============= +## Function exporting +* `.GETWauthThenNonAuth`, `.getGitCredsToken` are now exported + ## Bugfixes * minor * better fails when status is 403 for package dependency checking diff --git a/R/Require-helpers.R b/R/Require-helpers.R index caf06442..78eb9a54 100644 --- a/R/Require-helpers.R +++ b/R/Require-helpers.R @@ -1004,7 +1004,7 @@ getSHAfromGitHub <- function(acct, repo, br, verbose = getOption("Require.verbos unlink(tf) } if (isNotFound) { - token <- getGitCredsToken() + token <- .getGitCredsToken() mess <- character() if (is.null(token)) { mess <- "GitHub repository not accessible does it need authentication? " @@ -1386,7 +1386,7 @@ masterMainHEAD <- function(url, need) { usesGitCreds <- requireNamespace("gitcreds", quietly = TRUE) && requireNamespace("httr", quietly = TRUE) if (usesGitCreds) { - token <- getGitCredsToken() + token <- .getGitCredsToken() } if (is.null(token)) { ghp <- Sys.getenv("GITHUB_PAT") @@ -1423,7 +1423,7 @@ masterMainHEAD <- function(url, need) { messageVerbose(e$message, verbose = verbose) }) } else { - a <- try(GETWauthThenNonAuth(url, token, verbose = verbose)) + a <- try(.GETWauthThenNonAuth(url, token, verbose = verbose)) if (is(a, "try-error")) { if (any(grepl("Could not resolve host", a))) { warning(a) @@ -1452,7 +1452,7 @@ masterMainHEAD <- function(url, need) { outMasterMain <- try(download.file(urls[["TRUE"]][wh], destfile = destfile, quiet = TRUE), silent = TRUE) } else { outMasterMain <- try(silent = TRUE, { - a <- GETWauthThenNonAuth(urls[["TRUE"]][wh], token, verbose = verbose) + a <- .GETWauthThenNonAuth(urls[["TRUE"]][wh], token, verbose = verbose) # a <- httr::GET(urls[["TRUE"]][wh], httr::add_headers(Authorization = token)) if (grepl("404", httr::http_status(a)$message)) stop() @@ -1725,16 +1725,26 @@ rmEmptyFiles <- function(files, minSize = 100) { } -GETWauthThenNonAuth <- function(url, token, verbose = getOption("Require.verbose")) { +#' Use gitcreds to download a file from GitHub.com +#' +#' A wrapper around `httr::GET` that uses a GitHub token. +#' +#' @param url The url to check +#' @param token A GitHub token retrieved by e.g., gitcreds::gitcreds_get(use_cache = FALSE) +#' @inheritParams Require +#' @export +#' @return Nothing. Used for its side effects, a downloaded GitHub file or repository. +#' +.GETWauthThenNonAuth <- function(url, token, verbose = getOption("Require.verbose"), ...) { if (is.null(token)) { - a <- httr::GET(url) + a <- httr::GET(url, ...) } else { - a <- httr::GET(url, httr::add_headers(Authorization = token)) + a <- httr::GET(url, httr::add_headers(Authorization = token), ...) } if (grepl("Bad credentials", a) || grepl("404", httr::http_status(a)$message)) { if (grepl("Bad credentials", a)) messageVerbose(red("Git credentials do not work for this url: ", url, "\nAre they expired?"), verbose = verbose) - a <- httr::GET(url, httr::add_headers()) + a <- httr::GET(url, httr::add_headers(), ...) } a } @@ -1791,7 +1801,10 @@ masterOrMainFromGitRefs <- function(gitRefsSplit2) { br } -getGitCredsToken <- function() { +#' Gets the gitcreds token from the gitcreds local storage +#' @export +#' @return A github token. +.getGitCredsToken <- function() { token <- tryCatch( gitcreds::gitcreds_get(use_cache = FALSE), error = function(e) NULL