Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style modified files in a project (non package) #933

Open
Moohan opened this issue Oct 21, 2024 · 0 comments
Open

Style modified files in a project (non package) #933

Moohan opened this issue Oct 21, 2024 · 0 comments

Comments

@Moohan
Copy link

Moohan commented Oct 21, 2024

It would be great to have a few more example templates that can work generically on R projects that aren't packages. For example, styling or lining etc. I've developed a 'style modified files' workflow that works for me but I'm sure can be improved by people more technically competent!

name: Style Modified Files

on:
  pull_request_target: # Triggers when code is pushed to a PR's target branch
    types: [opened, synchronize, reopened]
    
permissions: write-all

jobs:
  style_check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.ref }} 
          fetch-depth: 0  # Fetch full history for accurate diff

      - name: Setup R
        uses: r-lib/actions/setup-r@v2
        with:
          use-public-rspm: true
        
      - name: Install pak (will install any system dependencies too)
        run: install.packages("pak")
        shell: Rscript {0}

      - name: Install styler and other packages
        run: pak::pkg_install(pkg = c("styler", "purrr", "gh"), dependencies = TRUE)
        shell: Rscript {0}

      - name: Enable styler cache
        run: styler::cache_activate()
        shell: Rscript {0}

      - name: Determine cache location
        id: styler-location
        run: |
          cat(
            "location=",
            styler::cache_info(format = "tabular")$location,
            "\n",
            file = Sys.getenv("GITHUB_OUTPUT"),
            append = TRUE,
            sep = ""
          )
        shell: Rscript {0}

      - name: Cache styler
        uses: actions/cache@v4
        with:
          path: ${{ steps.styler-location.outputs.location }}
          key: ${{ runner.os }}-styler-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-styler-
            ${{ runner.os }}-

      - name: Style modified R files
        run: |
          files <- gh::gh("GET https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files")
          changed_files <- purrr::map_chr(files, "filename")
          all_files <- list.files(recursive = TRUE)
          exclusions <- setdiff(all_files, changed_files)
          styler::style_dir(exclude_files = exclusions)
        shell: Rscript {0}

      - name: Commit and push changes
        uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "Style code"
          skip_checkout: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant