name: Python package

on:
  # The following config trigger the workflow upon commits but not tags creation
  push:
    branches:
      - '**'
    tags-ignore:
      - '**'
  pull_request:
  # Ensure this workflow's jobs can be triggered from other workflows, e.g. publish
  workflow_call:

jobs:
  build:

    runs-on: ubuntu-20.04
    strategy:
      max-parallel: 7
      matrix:
        python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

    steps:
    - uses: actions/checkout@v3

    - name: Extract repository name which is also our primary package name
      run: echo "REPO_NAME=$(basename $GITHUB_REPOSITORY)" >> $GITHUB_ENV

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
        cache: 'pip' # caching pip dependencies will speed up subsequent runs
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -e .[tests]
    - 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: Setup upterm session
#      uses: lhotari/action-upterm@v1
#      with:
#        ## limits ssh access and adds the ssh public key for the user which triggered the workflow
#        limit-access-to-actor: true
#        ## limits ssh access and adds the ssh public keys of the listed GitHub users
#        limit-access-to-users: dvershinin
    - name: Test with pytest
      run: |
        pytest -v -n auto --cov=$REPO_NAME --cov-report=xml --junitxml=junit/test-results-${{ matrix.python-version }}.xml
      env:
        LASTVERSION_GITHUB_API_TOKEN: ${{ secrets.LASTVERSION_GITHUB_API_TOKEN }}

    - name: Upload pytest test results
      uses: actions/upload-artifact@v3
      with:
        name: pytest-results-${{ matrix.python-version }}
        path: junit/test-results-${{ matrix.python-version }}.xml
      # Use always() to always run this step to publish test results when there are test failures
      if: ${{ always() }}

    - name: Upload coverage to Codacy
      run: |
        bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r coverage.xml
      env:
        CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
    - name: Update .spec files
      run: |
        # ensure changelog is generated
        echo "%packager Dick Solomon <info@example.com>" >> ~/.rpmmacros
        cd tests
        lastversion brotli.spec
        lastversion nginx-module-immutable.spec
        if lastversion libmozjpeg.spec; then
          exit 1
        fi
        rm -rf libmozjpeg.spec
      env:
        LASTVERSION_GITHUB_API_TOKEN: ${{ secrets.LASTVERSION_GITHUB_API_TOKEN }}
    - name: 'Upload brotli.spec artifact'
      uses: actions/upload-artifact@v3
      with:
        name: brotli.spec
        path: tests/brotli.spec
    - name: 'Upload nginx-module-immutable.spec artifact'
      uses: actions/upload-artifact@v3
      with:
        name: nginx-module-immutable.spec
        path: tests/nginx-module-immutable.spec
  build-brotli-rpm:
    needs: build
    runs-on: ubuntu-20.04
    container: getpagespeed/rpmbuilder:centos-8
    steps:
    - name: Download brotli.spec artifact
      uses: actions/download-artifact@v3
      with:
        name: brotli.spec
    - name: Build updated spec
      run: |
        mkdir -p /github/home/rpmbuild/{SOURCES,SPECS}/
        mv *.spec /sources/
        build
  build-nginx-module-immutable-rpm:
    needs: build
    runs-on: ubuntu-20.04
    container: getpagespeed/rpmbuilder:centos-8
    steps:
    - name: Download nginx-module-immutable.spec artifact
      uses: actions/download-artifact@v3
      with:
        name: nginx-module-immutable.spec
    - name: Build updated spec
      run: |
        mkdir -p /github/home/rpmbuild/{SOURCES,SPECS}/
        mv *.spec /sources/
        yum -y install nginx-devel
        build