|
| 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 | + pull_request: |
| 5 | + paths: |
| 6 | + [ |
| 7 | + "**.[rR]", |
| 8 | + "**.[qrR]md", |
| 9 | + "**.[rR]markdown", |
| 10 | + "**.[rR]nw", |
| 11 | + "**.[rR]profile", |
| 12 | + ] |
| 13 | + workflow_dispatch: |
| 14 | + |
| 15 | +name: style |
| 16 | + |
| 17 | +permissions: read-all |
| 18 | + |
| 19 | +jobs: |
| 20 | + style: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: write |
| 24 | + pull-requests: write |
| 25 | + env: |
| 26 | + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} |
| 27 | + steps: |
| 28 | + - name: Checkout repo |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + fetch-depth: 0 |
| 32 | + |
| 33 | + - name: Setup R |
| 34 | + uses: r-lib/actions/setup-r@v2 |
| 35 | + with: |
| 36 | + use-public-rspm: true |
| 37 | + |
| 38 | + - name: Install dependencies |
| 39 | + uses: r-lib/actions/setup-r-dependencies@v2 |
| 40 | + with: |
| 41 | + extra-packages: any::styler, any::roxygen2 |
| 42 | + needs: styler |
| 43 | + |
| 44 | + - name: Enable styler cache |
| 45 | + run: styler::cache_activate() |
| 46 | + shell: Rscript {0} |
| 47 | + |
| 48 | + - name: Determine cache location |
| 49 | + id: styler-location |
| 50 | + run: | |
| 51 | + cat( |
| 52 | + "location=", |
| 53 | + styler::cache_info(format = "tabular")$location, |
| 54 | + "\n", |
| 55 | + file = Sys.getenv("GITHUB_OUTPUT"), |
| 56 | + append = TRUE, |
| 57 | + sep = "" |
| 58 | + ) |
| 59 | + shell: Rscript {0} |
| 60 | + |
| 61 | + - name: Cache styler |
| 62 | + uses: actions/cache@v4 |
| 63 | + with: |
| 64 | + path: ${{ steps.styler-location.outputs.location }} |
| 65 | + key: ${{ runner.os }}-styler-${{ github.sha }} |
| 66 | + restore-keys: | |
| 67 | + ${{ runner.os }}-styler- |
| 68 | + ${{ runner.os }}- |
| 69 | +
|
| 70 | + - name: Style |
| 71 | + run: styler::style_pkg() |
| 72 | + shell: Rscript {0} |
| 73 | + |
| 74 | + - name: Commit and push changes |
| 75 | + id: commit |
| 76 | + run: | |
| 77 | + if FILES_TO_COMMIT=($(git diff-index --name-only ${{ github.sha }} \ |
| 78 | + | egrep --ignore-case '\.(R|[qR]md|Rmarkdown|Rnw|Rprofile)$')) |
| 79 | + then |
| 80 | + git config --local user.name "$GITHUB_ACTOR" |
| 81 | + git config --local user.email "[email protected]" |
| 82 | + git commit ${FILES_TO_COMMIT[*]} -m "Style code (GHA)" |
| 83 | + git pull --ff-only origin "${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" |
| 84 | + git push origin HEAD:"${GITHUB_HEAD_REF}" |
| 85 | + echo "changes_made=true" >> $GITHUB_OUTPUT |
| 86 | + echo "files_changed=${FILES_TO_COMMIT[*]}" >> $GITHUB_OUTPUT |
| 87 | + else |
| 88 | + echo "No changes to commit." |
| 89 | + echo "changes_made=false" >> $GITHUB_OUTPUT |
| 90 | + fi |
| 91 | +
|
| 92 | + - name: Comment on PR |
| 93 | + id: comment |
| 94 | + if: steps.commit.outputs.changes_made == 'true' && github.event_name == 'pull_request' |
| 95 | + uses: actions/github-script@v7 |
| 96 | + with: |
| 97 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + script: | |
| 99 | + const files = `${{ steps.commit.outputs.files_changed }}`.split(' ').join('\n- '); |
| 100 | + github.rest.issues.createComment({ |
| 101 | + issue_number: context.issue.number, |
| 102 | + owner: context.repo.owner, |
| 103 | + repo: context.repo.repo, |
| 104 | + body: `📝 Styler has automatically formatted the following R files in this PR:\n- ${files}\n\nPlease pull these changes to your local branch.` |
| 105 | + }) |
0 commit comments