Skip to content

Commit

Permalink
Spell check store as a gist (#405)
Browse files Browse the repository at this point in the history
* Restore render-preview to original set up

* Make test sync better

* Fix render-preview

* Creating gist

* Update docker image pull

* Add gh client to docker image

* Work on gists

* Add spelling package install

* Updates

* A few more edits

* Missing )

* Don't add spell check results file

* Add spelling errors to test

* Fix gist_key

* Use --add option

* Make sure url is printed out

* Revert "Add spelling errors to test"

This reverts commit 4633500.

* Add delete the gist to the delete-preview.yml

Co-authored-by: jhudsl-robot <[email protected]>
  • Loading branch information
cansavvy and jhudsl-robot authored Jan 19, 2022
1 parent 6ebda8b commit e5ee427
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 34 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/delete-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 5 additions & 12 deletions .github/workflows/render-bookdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ########################################

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/render-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 11 additions & 20 deletions .github/workflows/style-and-sp-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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"
9 changes: 8 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docker/install_github.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env Rscript

if (!"optparse" %in% installed.packages()) {
install.packages("optparse")
}

library(optparse)

################################ Set up options ################################
Expand Down
126 changes: 126 additions & 0 deletions scripts/get_the_gist.R
Original file line number Diff line number Diff line change
@@ -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())
}

4 changes: 4 additions & 0 deletions scripts/spell-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down

0 comments on commit e5ee427

Please sign in to comment.