name: Smokeshow

on:
  workflow_run:
    workflows: [Test]
    types: [completed]

permissions:
  statuses: write

env:
  UV_SYSTEM_PYTHON: 1

jobs:
  smokeshow:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest

    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - uses: actions/checkout@v5
      - uses: actions/setup-python@v6
        with:
          python-version: '3.9'
      - name: Setup uv
        uses: astral-sh/setup-uv@v7
        with:
          version: "0.4.15"
          enable-cache: true
          cache-dependency-glob: |
            requirements**.txt
            pyproject.toml
      - run: uv pip install -r requirements-github-actions.txt
      - uses: actions/download-artifact@v6
        with:
          name: coverage-html
          path: htmlcov
          github-token: ${{ secrets.GITHUB_TOKEN }}
          run-id: ${{ github.event.workflow_run.id }}
      # Try 5 times to upload coverage to smokeshow
      - name: Upload coverage to Smokeshow
        run: |
          for i in 1 2 3 4 5; do
            if smokeshow upload htmlcov; then
                echo "Smokeshow upload success!"
                break
            fi
            echo "Smokeshow upload error, sleep 1 sec and try again."
            sleep 1
          done
        env:
          SMOKESHOW_GITHUB_STATUS_DESCRIPTION: Coverage {coverage-percentage}
          SMOKESHOW_GITHUB_COVERAGE_THRESHOLD: 100
          SMOKESHOW_GITHUB_CONTEXT: coverage
          SMOKESHOW_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SMOKESHOW_GITHUB_PR_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
          SMOKESHOW_AUTH_KEY: ${{ secrets.SMOKESHOW_AUTH_KEY }}
