name: 🚨🚀 Lint and upload source distribution

on:
  push:
    paths-ignore:
      - "**.md"
    branches:
      - "long_lived/**"
      - main
      - "release/**"
  release:
    types: [published]
  pull_request:
    paths-ignore:
      - "**.md"
    branches:
      - "**"

concurrency:
  # SHA is added to the end if on `main` to let all main workflows run
  group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}-${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/') || startsWith(github.ref, 'refs/heads/long_lived/')) && github.sha || '' }}
  cancel-in-progress: true

permissions:
  contents: read
  id-token: write

jobs:
  mypy:
    name: ${{ matrix.os.emoji }} ${{ matrix.check.name }} - ${{ matrix.os.name }} ${{ matrix.arch.name }} ${{ matrix.python.major_dot_minor }}
    runs-on: ${{ matrix.os.runs-on[matrix.arch.matrix] }}
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        os:
          - name: Linux
            matrix: linux
            emoji: 🐧
            runs-on:
              intel: ubuntu-latest
              arm: [linux, arm64]
          - name: macOS
            matrix: macos
            emoji: 🍎
            runs-on:
              intel: macos-12
              arm: macos-13-arm64
          - name: Windows
            matrix: windows
            emoji: 🪟
            runs-on:
              intel: windows-latest
        arch:
          - name: ARM64
            matrix: arm
          - name: Intel
            matrix: intel
        python:
          - major_dot_minor: "3.8"
          - major_dot_minor: "3.9"
          - major_dot_minor: "3.10"
          - major_dot_minor: "3.11"
          - major_dot_minor: "3.12"
        check:
          - name: mypy
            command: |
              echo "MYPY VERSION IS: $(mypy --version)"
              python manage-mypy.py build-mypy-ini
              mypy
        exclude:
          - os:
              matrix: macos
            arch:
              matrix: arm
            python:
              major_dot_minor: "3.8"
          - os:
              matrix: windows
            arch:
              matrix: arm

    steps:
      - uses: chia-network/actions/clean-workspace@main

      - name: Add safe git directory
        uses: chia-network/actions/git-mark-workspace-safe@main

      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          submodules: recursive

      - uses: chia-network/actions/setup-python@main
        with:
          python-version: ${{ matrix.python.major_dot_minor }}

      - uses: ./.github/actions/install
        with:
          python-version: ${{ matrix.python.major_dot_minor }}
          development: true

      - uses: chia-network/actions/activate-venv@main

      - name: Check with ${{ matrix.check.name }}
        run: |
          ${{ matrix.check.command }}

  check:
    name: ${{ matrix.os.emoji }} ${{ matrix.check.name }} - ${{ matrix.os.name }} ${{ matrix.arch.name }} ${{ matrix.python.major_dot_minor }}
    runs-on: ${{ matrix.os.runs-on[matrix.arch.matrix] }}
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        os:
          - name: Linux
            matrix: linux
            emoji: 🐧
            runs-on:
              intel: ubuntu-latest
              arm: [linux, arm64]
        arch:
          - name: Intel
            matrix: intel
        python:
          - major_dot_minor: "3.10"
        check:
          - name: black
            command: black --check --diff .
          - name: flake8
            command: flake8 benchmarks build_scripts chia tools *.py
          - name: pylint
            command: pylint benchmarks build_scripts chia tools *.py
          - name: generated protocol tests
            command: |
              python3 -m chia._tests.util.build_network_protocol_files
              git diff --exit-code

    steps:
      - uses: chia-network/actions/clean-workspace@main

      - name: Add safe git directory
        uses: chia-network/actions/git-mark-workspace-safe@main

      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          submodules: recursive

      - uses: chia-network/actions/setup-python@main
        with:
          python-version: ${{ matrix.python.major_dot_minor }}

      - uses: ./.github/actions/install
        with:
          python-version: ${{ matrix.python.major_dot_minor }}
          development: true

      - uses: chia-network/actions/activate-venv@main

      - name: Check with ${{ matrix.check.name }}
        run: |
          ${{ matrix.check.command }}

  upload_source_dist:
    name: Lint and Upload source distribution
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    needs:
      - mypy
      - check
    strategy:
      matrix:
        python-version: [3.8]
        os: [ubuntu-latest]

    steps:
      - name: Add safe git directory
        uses: chia-network/actions/git-mark-workspace-safe@main

      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          submodules: recursive

      - uses: Chia-Network/actions/setup-python@main
        name: Install Python
        with:
          python-version: "3.10"

      - uses: ./.github/actions/install
        with:
          python-version: ${{ matrix.python.major_dot_minor }}
          development: true

      - uses: chia-network/actions/activate-venv@main

      - name: Build source distribution
        run: |
          # TODO: let poetry do this?
          python -m build --sdist --outdir dist .

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: dist
          path: ./dist

      - name: Publish distribution to PyPI
        if: startsWith(github.ref, 'refs/tags')
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          packages-dir: dist/
          skip-existing: true
