-
Notifications
You must be signed in to change notification settings - Fork 290
178 lines (156 loc) · 5.96 KB
/
Copy pathformat.yml
File metadata and controls
178 lines (156 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
name: Run clang-format on PR comment
on:
issue_comment:
types: [ created ]
permissions:
pull-requests: write
jobs:
# Job 1: Run formatting in sandbox with no write permissions
format-code:
if: >
github.event.issue.pull_request &&
contains(github.event.comment.body, '!run-clang-format') &&
(
github.event.comment.author_association == 'OWNER' ||
github.event.comment.author_association == 'MEMBER'
)
runs-on: windows-latest
permissions:
pull-requests: read
contents: read
outputs:
pr-ref: ${{ steps.pr.outputs.ref }}
pr-sha: ${{ steps.pr.outputs.sha }}
pr-repo: ${{ steps.pr.outputs.repo }}
has-changes: ${{ steps.format.outputs.has-changes }}
steps:
- name: Get PR info
id: pr
uses: actions/github-script@v7
with:
script: |
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
core.setOutput('ref', pr.data.head.ref);
core.setOutput('sha', pr.data.head.sha);
core.setOutput('repo', pr.data.head.repo.full_name);
return pr.data.head.ref;
- name: Checkout base repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Fetch PR branch
shell: cmd
run: |
git fetch origin pull/${{ github.event.issue.number }}/head:pr-head
git checkout pr-head
- name: Run clang-format
id: format
shell: pwsh
run: |
# 'run-clang-format' script needs 'VCINSTALLDIR' env (among maybe others) to be set
& cmd /c "call ./scripts/call-vcvars.cmd x64 && call ./scripts/format-changes.cmd origin/master"
if ($LASTEXITCODE -eq 0) {
"has-changes=false" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -NoNewline
} else {
"has-changes=true" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -NoNewline
}
# Don't let the exit code of 'git clang-format' cause this step to fail
exit 0
- name: Create patch
if: steps.format.outputs.has-changes == 'true'
shell: cmd
run: |
git diff > format-changes.patch
- name: Upload patch
if: steps.format.outputs.has-changes == 'true'
uses: actions/upload-artifact@v4
with:
name: format-patch
path: format-changes.patch
retention-days: 1
# Job 2: Apply changes with write permissions (runs trusted code only)
apply-changes:
needs: format-code
if: needs.format-code.outputs.has-changes == 'true'
runs-on: windows-latest
permissions:
contents: write
steps:
- name: Checkout base repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Fetch PR branch
env:
PR_NUMBER: ${{ github.event.issue.number }}
shell: cmd
run: |
git fetch origin pull/%PR_NUMBER%/head:pr-head
git checkout pr-head
- name: Download patch
uses: actions/download-artifact@v4
with:
name: format-patch
- name: Apply patch
shell: cmd
run: |
git apply format-changes.patch
del format-changes.patch
- name: Commit and push changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_REPO: ${{ needs.format-code.outputs.pr-repo }}
PR_REF: ${{ needs.format-code.outputs.pr-ref }}
shell: pwsh
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "Run clang-format on PR changes"
# Try to push to fork repo (works if maintainer edits allowed)
git push https://x-access-token:$env:GITHUB_TOKEN@github.com/$env:PR_REPO HEAD:$env:PR_REF
# Job 3: Run CI workflow
run-ci:
needs: apply-changes
uses: ./.github/workflows/ci.yml
with:
pr_number: ${{ github.event.issue.number }}
# Job 4: Comment on PR with results
comment-result:
needs: [format-code, apply-changes]
if: always() && needs.format-code.result != 'cancelled'
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Comment success
if: needs.format-code.result == 'success' && (needs.apply-changes.result == 'success' || needs.format-code.outputs.has-changes == 'false')
uses: actions/github-script@v7
with:
script: |
const hasChanges = '${{ needs.format-code.outputs.has-changes }}' === 'true';
const message = hasChanges
? '✅ clang-format completed successfully! Code formatting has been applied and committed to this PR.'
: '✅ clang-format completed successfully! No formatting changes were needed.';
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: message
});
- name: Comment failure
if: needs.format-code.result == 'failure' || needs.apply-changes.result == 'failure'
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '❌ clang-format failed. Please check the [action logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.'
});