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

jobs:
  upload_source_dist:
    name: Lint and Upload source distribution
    runs-on: [ubuntu-latest]
    timeout-minutes: 30

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

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

    - name: Test for secrets access
      id: check_secrets
      shell: bash
      run: |
        unset HAS_SECRET
        if [ -n "$SECRET" ]; then HAS_SECRET='true' ; fi
        echo HAS_SECRET=${HAS_SECRET} >> $GITHUB_OUTPUT
      env:
        SECRET: "${{ secrets.test_pypi_password }}"

    - uses: ./.github/actions/install
      with:
        python-version: '3.8'
        development: true

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

    - name: Lint source with black
      run: |
        black --check --diff .

    - name: Lint source with flake8
      run: |
        flake8 benchmarks build_scripts chia tests tools *.py

    - name: Lint source with mypy
      run: |
        echo "MYPY VERSION IS: $(mypy --version)"
        mypy

    - name: Lint source with pylint
      run: |
        pylint benchmarks build_scripts chia tests tools *.py

    - name: Build source distribution
      run: |
        python -m build --sdist --outdir dist .

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

    - name: Publish distribution to PyPI
      if: steps.check_secrets.outputs.HAS_SECRET && startsWith(github.ref, 'refs/tags')
      env:
        TWINE_USERNAME: __token__
        TWINE_NON_INTERACTIVE: 1
        TWINE_PASSWORD: ${{ secrets.pypi_password }}
      run: twine upload --non-interactive --skip-existing --verbose 'dist/*'
