name: API Stability Check

on:
  pull_request:
    paths:
      - 'ddtrace/tracer/**'
      - 'scripts/apiextractor/**'
      - '.github/workflows/api-check.yml'

jobs:
  check-api-stability:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0

      - name: Setup Go and development tools
        uses: ./.github/actions/setup-go
        with:
          go-version: stable
          tools-dir: ${{ github.workspace }}/_tools
          tools-bin: ${{ github.workspace }}/bin

      - name: Generate current API for ddtrace/tracer
        run: |
          go run ./scripts/apiextractor/api_extractor.go ./ddtrace/tracer > current_api.txt

      - name: Compare with committed API
        run: |
          if ! diff -u ./ddtrace/tracer/api.txt current_api.txt; then
            echo "❌ API changes detected but api.txt not updated!"
            echo "Please run 'go run ./scripts/apiextractor/api_extractor.go ./ddtrace/tracer > ./ddtrace/tracer/api.txt'"
            echo "and commit the changes."
            exit 1
          fi
          echo "✅ API is stable"
