Skip to content
Closed
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
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
* several minor
* better fails when status is 403 for package dependency checking
Expand Down
39 changes: 26 additions & 13 deletions R/Require-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -995,21 +995,21 @@ getSHAfromGitHub <- function(acct, repo, br, verbose = getOption("Require.verbos
}
fetf <- file.exists(tf)
gitRefs <- if (fetf) try(suppressWarnings(readLines(tf)), silent = TRUE) else ""
error403 <- any(grepl("status.+403", gitRefs))
isNotFound <- ((NROW(gitRefs) <= 5) && any(grepl("Not Found", gitRefs) ) ||
(any(grepl("cannot open URL", gitRefs))) || identical(gitRefs, "") ||
any(grepl("status.+403", gitRefs)))
(any(grepl("cannot open URL", gitRefs))) || identical(gitRefs, ""))

if (any(grepl("Bad credentials", gitRefs)) || isNotFound) {
if (any(grepl("Bad credentials", gitRefs)) || isNotFound || error403) {
if (fetf) {
unlink(tf)
}
if (isNotFound) {
token <- getGitCredsToken()
token <- .getGitCredsToken()
mess <- character()
if (is.null(token)) {
mess <- "GitHub repository not accessible does it need authentication? "
stop(paste0(mess, .txtDidYouSpell))
}
stop(paste0(mess, .txtDidYouSpell))
}
stop(gitRefs)
}
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
Loading