Skip to content

Commit a6e1e20

Browse files
Merge pull request #318 from lorenzwalthert/issue-291
- Template for GitHub Action Workflow (#291).
2 parents ffb10a1 + e7f9c2a commit a6e1e20

File tree

12 files changed

+254
-5
lines changed

12 files changed

+254
-5
lines changed

API

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ roxygenize_with_cache(key, dirs)
1616
snippet_generate(snippet, open = rstudioapi::isAvailable(), root = here::here())
1717
uninstall_precommit(scope = "repo", ask = "user", root = here::here())
1818
update_precommit()
19-
use_precommit(config_source = getOption("precommit.config_source"), force = FALSE, legacy_hooks = "forbid", open = rstudioapi::isAvailable(), install_hooks = TRUE, root = here::here())
19+
use_ci(ci = getOption("precommit.ci", "native"), force = FALSE, root = here::here())
20+
use_precommit(config_source = getOption("precommit.config_source"), force = FALSE, legacy_hooks = "forbid", open = rstudioapi::isAvailable(), install_hooks = TRUE, ci = getOption("precommit.ci", "native"), root = here::here())
2021
use_precommit_config(config_source = getOption("precommit.config_source"), force = FALSE, open = rstudioapi::isAvailable(), verbose = FALSE, root = here::here())
2122
version_precommit()

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export(roxygenize_with_cache)
1414
export(snippet_generate)
1515
export(uninstall_precommit)
1616
export(update_precommit)
17+
export(use_ci)
1718
export(use_precommit)
1819
export(use_precommit_config)
1920
export(version_precommit)

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ includes switching to R based hook for `readme-rmd-render`, avoiding the
55
{usethis} dependency, integration tests via GitHub Actions, auto-updates for
66
used packages, roxygen snippet generation and more. In addition:
77

8+
* `use_precommit()` gains a `ci` argument, defaulting to `"native"`, which will
9+
guide the user to set up continuous integration of the hooks with either
10+
[pre-commit.ci](https://pre-commit.ci) or
11+
[GitHub Actions](https://github.com/features/actions). New exported
12+
function `use_ci(ci = "native")` can be used to migrate existing repos. The
13+
default behavior for `ci` is governed by the R option `precommit.ci`.
814
* `use_precommit(..., install_hooks = TRUE)` is no longer blocking by default.
915
New option `precommit.block_install_hooks` (defaults to `FALSE`) governs the
1016
behavior (#312).

R/setup.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#' This function sets up pre-commit for your git repo.
44
#' @param install_hooks Whether to install environments for all available hooks.
55
#' If `FALSE`, environments are installed with first commit.
6+
#' @inheritParams use_ci
67
#' @param legacy_hooks How to treat hooks already in the repo which are not
78
#' managed by pre-commit. "forbid", the default, will cause `use_precommit()`
89
#' to fail if there are such hooks. "allow" will run these along with
@@ -39,6 +40,7 @@ use_precommit <- function(config_source = getOption("precommit.config_source"),
3940
legacy_hooks = "forbid",
4041
open = rstudioapi::isAvailable(),
4142
install_hooks = TRUE,
43+
ci = getOption("precommit.ci", "native"),
4244
root = here::here()) {
4345
rlang::arg_match(legacy_hooks, c("forbid", "allow", "remove"))
4446
assert_is_installed()
@@ -52,10 +54,61 @@ use_precommit <- function(config_source = getOption("precommit.config_source"),
5254
install_repo(root, install_hooks, legacy_hooks)
5355
if (open) {
5456
open_config(root)
57+
use_ci(ci)
58+
} else {
59+
if (ci == "gha") {
60+
use_ci(ci, force = force, root = root)
61+
}
5562
}
63+
5664
invisible(NULL)
5765
}
5866

67+
68+
#' Use continuous integration with pre-commit
69+
#'
70+
#' Sets up continuous integration, or prompts the user to do it manually.
71+
#'
72+
#' @param ci Specifies which continuous integration service to use. See
73+
#' `vignette("ci", package = "precommit")` for details. Defaults to
74+
#' `getOption("precommit.ci", "native")`, which is set to
75+
#' `"native"` on package loading (if unset). `"native"` sets up
76+
#' [pre-commit.ci](https://pre-commit.ci). Alternatively, `"gha"` can be used
77+
#' to set up [GitHub Actions](https://github.com/features/actions). Set value
78+
#' to `NULL` if you don't want to use a continuous integration.
79+
#' @param force Whether or not to overwrite an existing ci config file (only
80+
#' relevant for `ci = "gha"`).
81+
#' @inheritParams fallback_doc
82+
#' @export
83+
use_ci <- function(ci = getOption("precommit.ci", "native"),
84+
force = FALSE, root = here::here()) {
85+
if (is.na(ci)) {
86+
return()
87+
} else if (ci == "gha") {
88+
dest <- fs::path(root, ".github/workflows/pre-commit.yaml")
89+
fs::dir_create(fs::path_dir(dest))
90+
fs::file_copy(
91+
system.file("pre-commit-gha.yaml", package = "precommit"),
92+
dest,
93+
overwrite = force
94+
)
95+
cli::cli_alert_success(paste0(
96+
"Added GitHub Action template to ",
97+
"{.code .github/workflows/pre-commit.yaml}. Pre-commit hooks will ",
98+
"run on pull requests. If workflow fails, please file an issue in ",
99+
"{.code https://github.com/lorenzwalthert/precommit}."
100+
))
101+
} else if (ci == "native") {
102+
cli::cli_ul("Sign in with GitHub to authenticate {.url https://pre-commit.ci}.")
103+
Sys.sleep(2)
104+
utils::browseURL("https://pre-commit.ci")
105+
} else {
106+
rlang::abort(
107+
'Argument `ci` must be one of `"native"` (default), `"gha"` or `NULL`.'
108+
)
109+
}
110+
}
111+
59112
#' Auto-update your hooks
60113
#'
61114
#' Runs [`pre-commit autoupdate`](https://pre-commit.com/#pre-commit-autoupdate).

R/zzz.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
op <- options()
33
op.precommit <- list(
44
precommit.executable = path_derive_precommit_exec(),
5-
precommit.block_install_hooks = FALSE
5+
precommit.block_install_hooks = FALSE,
6+
precommit.ci = "native"
67
)
78
toset <- !(names(op.precommit) %in% names(op))
89
if (any(toset)) options(op.precommit[toset])

inst/WORDLIST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ getwd
5959
gh
6060
github
6161
gitignore
62+
Gitlab
6263
grDevices
6364
grepl
6465
gsub

inst/pre-commit-gha.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: pre-commit
2+
on:
3+
push:
4+
branches-ignore:
5+
- 'master'
6+
- 'main'
7+
pull_request:
8+
types: [opened, synchronize, reopened, ready_for_review]
9+
10+
jobs:
11+
pre-commit:
12+
runs-on: ubuntu-18.04
13+
if: >-
14+
!contains(github.event.head_commit.message, 'ci skip') &&
15+
(
16+
startsWith(github.ref, 'refs/heads') ||
17+
github.event.pull_request.draft == false
18+
)
19+
steps:
20+
- name: Cancel Previous Runs
21+
uses: styfle/[email protected]
22+
with:
23+
access_token: ${{ github.token }}
24+
- uses: actions/checkout@v2
25+
with:
26+
fetch-depth: 0
27+
- name: Install system dependencies
28+
if: runner.os == 'Linux'
29+
run: |
30+
# your system installation code here
31+
# sudo apt-get install -y libcurl4-openssl-dev
32+
- name: Set up Python
33+
uses: actions/setup-python@v2
34+
with:
35+
python-version: "3.8"
36+
architecture: "x64"
37+
- name: Run pre-commit
38+
uses: pre-commit/[email protected]
39+
- name: Commit files
40+
if: failure() && startsWith(github.ref, 'refs/heads')
41+
run: |
42+
if [[ `git status --porcelain --untracked-files=no` ]]; then
43+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
44+
git config --local user.name "github-actions[bot]"
45+
git checkout -- .github/workflows
46+
git commit -m "pre-commit" -a
47+
fi
48+
- name: Push changes
49+
if: failure() && startsWith(github.ref, 'refs/heads')
50+
uses: ad-m/github-push-action@master
51+
with:
52+
github_token: ${{ secrets.GITHUB_TOKEN }}
53+
branch: ${{ github.ref }}
54+
env:
55+
RENV_CONFIG_CACHE_ENABLED: FALSE

man/use_ci.Rd

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/use_precommit.Rd

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test.R

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)