Skip to content
Merged
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
8 changes: 2 additions & 6 deletions R/borrow_chapter.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#' the child document upon bringing it into the render.
#' @param branch Default is to pull from main branch, but need to declare if
#' other branch is needed.
#' @param token A personal access token from GitHub. Only necessary if the
#' repository being checked is a private repository.
#' @param base_url it's assumed this is coming from github so it is by default
#' 'https://raw.githubusercontent.com/'
#' @param dest_dir A file path where the file should be stored upon arrival to
Expand Down Expand Up @@ -77,7 +75,6 @@ borrow_chapter <- function(doc_path,
remove_h1 = FALSE,
tag_replacement = NULL,
branch = "main",
token = NULL,
base_url = "https://raw.githubusercontent.com",
dest_dir = file.path("resources", "other_chapters")) {
# Declare file names
Expand All @@ -104,12 +101,11 @@ borrow_chapter <- function(doc_path,
if (!is_wiki) {
exists <- check_git_repo(
repo_name = repo_name,
token = token,
verbose = FALSE,
silent = TRUE
)
if (!exists) {
warning(paste(repo_name, "was not found in GitHub. If it is a private repository, make sure your credentials have been provided"))
warning(paste(repo_name, "was not found in GitHub. Borrow chapter does not work with private repositories. Please check that the repository exists and is public."))
}
}

Expand All @@ -131,7 +127,7 @@ borrow_chapter <- function(doc_path,
}

# Remove leanbuild::set_knitr_image_path() from downloaded file
file_contents <- readLines(doc_path)
file_contents <- readLines(dest_file)
file_contents <- gsub("leanbuild::set_knitr_image_path\\(\\)", "", file_contents)

# If remove_header = TRUE
Expand Down
51 changes: 19 additions & 32 deletions R/github_handling.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ get_pages_url <- function(repo_name,
# Github api get
response <- httr::GET(
url,
httr::add_headers(Authorization = paste0("token ", token)),
httr::add_headers(Authorization = paste0("Bearer ", token)),
httr::accept_json()
)

Expand Down Expand Up @@ -165,7 +165,7 @@ get_repo_info <- function(repo_name,
# Github api get
response <- httr::GET(
url,
httr::add_headers(Authorization = paste0("token ", token)),
httr::add_headers(Authorization = paste0("Bearer ", token)),
httr::accept_json()
)
}
Expand All @@ -192,8 +192,6 @@ get_repo_info <- function(repo_name,
#' @param silent TRUE/FALSE of whether the warning from the git ls-remote
#' command should be echoed back if it does fail.
#' @param verbose TRUE/FALSE do you want more progress messages?
#' @param return_repo TRUE/FALSE of whether or not the output from git ls-remote
#' should be saved to a file (if the repo exists)
#'
#' @return A TRUE/FALSE whether or not the repository exists. Optionally the
#' output from git ls-remote if return_repo = TRUE.
Expand All @@ -208,7 +206,6 @@ get_repo_info <- function(repo_name,
check_git_repo <- function(repo_name,
token = NULL,
silent = TRUE,
return_repo = FALSE,
verbose = TRUE) {
if (verbose) {
message(paste("Checking for remote git repository:", repo_name))
Expand All @@ -219,39 +216,29 @@ check_git_repo <- function(repo_name,
# Try to get credentials other way
if (is.null(token)) {
# Get auth token
token <- get_token(app_name = "github")
token <- tryCatch(
suppressWarnings(get_token(app_name = "github")),
error = function(msg) NULL
)
}

# Run git ls-remote
if (!grepl("Error", token[1])) {
# If token is supplied, use it
test_repo <- report(
try(system(paste0("git ls-remote https://", token, "@github.com/", repo_name),
intern = TRUE, ignore.stderr = TRUE
))
)
url <- paste0("https://api.github.com/repos/", repo_name)

# Github api HEAD
result <- if(is.null(token)) {
httr::HEAD(url)
} else {
# Try to git ls-remote the repo_name given
test_repo <- report
try(system(paste0("git ls-remote https://github.com/", repo_name),
intern = TRUE, ignore.stderr = TRUE
))
httr::HEAD(
url,
httr::add_headers(Authorization = paste0("Bearer ", token)),
)
}
# If 128 is returned as a status attribute it means it failed
exists <- ifelse(is.null(attr(test_repo, "status")), TRUE, FALSE)

if (return_repo && exists) {
# Make file name
output_file <- paste0("git_ls_remote_", gsub("/", "_", repo_name))

# Tell the user the file was saved
message(paste("Saving output from git ls-remote to file:", output_file))

# Write to file
writeLines(exists, file.path(output_file))
if (httr::status_code(result) != 200) {
return(FALSE)
} else {
return(TRUE)
}

return(exists)
}


Expand Down
6 changes: 1 addition & 5 deletions man/borrow_chapter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions man/check_git_repo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading