Skip to content

Commit

Permalink
Merge pull request #295 from ropensci/precommit-ci-lite-linter
Browse files Browse the repository at this point in the history
- Polish lint and styling actions
  • Loading branch information
philipp-baumann authored Sep 16, 2024
2 parents 428d590 + fdd2889 commit 1783f72
Show file tree
Hide file tree
Showing 69 changed files with 2,272 additions and 162 deletions.
5 changes: 4 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ create_dev_env.R
^\.pre-commit-config\.yaml$
^data-raw$
.envrc
.direnv
.direnv

# flint files
^flint$
55 changes: 55 additions & 0 deletions .github/workflows/flint-code-formatter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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]
release:
types: [published]
workflow_dispatch:

name: flint-code-formatter

jobs:
flint:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: rix-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

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

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

- name: Install flint
run: install.packages("flint", repos = c("https://etiennebacher.r-universe.dev/", getOption("repos")))
shell: Rscript {0}

- name: Run flint
run: flint::lint_package(exclude_path = "inst")
shell: Rscript {0}

- name: config bot user and check for changes
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
git diff-index --quiet HEAD || echo "has_changes=true" >> $GITHUB_ENV
- name: commit if changes
if: env.has_changes == 'true'
run: |
git add \*.R
git commit -m 'Check and format code with {flint}'
- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
38 changes: 0 additions & 38 deletions .github/workflows/lint.yaml

This file was deleted.

8 changes: 4 additions & 4 deletions .github/workflows/run_rhub.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: run-rhub-checks

Expand All @@ -28,6 +26,8 @@ jobs:
# signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
# If you chose API tokens for write access OR if you have a private cache
authToken: '${{ secrets.CACHIX_AUTH }}'

- name: Run checks
run: nix-shell --run "Rscript -e \"rhub::rhub_check(platforms = c('linux','macos','macos-arm64','windows','ubuntu-next','ubuntu-release'), branch = 'main')\""
run: |
nix-shell --run "Rscript -e \"rhub::rhub_check(platforms = c('linux','macos','macos-arm64','windows','ubuntu-next','ubuntu-release'))\""
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
pull_request:
branches: [main, master]

name: styler
name: style-and-lint

permissions: write-all

Expand All @@ -20,6 +20,10 @@ jobs:
steps:
- uses: actions/checkout@v4

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

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@main

Expand All @@ -33,31 +37,23 @@ jobs:
- name: Run styler::style_pkg
run: nix-shell --run "Rscript -e 'styler::style_pkg()'"

- name: Check if PR exists
id: check_pr
run: |
PR=$(gh pr list -S 'Style package' --json number --jq '.[0].number')
echo "PR_NUMBER=$PR" >> $GITHUB_ENV
- name: Run lintr
run: nix-shell --run "Rscript -e 'lintr::lint_package()'"
env:
LINTR_ERROR_ON_LINT: true

- name: Configure user and check for changes
- name: config bot user and check for changes
run: |
git config --global user.email "ga-ci@no-reply.com"
git config --global user.name "CI Robot"
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
git diff-index --quiet HEAD || echo "has_changes=true" >> $GITHUB_ENV
- name: Commit and push changes
- name: commit if changes
if: env.has_changes == 'true'
run: |
git add .
git commit -m "Styled package"
git push origin main:style_pkg --force
git add \*.R
git commit -m 'Style via {styler}'
- name: Create Pull Request
if: env.PR_NUMBER == ''
uses: peter-evans/create-pull-request@v6
- uses: r-lib/actions/pr-push@v2
with:
branch: style_pkg
title: 'Style package'
body: 'Automated PR to style package using `styler:style_pkg()`'
base: main
branch-suffix: ''
repo-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.httr-oauth
inst/doc
docs
flint/cache_file_state.rds
result
/doc/
/Meta/
Expand Down
10 changes: 8 additions & 2 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
linters: linters_with_defaults(
line_length_linter(100),
commented_code_linter = NULL
line_length_linter = line_length_linter(100),
commented_code_linter = NULL,
object_usage_linter = NULL,
cyclocomp_linter = NULL
)
exclusions: list(
"tests/testthat/test-fetchers.R",
"vignettes/"
)
encoding: "UTF-8"
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# All available hooks: https://pre-commit.com/hooks.html
# R specific hooks: https://github.com/lorenzwalthert/precommit
repos:
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.3.9001
hooks:
- id: style-files
args: [--style_pkg=styler, --style_fun=tidyverse_style, --cache-root=styler-perm]
require_serial: true
- id: lintr
exclude: >
(?x)^(
tests/testthat/test-fetchers\.R|
vignettes/.*
)$
- id: parsable-R
exclude: >
(?x)^(
tests/testthat/in/.*|
)$
- id: no-browser-statement

