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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(check_pkgs_availability)
export(install_missing_pkgs)
export(list_files_github_gists)
export(list_files_github_repo)
export(list_files_wd)
export(network_plot)
Expand Down
68 changes: 16 additions & 52 deletions R/list-files-gh-gists.R
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
get_gist_pages <- function(user) {

req_user <- httr::GET(glue::glue("https://api.github.com/users/{user}"))

# Check HTTP response first
if (httr::status_code(req_user) != 200) {
warning("Failed to fetch GitHub user info for '\", user,\"' (status ", httr::status_code(req_user), "). Returning 0 pages.")
return(0L)
}

body <- httr::content(req_user)
num_gists <- body$public_gists

# Defensive handling: ensure we have a single non-negative integer
if (is.null(num_gists) || length(num_gists) == 0) return(0L)
num_gists <- as.integer(num_gists)
if (is.na(num_gists) || num_gists <= 0L) return(0L)

# Compute pages using ceiling to avoid remainder logic problems
num_pages <- ceiling(num_gists / 100)
num_pages
}

get_gist_content <- function(user) {

num_pages <- get_gist_pages(user)

# If there are no pages, return an empty list
if (num_pages == 0L) return(list())

# gists themselves
req_content <- vector("list", num_pages)

for (i in seq_len(num_pages)) {

req <- httr::GET(
glue::glue("https://api.github.com/users/{user}/gists"),
query = list(per_page = 100, page = i)
)

if (httr::status_code(req) != 200) {
warning("Failed to fetch gists for page ", i, " (status ", httr::status_code(req), "). Skipping page.")
req_content[[i]] <- list()
next
}

req_content[[i]] <- httr::content(req)
}

purrr::flatten(req_content)
}

#' List files from a user's GitHub gists
#'
#' Return a tibble of files (relative and raw URLs) from a user's public GitHub gists.
#'
#' @param user Character. GitHub username whose gists will be inspected.
#' @param pattern A regex (or object accepted by `stringr::regex()`) used to
#' filter file names. By default only R, R Markdown and Quarto files are kept.
#' @return A tibble with columns `relative_paths` (file names) and
#' `absolute_paths` (raw file URLs). If the user has no gists an empty tibble
#' with those columns is returned.
#' @export
#' @examples
#' \dontrun{
#' files <- list_files_github_gists('hadley')
#' head(files)
#' }
list_files_github_gists <- function(user,
pattern = stringr::regex("(r|rmd|rmarkdown|qmd)$", ignore_case = TRUE)) {

Expand Down
44 changes: 44 additions & 0 deletions man/list_files_github_gists.Rd

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

Loading