name: Shardok Sanitizer Test

on:
  push:
    branches: ["main"]
    paths:
      - "src/main/cpp/net/eagle0/shardok/**"
      - "src/test/cpp/net/eagle0/shardok/**"
      - "src/main/protobuf/net/eagle0/shardok/**"
      - "src/main/protobuf/net/eagle0/common/**"
      - "WORKSPACE"
      - "MODULE.bazel"
      - "MODULE.bazel.lock"
      - "BUILD.bazel"
      - ".bazelrc"
      - ".github/actions/setup-bazel/**"
      - ".github/workflows/shardok_sanitizer_test.yml"
      - "ci/github_actions/ensure_bazel_installed.sh"
      - "ci/github_actions/summarize_bazel_bep.py"
  pull_request:
    paths:
      - "src/main/cpp/net/eagle0/shardok/**"
      - "src/test/cpp/net/eagle0/shardok/**"
      - "src/main/protobuf/net/eagle0/shardok/**"
      - "src/main/protobuf/net/eagle0/common/**"
      - "WORKSPACE"
      - "MODULE.bazel"
      - "MODULE.bazel.lock"
      - "BUILD.bazel"
      - ".bazelrc"
      - ".github/actions/setup-bazel/**"
      - ".github/workflows/shardok_sanitizer_test.yml"
      - "ci/github_actions/ensure_bazel_installed.sh"
      - "ci/github_actions/summarize_bazel_bep.py"

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  shardok-sanitizer-test:
    name: Shardok ${{ matrix.name }}
    runs-on: [self-hosted, bazel]
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: ASan
            config: asan
          - name: UBSan
            config: ubsan

    steps:
      - name: Ensure Git LFS available for checkout
        run: |
          COMMON_PATHS=(/opt/homebrew/bin /usr/local/bin)
          for path in "${COMMON_PATHS[@]}"; do
            if [ -d "$path" ]; then
              echo "$path" >> "$GITHUB_PATH"
              export PATH="$path:$PATH"
            fi
          done

          if command -v git-lfs >/dev/null 2>&1; then
            git-lfs version
            exit 0
          fi

          brew install git-lfs
          git-lfs version
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          persist-credentials: false
          lfs: false
      - name: Setup Bazel
        uses: ./.github/actions/setup-bazel
      - name: Run Shardok sanitizer tests
        run: bazel test --config=${{ matrix.config }} --build_event_json_file=${{ matrix.config }}.json //src/test/cpp/net/eagle0/shardok/...
      - name: Summarize Bazel build metrics
        if: always()
        run: python3 ci/github_actions/summarize_bazel_bep.py ${{ matrix.config }}.json
      - name: Archive sanitizer test results
        if: always()
        uses: actions/upload-artifact@v7
        with:
          name: shardok-${{ matrix.config }}.json
          path: ${{ matrix.config }}.json
          retention-days: 3
