-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add auto-fix option for automatic clang-format fixing #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| name: cpp-linter (auto-fix) | ||
| on: | ||
| pull_request: | ||
| branches: [main, master, develop] | ||
| paths: ['**.c', '**.cpp', '**.h', '**.hpp', '**.cxx', '**.hxx', '**.cc', '**.hh', '**CMakeLists.txt', 'meson.build', '**.cmake'] | ||
|
|
||
| jobs: | ||
| cpp-linter: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: write # needed for auto-fix commits | ||
| pull-requests: write | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
|
|
||
| - uses: cpp-linter/cpp-linter-action@v2 | ||
| id: linter | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| with: | ||
| style: 'file' # Use .clang-format config file | ||
| tidy-checks: '-*' # disable clang-tidy | ||
| auto-fix: 'true' # auto-apply clang-format fixes | ||
|
|
||
| - name: Fail fast?! | ||
| if: steps.linter.outputs.clang-format-checks-failed > 0 | ||
| run: exit 1 |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -209,6 +209,24 @@ inputs: | |||||||||
| Set this option to `true` to prevent Pull Request reviews from approving or requesting changes. | ||||||||||
| default: 'false' | ||||||||||
| required: false | ||||||||||
| auto-fix: | ||||||||||
| description: | | ||||||||||
| Set this option to `true` to automatically apply clang-format fixes | ||||||||||
| and commit them back to the PR branch. | ||||||||||
|
|
||||||||||
| When enabled, cpp-linter runs with ``--fix``, which applies | ||||||||||
| ``clang-format -i`` on files with style issues. After that, | ||||||||||
| a new commit is pushed to the PR branch with the formatted changes. | ||||||||||
|
|
||||||||||
| This option has no effect on clang-tidy issues. | ||||||||||
| default: 'false' | ||||||||||
| required: false | ||||||||||
| auto-fix-commit-msg: | ||||||||||
| description: | | ||||||||||
| Custom commit message for the auto-fix commit. | ||||||||||
| Only used when ``auto-fix`` is ``true``. | ||||||||||
| default: 'style: apply styling format fix' | ||||||||||
| required: false | ||||||||||
| jobs: | ||||||||||
| description: | | ||||||||||
| The number of jobs to run in parallel. | ||||||||||
|
|
@@ -458,6 +476,9 @@ runs: | |||||||||
| '--passive-reviews=${{ inputs.passive-reviews }}' | ||||||||||
| '--jobs=${{ inputs.jobs }}' | ||||||||||
| ] | ||||||||||
| if '${{ inputs.auto-fix }}' == 'true' { | ||||||||||
| $args = ($args | append ['--fix']) | ||||||||||
| } | ||||||||||
| mut uv_args = [run --no-sync --project $action_path --directory (pwd)] | ||||||||||
|
|
||||||||||
| let gh_action_debug = $env | get --optional 'ACTIONS_STEP_DEBUG' | ||||||||||
|
|
@@ -483,3 +504,28 @@ runs: | |||||||||
|
|
||||||||||
| print $"\n(ansi purple)Running cpp-linter(ansi reset)" | ||||||||||
| ^$'($env.UV_INSTALL_DIR)/uv' ...$uv_args cpp-linter ...$args | ||||||||||
|
|
||||||||||
| - name: Auto-commit clang-format fixes | ||||||||||
| if: inputs.auto-fix == 'true' || inputs.auto-fix == true | ||||||||||
| shell: nu {0} | ||||||||||
| run: | | ||||||||||
| let has_changes = (^git diff --exit-code) | complete | $in.exit_code != 0 | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this returns true, then it is because cpp-linter left some cache artifacts behind. Currently, cpp-linter makes sure the scanned source files are unchanged. Any changes are done in memory or in cache. If changes are done to the C/C++ sources, then they are undone before exiting This feature needs special handling in cpp-linter first; see cpp-linter/cpp-linter#82 for a discussion of the complexity that hasn't been addressed yet.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sorry, I didn't see you submitted cpp-linter/cpp-linter#202 |
||||||||||
| if $has_changes { | ||||||||||
| ^git config user.name 'github-actions[bot]' | ||||||||||
| ^git config user.email '41898282+github-actions[bot]@users.noreply.github.com' | ||||||||||
|
Comment on lines
+514
to
+515
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to use the env vars present:
Suggested change
Also, this can be done for a single let git_user = $"user.name=($env.GITHUB_ACTOR)"
let git_email = $"user.email=($env.GITHUB_ACTOR_ID)+($env.GITHUB_ACTOR)@users.noreply.github.com"
^git -c $git_user -c $git_email commit -m $commit_msg
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if the outside contributor can commit with their own user/email in GitHub actions.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
By using the GITHUB_ACTOR(_ID), we can at least point to the committer that caused violations to be auto-fixed with a new commit.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I do this all the time with my automated publishing scripts. See the nu script I wrote for publishing packages from cpp-linter-rs |
||||||||||
| ^git add -A | ||||||||||
| let commit_msg = if ('${{ inputs.auto-fix-commit-msg }}' | is-empty) { | ||||||||||
| 'style: apply styling format fix' | ||||||||||
| } else { | ||||||||||
| '${{ inputs.auto-fix-commit-msg }}' | ||||||||||
| } | ||||||||||
| ^git commit -m $"($commit_msg)" | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my previous comment about setting the user name and email of a single git command. |
||||||||||
| let push_result = (^git push) | complete | ||||||||||
| if $push_result.exit_code != 0 { | ||||||||||
| print $"(ansi yellow)::warning title=Auto-fix push failed::($push_result.stderr)(ansi reset)" | ||||||||||
| } else { | ||||||||||
| print $"(ansi green)Auto-fix commit pushed successfully(ansi reset)" | ||||||||||
| } | ||||||||||
| } else { | ||||||||||
| print $"(ansi green)No formatting changes to commit(ansi reset)" | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,3 +78,15 @@ The [`tidy-review`](inputs-outputs.md#tidy-review), [`format-review`](inputs-out | |
| permissions: | ||
| pull-requests: write | ||
| ``` | ||
|
|
||
| ## Auto-fix | ||
|
|
||
| The [`auto-fix`](inputs-outputs.md#auto-fix) feature requires the following permission | ||
| in addition to any other permissions needed for other features: | ||
|
|
||
| ```yaml | ||
| permissions: | ||
| contents: write # (1)! | ||
| ``` | ||
|
|
||
| 1. Needed to commit and push the formatted changes back to the PR branch. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also note that this permission is only needed by the token used in However, the - uses: actions/checkout@v6
with:
# MY_TOKEN must have `contents: write` permission.
# using default token does not trigger CI runs on `git push`
token: ${{ secrets.MY_TOKEN }}
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another problem that might arise here is when auto-fixing PR changes from a third-party fork. My previous comment was made with experience about pushing changes to the same remote. If the changes need to be pushed to a different remote, then that is a different set of permissions. |
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The self-test CI fails here because all variables are immutable by default in nushell. To declare them mutable, use
mutinstead oflet(above this line):