name: Python Package

on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
      - '*'
    tags:
      - '*'

jobs:
  mypy-required:
    runs-on: 'ubuntu-latest'

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Workaround for worktree config
        run: git config --unset-all extensions.worktreeConfig || true

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"
          cache: "pip"

      - name: Install Python packages
        run: |
          python -m pip install -e .
          python -m pip install mypy

      - name: Mypy on required files
        uses: tsuyoshicho/action-mypy@v5
        with:
          github_token: ${{ secrets.github_token }}
          # Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review].
          reporter: github-check
          setup_method: nothing
          target: --config-file=pyproject.toml
          fail_on_error: true
          install_types: false

  mypy-modified:
    runs-on: 'ubuntu-latest'

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Workaround for worktree config
        run: git config --unset-all extensions.worktreeConfig || true

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"
          cache: "pip"

      - name: Install Python packages
        run: |
          python -m pip install -e .
          python -m pip install mypy

      - name: Mypy on modified files
        uses: tsuyoshicho/action-mypy@v5
        with:
          github_token: ${{ secrets.github_token }}
          # Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review].
          reporter: github-check
          setup_method: nothing
          level: warning
          fail_on_error: false
          install_types: false
  
  build:
    runs-on: ubuntu-latest
    name: Wheel

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          submodules: 'recursive'

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: "3.10"
     
      - name: Install Python packages
        run: python -m pip install --upgrade build pip

      - name: build wheel
        run: python -m build --wheel

      - name: build sdist
        run: python -m build --sdist
      
      - name: Store the packages
        uses: actions/upload-artifact@v4
        with:
          name: python-package-distributions
          path: dist

  pypi:
    runs-on: 'ubuntu-latest'
    needs:
      - mypy-required
      - build
    if: ${{ github.event_name != 'pull_request' }}

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

      - name: Publish 📦 to TestPyPI
        if: ${{ github.ref == 'refs/heads/patcher' }}
        uses: pypa/gh-action-pypi-publish@master
        with:
          password: ${{ secrets.testpypi_password }}
          repository_url: https://test.pypi.org/legacy/
      
      - name: Publish 📦 to PyPI
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        uses: pypa/gh-action-pypi-publish@master
        with:
          password: ${{ secrets.pypi_password }}
