Skip to content

Commit

Permalink
rename & export .GETWauthThenNonAuth, .getGitCredsToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliot McIntire committed Nov 5, 2024
1 parent 14cfaf2 commit 54d192b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 22 additions & 9 deletions R/Require-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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? "
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 54d192b

Please sign in to comment.