name: CI jit coverage

on:
  push:
    branches: [main]
  pull_request:
    paths:
      - ".github/workflows/ci-jit-coverage.yml"
      - ".github/run-jit-coverage-as-lit-studios.py"
      - ".github/load-jit-coverage-report.py"
      - "examples/coverage/*"
  workflow_dispatch: {}
  schedule:
    - cron: "0 0 * * *" # every midnight

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

defaults:
  run:
    shell: bash

jobs:
  launcher-jit-coverage:
    runs-on: "ubuntu-22.04"
    #timeout-minutes: 55
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-python@v6
        with:
          python-version: "3.10"

      - name: Build package
        run: |
          pip install -U build
          python -m build --sdist --wheel --outdir dist/
          ls -l dist/

      - name: Run thunder.jit coverage
        env:
          LIGHTNING_USER_ID: ${{ secrets.LIGHTNING_USER_ID }}
          LIGHTNING_API_KEY: ${{ secrets.LIGHTNING_API_KEY }}
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
        run: |
          pip install lightning_sdk tqdm -U -q
          python .github/run-jit-coverage-as-lit-studios.py ${{ github.run_id }}

      - name: Load report
        if: always()
        id: load-report
        run: |
          report=$(python .github/load-jit-coverage-report.py)
          echo "REPORT<<EOF" >> $GITHUB_ENV
          echo "$report" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV

      - uses: act10ns/slack@v2
        # if: always() && github.event_name != 'pull_request'
        with:
          webhook-url: ${{ secrets.SLACK_WEBHOOK_URL }}
          status: ${{ job.status }}
          message: |
            *thunder.jit coverage CI* - [${{ job.status }}]
            ${{ env.REPORT }}
            ref: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
