diff --git a/.github/workflows/delete-preview.yml b/.github/workflows/delete-preview.yml index b910ad45..642fea0a 100644 --- a/.github/workflows/delete-preview.yml +++ b/.github/workflows/delete-preview.yml @@ -22,3 +22,15 @@ jobs: with: github_token: ${{ secrets.GH_PAT }} branches: preview-${{ github.event.pull_request.number }} + + - name: Delete gist + id: gist + run: | + echo ${{ secrets.GH_PAT }} > docker/git_token.txt + gist_key=${GITHUB_REPOSITORY}_spell_check_${{ github.event.pull_request.number }} + gist_url=$(Rscript --vanilla scripts/get_the_gist.R \ + --git_pat docker/git_token.txt \ + --gist_key $gist_key \ + --file resources/spell_check_results.tsv \ + --delete) + rm docker/git_token.txt diff --git a/.github/workflows/render-bookdown.yml b/.github/workflows/render-bookdown.yml index 6b0d97a1..90d3cf3b 100644 --- a/.github/workflows/render-bookdown.yml +++ b/.github/workflows/render-bookdown.yml @@ -19,9 +19,10 @@ on: - assets/* jobs: # This workflow contains a single job called "build-all" - build-all: - # The type of runner that the job will run on + render-main: runs-on: ubuntu-latest + container: + image: jhudsl/course_template # Steps represent a sequence of tasks that will be executed as part of the job steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it @@ -74,21 +75,13 @@ jobs: # Run bookdown rendering - name: Run bookdown render id: bookdown - run: | - docker run \ - --mount type=bind,target=/home/rstudio,source=$PWD \ - jhudsl/course_template \ - Rscript -e "bookdown::render_book('index.Rmd', output_format = 'all')" + run: Rscript -e "bookdown::render_book('index.Rmd', output_format = 'all')" ##### If you do not wish to host your course on Coursera, you can delete this section ##### # Run Coursera version - name: Run Coursera version of render id: coursera - run: | - docker run \ - --mount type=bind,target=/home/rstudio,source=$PWD \ - jhudsl/course_template \ - Rscript -e "leanbuild::render_coursera()" + run: Rscript -e "leanbuild::render_coursera()" ##### End of Coursera rendering section ######################################## diff --git a/.github/workflows/render-preview.yml b/.github/workflows/render-preview.yml index e68aa519..a21c7984 100644 --- a/.github/workflows/render-preview.yml +++ b/.github/workflows/render-preview.yml @@ -11,7 +11,7 @@ permissions: write-all jobs: # This workflow contains a single job called "build-all" - build-all: + render-preview: runs-on: ubuntu-latest container: image: jhudsl/course_template diff --git a/.github/workflows/style-and-sp-check.yml b/.github/workflows/style-and-sp-check.yml index 04510f4b..67b22215 100644 --- a/.github/workflows/style-and-sp-check.yml +++ b/.github/workflows/style-and-sp-check.yml @@ -41,31 +41,23 @@ jobs: name: spell-check-results path: resources/spell_check_results.tsv - - name: Spell check errors + - name: Upload to Gist + id: gist run: | - # Get on the preview branch if it exists. If it doesn't create it. - branch_name='preview-${{ github.event.pull_request.number }}' - git rev-parse --quiet --verify $branch_name >/dev/null && exists=true || exists=false - - if [[ $exists == true ]]; - then - git checkout $branch_name - git pull --set-upstream origin $branch_name --allow-unrelated-histories - else - git checkout -b $branch_name - fi - - git add --force resources/spell_check_results.tsv - git commit -m 'Add spell check file' - git push --force --set-upstream origin $branch_name + echo ${{ secrets.GH_PAT }} > docker/git_token.txt + gist_key=${GITHUB_REPOSITORY}_spell_check_${{ github.event.pull_request.number }} + gist_url=$(Rscript --vanilla scripts/get_the_gist.R \ + --git_pat docker/git_token.txt \ + --gist_key $gist_key \ + --file resources/spell_check_results.tsv) + echo "::set-output name=gist_url::$gist_url" + rm docker/git_token.txt - name: Build components of the spell check comment id: build-components env: GH_PAT: ${{ secrets.GH_PAT }} run: | - branch_name='preview-${{ github.event.pull_request.number }}' - sp_error_url=https://raw.githubusercontent.com/${GITHUB_REPOSITORY}/$branch_name/resources/spell_check_results.tsv echo ::set-output name=time::$(date +'%Y-%m-%d') echo ::set-output name=commit_id::$GITHUB_SHA echo ::set-output name=sp_error_url::$sp_error_url @@ -88,7 +80,7 @@ jobs: body: | :warning: There are spelling errors that need to be addressed. [Read this guide for more info](https://github.com/jhudsl/OTTR_Template/wiki/Spell-check). - [Download the errors here.](${{ steps.build-components.outputs.sp_error_url }}) + [Download the errors here.](${{ steps.gist.outputs.gist_url }}) _Comment updated at ${{ steps.build-components.outputs.time }} with changes from ${{ steps.build-components.outputs.commit_id }}_ edit-mode: replace @@ -114,6 +106,5 @@ jobs: - name: Commit styled files run: | git add \*.Rmd - git add resources/spell_check_results.tsv git commit -m 'Style Rmds' || echo "No changes to commit" git push origin || echo "No changes to commit" diff --git a/docker/Dockerfile b/docker/Dockerfile index 7eb6d1a6..d3c50bc9 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -11,7 +11,14 @@ RUN apt-get install -y --no-install-recommends \ libxt6 \ libpoppler-cpp-dev \ vim \ - libglpk40 + libglpk40 \ + curl \ + gpg + +# Install gh +RUN curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | gpg --dearmor -o /usr/share/keyrings/githubcli-archive-keyring.gpg; +RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null; +RUN apt update && apt install -y gh; # Remove old symlinks to old pandoc RUN unlink /usr/lib/rstudio-server/bin/pandoc/pandoc diff --git a/docker/install_github.R b/docker/install_github.R index 5c0822f3..45195a59 100644 --- a/docker/install_github.R +++ b/docker/install_github.R @@ -1,5 +1,9 @@ #!/usr/bin/env Rscript +if (!"optparse" %in% installed.packages()) { + install.packages("optparse") +} + library(optparse) ################################ Set up options ################################ diff --git a/scripts/get_the_gist.R b/scripts/get_the_gist.R new file mode 100644 index 00000000..4beb79e9 --- /dev/null +++ b/scripts/get_the_gist.R @@ -0,0 +1,126 @@ +# Handle Gists! + +library(magrittr) + +# Find .git root directory +root_dir <- rprojroot::find_root(rprojroot::has_dir(".git")) + +option_list <- list( + optparse::make_option( + opt_str = c("--gist_key"), + type = "character", + default = NULL, + help = "The key for what the spell check file for this pull request and repository is + In github actions will be: e.g. ${{ github.repository }}_spell_check_${{ github.event.pull_request.number }}", + ), + optparse::make_option( + c("--file"), + type = "character", + default = NULL, + help = "The file to be stored in the gist. Must be relative to base git repository", + ), + optparse::make_option( + opt_str = "--delete", type = "character", + default = "FALSE", help = "Needs a 'TRUE/FALSE' whether the gist should be deleted after we find it" + ), + optparse::make_option( + c("--git_pat"), + type = "character", + default = NULL, + help = "GitHub personal access token stored in a text file", + ), + optparse::make_option( + opt_str = c("-v", "--verbose"), type = "character", + default = "FALSE", help = "Needs a 'TRUE/FALSE' whether more messages should be printed" + ) +) + +# Read the arguments passed +opt_parser <- optparse::OptionParser(option_list = option_list) +opt <- optparse::parse_args(opt_parser) +opt$delete <- as.logical(opt$delete) +opt$verbose <- as.logical(opt$verbose) +opt$file <- file.path(root_dir, opt$file) + + +if (!file.exists(opt$file) && !opt$delete) { + stop(paste("Specified file:", opt$file, "does not exist")) +} + +# Make sure we can login before we continue +system(paste("gh auth login --with-token <", opt$git_pat)) + +get_the_gist <- function(user = "jhudsl-robot", + verbose = TRUE) { + + # Declare URL + url <- paste0("https://api.github.com/users/", user, "/gists?per_page=1000000") + + # Github api get + response <- httr::GET( + url, + httr::accept_json() + ) + + if (httr::http_error(response)) { + warning(paste0("url: ", url, " failed")) + } + + # Get content as JSON + gists <- httr::content(response, as = "parsed") + + + description_key <- unlist(gists)[grep("description", names(unlist(gists)))] + id <- unlist(gists)[grep("^id$", names(unlist(gists)))] + gist_urls <- unlist(gists)[grep("files.spell_check_results.tsv.raw_url", names(unlist(gists)))] + + gists_df <- data.frame(description_key, id, gist_urls) + + return(gists_df) +} + +gist_df <- get_the_gist() + + +if (nrow(gist_df) < 1 ){ + if (opt$verbose) warning("No gists found at all") + gist_target <- gist_df +} else { + gist_target <- gist_df %>% + dplyr::filter(description_key == opt$gist_key) +} + +if (nrow(gist_target) < 1 ) { + # If gist is not found: + if (opt$verbose) message(paste0("No gists match the key:", opt$gist_key)) + if (opt$delete) { + # Warn that there isn't anything to delete + warning("No gist to delete.") + } else { + # Create it + message(paste("Creating gist:", opt$gist_key)) + system(paste("gh gist create --public", opt$file, "-d", opt$gist_key)) + } +} else { + # If gist is found: + if (opt$delete) { + # Delete it + message(paste("Deleting gist:", opt$gist_key)) + system(paste("gh gist delete", gist_target$id)) + } else { + # Or update it + message(paste("Updating gist:", opt$gist_key)) + system(paste("gh gist edit", gist_target$id, "-f", opt$file, "--add", opt$file)) + } +} + + +if (!opt$delete) { + # Print out the url + gist_target <- get_the_gist() %>% + dplyr::filter(description_key == opt$gist_key) %>% + dplyr::pull(gist_urls) + + write(gist_target, stdout()) +} + diff --git a/scripts/spell-check.R b/scripts/spell-check.R index f25eb2db..54445781 100644 --- a/scripts/spell-check.R +++ b/scripts/spell-check.R @@ -10,6 +10,10 @@ library(magrittr) +if (!("spelling" %in% installed.packages())){ + install.packages("spelling") +} + # Find .git root directory root_dir <- rprojroot::find_root(rprojroot::has_dir(".git"))