# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application

on:
  push:
    branches: [ dev, main, release ]
    tags: v*
  pull_request:
    branches: [ dev, main, release ]

permissions:
  contents: read

jobs:
  build:

    runs-on: ubuntu-latest
    timeout-minutes: 15

    steps:
    - uses: actions/checkout@v3

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

    - name: Install dependencies
      run: |
        sudo apt-get install -y libopenslide-dev libopenjp2-7
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    - name: Echo OpenSlide Version
      run: dpkg -s libopenslide-dev | grep Version

    - name: Echo OpenJPEG Version
      run: opj_compress -h | head

    - name: List PIP Package Versions
      run: |
        pip list

    - name: Lint with flake8
      run: |
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

    - name: Test with pytest
      run: |
        pytest
    - uses: actions/upload-artifact@v2
      with:
        name: coverage
        path: coverage.xml

    - name: Report test coverage to Codecov
      uses: codecov/codecov-action@v2
      with:
        files: coverage.xml
        fail_ci_if_error: false
        verbose: true

    - name: Report test coverage to DeepSource
      uses: deepsourcelabs/test-coverage-action@master
      with:
        key: python
        coverage-file: coverage.xml
        dsn: ${{ secrets.DEEPSOURCE_DSN }}
        fail-ci-on-error: false

  release:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    needs: build

    steps:
    - uses: actions/checkout@v3

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

    - name: Install dependencies
      run: |
        sudo apt-get install -y libopenslide-dev libopenjp2-7
        python -m pip install --upgrade pip
        pip install -r requirements.txt
        pip install build

    - name: Build package
      run: python -m build

    - name: Publish package to Test PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        user: __token__
        password: ${{ secrets.TEST_PYPI_API_TOKEN }}
        repository_url: https://test.pypi.org/legacy/
        skip_existing: true

    - name: Publish package to PyPI
      if: startsWith(github.ref, 'refs/tags/v')  # Only run on tags starting with v
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        user: __token__
        password: ${{ secrets.PYPI_API_TOKEN }}