default_stages: ["commit"]

ci:
# skip: [consistent-release-tag, spell-check-ordered-exclude, pkgdown]
autoupdate_schedule: monthly
5 changes: 4 additions & 1 deletion R/detect_os.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ detect_os <- function() {
#' @noRd
generate_locale_archive <- function(os) {
if (os == "Linux" || os == "Darwin") {
'LOCALE_ARCHIVE = if pkgs.system == \"x86_64-linux\" then \"${pkgs.glibcLocales}/lib/locale/locale-archive\" else \"\";'
paste0(
'LOCALE_ARCHIVE = if pkgs.system == \"x86_64-linux\" then ',
'\"${pkgs.glibcLocales}/lib/locale/locale-archive\" else \"\";'
)
} else {
stop("Operating System unsupported")
}
Expand Down
15 changes: 13 additions & 2 deletions R/fetchers.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ fetchzip <- function(archive_pkg, sri_hash = NULL) {
#' @noRd
remove_base <- function(list_imports) {
imports_nobase <- gsub(
"(^base$)|(^compiler$)|(^datasets$)|(^grDevices$)|(^graphics$)|(^grid$)|(^methods$)|(^parallel$)|(^profile$)|(^splines$)|(^stats$)|(^stats4$)|(^tcltk$)|(^tools$)|(^translations$)|(^utils$)",
paste0(
"(^base$)|(^compiler$)|(^datasets$)|(^grDevices$)|(^graphics$)|(^grid$)|",
"(^methods$)|(^parallel$)|(^profile$)|(^splines$)|(^stats$)|",
"(^stats4$)|(^tcltk$)|(^tools$)|(^translations$)|(^utils$)"
),
NA_character_,
list_imports
)
Expand Down Expand Up @@ -233,7 +237,14 @@ fetchgits <- function(git_pkgs) {

paste(lapply(git_pkgs, fetchgit), collapse = "\n")
} else {
stop("There is something wrong with the input. Make sure it is either a list of three elements 'package_name', 'repo_url' and 'commit' or a list of lists with these three elements")
stop(
paste0(
"There is something wrong with the input. ",
"Make sure it is either a list of three elements ",
"'package_name', 'repo_url' and 'commit', or ",
"a list of lists with these three elements"
)
)
}
}

Expand Down
18 changes: 14 additions & 4 deletions R/get_latest.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' Get the latest R version and packages
#' @param r_version Character. R version to look for, for example, "4.2.0". If a nixpkgs revision is provided instead, this gets returned.
#' @param r_version Character. R version to look for, for example, "4.2.0". If a
#' nixpkgs revision is provided instead, this gets returned.
#' @return A character. The commit hash of the latest nixpkgs-unstable revision
#' @importFrom curl new_handle curl_fetch_memory handle_reset
#' @importFrom jsonlite fromJSON
Expand All @@ -18,7 +19,13 @@ get_latest <- function(r_version) {
} else if (
!(r_version %in% c("bleeding_edge", "frozen_edge", available_r()))
) {
stop("The provided R version is likely wrong.\nPlease check that you provided a correct R version.\nYou can list available versions using `available_r()`.\nYou can also directly provide a commit, but you need to make sure it points to the right repo used by `rix()`.\nYou can also use 'bleeding_edge' and 'frozen_edge'.")
stop(
"The provided R version is likely wrong.\nPlease check that you ",
"provided a correct R version.\nYou can list available versions using ",
"`available_r()`.\nYou can also directly provide a commit, but you need ",
"to make sure it points to the right repo used by `rix()`.\nYou can ",
"also use 'bleeding_edge' and 'frozen_edge'."
)
} else if (!is_online) {
stop("ERROR! You don't seem to be connected to the internet.")
} else if (r_version == "bleeding_edge") {
Expand All @@ -33,11 +40,14 @@ get_latest <- function(r_version) {
#' @noRd
get_right_commit <- function(r_version) {
if (r_version == "frozen_edge") {
# nolint next: line_length_linter
api_url <- "https://api.github.com/repos/rstats-on-nix/nixpkgs/commits?sha=r-daily"
} else if (r_version %in% Filter(function(x) `!=`(x, "latest"), available_r())) { # all but latest

} else if (
r_version %in% Filter(function(x) `!=`(x, "latest"), available_r())
) { # all but latest
return(sysdata$revision[sysdata$version == r_version])
} else {
# nolint next: line_length_linter
api_url <- "https://api.github.com/repos/NixOS/nixpkgs/commits?sha=nixpkgs-unstable"
}

Expand Down
7 changes: 5 additions & 2 deletions R/make_nixpkgs_url.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' make_nixpkgs_url Find the right Nix revision
#' @param r_version Character. R version to look for, for example, "4.2.0". If a nixpkgs revision is provided instead, this gets returned.
#' @param r_version Character. R version to look for, for example, "4.2.0". If a
#' nixpkgs revision is provided instead, this gets returned.
#' @return A character. The url to use
#'
#' @examples
Expand All @@ -15,7 +16,9 @@ make_nixpkgs_url <- function(r_ver) {
latest_commit <- get_latest(r_ver)

list(
"url" = paste0("https://github.com/", github_repo, "archive/", latest_commit, ".tar.gz"),
"url" = paste0(
"https://github.com/", github_repo, "archive/", latest_commit, ".tar.gz"
),
"latest_commit" = latest_commit,
"r_ver" = r_ver
)
Expand Down
9 changes: 6 additions & 3 deletions R/nix_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ nix_build <- function(project_path = ".",
choices = c("simple", "quiet", "verbose")
)
# if nix store is not PATH variable; e.g. on macOS (system's) RStudio
PATH <- set_nix_path()
PATH <- set_nix_path() # nolint: object_name_linter
if (isTRUE(nzchar(Sys.getenv("NIX_STORE")))) {
# for Nix R sessions, guarantee that the system's user library
# (R_LIBS_USER) is not in the search path for packages => run-time purity
current_libpaths <- .libPaths()
# don't do this in covr test environment, because this sets R_LIBS_USER
# to multiple paths
R_LIBS_USER <- Sys.getenv("R_LIBS_USER")
R_LIBS_USER <- Sys.getenv("R_LIBS_USER") # nolint: object_name_linter
if (isFALSE(nzchar(Sys.getenv("R_COVR")))) {
remove_r_libs_user()
}
} else {
# nolint next: object_name_linter
LD_LIBRARY_PATH_default <- Sys.getenv("LD_LIBRARY_PATH")
if (nzchar(LD_LIBRARY_PATH_default)) {
# On some systems, like Ubuntu 22.04, we found that a preset
Expand All @@ -56,7 +57,7 @@ nix_build <- function(project_path = ".",
fix_ld_library_path()
cat(
"* Current LD_LIBRARY_PATH in system R session is:",
LD_LIBRARY_PATH_default
LD_LIBRARY_PATH_default # nolint: object_name_linter
)
cat("\n", "Setting `LD_LIBRARY_PATH` to `''` during `nix_build()`")
}
Expand All @@ -65,6 +66,7 @@ nix_build <- function(project_path = ".",
nix_dir <- normalizePath(project_path)
nix_file <- file.path(nix_dir, "default.nix")

# nolint start: line_length_linter
stopifnot(
"`project_path` must be character of length 1." =
is.character(project_path) && length(project_path) == 1L,
Expand All @@ -73,6 +75,7 @@ nix_build <- function(project_path = ".",
"`nix-build` not available. To install, we suggest you follow https://zero-to-nix.com/start/install ." =
isTRUE(has_nix_build)
)
# nolint end

max_jobs <- getOption("rix.nix_build_max_jobs", default = 1L)
stopifnot(
Expand Down
Loading

0 comments on commit 1783f72

Please sign in to comment.