From 2ecb162dfb288fb9962bc2b4ca3f72580373e4d5 Mon Sep 17 00:00:00 2001 From: Riccardo Mori Date: Fri, 7 Apr 2023 16:19:57 +0200 Subject: [PATCH] Add code formatter in the CI pipeline (#7372) * Create formatter CI This step in the CI pipeline is responsible for enforcing the coding style to the cpp code. * Update formatter.yml * Give writing permission to the code formatter action * Add the workflow-dispatch event trigger * Add first sketch of coding style * Run build CI only after the formatter * Disable include sorting on the formatter * Fix formatter CI * Fix code formatter in pull requests * Fix code formatter CI Commit the changes made by the formatter only when pushing to a remote branch. When dealing with pull requests the CI fails if the code is not properly formatted and does not commit the changes * Remove formatter CI from PR * Enable Build CI in PR --- .clang-format | 7 +++++++ .github/workflows/build.yml | 9 ++++++--- .github/workflows/formatter.yml | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 .clang-format create mode 100644 .github/workflows/formatter.yml diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..d1f0fa4993 --- /dev/null +++ b/.clang-format @@ -0,0 +1,7 @@ +--- +BasedOnStyle: Google +--- +Language: Cpp +IndentWidth: 2 +SortIncludes: Never +--- diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b160dd3766..3d43e1ae9b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,9 +1,12 @@ name: Build on: - push: - branches: [ "dev" ] pull_request: - branches: [ "dev" ] + branches: [dev] + workflow_run: + workflows: [Formatter] + types: + - completed + branches: [dev] workflow_dispatch: jobs: build: diff --git a/.github/workflows/formatter.yml b/.github/workflows/formatter.yml new file mode 100644 index 0000000000..8b06e9df32 --- /dev/null +++ b/.github/workflows/formatter.yml @@ -0,0 +1,28 @@ +# This workflow uses clang-format to force the right coding style. +name: Formatter + +on: + push: + branches: [ "dev" ] + workflow_dispatch: +jobs: + formatter: + runs-on: ubuntu-latest + + permissions: + # Give the default GITHUB_TOKEN write permission to commit and push the changed files back to the repository. + contents: write + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Run formatter + run: find ./src ./include/ -type f \( -name '*.cpp' -o -name '*.h' \) -exec clang-format --style=file -i '{}' \; + + # Commit the changes when pushing to a remote branch + - name: Commit + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_user_name: clang-format-bot + commit_message: 'Automated commit of clang-format CI changes.'