From 65176bf824b4a5e8af6d44f5fecfebe76c0fbf55 Mon Sep 17 00:00:00 2001 From: James McMahon Date: Mon, 19 May 2025 16:04:09 +0100 Subject: [PATCH 1/3] Replace styler with air https://posit-dev.github.io/air/formatter.html https://www.tidyverse.org/blog/2025/02/air/ https://yohann-data.fr/posts/airformatter/ Update phs_style_and_document.yaml Split the style and document actions Remove automatic documentation job from workflow Removed the phs_document job for automatic documentation generation and its dependencies. Update phs_package_checks.yaml Update phs_style.yaml to add default air.toml Update phs_style.yaml Update phs_document.yaml Update phs_document.yaml Update phs_style.yaml Update phs_style.yaml Update phs_document.yaml Update phs_package_checks.yaml Update job dependencies in phs_package_checks.yaml --- .github/workflows/phs_document.yaml | 4 - .github/workflows/phs_package_checks.yaml | 30 +++- .github/workflows/phs_style.yaml | 148 ++++++++++++++++++ .github/workflows/phs_style_and_document.yaml | 137 ---------------- 4 files changed, 170 insertions(+), 149 deletions(-) create mode 100644 .github/workflows/phs_style.yaml delete mode 100644 .github/workflows/phs_style_and_document.yaml diff --git a/.github/workflows/phs_document.yaml b/.github/workflows/phs_document.yaml index 62826ec..77af836 100644 --- a/.github/workflows/phs_document.yaml +++ b/.github/workflows/phs_document.yaml @@ -20,10 +20,6 @@ jobs: container: image: ghcr.io/public-health-scotland/phs-r-image - permissions: - contents: write - pull-requests: write - env: GITHUB_PAT: ${{ secrets.PHS_ACTIONS_TOKEN || github.token }} diff --git a/.github/workflows/phs_package_checks.yaml b/.github/workflows/phs_package_checks.yaml index 922f1e4..585e79a 100644 --- a/.github/workflows/phs_package_checks.yaml +++ b/.github/workflows/phs_package_checks.yaml @@ -48,8 +48,8 @@ jobs: description: 'Package checks in progress', context: 'PHS Package Checks' }); - Style_and_document: - uses: ./.github/workflows/phs_style_and_document.yaml + Style: + uses: ./.github/workflows/phs_style.yaml permissions: contents: write pull-requests: write @@ -58,8 +58,18 @@ jobs: concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true + Document: + needs: [Style] + uses: ./.github/workflows/phs_document.yaml + permissions: + contents: write + pull-requests: write + secrets: inherit + concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true Build_README: - needs: [Style_and_document] + needs: [Style] uses: ./.github/workflows/phs_build_readme.yaml permissions: contents: write @@ -70,7 +80,7 @@ jobs: group: ${{ github.workflow }}-build-readme-${{ github.ref }} cancel-in-progress: true R-CMD-check-core: - needs: [Style_and_document] + needs: [Style, Document] uses: ./.github/workflows/phs_R-CMD-check-core.yaml permissions: contents: read @@ -80,7 +90,7 @@ jobs: group: ${{ inputs.limit_parallel == false && format('r-cmd-check-{0}-{1}', github.workflow, github.run_id) || 'phs-core-check-queue' }} cancel-in-progress: false R-CMD-check-extended: - needs: [Style_and_document] + needs: [Style, Document] if: ${{ !github.event.pull_request.draft }} uses: ./.github/workflows/phs_R-CMD-check-extended.yaml permissions: @@ -102,7 +112,8 @@ jobs: cancel-in-progress: false pkgdown: needs: - - Style_and_document + - Style + - Document - Build_README - R-CMD-check-core - R-CMD-check-extended @@ -113,10 +124,12 @@ jobs: contents: write packages: read secrets: inherit + # Final job to update status finalize: needs: - - Style_and_document + - Style + - Document - Build_README - R-CMD-check-core - R-CMD-check-extended @@ -136,7 +149,8 @@ jobs: uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: script: | - const allSuccess = ${{ needs.Style_and_document.result == 'success' && + const allSuccess = ${{ needs.Style.result == 'success' && + needs.Document.result == 'success' && needs.Build_README.result == 'success' && needs.R-CMD-check-core.result == 'success' && (needs.R-CMD-check-extended.result == 'success' || needs.R-CMD-check-extended.result == 'skipped') && diff --git a/.github/workflows/phs_style.yaml b/.github/workflows/phs_style.yaml new file mode 100644 index 0000000..58f8737 --- /dev/null +++ b/.github/workflows/phs_style.yaml @@ -0,0 +1,148 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + workflow_call: + +name: phs_style.yaml + +permissions: + contents: read + pull-requests: read + +jobs: + phs_style: + name: "Automatically style the code using air" + runs-on: ubuntu-latest + + permissions: + contents: write + pull-requests: write + + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + + steps: + - name: Checkout repo + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 + with: + fetch-depth: 0 + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref_name }} + + - name: Install posit-dev/air + uses: posit-dev/setup-air@63e80dedb6d275c94a3841e15e5ff8691e1ab237 # v1.0.0 + + - name: Include standard air.toml (overwrite existing) + run: | + echo '[format]' > air.toml + echo 'line-ending = "lf"' >> air.toml + + - name: Add air.toml to .Rbuildignore if it exists + run: | + if [ -f .Rbuildignore ]; then + grep -qxF 'air.toml' .Rbuildignore || echo 'air.toml' >> .Rbuildignore + fi + + - name: Format using air + run: air format . + + - name: Detect changed .Rmd files + id: changed_rmd + shell: bash + run: | + if [ "${{ github.event_name }}" = "pull_request" ]; then + BASE_SHA="${{ github.event.pull_request.base.sha }}" + HEAD_SHA="${{ github.event.pull_request.head.sha }}" + else + BASE_SHA="${{ github.event.before }}" + HEAD_SHA="${{ github.sha }}" + fi + + echo "Diffing $BASE_SHA..$HEAD_SHA" + + # Handle first-push edge case: all-zero before SHA + if [ "$BASE_SHA" = "0000000000000000000000000000000000000000" ]; then + CHANGED_RMD="$(git ls-files | grep -E '\.Rmd$' || true)" + else + CHANGED_RMD="$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" | grep -E '\.Rmd$' || true)" + fi + + if [ -n "$CHANGED_RMD" ]; then + echo "rmd_changed=true" >> "$GITHUB_OUTPUT" + printf "%s\n" "$CHANGED_RMD" + else + echo "rmd_changed=false" >> "$GITHUB_OUTPUT" + echo "No .Rmd changes detected." + fi + + - name: Setup R + if: steps.changed_rmd.outputs.rmd_changed == 'true' + uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - name: Install styler deps (cached) + if: steps.changed_rmd.outputs.rmd_changed == 'true' + uses: r-lib/actions/setup-r-dependencies@v2 + with: + packages: | + any::styler + any::roxygen2 + any::knitr + + - name: Enable styler cache + if: steps.changed_rmd.outputs.rmd_changed == 'true' + run: styler::cache_activate() + shell: Rscript {0} + + - name: Determine styler cache location + if: steps.changed_rmd.outputs.rmd_changed == 'true' + 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 results + if: steps.changed_rmd.outputs.rmd_changed == 'true' + uses: actions/cache@v5 + with: + path: ${{ steps.styler-location.outputs.location }} + key: ${{ runner.os }}-styler-${{ hashFiles('**/*.Rmd', '**/*.R') }} + restore-keys: | + ${{ runner.os }}-styler- + + - name: Style .Rmd using styler + if: steps.changed_rmd.outputs.rmd_changed == 'true' + run: Rscript -e 'styler::style_dir(".", filetype = c("rmd", "rmarkdown"))' + + - name: Commit and push changes + id: commit_step_style + uses: stefanzweifel/git-auto-commit-action@04702edda442b2e678b25b537cec683a1493fcb9 # v7.1.0 + with: + commit_message: "Style code (GHA)" + commit_user_name: ${{ github.actor }} + commit_user_email: ${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com + continue-on-error: true + + - name: Create Pull Request + if: ${{ steps.commit_step_style.outputs.changes_detected == 'true' && steps.commit_step_style.outcome == 'failure' }} + uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1 + with: + commit-message: "Style code (GHA)" + branch: auto-style-code + delete-branch: true + title: "Style code (GHA)" + labels: maintenance + base: ${{ github.event.pull_request.base.ref }} + body: | + This Pull Request was automatically created to apply styling changes to your code. + Please review and merge this PR. + assignees: ${{ github.actor }} + reviewers: ${{ github.actor }} + diff --git a/.github/workflows/phs_style_and_document.yaml b/.github/workflows/phs_style_and_document.yaml deleted file mode 100644 index 7258bf7..0000000 --- a/.github/workflows/phs_style_and_document.yaml +++ /dev/null @@ -1,137 +0,0 @@ -# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples -# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help -on: - workflow_call: - secrets: - PHS_ACTIONS_TOKEN: - required: false - -name: phs_style_and_document.yaml - -permissions: - contents: read - pull-requests: read - packages: read - -jobs: - phs_style_and_document: - name: "Automatically style and document the code" - runs-on: ubuntu-latest - container: - image: ghcr.io/public-health-scotland/phs-r-image - - permissions: - contents: write - pull-requests: write - packages: read - - env: - GITHUB_PAT: ${{ secrets.PHS_ACTIONS_TOKEN || github.token }} - - steps: - - name: Harden runner - uses: step-security/harden-runner@bf7454d06d71f1098171f2acdf0cd4708d7b5920 # v2.20.0 - with: - egress-policy: audit - - - name: Mark workspace as safe for Git - run: git config --global --add safe.directory "$GITHUB_WORKSPACE" - - - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 - with: - fetch-depth: 0 - ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref_name }} - token: ${{ secrets.PHS_ACTIONS_TOKEN || github.token }} - - - 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@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ steps.styler-location.outputs.location }} - key: ${{ runner.os }}-styler-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-styler- - ${{ runner.os }}- - - - name: Style code - run: styler::style_pkg() - shell: Rscript {0} - - - name: Commit and push changes - id: commit_step_style - uses: stefanzweifel/git-auto-commit-action@4a55954c782fc1ea30b9056cd3e7a2b40ca8887d # v7.2.0 - with: - commit_message: "Style code (GHA)" - commit_user_name: ${{ github.actor }} - commit_user_email: ${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com - continue-on-error: true - - - name: Create Pull Request - if: ${{ steps.commit_step_style.outputs.changes_detected == 'true' && steps.commit_step_style.outcome == 'failure' }} - uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1 - with: - commit-message: "Style code (GHA)" - branch: auto-style-code - delete-branch: true - title: "Style code (GHA)" - labels: maintenance - base: ${{ github.event.pull_request.base.ref }} - body: | - This Pull Request was automatically created to apply styling changes to your code. - Please review and merge this PR. - assignees: ${{ github.actor }} - reviewers: ${{ github.actor }} - - - name: Normalise DESCRIPTION - run: desc::desc_normalize() - shell: Rscript {0} - - - name: Install dependencies - uses: r-lib/actions/setup-r-dependencies@a51a8012b0aab7c32ef9d19bf54da93f3254335e # v2.12.0 - - - name: Document package - run: roxygen2::roxygenise() - shell: Rscript {0} - - - name: Commit and push changes - id: commit_step_document - uses: stefanzweifel/git-auto-commit-action@4a55954c782fc1ea30b9056cd3e7a2b40ca8887d # v7.2.0 - with: - commit_message: "Document package (GHA)" - commit_user_name: ${{ github.actor }} - commit_user_email: ${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com - continue-on-error: true - - - name: Create Pull Request - if: ${{ steps.commit_step_document.outputs.changes_detected == 'true' && steps.commit_step_document.outcome == 'failure' }} - uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1 - with: - token: ${{ secrets.PHS_ACTIONS_TOKEN || github.token }} - commit-message: "Document package (GHA)" - branch: auto-document-code - delete-branch: true - title: "Document package (GHA)" - labels: maintenance - base: ${{ github.event.pull_request.base.ref }} - body: | - This Pull Request was automatically created to apply documentation changes to your code. - Please review and merge this PR. - assignees: ${{ github.actor }} - reviewers: ${{ github.actor }} From 6d965870661023403daac36f1fcf7b49e5a59bf4 Mon Sep 17 00:00:00 2001 From: James McMahon Date: Tue, 14 Jul 2026 12:30:24 +0100 Subject: [PATCH 2/3] Add in packages: read permission --- .github/workflows/phs_package_checks.yaml | 4 ++-- .github/workflows/phs_pkgdown.yaml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/phs_package_checks.yaml b/.github/workflows/phs_package_checks.yaml index 585e79a..d1ae27c 100644 --- a/.github/workflows/phs_package_checks.yaml +++ b/.github/workflows/phs_package_checks.yaml @@ -124,7 +124,7 @@ jobs: contents: write packages: read secrets: inherit - + # Final job to update status finalize: needs: @@ -150,7 +150,7 @@ jobs: with: script: | const allSuccess = ${{ needs.Style.result == 'success' && - needs.Document.result == 'success' && + needs.Document.result == 'success' && needs.Build_README.result == 'success' && needs.R-CMD-check-core.result == 'success' && (needs.R-CMD-check-extended.result == 'success' || needs.R-CMD-check-extended.result == 'skipped') && diff --git a/.github/workflows/phs_pkgdown.yaml b/.github/workflows/phs_pkgdown.yaml index eb64670..dfb160a 100644 --- a/.github/workflows/phs_pkgdown.yaml +++ b/.github/workflows/phs_pkgdown.yaml @@ -7,6 +7,7 @@ name: pkgdown.yaml permissions: contents: read + packages: read jobs: pkgdown: From 99c9622ba5fc6c978548a5e95ce2e218eafd69c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?James=20Hayes=20=28n=C3=A9=20McMahon=29?= Date: Tue, 14 Jul 2026 15:24:25 +0100 Subject: [PATCH 3/3] Update package permissions in workflow YAML Changed permissions for package access in workflows. --- .github/workflows/phs_package_checks.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phs_package_checks.yaml b/.github/workflows/phs_package_checks.yaml index d1ae27c..581b62c 100644 --- a/.github/workflows/phs_package_checks.yaml +++ b/.github/workflows/phs_package_checks.yaml @@ -53,7 +53,6 @@ jobs: permissions: contents: write pull-requests: write - packages: read secrets: inherit concurrency: group: ${{ github.workflow }}-${{ github.ref }} @@ -64,6 +63,7 @@ jobs: permissions: contents: write pull-requests: write + packages: read secrets: inherit concurrency: group: ${{ github.workflow }}-${{ github.ref }}