From 8df1becb5aa12756715a85f74857c37fa45fd2a3 Mon Sep 17 00:00:00 2001 From: cansavvy Date: Thu, 29 Feb 2024 11:12:51 -0500 Subject: [PATCH] Delete scripts and have them downloaded from ottr-reports --- .github/workflows/check-url.yml | 3 +- .github/workflows/render-all.yml | 3 +- .github/workflows/transfer-rendered-files.yml | 7 +- scripts/find_issue.R | 55 ----------- scripts/git_repo_check.R | 97 ------------------- scripts/make_screenshots.R | 74 -------------- scripts/ottr-fy.R | 67 ------------- 7 files changed, 8 insertions(+), 298 deletions(-) delete mode 100644 scripts/find_issue.R delete mode 100644 scripts/git_repo_check.R delete mode 100644 scripts/make_screenshots.R delete mode 100644 scripts/ottr-fy.R diff --git a/.github/workflows/check-url.yml b/.github/workflows/check-url.yml index 493ea2be..9ee7fe9a 100644 --- a/.github/workflows/check-url.yml +++ b/.github/workflows/check-url.yml @@ -86,7 +86,8 @@ jobs: run: | echo "$GH_PAT" echo "$GITHUB_REPOSITORY" - issue_exists=$(Rscript --vanilla scripts/find_issue.R --repo $GITHUB_REPOSITORY --git_pat $GH_PAT) + curl -o find_issue.R https://raw.githubusercontent.com/jhudsl/ottr-reports/main/scripts/find-issue.R + issue_exists=$(Rscript --vanilla find_issue.R --repo $GITHUB_REPOSITORY --git_pat $GH_PAT) echo URL issue exists: $issue_exists echo "issue_existence=$issue_exists" >> $GITHUB_OUTPUT diff --git a/.github/workflows/render-all.yml b/.github/workflows/render-all.yml index 4c1058c3..052d7f11 100644 --- a/.github/workflows/render-all.yml +++ b/.github/workflows/render-all.yml @@ -153,7 +153,8 @@ jobs: rm -rf resources/chapt_screen_images # Make new screenshots - chapt_urls=$(Rscript --vanilla scripts/make_screenshots.R \ + curl -o make_screenshots.R https://raw.githubusercontent.com/jhudsl/ottr-reports/main/scripts/make_screenshots.R + chapt_urls=$(Rscript --vanilla make_screenshots.R \ --git_pat ${{ secrets.GH_PAT }} \ --repo $GITHUB_REPOSITORY \ --output_dir resources/chapt_screen_images) diff --git a/.github/workflows/transfer-rendered-files.yml b/.github/workflows/transfer-rendered-files.yml index 776c7791..dae41411 100644 --- a/.github/workflows/transfer-rendered-files.yml +++ b/.github/workflows/transfer-rendered-files.yml @@ -47,7 +47,7 @@ jobs: with: path: bookdown token: ${{ secrets.GH_PAT }} - + - name: Run git repo check id: git_repo_check env: @@ -58,7 +58,8 @@ jobs: echo $QUIZ_REPO # Run repo check script - results=$(Rscript --vanilla bookdown/scripts/git_repo_check.R --repo "$GITHUB_REPOSITORY" --git_pat "$GH_PAT") + curl -o git_repo_check.R https://raw.githubusercontent.com/jhudsl/ottr-reports/main/scripts/git_repo_check.R + results=$(Rscript --vanilla git_repo_check.R --repo "$GITHUB_REPOSITORY" --git_pat "$GH_PAT") echo $QUIZ_REPO exists: $results echo "git_results=$results" >> $GITHUB_OUTPUT @@ -81,7 +82,7 @@ jobs: # Copy over images folder (from bookdown to quizzes repo) mkdir -p quizzes/resources/chapt_screen_images cp bookdown/resources/chapt_screen_images/* quizzes/resources/chapt_screen_images - + # Copy over _bookdown.yml (from bookdown to quizzes repo) cp bookdown/_bookdown.yml quizzes/_bookdown.yml diff --git a/scripts/find_issue.R b/scripts/find_issue.R deleted file mode 100644 index 9661a569..00000000 --- a/scripts/find_issue.R +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env Rscript - -# Written by Candace Savonen Sept 2021 - -if (!("optparse" %in% installed.packages())){ - install.packages("optparse") -} - -library(optparse) - -option_list <- list( - optparse::make_option( - c("--repo"), - type = "character", - default = NULL, - help = "GitHub repository name, e.g. jhudsl/OTTR_Template", - ), - optparse::make_option( - c("--git_pat"), - type = "character", - default = NULL, - help = "GitHub personal access token", - ) -) - -# Read the arguments passed -opt_parser <- optparse::OptionParser(option_list = option_list) -opt <- optparse::parse_args(opt_parser) - -repo <- opt$repo -git_pat <- opt$git_pat - -if (!is.character(repo)) { - repo <- as.character(repo) -} - -# Github api get -result <- httr::GET( - paste0("https://api.github.com/repos/", repo, "/issues"), - httr::add_headers(Authorization = paste0("Bearer ", git_pat)), - httr::accept_json() -) - -if (httr::status_code(result) != 200) { - httr::stop_for_status(result) -} - -# Process and return results -result_content <- httr::content(result, "text") -result_list <- jsonlite::fromJSON(result_content) - -issue_exists <- length(grep('Broken URLs found in the course!', result_list$title)) - -# Print out the result -write(issue_exists, stdout()) diff --git a/scripts/git_repo_check.R b/scripts/git_repo_check.R deleted file mode 100644 index fd4b8f56..00000000 --- a/scripts/git_repo_check.R +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env Rscript - -# Written by Candace Savonen Sept 2021 - -if (!("optparse" %in% installed.packages())){ - install.packages("optparse") -} - -library(optparse) - -option_list <- list( - optparse::make_option( - c("--repo"), - type = "character", - default = NULL, - help = "GitHub repository name, e.g. jhudsl/OTTR_Template", - ), - optparse::make_option( - c("--git_pat"), - type = "character", - default = NULL, - help = "GitHub personal access token", - ) -) - -# Read the arguments passed -opt_parser <- optparse::OptionParser(option_list = option_list) -opt <- optparse::parse_args(opt_parser) - -repo <- opt$repo -git_pat <- opt$git_pat - -if (!is.character(repo)) { - repo <- as.character(repo) -} - -check_git_repo <- function(repo, git_pat = NULL, silent = TRUE, return_repo = FALSE) { - # Given a repository name, check with git ls-remote whether the repository - # exists and return a TRUE/FALSE - - # Inputs: - # repo: the name of the repository, e.g. jhudsl/OTTR_Template - # git_pat: A personal access token from GitHub. Only necessary if the repository being - # checked is a private repository. - # silent: TRUE/FALSE of whether the warning from the git ls-remote command - # should be echoed back if it does fail. - # return_repo: TRUE/FALSE of whether or not the output from git ls-remote - # should be saved to a file (if the repo exists) - - # Returns: - # A TRUE/FALSE whether or not the repository exists. - # Optionally the output from git ls-remote if return_repo = TRUE. - - message(paste("Checking for remote git repository:", repo)) - - # If silent = TRUE don't print out the warning message from the 'try' - report <- ifelse(silent, suppressWarnings, message) - - if (!is.null(git_pat)) { - # If git_pat is supplied, use it - test_repo <- report( - try(system(paste0("git ls-remote https://", git_pat, "@github.com/", repo), - intern = TRUE, ignore.stderr = TRUE - )) - ) - } else { - - # Try to git ls-remote the repo given - test_repo <- report( - try(system(paste0("git ls-remote https://github.com/", repo), - intern = TRUE, ignore.stderr = TRUE - )) - ) - } - # 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)) - - # 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)) - } - - return(exists) -} - -# Change repo name to its Leanpub equivalent: -repo <- gsub("_Template", "", repo) -repo <- paste0(repo, "_Quizzes") - -# Print out the result -write(check_git_repo(repo, git_pat = git_pat), stdout()) diff --git a/scripts/make_screenshots.R b/scripts/make_screenshots.R deleted file mode 100644 index c57a182e..00000000 --- a/scripts/make_screenshots.R +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env Rscript -# Written by Candace Savonen Jan 2022 - -if (!('devtools' %in% installed.packages())) { - # install.packages("remotes", repos = "http://cran.us.r-project.org") -} - -if (!('optparse' %in% installed.packages())) { - # install.packages("optparse", repos = "http://cran.us.r-project.org") -} - -webshot::install_phantomjs() - -library(optparse) -library(magrittr) - -option_list <- list( - optparse::make_option( - c("--repo"), - type = "character", - default = NULL, - help = "GitHub repository name, e.g. jhudsl/OTTR_Template", - ), - optparse::make_option( - c("--git_pat"), - type = "character", - default = NULL, - help = "GitHub personal access token", - ), - optparse::make_option( - c("--output_dir"), - type = "character", - default = "resources/chapt_screen_images", - help = "Output directory where the chapter's screen images should be stored", - ), - optparse::make_option( - c("--base_url"), - type = "character", - default = NULL, - help = "Output directory where the chapter's screen images should be stored", - ) -) - -# Read the arguments passed -opt_parser <- optparse::OptionParser(option_list = option_list) -opt <- optparse::parse_args(opt_parser) - -output_folder <- file.path(opt$output_dir) -if (!dir.exists(output_folder)) { - dir.create(output_folder, recursive = TRUE) -} - -if (is.null(opt$base_url)) { - base_url <- cow::get_pages_url(repo_name = opt$repo, git_pat = opt$git_pat) - base_url <- gsub("/$", "", base_url) -} - -chapt_df <- ottrpal::get_chapters(base_url = file.path(base_url, "no_toc/")) - -file_names <- lapply(chapt_df$url, function(url) { - file_name <- gsub(".html", ".png", file.path(output_folder, basename(url))) - # Get rid of special characters - webshot::webshot(url, file_name) - file_name <- gsub(":|?|!|\\'", "", file_name) - message(paste("Screenshot saved:", file_name)) - return(file_name) -}) - -# Save file of chapter urls and file_names -chapt_df %>% - dplyr::mutate(img_path = unlist(file_names)) %>% - readr::write_tsv(file.path(output_folder, "chapter_urls.tsv")) - -message(paste("Image Chapter key written to: ", file.path(output_folder, "chapter_urls.tsv"))) diff --git a/scripts/ottr-fy.R b/scripts/ottr-fy.R deleted file mode 100644 index aa079cee..00000000 --- a/scripts/ottr-fy.R +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env Rscript - -# This script downloads all the files and sets up the folders you need to -# OTTR-fy a repository that has markdown or R Markdown files - -system("git checkout -b 'robot/ottr-fy'") - -if (!('optparse' %in% installed.packages())) { - install.packages("optparse") -} -library(optparse) -library(magrittr) - -option_list <- list( - optparse::make_option( - c("--bookdown"), - action = "store_true", - help = "Is this a bookdown repository already? If used, means bookdown repo.", - ) -) - -# Read the arguments passed -opt_parser <- optparse::OptionParser(option_list = option_list) -opt <- optparse::parse_args(opt_parser) - -# Find .git root directory -root_dir <- rprojroot::find_root(rprojroot::has_dir(".git")) - -base_url <- "https://raw.githubusercontent.com/jhudsl/OTTR_Template/main/" - -needed_files <- c( - ".github/workflows/pull_request.yml", - ".github/workflows/render-all.yml", - ".github/workflows/delete-preview.yml", - "scripts/git_repo_check.R", - "scripts/make_screenshots.R", - "_bookdown.yml", - "_output.yml", - "book.bib", - "config_automation.yml", - "assets/big-image.html", - "assets/footer.html" - ) - -# If this is bookdown, we don't want to copy over the bookdown.yml or output.yml files -if (opt$bookdown) { - needed_files <- setdiff(needed_files, - c("_bookdown.yml", "_output.yml", "assets/big-image.html", "assets/footer.html", "book.bib")) -} - -# Set up a file list with the destination locations as the names -url_to_files <- paste0(base_url, needed_files) -names(url_to_files) <- file.path(root_dir, needed_files) - -# Download the file in the respective place -for (index in 1:length(url_to_files)) { - dest_folder <- dirname(names(url_to_files)[index]) - if (!dir.exists(dest_folder)){ - dir.create(dest_folder, recursive = TRUE) - } - download.file(url = url_to_files[index], destfile = names(url_to_files)[index]) -} - -system("git add .") -system("git config commit.gpgsign false") -system("git commit -m 'Add ottr-fying files'") -system("git push --set-upstream origin robot/ottr-fy")