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 · 2 comments
Open

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

Moohan opened this issue Oct 21, 2024 · 2 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
@gaborcsardi
Copy link
Member

@arisp99 why does the style workflow only work for packages? E.g. why do we need to install the dependencies of the package we are styling? I.e. why this step?
4219b22#diff-79e90602380ac63f7f5a95cf15bfc3203ade876f661244d78fc60760cf4f6714R27

@arisp99
Copy link
Contributor

arisp99 commented Nov 12, 2024

To be completely honest, I am not sure what the original thinking was for that. Based on some quick tests I just ran, it looks like the workflow would work without installing the package dependencies and only installing the {styler} package. So the style workflow can be changed to work more generally.

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

3 participants