Skip to content

Commit

Permalink
Added lint and styler workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
asadow committed Nov 3, 2023
1 parent 1f92bac commit 4ed1c7e
Show file tree
Hide file tree
Showing 31 changed files with 803 additions and 505 deletions.
1 change: 0 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
HTTR2_KEY_MEGAMATION: ${{ secrets.HTTR2_KEY_MEGAMATION }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/document.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
paths: ["R/**"]

name: Document

jobs:
document:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup R
uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- name: Install dependencies
uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::roxygen2
needs: roxygen2

- name: Document
run: roxygen2::roxygenise()
shell: Rscript {0}

- name: Commit and push changes
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add man/\* NAMESPACE DESCRIPTION
git commit -m "Update documentation" || echo "No changes to commit"
git pull --ff-only
git push origin
32 changes: 32 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: lint

jobs:
lint:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::lintr, local::.
needs: lint

- name: Lint
run: lintr::lint_package()
shell: Rscript {0}
env:
LINTR_ERROR_ON_LINT: true
79 changes: 79 additions & 0 deletions .github/workflows/pr-commands.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
issue_comment:
types: [created]

name: Commands

jobs:
document:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }}
name: document
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::roxygen2
needs: pr-document

- name: Document
run: roxygen2::roxygenise()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add man/\* NAMESPACE
git commit -m 'Document'
- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

style:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }}
name: style
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2

- name: Install dependencies
run: install.packages("styler")
shell: Rscript {0}

- name: Style
run: styler::style_pkg()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add \*.R
git commit -m 'Style'
- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
33 changes: 33 additions & 0 deletions .github/workflows/render-rmarkdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
paths: ['**.Rmd']

name: render-rmarkdown

jobs:
render-rmarkdown:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2

- uses: r-lib/actions/setup-renv@v2

- name: Render Rmarkdown files and Commit Results
run: |
RMD_PATH=($(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep '[.]Rmd$'))
Rscript -e 'for (f in commandArgs(TRUE)) if (file.exists(f)) rmarkdown::render(f)' ${RMD_PATH[*]}
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git commit ${RMD_PATH[*]/.Rmd/.md} -m 'Re-build Rmarkdown files' || echo "No changes to commit"
git push origin || echo "No changes to commit"
73 changes: 73 additions & 0 deletions .github/workflows/style.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
paths: ["**.[rR]", "**.[qrR]md", "**.[rR]markdown", "**.[rR]nw", "**.[rR]profile"]

name: Style

jobs:
style:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup R
uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- name: Install dependencies
uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::styler, any::roxygen2
needs: styler

- name: Enable styler cache
run: styler::cache_activate()
shell: Rscript {0}

- name: Determine cache location
id: styler-location
run: |
cat(
"location=",
styler::cache_info(format = "tabular")$location,
"\n",
file = Sys.getenv("GITHUB_OUTPUT"),
append = TRUE,
sep = ""
)
shell: Rscript {0}

- name: Cache styler
uses: actions/cache@v3
with:
path: ${{ steps.styler-location.outputs.location }}
key: ${{ runner.os }}-styler-${{ github.sha }}
restore-keys: |
${{ runner.os }}-styler-
${{ runner.os }}-
- name: Style
run: styler::style_pkg()
shell: Rscript {0}

- name: Commit and push changes
run: |
if FILES_TO_COMMIT=($(git diff-index --name-only ${{ github.sha }} \
| egrep --ignore-case '\.(R|[qR]md|Rmarkdown|Rnw|Rprofile)$'))
then
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git commit ${FILES_TO_COMMIT[*]} -m "Style code (GHA)"
git pull --ff-only
git push origin
else
echo "No changes to commit."
fi
77 changes: 38 additions & 39 deletions R/creds.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,53 @@
#' )
#' }
mm_set_creds <- function(key, url, overwrite = FALSE) {
check_string(key)
check_bool(overwrite)
url <- check_url(url)
check_string(key)
check_bool(overwrite)
url <- check_url(url)

home <- Sys.getenv("HOME")
renv <- file.path(home, ".Renviron")
home <- Sys.getenv("HOME")
renv <- file.path(home, ".Renviron")

# If needed, backup original .Renviron before doing anything
if (file.exists(renv)) {
file.copy(renv, file.path(home, ".Renviron_backup"))
}
# If needed, backup original .Renviron before doing anything
if (file.exists(renv)) {
file.copy(renv, file.path(home, ".Renviron_backup"))
}

if (!file.exists(renv)) {
file.create(renv)
} else {
if (isTRUE(overwrite)) {
cli::cli_alert_info(
"If needed, your original {.file {'.Renviron'}}
if (!file.exists(renv)) {
file.create(renv)
} else {
if (isTRUE(overwrite)) {
cli::cli_alert_info(
"If needed, your original {.file {'.Renviron'}}
is backed up and stored in
{.file {home}}."
)
oldenv <- readLines(renv)
newenv <- oldenv[-grep("MEGAMATION_KEY|MEGAMATION_URL", oldenv)]
writeLines(newenv, renv)
}
else {
tv <- readLines(renv)
if (any(grepl("MEGAMATION_KEY|MEGAMATION_URL", tv))) {
cli::cli_abort(c(
"Megamation credentials already exist.",
"i" = "You can set {.arg overwrite = TRUE}
)
oldenv <- readLines(renv)
newenv <- oldenv[-grep("MEGAMATION_KEY|MEGAMATION_URL", oldenv)]
writeLines(newenv, renv)
} else {
tv <- readLines(renv)
if (any(grepl("MEGAMATION_KEY|MEGAMATION_URL", tv))) {
cli::cli_abort(c(
"Megamation credentials already exist.",
"i" = "You can set {.arg overwrite = TRUE}
to overwrite them."
))
}
))
}
}
}

keyconcat <- glue::glue("MEGAMATION_KEY = '{key}'")
urlconcat <- glue::glue("MEGAMATION_URL = '{url}'")
keyconcat <- glue::glue("MEGAMATION_KEY = '{key}'")
urlconcat <- glue::glue("MEGAMATION_URL = '{url}'")

# Append credentials to .Renviron file
write(keyconcat, renv, sep = "\n", append = TRUE)
write(urlconcat, renv, sep = "\n", append = TRUE)
# Append credentials to .Renviron file
write(keyconcat, renv, sep = "\n", append = TRUE)
write(urlconcat, renv, sep = "\n", append = TRUE)

Sys.setenv(
MEGAMATION_KEY = key,
MEGAMATION_URL = url
)
Sys.setenv(
MEGAMATION_KEY = key,
MEGAMATION_URL = url
)

cli::cli_alert_success("Set and loaded Megamation API credentials.")
}
cli::cli_alert_success("Set and loaded Megamation API credentials.")
}
2 changes: 1 addition & 1 deletion R/env.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ testing_key <- function() {
httr2::secret_decrypt(
"4E5GlxeUybPJnCQQnwyDGsPIncZI526gyfk",
"HTTR2_KEY_MEGAMATION"
)
)
}

#' Get `MEGAMATION_URL` env var
Expand Down
5 changes: 3 additions & 2 deletions R/error.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#' @examples
#' fake_mm_resp <- httr2::response_json(
#' body = list(
#' detail = "This is a fake detail/message from the API's response.")
#' )
#' detail = "This is a fake detail/message from the API's response."
#' )
#' )
#' mm_error_body(fake_mm_resp)
mm_error_body <- function(resp) {
if (!httr2::resp_has_body(resp)) {
Expand Down
1 change: 0 additions & 1 deletion R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ mm_resp_extract <- function(resp) {
mm_resp_parse() |>
parsed_extract(.from)
}

Loading

0 comments on commit 4ed1c7e

Please sign in to comment.