name: Code style check

on:
  pull_request:
    branches: [ master ]
    paths:  # Should stay in sync with tools/unix/clang-format.sh
      - '.github/workflows/code-style-check.yaml'
      - 'android/app/src/**.java'
      - 'android/sdk/src/**.java'
      - 'android/sdk/src/main/cpp/**.[ch]pp'
      - 'dev_sandbox/**.[ch]pp'
      - 'generator/**.[ch]pp'
      - 'iphone/**.[ch]pp'
      - 'iphone/**.[hm]'
      - 'iphone/**.mm'
      - 'libs/**.[ch]pp'
      - 'libs/**.[hm]'
      - '!libs/indexer/drules_struct.pb.h'
      - 'libs/**.mm'
      - 'qt/**.[ch]pp'
      - 'qt/**.h'
      - 'tools/**.[ch]pp'
      - '.clang-format'
      - '.clang-format-ignore'

jobs:
  code-style-check:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - name: Install clang-format
        run: |
          sudo apt purge -y clang-format-18  # Remove default old version of clang-format
          wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
          echo 'deb http://apt.llvm.org/noble/ llvm-toolchain-noble-21 main' | sudo tee /etc/apt/sources.list.d/llvm-toolchain-noble-21.list
          sudo apt-get update
          sudo apt-get install -y clang-format-21
          sudo update-alternatives --force --install /usr/bin/clang-format clang-format /usr/bin/clang-format-21 10
          sudo update-alternatives --force --install /usr/bin/git-clang-format git-clang-format /usr/bin/git-clang-format-21 10
          clang-format --version

      - name: Checkout sources
        uses: actions/checkout@v6

      - name: Run clang-format
        run: tools/unix/clang-format.sh

      - name: Post clang-format comments
        if: failure()
        uses: reviewdog/action-suggester@aa38384ceb608d00f84b4690cacc83a5aba307ff # v1.24.0
        with:
          fail_level: error
          tool_name: clang-format
        env:
          GITHUB_TOKEN: ${{ secrets.REVIEWDOG_TOKEN }}
