name: test

# Run on pull requests and on the master branch itself.
on:
  push:
    branches:
      - master
      - refactor
    tags:
      - "*"
  pull_request:
  workflow_dispatch:

jobs:
  build_and_test:
    runs-on: ubuntu-22.04
    steps:
      # Checkout the code.
      - uses: actions/checkout@v2

      - name: Build
        run: docker compose build --build-arg uid=`id -u` --build-arg gid=`id -g`

      - name: Lint
        run: docker compose run -e QT_QPA_PLATFORM=offscreen qgis-desktop make flake8

      - name: Test
        run: docker compose run -e QT_QPA_PLATFORM=offscreen qgis-desktop make test

      - name: Docstring coverage report
        run: docker compose run qgis-desktop make docstrings

      - name: Submit coverage to coveralls.io
        # current directory must be marked as safe, or git rev-parse --abbrev-ref HEAD will complain of dubious ownership
        # this causes coveralls to raise an exception
        run: docker compose run qgis-desktop bash -c 'git config --global --add safe.directory $(pwd) && coveralls || true'
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}

      - name: Build zip
        run: docker compose run qgis-desktop make zip

      - name: Publish package
        if: startsWith(github.event.ref, 'refs/tags')
        run: bash upload-artifact.sh
        env:
          THREEDITOOLBOX_ARTIFACTS_KEY: ${{ secrets.THREEDITOOLBOX_ARTIFACTS_KEY }}

      - name: Publish dev-only package
        if: ${{ !startsWith(github.event.ref, 'refs/tags') }}
        run: bash upload-dev-artifact.sh
        env:
          THREEDITOOLBOX_DEV_ARTIFACTS_KEY: ${{ secrets.THREEDITOOLBOX_DEV_ARTIFACTS_KEY }}
