-
-
Notifications
You must be signed in to change notification settings - Fork 269
204 lines (189 loc) · 7.9 KB
/
clang-format.yml
File metadata and controls
204 lines (189 loc) · 7.9 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
name: clang-format
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches:
- dev
workflow_dispatch:
inputs:
lint_mode:
description: 'Select lint mode: check_only, auto_commit, or create_pr'
required: true
default: 'check_only'
type: choice
options:
- check_only
- create_pr
- auto_commit
issue_comment:
types: [created] # Listen for new comments on issues/PRs
jobs:
clang-format-lint:
runs-on: ubuntu-latest
# Run if:
# - PR event
# - workflow_dispatch
# - issue_comment on a PR with /clang-format AND commenter is halx99
if: |
(github.event_name == 'pull_request') ||
(github.event_name == 'workflow_dispatch') ||
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request != null &&
startsWith(github.event.comment.body, '/clang-format') &&
contains(fromJSON('["halx99"]'), github.event.comment.user.login))
steps:
# Reply start message when triggered by comment
- name: Reply start message
if: github.event_name == 'issue_comment'
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.AX_BOT_TOKEN || github.token }}
issue-number: ${{ github.event.issue.number }}
body: |
👋 @${{ github.event.comment.user.login }} Command **/clang-format** received. Running code formatting, please wait...
# Get PR info when triggered by a comment
- name: Get PR info (for comment trigger)
if: github.event_name == 'issue_comment'
id: pr
uses: actions/github-script@v7
with:
github-token: ${{ secrets.AX_BOT_TOKEN || github.token }}
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.issue.number
});
core.setOutput('head_ref', pr.head.ref);
core.setOutput('head_repo', pr.head.repo.full_name);
# Determine lint_mode to auto_commit if triggered by /clang-format comment and store head commit sha
- name: Prepare clang-format lint
id: pp
shell: pwsh
run: |
if ($env:GITHUB_EVENT_NAME -eq 'issue_comment') {
$head_repo = "${{ steps.pr.outputs.head_repo }}"
$head_ref = "${{ steps.pr.outputs.head_ref }}"
}
elseif ($env:GITHUB_EVENT_NAME -eq 'pull_request') {
$head_repo = "${{ github.event.pull_request.head.repo.full_name }}"
$head_ref = "${{ github.event.pull_request.head.ref }}"
}
else {
$head_repo = "${{ github.repository }}"
$head_ref = "${{ github.head_ref || github.ref_name }}"
}
if (!$head_repo -or !$head_ref) {
Write-Error "❌ head_repo or head_ref is empty"
exit 1
}
if ($env:GITHUB_EVENT_NAME -eq 'issue_comment') {
echo "Set lint_mode=auto_commit for ${{ github.event.comment.user.login }} Command **/clang-format**"
$lint_mode = 'auto_commit'
}
else {
$lint_mode = "${{ inputs.lint_mode || 'check_only' }}"
}
echo "head_repo=$head_repo" >> ${env:GITHUB_OUTPUT}
echo "head_ref=$head_ref" >> ${env:GITHUB_OUTPUT}
echo "lint_mode=$lint_mode" >> ${env:GITHUB_OUTPUT}
Write-Host "head_repo=$head_repo, head_ref=$head_ref, lint_mode=$lint_mode"
# Checkout correct branch
- uses: actions/checkout@v5
with:
repository: ${{ steps.pp.outputs.head_repo }}
ref: ${{ steps.pp.outputs.head_ref }}
token: ${{ secrets.AX_BOT_TOKEN || github.token }}
- name: Run clang-format lint
uses: DoozyX/clang-format-lint-action@v0.20
with:
source: './axmol ./extensions ./tests ./templates'
exclude: './3rdparty ./extensions/ImGui/**/im* ./extensions/fairygui ./extensions/Live2D ./extensions/Effekseer ./extensions/scripting/lua-bindings/auto ./extensions/spine ./tests/cpp-tests/Source/Box2DTestBed/samples ./tests/fairygui-tests ./tests/live2d-tests ./extensions/**/*_generated.h'
extensions: 'h,cpp,c,mm'
clangFormatVersion: 20
inplace: True
# check_only mode
- name: Check for uncommitted changes
if: ${{ steps.pp.outputs.lint_mode == 'check_only' }}
shell: pwsh
run: |
git diff --quiet
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ clang-format check failed: Uncommitted formatting changes detected."
Write-Host "=== The following differences require formatting ==="
git --no-pager diff
Write-Host "===================================================="
exit 1
}
else {
Write-Host "✅ clang-format check passed: No formatting changes needed."
}
- name: Prepare for create pull request
if: ${{ steps.pp.outputs.lint_mode == 'create_pr' }}
id: ppr
shell: pwsh
run: |
$head_commit_sha = $(git rev-parse --short HEAD)
echo "head_commit_sha=$head_commit_sha" >> ${env:GITHUB_OUTPUT}
Write-Host "ppr: head_commit_sha=$head_commit_sha"
# create_pr mode
- name: Create pull request
if: ${{ steps.pp.outputs.lint_mode == 'create_pr' }}
id: cpr
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.AX_BOT_TOKEN || github.token }}
push-to-fork: axmol-bot/axmol
commit-message: Committing clang-format changes
signoff: false
branch: clang_format_for_${{ steps.ppr.outputs.head_commit_sha }}
delete-branch: true
title: 'Committing clang-format changes ${{ steps.ppr.outputs.head_commit_sha }}'
body: |
RT
- Auto-generated by [create-pull-request][1]
[1]: https://github.com/peter-evans/create-pull-request
labels: |
clang-format
automated pr
pinned
assignees: axmol-bot
reviewers: halx99
draft: false
- name: Check pull request outputs
if: ${{ steps.pp.outputs.lint_mode == 'create_pr' && steps.cpr.outputs.pull-request-number }}
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
# auto_commit mode (including comment trigger)
- name: Commit clang-format changes to PR source branch
if: ${{ steps.pp.outputs.lint_mode == 'auto_commit' }}
uses: EndBug/add-and-commit@v9
with:
author_name: axmol-bot
author_email: 116471739+axmol-bot@users.noreply.github.com
committer_name: GitHub Actions
committer_email: 41898282+github-actions[bot]@users.noreply.github.com
message: 'Committing clang-format changes'
new_branch: ${{ steps.pp.outputs.head_ref }}
env:
GITHUB_TOKEN: ${{ secrets.AX_BOT_TOKEN || github.token }}
# Reply finish message after auto_commit success
- name: Reply finish message
if: ${{ success() && steps.pp.outputs.lint_mode == 'auto_commit' && github.event_name == 'issue_comment' }}
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.AX_BOT_TOKEN || github.token }}
issue-number: ${{ github.event.issue.number }}
body: |
✅ Formatting completed and changes have been committed to the PR branch: `${{ steps.pp.outputs.head_ref }}`.
@${{ github.event.comment.user.login }} please refresh to see the latest code.
# Reply failure message if job fails
- name: Reply failure message
if: ${{ failure() && github.event_name == 'issue_comment' }}
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.AX_BOT_TOKEN || github.token }}
issue-number: ${{ github.event.issue.number }}
body: |
❌ Formatting failed. Please check the GitHub Actions logs for details.