# Copyright 2026 The OpenXLA Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
name: Clang Tidy
permissions:
  contents: read
on:
  pull_request:
jobs:
  clang-tidy-cuda:
    runs-on: "linux-x86-n2-16"
    container:
      image: "us-docker.pkg.dev/ml-oss-artifacts-published/ml-public-container/ml-build:latest"  # zizmor: ignore[unpinned-images]
    defaults:
      run:
        shell: bash
    timeout-minutes: 30
    continue-on-error: true
    if: |
      github.event.sender.type == 'User' ||
      contains(github.event.pull_request.body, 'FORCE_TEST_ACTIONS')
    steps:
      - name: "Checking out repository"
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          # Fetch more commits to ensure we can find the merge base for clang-tidy.
          # This is better than 0 since we don't want to fetch everything as well.
          fetch-depth: '100'
          persist-credentials: false
      - name: "Update base branch"
        run: |
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          BASE_REF="${GITHUB_BASE_REF:-main}"
          # Keep this fetch unshallow so clang-tidy can find the merge base even
          # when the PR branch is more than checkout fetch-depth commits behind.
          git fetch origin "refs/heads/${BASE_REF}:refs/remotes/origin/${BASE_REF}"
      - name: Configure Workspace
      # The actual compute capability does not matter. This allows deviceless run for this pipeline.
      # The common configs ensure that bazel query and bazel build don't refetch repo mappings.
        run: |
          set -euo pipefail
          ./configure.py --backend=CUDA --cuda_compute_capabilities="9.0"
          echo "common --config cuda_nvcc" >> xla_configure.bazelrc
          echo "common --config rbe_linux_cpu" >> xla_configure.bazelrc
      - name: "Run Clang Tidy"
        env:
          TARGET_REF: "origin/${{ github.base_ref || 'main' }}"
          TAGS_TO_IGNORE: "no_oss|rocm-only|oneapi-only"
          BAZEL_CMD: "bazel"
        run: |
          set -euo pipefail
          ./build_tools/ci/run_clang_tidy.sh
