Skip to content

Commit 8125d3d

Browse files
committed
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.
1 parent d3865b1 commit 8125d3d

2 files changed

Lines changed: 64 additions & 17 deletions

File tree

.github/workflows/format-apply.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Format Apply
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Format Check"]
6+
types: [completed]
7+
8+
jobs:
9+
apply:
10+
runs-on: ubuntu-latest
11+
if: github.event.workflow_run.event == 'pull_request'
12+
permissions:
13+
contents: read
14+
15+
steps:
16+
- name: Download diff artifact
17+
uses: actions/download-artifact@v4
18+
with:
19+
name: format-diff
20+
run-id: ${{ github.event.workflow_run.id }}
21+
github-token: ${{ secrets.GITHUB_TOKEN }}
22+
23+
- name: Read PR head
24+
id: head
25+
run: |
26+
echo "repo=$(cat head-repo.txt)" >> "$GITHUB_OUTPUT"
27+
echo "branch=$(cat head-branch.txt)" >> "$GITHUB_OUTPUT"
28+
29+
- name: Checkout PR head
30+
uses: actions/checkout@v6
31+
with:
32+
repository: ${{ steps.head.outputs.repo }}
33+
ref: ${{ steps.head.outputs.branch }}
34+
token: ${{ secrets.GITHUB_TOKEN }}
35+
36+
- name: Apply diff
37+
run: git apply $GITHUB_WORKSPACE/../format.diff
38+
39+
- name: Commit and push
40+
uses: stefanzweifel/git-auto-commit-action@v5
41+
with:
42+
commit_message: "Automatically apply formatting and lint fixes"

.github/workflows/format.yml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ jobs:
1212
runs-on: ubuntu-latest
1313
permissions:
1414
contents: read
15-
pull-requests: write
1615

1716
steps:
1817
- name: Checkout code
1918
uses: actions/checkout@v6
2019

21-
- name: Setup LLVM 22
20+
- name: Setup LLVM
2221
uses: ZhongRuoyu/setup-llvm@v0
2322
with:
2423
llvm-version: 22
@@ -38,36 +37,42 @@ jobs:
3837
- name: Apply C++ formatting fixes
3938
run: find cpp2rust tests -name '*.cpp' -o -name '*.h' -o -name '*.c' | xargs clang-format -i
4039

41-
- name: Suggest C++ formatting fixes
42-
if: github.event_name == 'pull_request'
43-
uses: reviewdog/action-suggester@v1
44-
with:
45-
tool_name: clang-format
46-
4740
- name: Apply Rust lint fixes
4841
run: |
4942
cargo clippy --fix --allow-dirty --manifest-path rules/Cargo.toml --all-targets
5043
cargo +nightly clippy --fix --allow-dirty --manifest-path rule-preprocessor/Cargo.toml --all-targets
5144
cargo clippy --fix --allow-dirty --manifest-path libcc2rs/Cargo.toml --all-targets
5245
53-
- name: Suggest Rust lint fixes
54-
if: github.event_name == 'pull_request'
55-
uses: reviewdog/action-suggester@v1
56-
with:
57-
tool_name: clippy
58-
5946
- name: Apply Rust formatting fixes
6047
run: |
6148
cargo fmt --manifest-path rules/Cargo.toml
6249
cargo fmt --manifest-path rule-preprocessor/Cargo.toml
6350
cargo fmt --manifest-path libcc2rs/Cargo.toml
6451
find tests -name '*.rs' -print0 | xargs -0 rustfmt
6552
66-
- name: Suggest Rust formatting fixes
53+
- name: Capture diff for later auto-commit
54+
id: diff
6755
if: github.event_name == 'pull_request'
68-
uses: reviewdog/action-suggester@v1
56+
run: |
57+
git diff > /tmp/format.diff
58+
echo "${{ github.event.pull_request.head.repo.full_name }}" > /tmp/head-repo.txt
59+
echo "${{ github.event.pull_request.head.ref }}" > /tmp/head-branch.txt
60+
if [ -s /tmp/format.diff ]; then
61+
echo "has_diff=true" >> "$GITHUB_OUTPUT"
62+
else
63+
echo "has_diff=false" >> "$GITHUB_OUTPUT"
64+
fi
65+
66+
- name: Upload diff artifact
67+
if: github.event_name == 'pull_request' && steps.diff.outputs.has_diff == 'true'
68+
uses: actions/upload-artifact@v4
6969
with:
70-
tool_name: rustfmt
70+
name: format-diff
71+
path: |
72+
/tmp/format.diff
73+
/tmp/head-repo.txt
74+
/tmp/head-branch.txt
75+
retention-days: 1
7176

7277
- name: Check C++ formatting
7378
run: find cpp2rust tests -name '*.cpp' -o -name '*.h' -o -name '*.c' | xargs clang-format --dry-run --Werror

0 commit comments

Comments
 (0)