From 336de78c25a701944cee1cd0ea42bc0673d59ab7 Mon Sep 17 00:00:00 2001 From: boschmitt <7152025+boschmitt@users.noreply.github.com> Date: Thu, 28 Nov 2024 00:03:26 +0100 Subject: [PATCH] [ci] Add workflow to check c++ code formatting. Signed-off-by: boschmitt <7152025+boschmitt@users.noreply.github.com> --- .clang-format | 4 +- .github/workflows/pr_sanity_checks.yaml | 102 ++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/pr_sanity_checks.yaml diff --git a/.clang-format b/.clang-format index e695289..ae27124 100644 --- a/.clang-format +++ b/.clang-format @@ -2,10 +2,8 @@ BasedOnStyle: LLVM AlwaysBreakTemplateDeclarations: Yes IncludeCategories: - Regex: '^<' - Priority: 4 - - Regex: '^"(llvm|llvm-c|clang|clang-c|mlir|mlir-c)/' Priority: 3 - - Regex: '^"(qoda|\.\.)/' + - Regex: '^"(cudaq|\.\.)/' Priority: 2 - Regex: '.*' Priority: 1 diff --git a/.github/workflows/pr_sanity_checks.yaml b/.github/workflows/pr_sanity_checks.yaml new file mode 100644 index 0000000..ed7f946 --- /dev/null +++ b/.github/workflows/pr_sanity_checks.yaml @@ -0,0 +1,102 @@ +name: PR sanity checks + +on: + pull_request: + branches: + - main + types: [opened, synchronize, reopened] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + LLVM_VERSION: 16 + +jobs: + check-changes: + name: Check changes + runs-on: ubuntu-latest + outputs: + check-cpp: ${{ steps.filter.outputs.check-cpp }} + check-all-cpp: ${{ steps.filter.outputs.check-all-cpp }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + set-safe-directory: true + + - name: Check what needs testing + uses: dorny/paths-filter@v3 + id: filter + with: + base: ${{ github.base_ref }} + filters: | + check-all-cpp: + - '.clang-format' + check-cpp: + - '**/*.cpp' + - '**/*.h' + + check-clang-format: + name: Check C++ code formatting + if: needs.check-changes.outputs.check-cpp == 'true' || needs.check-changes.outputs.check-all-cpp == 'true' + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + set-safe-directory: true + + - name: Install clang-format + run: | + # Requirements + sudo apt-get install -y wget add-apt-repository gpg + + # Obtain VERSION_CODENAME and UBUNTU_CODENAME + source /etc/os-release + + wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc + sudo add-apt-repository "deb http://apt.llvm.org/${UBUNTU_CODENAME}/ llvm-toolchain-${UBUNTU_CODENAME}-${LLVM_VERSION} main" + sudo apt-get update && sudo apt-get install -y --no-install-recommends clang-format-${LLVM_VERSION} + + # If the `clang-format` file changes, we require the reformatting of all + # files. See https://github.com/NVIDIA/cudaqx/pull/15#discussion_r1868174072 + - name: clang-format all things + if: needs.check-changes.outputs.check-all-cpp == 'true' + run: | + git ls-files '*.cpp' '*.h' | xargs clang-format-${LLVM_VERSION} -i + + if ! git diff --exit-code; then + git diff --ignore-submodules > clang-format.patch + echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY + exit 1 + fi + echo "🟩 Clang-format found no formatting problems" >> $GITHUB_STEP_SUMMARY + exit 0 + + - name: clang-format changed files + if: needs.check-changes.outputs.check-all-cpp != 'true' + run: | + # We did a shallow clone, and thus we need to make sure to fetch the base + # commit. + git fetch --recurse-submodules=no origin ${{ github.base_ref }} + DIFF_COMMIT_SHA=$(git rev-parse origin/${{ github.base_ref }}) + + git config clangFormat.binary clang-format-$LLVM_VERSION + if ! git clang-format $DIFF_COMMIT_SHA; then + git diff --ignore-submodules > clang-format.patch + echo "🟥 Clang-format found formatting problems (check the uploaded artifact)." >> $GITHUB_STEP_SUMMARY + exit 1 + fi + echo "🟩 Clang-format found no formatting problems" >> $GITHUB_STEP_SUMMARY + exit 0 + + - name: Upload format patch + uses: actions/upload-artifact@v4 + continue-on-error: true + if: ${{ failure() }} + with: + name: clang-format-patch + path: clang-*.patch +