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+ if (ci == " native" ) {
58+ use_ci(ci )
59+ }
60+ } else {
61+ if (ci == " gha" ) {
62+ use_ci(ci , force = force , root = root )
63+ }
5564 }
65+
5666 invisible (NULL )
5767}
5868
69+
70+ # ' Use continuous integration with pre-commit
71+ # '
72+ # ' Sets up continuous integration, or prompts the user to do it manually.
73+ # '
74+ # ' @param ci Specifies which continuous integration service to use. See
75+ # ' `vignette("ci", package = "precommit")` for details. Defaults to
76+ # ' `getOption("precommit.ci", "native")`, which is set to
77+ # ' `"native"` on package loading (if unset). `"native"` sets up
78+ # ' [pre-commit.ci](https://pre-commit.ci). Alternatively, `"gha"` can be used
79+ # ' to set up [GitHub Actions](https://github.com/features/actions). Set value
80+ # ' to `NULL` if you don't want to use a continuous integration.
81+ # ' @inheritParams fallback_doc
82+ # ' @export
83+ use_ci <- function (ci = getOption(" precommit.ci" , " native" ),
84+ open = interactive(), 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+ 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).
0 commit comments