# SPDX-FileCopyrightText: 2021 Helmholtz-Zentrum für Umweltforschung GmbH - UFZ
#
# SPDX-License-Identifier: GPL-3.0-or-later


# ===========================================================
# Hints
# ===========================================================

# $PYPI_PKG_NAME
#   The variable PYPI_PKG_NAME is used in setup.py to determine
#   how to name the tarball package. If not set the package is
#   named 'saqc'.

# $TESTPYPI_TOKEN
#   The upload token used for testpypi, set it on the gitlab
#   page and enable masking to prevent revealing

# ===========================================================
# preparation
# ===========================================================

variables:
  GIT_SUBMODULE_STRATEGY: recursive

stages:
  - compliance
  - test
  - build
  - deploy

default:
  image: python:3.10
  before_script:
    - pip install --upgrade pip
    - pip install -r requirements.txt
    - pip install -r tests/requirements.txt

# ===========================================================
# Compliance stage
# ===========================================================

# check if everything is properly formatted
black:
  stage: compliance
  script:
    - pip install black
    - black --check .

# check if imports are sorted
isort:
  stage: compliance
  script:
    - pip install isort
    - isort --check .

# Check compliance with the REUSE specification
reuse:
  stage: compliance
  image:
    name: fsfe/reuse:latest
    entrypoint: [""]
  before_script:
    - echo "Override default 'before_script'..."
  script:
    - reuse lint


# ===========================================================
# Testing stage
# ===========================================================

# make (visual) coverage in gitlab merge request diff's
coverage:
  stage: test
  allow_failure: true
  script:
    - pip install pytest-cov coverage
    - pytest --cov=saqc tests --ignore=tests/fuzzy -Werror
  after_script:
    - coverage xml
  # regex to find the coverage percentage in the job output
  coverage: '/^TOTAL.+?(\d+\%)$/'
  artifacts:
    when: always
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml


python38:
  stage: test
  image: python:3.8
  script:
    - pytest tests -Werror --junitxml=report.xml
    - python -m saqc --config docs/resources/data/config.csv --data docs/resources/data/data.csv --outfile /tmp/test.csv
  artifacts:
    when: always
    reports:
      junit: report.xml


python39:
  stage: test
  image: python:3.9
  script:
    - pytest tests -Werror --junitxml=report.xml
    - python -m saqc --config docs/resources/data/config.csv --data docs/resources/data/data.csv --outfile /tmp/test.csv
  artifacts:
    when: always
    reports:
      junit: report.xml


python310:
  stage: test
  image: python:3.10
  script:
    - pytest tests -Werror --junitxml=report.xml
    - python -m saqc --config docs/resources/data/config.csv --data docs/resources/data/data.csv --outfile /tmp/test.csv
  artifacts:
    when: always
    reports:
      junit: report.xml

python311:
  stage: test
  image: python:3.11
  script:
    - pytest tests -Werror --junitxml=report.xml
    - python -m saqc --config docs/resources/data/config.csv --data docs/resources/data/data.csv --outfile /tmp/test.csv
  artifacts:
    when: always
    reports:
      junit: report.xml

doctest:
  stage: test
  script:
    - cd docs
    - pip install -r requirements.txt
    - make doc  # needed for _api tests
    - make test


# ===========================================================
# Building stage
# ===========================================================
# check if we are able to build a wheel
# and if the import works
wheel38:
  stage: build
  image: python:3.8
  variables:
    PYPI_PKG_NAME: "saqc-dev"
  script:
    - pip install wheel
    - pip wheel .
    - pip install .
    - python -c 'import saqc; print(f"{saqc.__version__=}")'
wheel39:
  stage: build
  image: python:3.9
  variables:
    PYPI_PKG_NAME: "saqc-dev"
  script:
    - pip install wheel
    - pip wheel .
    - pip install .
    - python -c 'import saqc; print(f"{saqc.__version__=}")'
wheel310:
  stage: build
  image: python:3.10
  variables:
    PYPI_PKG_NAME: "saqc-dev"
  script:
    - pip install wheel
    - pip wheel .
    - pip install .
    - python -c 'import saqc; print(f"{saqc.__version__=}")'
wheel311:
  stage: build
  image: python:3.11
  variables:
    PYPI_PKG_NAME: "saqc-dev"
  script:
    - pip install wheel
    - pip wheel .
    - pip install .
    - python -c 'import saqc; print(f"{saqc.__version__=}")'

docs:
  stage: build
  script:
    - cd docs
    - pip install -r requirements.txt
    - make doc

# ===========================================================
# Extra Pipeline (run with a successful run of all other jobs on develop)
# ===========================================================

upload_testpypi:
  stage: deploy
  only:
    - develop
  except:
    - schedules
  variables:
    PYPI_PKG_NAME: "saqc-dev"
    TWINE_USERNAME: __token__
    TWINE_PASSWORD: $TESTPYPI_TOKEN
  script:
    - pip install build twine
    - python -m build
    - twine check --strict dist/*
    - twine upload -r testpypi dist/*
    - echo "install latest develop version with (all option flags are necessary):"
    - echo "pip install --pre -U -i https://test.pypi.org/simple/ --extra-index-url http://pypi.org/simple saqc-dev`"

# make html docu with sphinx
pages:
  stage: deploy
  only:
    - develop
  except:
    - schedules
  script:
    - cd docs/
    - pip install -r requirements.txt
    - make doc
    - cp -r _build/html ../public
  artifacts:
    paths:
      - public
