Skip to content

Commit b3e4209

Browse files
authored
setup pr-commands and styler (#43)
* setup pr-commands and styler * relax condition for testing * make pr commands strict again
1 parent 7c894ef commit b3e4209

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

.github/workflows/pr-commands.yaml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
name: pr-commands.yaml
8+
9+
permissions: read-all
10+
11+
jobs:
12+
document:
13+
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }}
14+
name: document
15+
runs-on: ubuntu-latest
16+
env:
17+
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
18+
permissions:
19+
contents: write
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- uses: r-lib/actions/pr-fetch@v2
24+
with:
25+
repo-token: ${{ secrets.GITHUB_TOKEN }}
26+
27+
- uses: r-lib/actions/setup-r@v2
28+
with:
29+
use-public-rspm: true
30+
31+
- uses: r-lib/actions/setup-r-dependencies@v2
32+
with:
33+
extra-packages: any::roxygen2
34+
needs: pr-document
35+
36+
- name: Document
37+
run: roxygen2::roxygenise()
38+
shell: Rscript {0}
39+
40+
- name: commit
41+
run: |
42+
git config --local user.name "$GITHUB_ACTOR"
43+
git config --local user.email "[email protected]"
44+
git add man/\* NAMESPACE
45+
git commit -m 'Document'
46+
47+
- uses: r-lib/actions/pr-push@v2
48+
with:
49+
repo-token: ${{ secrets.GITHUB_TOKEN }}
50+
51+
style:
52+
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }}
53+
name: style
54+
runs-on: ubuntu-latest
55+
env:
56+
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
57+
permissions:
58+
contents: write
59+
steps:
60+
- uses: actions/checkout@v4
61+
62+
- uses: r-lib/actions/pr-fetch@v2
63+
with:
64+
repo-token: ${{ secrets.GITHUB_TOKEN }}
65+
66+
- uses: r-lib/actions/setup-r@v2
67+
68+
- name: Install dependencies
69+
run: install.packages(c("styler", "roxygen2"))
70+
shell: Rscript {0}
71+
72+
- name: Style
73+
run: |
74+
source("inst/styler/bgms_style.R")
75+
styler::style_pkg(
76+
style = bgms_style,
77+
exclude_files = c("R/RcppExports\\.R", "R/cpp11\\.R", "R/import-standalone.*\\.R", "\\.Rprofile\\.R")
78+
)
79+
80+
shell: Rscript {0}
81+
82+
- name: commit
83+
run: |
84+
git config --local user.name "$GITHUB_ACTOR"
85+
git config --local user.email "[email protected]"
86+
git add \*.R
87+
git commit -m 'Style'
88+
89+
- uses: r-lib/actions/pr-push@v2
90+
with:
91+
repo-token: ${{ secrets.GITHUB_TOKEN }}

inst/styler/bgms_style.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
bgms_style <- function() {
2+
style <- styler::tidyverse_style()
3+
# style$space$spacing_before_comments()
4+
# "if (condition)" -> "if(condition)"
5+
style$space$add_space_after_for_if_while <- function(pd_flat) {
6+
paren_after <- pd_flat$token %in% c("FOR", "IF", "WHILE")
7+
if (!any(paren_after)) {
8+
return(pd_flat)
9+
}
10+
pd_flat$spaces[paren_after & (pd_flat$newlines == 0L)] <- 0L
11+
pd_flat
12+
}
13+
14+
# "a <- 1" -> "a = 1"
15+
style$token$force_assignment_op <- function(pd) {
16+
to_replace <- pd$token == "EQ_ASSIGN"
17+
pd$token[to_replace] <- "LEFT_ASSIGN"
18+
pd$text[to_replace] <- "="
19+
pd
20+
}
21+
style
22+
}
23+
24+
# run the styler locally via
25+
# note that this cannot be undone! make sure to commit your changes beforehand
26+
# source("inst/styler/bgms_style.R")
27+
# styler::style_pkg(
28+
# style = bgms_style,
29+
# exclude_files = c("R/RcppExports\\.R", "R/cpp11\\.R", "R/import-standalone.*\\.R", "\\.Rprofile\\.R")
30+
# )

0 commit comments

Comments
 (0)