Skip to content

Commit

Permalink
Making progress on listing files in a GitHub repo
Browse files Browse the repository at this point in the history
  • Loading branch information
njlyon0 committed Jul 6, 2023
1 parent 34777a7 commit b357bf9
Showing 1 changed file with 111 additions and 7 deletions.
118 changes: 111 additions & 7 deletions dev/spell-exploration.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,127 @@ librarian::shelf(tidyverse, dndR)
## https://github.com/Traneptora/grimoire/tree/master

# Load needed library
library(httr)
library(gh); library(purrr)


library(gh)

grim_conts <- gh::gh("/repos/Traneptora/grimoire/contents")
str(grim_conts)
# Identify a repo URL
repo_url <- "https://github.com/Traneptora/grimoire"
folder <- "_includes"

# Strip out owner and repo name
repo_bits <- stringr::str_split_1(string = gsub(x = repo_url,
pattern = "https\\:\\/\\/github.com\\/",
replacement = ""),
pattern = "/")

for(i in 1:length(grim_conts)){
# Build first query
repo_query_raw <- paste("/repos", repo_bits[1], repo_bits[2], "contents", sep = "/")

message("Element ", i, " is ", grim_conts[[i]]$name)
# Assemble API query
repo_query <- ifelse(nchar(folder) > 0,
yes = paste0(repo_query_raw, "/", folder),
no = repo_query_raw)

# Process GitHub URL into GitHub API query format
repo_conts <- gh::gh(endpoint = repo_query)

# Make empty vectors for later use
repo_values <- NULL
repo_types <- NULL

# Identify files in that folder
for(k in 1:length(repo_conts)){

# Strip out vector of file/directory names and types
repo_values <- c(repo_values, repo_conts[[k]]$name)
repo_types <- c(repo_types, repo_conts[[k]]$type)

}

# Create a dataframe from this
repo_df <- data.frame("name" = repo_values,
"type" = repo_types)




# Identify top level contents
top <- gh::gh("/repos/Traneptora/grimoire/contents")

# Make two empty vectors
top_names <- NULL
top_types <- NULL

# Loop across resulting list
for(k in 1:length(top)){

# Strip out vector of file/directory names and types
top_names <- c(top_names, top[[k]]$name)
top_types <- c(top_types, top[[k]]$type)

}

# Create a dataframe from this
top_df <- data.frame("name" = top_names,
"type" = top_types) %>%
# Add some housekeeping columns we need later
dplyr::mutate(listed = ifelse(type == "dir",
yes = FALSE, no = NA),
parent = ".")

# Check out that product
top_df

# Duplicate this object
contents <- top_df

# While any folders are not identified
while(FALSE %in% contents$listed){

# Loop across contents
for(w in 1:nrow(contents)){

# Only operate on unlisted directories (otherwise skip)
if(contents[w, ]$type == "dir" & contents[w, ]$listed == FALSE){

# Message start of processing
message("Listing contents of directory '", contents[w, ]$name, "'")

# Identify contents of that folder
sub_list <- gh::gh(paste0("/repos/Traneptora/grimoire/contents/", contents[w, ]$name))



} # Close conditional



# # Skip non-directories
# if(contents[w, ]$type != "dir"){
# message("Skipping non-directory '", contents[w, ]$name, "'")
# }
#
# # Skip previously listed directories
# if(contents[w, ]$type == "dir" & contents[w, ]$listed == TRUE){
# message("Skipping previously listed directory '", contents[w, ]$name)
# }

# Otherwise...



} # Close `for` loop



} # Close `while` loop






grim_conts[[11]]$type



Expand Down

0 comments on commit b357bf9

Please sign in to comment.