name: python-coverage

on:
  push:
    branches: [ "master" ]
    paths:
      - '**.py'
      - '**.cpp'
      - '**.cu'
      - '**.h'
      - '**.hpp'
      - '**.cc'
      - 'CMakeLists.txt'
      - '**/*.cmake'
      - '**/*.mk'
      - 'Makefile'
      - '!src/flag_gems/experimental_ops/**'
  pull_request:
    branches: [ "master" ]
    paths:
      - '**.py'
      - '**.cpp'
      - '**.cu'
      - '**.h'
      - '**.hpp'
      - '**.cc'
      - 'CMakeLists.txt'
      - '**/*.cmake'
      - '**/*.mk'
      - 'Makefile'
      - '!src/flag_gems/experimental_ops/**'

jobs:
  blas-op-test:
    concurrency:
      group: blas-op-test-${{ github.event.pull_request.number || github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/blas-op-test.yaml

  model-test:
    concurrency:
      group: model-test-${{ github.event.pull_request.number || github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/model-test.yaml

  op-test-quick-cpu:
    concurrency:
      group: op-test-quick-cpu-${{ github.event.pull_request.number || github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/op-test-quick-cpu.yaml

  op-utils-test:
    concurrency:
      group: op-utils-test-${{ github.event.pull_request.number || github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/op-utils-test.yaml

  pointwise-op-test:
    concurrency:
      group: pointwise-op-test-${{ github.event.pull_request.number || github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/pointwise-op-test.yaml

  reduction-op-test:
    concurrency:
      group: reduction-op-test-${{ github.event.pull_request.number || github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/reduction-op-test.yaml

  special-op-test:
    concurrency:
      group: special-op-test-${{ github.event.pull_request.number || github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/special-op-test.yaml

  coverage-test:
    concurrency:
      group: coverage-test-${{ github.event.pull_request.number || github.ref }}
      cancel-in-progress: true
    runs-on: cpu
    needs: [blas-op-test, model-test, op-test-quick-cpu, op-utils-test, pointwise-op-test, reduction-op-test, special-op-test]
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Get python coverage
        shell: bash
        run: |
          echo "current dir: $(pwd)"
          git config --global --add safe.directory ../FlagGems
          if [ "${{ github.event_name }}" == "pull_request" ]; then
            echo "This is a pull request event. PR number is ${{ github.event.pull_request.number }}"
            PR_ID=${{ github.event.pull_request.number }}
          elif [ "${{ github.event_name }}" == "push" ]; then
            PR_NUMBER=$(git log -1 --pretty=format:'%s' | grep -oE '#[0-9]+' | grep -oE '[0-9]+')
            echo "This is a push event. The relate PR number is ${PR_NUMBER}"
            PR_ID=${PR_NUMBER}
          fi
          bash tools/code_coverage/coverage.sh ${PR_ID}

      - name: Report python coverage
        shell: bash
        run: |
          git config --global --add safe.directory ../FlagGems
          if [ "${{ github.event_name }}" == "pull_request" ]; then
            echo "This is a pull request event. PR number is ${{ github.event.pull_request.number }}"
            PR_ID=${{ github.event.pull_request.number }}
          elif [ "${{ github.event_name }}" == "push" ]; then
            PR_NUMBER=$(git log -1 --pretty=format:'%s' | grep -oE '#[0-9]+' | grep -oE '[0-9]+')
            echo "This is a push event. The relate PR number is ${PR_NUMBER}"
            PR_ID=${PR_NUMBER}
          fi
          bash tools/code_coverage/report_coverage.sh ${PR_ID}