Skip to content

Commit dd01d24

Browse files
committed
feat: add auto-fix option to automatically format and commit clang-format fixes
Adds two new inputs: - auto-fix: runs cpp-linter --fix and auto-commits changes - fix-commit-msg: custom commit message (default: 'style: apply styling format fix') The auto-fix workflow: 1. Runs cpp-linter with --fix (applies clang-format -i) 2. If there are changed files, commits and pushes as github-actions[bot] 3. Push failures degrade to a warning Closes #439
1 parent 4f2a403 commit dd01d24

6 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: cpp-linter (auto-fix)
2+
on:
3+
pull_request:
4+
branches: [main, master, develop]
5+
paths: ['**.c', '**.cpp', '**.h', '**.hpp', '**.cxx', '**.hxx', '**.cc', '**.hh', '**CMakeLists.txt', 'meson.build', '**.cmake']
6+
7+
jobs:
8+
cpp-linter:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write # needed for auto-fix commits
12+
pull-requests: write
13+
steps:
14+
- uses: actions/checkout@v5
15+
16+
- uses: cpp-linter/cpp-linter-action@v2
17+
id: linter
18+
env:
19+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20+
with:
21+
style: 'file' # Use .clang-format config file
22+
tidy-checks: '-*' # disable clang-tidy
23+
auto-fix: 'true' # auto-apply clang-format fixes
24+
25+
- name: Fail fast?!
26+
if: steps.linter.outputs.clang-format-checks-failed > 0
27+
run: exit 1

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,37 @@ For all explanations of our available input parameters and output variables, see
6767
6868
See also our [example recipes][recipes-doc].
6969
70+
### Auto-fix clang-format issues
71+
72+
You can enable automatic fixing of clang-format issues by setting `auto-fix: 'true'`.
73+
When enabled, the action will:
74+
75+
1. Run clang-format detection as usual
76+
2. Apply `clang-format -i` to fix any files with style issues
77+
3. Commit and push the formatted changes back to the PR branch
78+
79+
```yaml
80+
steps:
81+
- uses: actions/checkout@v5
82+
- uses: cpp-linter/cpp-linter-action@v2
83+
id: linter
84+
env:
85+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
86+
with:
87+
style: 'file'
88+
auto-fix: 'true' # automatically fix format issues
89+
```
90+
91+
> [!TIP]
92+
> To prevent the auto-fix commit from triggering another CI run, include a
93+
> `[skip ci]` (or `[ci skip]`, `[no ci]`, etc.) tag in your custom commit message:
94+
>
95+
> ```yaml
96+
> with:
97+
> auto-fix: 'true'
98+
> fix-commit-msg: 'style: apply styling format fix [skip ci]'
99+
> ```
100+
70101
## Used By
71102

72103
<p align="center">

action.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ inputs:
209209
Set this option to `true` to prevent Pull Request reviews from approving or requesting changes.
210210
default: 'false'
211211
required: false
212+
auto-fix:
213+
description: |
214+
Set this option to `true` to automatically apply clang-format fixes
215+
and commit them back to the PR branch.
216+
217+
When enabled, cpp-linter runs with ``--fix``, which applies
218+
``clang-format -i`` on files with style issues. After that,
219+
a new commit is pushed to the PR branch with the formatted changes.
220+
221+
This option has no effect on clang-tidy issues.
222+
default: 'false'
223+
required: false
224+
fix-commit-msg:
225+
description: |
226+
Custom commit message for the auto-fix commit.
227+
Only used when ``auto-fix`` is ``true``.
228+
default: 'style: apply styling format fix'
229+
required: false
212230
jobs:
213231
description: |
214232
The number of jobs to run in parallel.
@@ -447,6 +465,9 @@ runs:
447465
'--passive-reviews=${{ inputs.passive-reviews }}'
448466
'--jobs=${{ inputs.jobs }}'
449467
]
468+
if '${{ inputs.auto-fix }}' == 'true' {
469+
$args = ($args | append ['--fix'])
470+
}
450471
mut uv_args = [run --no-sync --project $action_path --directory (pwd)]
451472
452473
let gh_action_debug = $env | get --optional 'ACTIONS_STEP_DEBUG'
@@ -472,3 +493,28 @@ runs:
472493
473494
print $"\n(ansi purple)Running cpp-linter(ansi reset)"
474495
^$'($env.UV_INSTALL_DIR)/uv' ...$uv_args cpp-linter ...$args
496+
497+
- name: Auto-commit clang-format fixes
498+
if: inputs.auto-fix == 'true' || inputs.auto-fix == true
499+
shell: nu {0}
500+
run: |
501+
let has_changes = (^git diff --exit-code) | complete | $in.exit_code != 0
502+
if $has_changes {
503+
^git config user.name 'github-actions[bot]'
504+
^git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
505+
^git add -A
506+
let commit_msg = if ('${{ inputs.fix-commit-msg }}' | is-empty) {
507+
'style: apply styling format fix'
508+
} else {
509+
'${{ inputs.fix-commit-msg }}'
510+
}
511+
^git commit -m $"($commit_msg)"
512+
let push_result = (^git push) | complete
513+
if $push_result.exit_code != 0 {
514+
print $"(ansi yellow)::warning title=Auto-fix push failed::($push_result.stderr)(ansi reset)"
515+
} else {
516+
print $"(ansi green)Auto-fix commit pushed successfully(ansi reset)"
517+
}
518+
} else {
519+
print $"(ansi green)No formatting changes to commit(ansi reset)"
520+
}

docs/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ inputs:
4747
passive-reviews:
4848
minimum-version: '2.12.0'
4949
required-permission: 'pull-requests: write #pull-request-reviews'
50+
auto-fix:
51+
minimum-version: '2.19.0'
52+
required-permission: 'contents: write #auto-fix'
53+
fix-commit-msg:
54+
minimum-version: '2.19.0'
5055
jobs:
5156
minimum-version: '2.11.0'
5257
cache-enable:

docs/examples/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22
[style]: ../inputs-outputs.md#style
33
[tidy-checks]: ../inputs-outputs.md#tidy-checks
44
[thread-comments]: ../inputs-outputs.md#thread-comments
5+
[auto-fix]: ../inputs-outputs.md#auto-fix
56

67
# Recipes
78

89
Here are some example workflows to get started quickly.
910

11+
=== "auto-fix clang-format"
12+
13+
``` yaml
14+
--8<-- ".github/workflows/examples/auto-fix.yml"
15+
```
16+
17+
1. See also [`auto-fix`][auto-fix]
18+
2. See also [`style`][style]
19+
3. See also [`tidy-checks`][tidy-checks]
20+
1021
=== "only clang-tidy"
1122

1223
``` yaml

docs/permissions.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,15 @@ The [`tidy-review`](inputs-outputs.md#tidy-review), [`format-review`](inputs-out
7878
permissions:
7979
pull-requests: write
8080
```
81+
82+
## Auto-fix
83+
84+
The [`auto-fix`](inputs-outputs.md#auto-fix) feature requires the following permission
85+
in addition to any other permissions needed for other features:
86+
87+
```yaml
88+
permissions:
89+
contents: write # (1)!
90+
```
91+
92+
1. Needed to commit and push the formatted changes back to the PR branch.

0 commit comments

Comments
 (0)