From b29742f7af2502246cac12afba196e2749b488ad Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 22 Apr 2026 17:51:06 +0100 Subject: [PATCH 1/5] Suggest changes instead of automatically fixing them --- .github/workflows/format.yml | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 85a91e23..5678ecfc 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -11,7 +11,8 @@ jobs: format: runs-on: ubuntu-latest permissions: - contents: write + contents: read + pull-requests: write steps: - name: Checkout code @@ -37,12 +38,24 @@ jobs: - name: Apply C++ formatting fixes run: find cpp2rust tests -name '*.cpp' -o -name '*.h' -o -name '*.c' | xargs clang-format -i + - name: Suggest C++ formatting fixes + if: github.event_name == 'pull_request' + uses: reviewdog/action-suggester@v1 + with: + tool_name: clang-format + - name: Apply Rust lint fixes run: | cargo clippy --fix --allow-dirty --manifest-path rules/Cargo.toml --all-targets cargo +nightly clippy --fix --allow-dirty --manifest-path rule-preprocessor/Cargo.toml --all-targets cargo clippy --fix --allow-dirty --manifest-path libcc2rs/Cargo.toml --all-targets + - name: Suggest Rust lint fixes + if: github.event_name == 'pull_request' + uses: reviewdog/action-suggester@v1 + with: + tool_name: clippy + - name: Apply Rust formatting fixes run: | cargo fmt --manifest-path rules/Cargo.toml @@ -50,11 +63,11 @@ jobs: cargo fmt --manifest-path libcc2rs/Cargo.toml find tests -name '*.rs' -print0 | xargs -0 rustfmt - - name: Commit auto-fixes - if: github.ref != 'refs/heads/master' - uses: stefanzweifel/git-auto-commit-action@v5 + - name: Suggest Rust formatting fixes + if: github.event_name == 'pull_request' + uses: reviewdog/action-suggester@v1 with: - commit_message: "Automatically apply formatting and lint fixes" + tool_name: rustfmt - name: Check C++ formatting run: find cpp2rust tests -name '*.cpp' -o -name '*.h' -o -name '*.c' | xargs clang-format --dry-run --Werror From d3865b128cbf3560eb758a6af314a813bc507245 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 22 Apr 2026 17:52:16 +0100 Subject: [PATCH 2/5] Intentionally break formatting --- cpp2rust/converter/converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 359210aa..18062284 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -6,10 +6,10 @@ #include #include #include -#include #include #include +#include #include "compiler.h" #include "converter/converter_lib.h" From 8125d3d1bc094af800dc90b0fa8693f9f3c34514 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 22 Apr 2026 18:18:56 +0100 Subject: [PATCH 3/5] Split auto-fixing in 2 workflows format.yml, which runs on pull_request, does not have write access to neither base nor fork repos. To fix this, split auto-fixing in 2 steps: first format.yml publishes an artifact with the diff, then format-apply.yml fetches the artifact and commits the auto-fixes. This works because format-applt.yml runs on workflow_run, not pull_request, and has write permissions. --- .github/workflows/format-apply.yml | 42 ++++++++++++++++++++++++++++++ .github/workflows/format.yml | 39 +++++++++++++++------------ 2 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/format-apply.yml diff --git a/.github/workflows/format-apply.yml b/.github/workflows/format-apply.yml new file mode 100644 index 00000000..66e39476 --- /dev/null +++ b/.github/workflows/format-apply.yml @@ -0,0 +1,42 @@ +name: Format Apply + +on: + workflow_run: + workflows: ["Format Check"] + types: [completed] + +jobs: + apply: + runs-on: ubuntu-latest + if: github.event.workflow_run.event == 'pull_request' + permissions: + contents: read + + steps: + - name: Download diff artifact + uses: actions/download-artifact@v4 + with: + name: format-diff + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Read PR head + id: head + run: | + echo "repo=$(cat head-repo.txt)" >> "$GITHUB_OUTPUT" + echo "branch=$(cat head-branch.txt)" >> "$GITHUB_OUTPUT" + + - name: Checkout PR head + uses: actions/checkout@v6 + with: + repository: ${{ steps.head.outputs.repo }} + ref: ${{ steps.head.outputs.branch }} + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Apply diff + run: git apply $GITHUB_WORKSPACE/../format.diff + + - name: Commit and push + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "Automatically apply formatting and lint fixes" diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 5678ecfc..81a790c5 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -12,13 +12,12 @@ jobs: runs-on: ubuntu-latest permissions: contents: read - pull-requests: write steps: - name: Checkout code uses: actions/checkout@v6 - - name: Setup LLVM 22 + - name: Setup LLVM uses: ZhongRuoyu/setup-llvm@v0 with: llvm-version: 22 @@ -38,24 +37,12 @@ jobs: - name: Apply C++ formatting fixes run: find cpp2rust tests -name '*.cpp' -o -name '*.h' -o -name '*.c' | xargs clang-format -i - - name: Suggest C++ formatting fixes - if: github.event_name == 'pull_request' - uses: reviewdog/action-suggester@v1 - with: - tool_name: clang-format - - name: Apply Rust lint fixes run: | cargo clippy --fix --allow-dirty --manifest-path rules/Cargo.toml --all-targets cargo +nightly clippy --fix --allow-dirty --manifest-path rule-preprocessor/Cargo.toml --all-targets cargo clippy --fix --allow-dirty --manifest-path libcc2rs/Cargo.toml --all-targets - - name: Suggest Rust lint fixes - if: github.event_name == 'pull_request' - uses: reviewdog/action-suggester@v1 - with: - tool_name: clippy - - name: Apply Rust formatting fixes run: | cargo fmt --manifest-path rules/Cargo.toml @@ -63,11 +50,29 @@ jobs: cargo fmt --manifest-path libcc2rs/Cargo.toml find tests -name '*.rs' -print0 | xargs -0 rustfmt - - name: Suggest Rust formatting fixes + - name: Capture diff for later auto-commit + id: diff if: github.event_name == 'pull_request' - uses: reviewdog/action-suggester@v1 + run: | + git diff > /tmp/format.diff + echo "${{ github.event.pull_request.head.repo.full_name }}" > /tmp/head-repo.txt + echo "${{ github.event.pull_request.head.ref }}" > /tmp/head-branch.txt + if [ -s /tmp/format.diff ]; then + echo "has_diff=true" >> "$GITHUB_OUTPUT" + else + echo "has_diff=false" >> "$GITHUB_OUTPUT" + fi + + - name: Upload diff artifact + if: github.event_name == 'pull_request' && steps.diff.outputs.has_diff == 'true' + uses: actions/upload-artifact@v4 with: - tool_name: rustfmt + name: format-diff + path: | + /tmp/format.diff + /tmp/head-repo.txt + /tmp/head-branch.txt + retention-days: 1 - name: Check C++ formatting run: find cpp2rust tests -name '*.cpp' -o -name '*.h' -o -name '*.c' | xargs clang-format --dry-run --Werror From 9a3bb55c9708419ffa409f8bc1ca9d1081b44e73 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 22 Apr 2026 18:32:40 +0100 Subject: [PATCH 4/5] Fix paths --- .github/workflows/format-apply.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/format-apply.yml b/.github/workflows/format-apply.yml index 66e39476..88bbd7d6 100644 --- a/.github/workflows/format-apply.yml +++ b/.github/workflows/format-apply.yml @@ -19,12 +19,13 @@ jobs: name: format-diff run-id: ${{ github.event.workflow_run.id }} github-token: ${{ secrets.GITHUB_TOKEN }} + path: ${{ runner.temp }}/format-diff - name: Read PR head id: head run: | - echo "repo=$(cat head-repo.txt)" >> "$GITHUB_OUTPUT" - echo "branch=$(cat head-branch.txt)" >> "$GITHUB_OUTPUT" + echo "repo=$(cat ${{ runner.temp }}/format-diff/head-repo.txt)" >> "$GITHUB_OUTPUT" + echo "branch=$(cat ${{ runner.temp }}/format-diff/head-branch.txt)" >> "$GITHUB_OUTPUT" - name: Checkout PR head uses: actions/checkout@v6 @@ -34,7 +35,7 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} - name: Apply diff - run: git apply $GITHUB_WORKSPACE/../format.diff + run: git apply ${{ runner.temp }}/format-diff/format.diff - name: Commit and push uses: stefanzweifel/git-auto-commit-action@v5 From 90d6528ebba99681d477c5a2bdfe3645ad0e349c Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 22 Apr 2026 18:37:01 +0100 Subject: [PATCH 5/5] Add write permissions --- .github/workflows/format-apply.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/format-apply.yml b/.github/workflows/format-apply.yml index 88bbd7d6..5b7ceb71 100644 --- a/.github/workflows/format-apply.yml +++ b/.github/workflows/format-apply.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest if: github.event.workflow_run.event == 'pull_request' permissions: - contents: read + contents: write steps: - name: Download diff artifact