name: PyPI
on:
  push:
    branches:
      - main
  pull_request:
    branches: [main]
  release:
    types: [published]

# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
  # The concurrency group contains the workflow name and the branch name for pull requests
  # or the commit hash for any other events.
  group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
  cancel-in-progress: true

permissions: {}

jobs:
  check_changes:
    runs-on: ubuntu-latest
    outputs:
      should_run: ${{ steps.set_should_run.outputs.should_run }}
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false
      - uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36  # v3.0.2
        id: filter
        with:
          filters: |
            any_changed:
              - '.github/workflows/pypi.yml'
              - 'pyproject.toml'
              - 'setup.py'
              - 'pytensor/_version.py'
              - 'pytensor/scan_perform.pyx'
              - 'pytensor/scan_perform_ext.py'
      - name: Set should_run output
        id: set_should_run
        run: |
          if [[ "${{ github.event_name == 'release' || 
                github.ref == 'refs/heads/main' ||
                (
                  github.event_name == 'pull_request'
                  && steps.filter.outputs.any_changed == 'true'
                )
              }}" == "true" ]]; then
            echo "should_run=true" >> $GITHUB_OUTPUT
          else
            echo "should_run=false" >> $GITHUB_OUTPUT
          fi

  # The job to build precompiled pypi wheels.
  make_sdist:
    name: Make SDist
    needs: check_changes
    # Run if it's a release or if relevant files changed on main
    if: |
      needs.check_changes.outputs.should_run == 'true'
    runs-on: ubuntu-latest
    permissions:
      # write id-token and attestations are required to attest build provenance
      id-token: write
      attestations: write
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          fetch-depth: 0
          submodules: true
          persist-credentials: false

      - name: Build SDist
        run: pipx run build --sdist

      - name: Attest GitHub build provenance
        uses: actions/attest-build-provenance@977bb373ede98d70efdf65b84cb5f73e068dcc2a # v3.0.0
        # Don't attest from forks
        if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
        with:
          subject-path: dist/*.tar.gz

      - uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: sdist
          path: dist/*.tar.gz

  run_checks:
    name: Build & inspect our package.
    needs: check_changes
    # Run if it's a release or if relevant files changed on main
    if: |
      needs.check_changes.outputs.should_run == 'true'
    # Note: the resulting builds are not actually published.
    # This is purely for additional testing and diagnostic purposes.
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          fetch-depth: 0
          persist-credentials: false
      - uses: hynek/build-and-inspect-python-package@efb823f52190ad02594531168b7a2d5790e66516 # v2.14.0

  build_wheels:
    name: Build wheels for ${{ matrix.platform }}
    needs: check_changes
    # Run if it's a release or if relevant files changed on main
    if: |
      needs.check_changes.outputs.should_run == 'true'
    runs-on: ${{ matrix.platform }}
    permissions:
      # write id-token and attestations are required to attest build provenance
      id-token: write
      attestations: write
    strategy:
      matrix:
        platform:
          - macos-latest
          - windows-latest
          - ubuntu-latest
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Build wheels
        uses: pypa/cibuildwheel@9c00cb4f6b517705a3794b22395aedc36257242c # v3.2.1

      - name: Attest GitHub build provenance
        uses: actions/attest-build-provenance@977bb373ede98d70efdf65b84cb5f73e068dcc2a # v3.0.0
        # Don't attest from forks
        if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
        with:
          subject-path: ./wheelhouse/*.whl

      - uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: wheels-${{ matrix.platform }}
          path: ./wheelhouse/*.whl

  build_universal_wheel:
    name: Build universal wheel for Pyodide
    needs: check_changes
    # Run if it's a release or if relevant files changed on main
    if: |
      needs.check_changes.outputs.should_run == 'true'
    runs-on: ubuntu-latest
    permissions:
      # write id-token and attestations are required to attest build provenance
      id-token: write
      attestations: write
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Set up Python
        uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: pip install --upgrade setuptools numpy versioneer wheel

      - name: Build universal wheel
        run: |
          PYODIDE=1 python setup.py bdist_wheel --universal

      - name: Attest GitHub build provenance
        uses: actions/attest-build-provenance@977bb373ede98d70efdf65b84cb5f73e068dcc2a # v3.0.0
        # Don't attest from forks
        if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
        with:
          subject-path: dist/*.whl

      - uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: universal_wheel
          path: dist/*.whl

  check_dist:
    name: Check dist
    needs: [check_changes, make_sdist, build_wheels]
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
        with:
          name: sdist
          path: dist

      - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
        with:
          pattern: wheels-*
          path: dist
          merge-multiple: true

      - name: Set up Python
        uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
        with:
          python-version: '3.11'

      - name: Check SDist
        run: |
          mkdir -p test-sdist
          cd test-sdist
          python -m venv venv-sdist
          venv-sdist/bin/python -m pip install ../dist/pytensor-*.tar.gz
          # check import
          venv-sdist/bin/python -c "import pytensor;print(pytensor.__version__)"
          # check import cython module
          venv-sdist/bin/python -c 'from pytensor.scan import scan_perform; print(scan_perform.get_version())'

      - run: pipx run twine check --strict dist/*

  upload_pypi:
    name: Upload to PyPI on release
    # Use the `release` GitHub environment to protect the Trusted Publishing (OIDC)
    # workflow by requiring signoff from a maintainer.
    environment: release
    permissions:
      # write id-token is required for trusted publishing (OIDC)
      id-token: write
    needs: [check_dist]
    runs-on: ubuntu-latest
    # Don't publish from forks
    if: github.repository_owner == 'pymc-devs' && github.event_name == 'release' && github.event.action == 'published'
    steps:
      - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
        with:
          name: sdist
          path: dist

      - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
        with:
          pattern: wheels-*
          path: dist
          merge-multiple: true

      - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
        with:
          name: universal_wheel
          path: dist

      - uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0
        # Implicitly attests that the packages were uploaded in the context of this workflow.
