diff --git a/R/borrow_chapter.R b/R/borrow_chapter.R index ada523e..28138a9 100644 --- a/R/borrow_chapter.R +++ b/R/borrow_chapter.R @@ -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 @@ -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 @@ -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.")) } } @@ -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 diff --git a/R/github_handling.R b/R/github_handling.R index c6a6810..8dd8204 100644 --- a/R/github_handling.R +++ b/R/github_handling.R @@ -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() ) @@ -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() ) } @@ -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. @@ -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)) @@ -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) } diff --git a/man/borrow_chapter.Rd b/man/borrow_chapter.Rd index cae81ca..818415c 100644 --- a/man/borrow_chapter.Rd +++ b/man/borrow_chapter.Rd @@ -10,7 +10,6 @@ borrow_chapter( remove_h1 = FALSE, tag_replacement = NULL, branch = "main", - token = NULL, base_url = "https://raw.githubusercontent.com", dest_dir = file.path("resources", "other_chapters") ) @@ -34,9 +33,6 @@ the child document upon bringing it into the render.} \item{branch}{Default is to pull from main branch, but need to declare if other branch is needed.} -\item{token}{A personal access token from GitHub. Only necessary if the -repository being checked is a private repository.} - \item{base_url}{it's assumed this is coming from github so it is by default 'https://raw.githubusercontent.com/'} @@ -65,7 +61,7 @@ Rmd from another repository knitted into the Rmd. # For a file in another repository: # ```{r, echo=FALSE, results='asis'} borrow_chapter( - doc_path = "docs/02-chapter_of_course.md", + doc_path = "02-chapter_of_course.Rmd", repo_name = "ottrproject/OTTR_Template" ) # ``` diff --git a/man/check_git_repo.Rd b/man/check_git_repo.Rd index fe48570..34a076f 100644 --- a/man/check_git_repo.Rd +++ b/man/check_git_repo.Rd @@ -4,13 +4,7 @@ \alias{check_git_repo} \title{Check if a repository exists on GitHub} \usage{ -check_git_repo( - repo_name, - token = NULL, - silent = TRUE, - return_repo = FALSE, - verbose = TRUE -) +check_git_repo(repo_name, token = NULL, silent = TRUE, verbose = TRUE) } \arguments{ \item{repo_name}{the name of the repository, e.g. ottrproject/OTTR_Template} @@ -21,9 +15,6 @@ repository being checked is a private repository.} \item{silent}{TRUE/FALSE of whether the warning from the git ls-remote command should be echoed back if it does fail.} -\item{return_repo}{TRUE/FALSE of whether or not the output from git ls-remote -should be saved to a file (if the repo exists)} - \item{verbose}{TRUE/FALSE do you want more progress messages?} } \value{