image: registry.git.cccv.de/uffd/docker-images/buster

variables:
  DEBIAN_FRONTEND: noninteractive 
  GIT_SUBMODULE_STRATEGY: normal
  PYTHONPATH: deps/ldapalchemy

before_script:
  - python3 -V
  - lsb_release -a
  - uname -a
  - python3 -m pylint --version
  - python3 -m coverage --version
  - echo "${CI_COMMIT_TAG}" | grep -qE "v[0-9]+[.][0-9]+[.][0-9]+.*" && export UFFD_PACKAGE_VERSION="${CI_COMMIT_TAG#v}" || export UFFD_PACKAGE_VERSION="${CI_COMMIT_SHA}"

build:pip:
  stage: build
  script:
  - pip3 install build
  - PACKAGE_VERSION="${UFFD_PACKAGE_VERSION}" python3 -m build
  artifacts:
    paths:
      - dist/*

build:apt:
  stage: build
  script:
    - apt update
    - apt-get install -y python3-all debhelper python3-pip git-buildpackage
    - export PYBUILD_INSTALL_ARGS="--install-lib=/usr/share/uffd/ --install-scripts=/usr/share/uffd/"
    - gbp dch --ignore-branch --debian-tag=v%\(version\)s
    - dpkg-buildpackage -us -uc
    - mkdir build
    - mv ../*.deb build/
  artifacts:
    paths:
      - build/*.deb
      - debian/changelog

db_migrations_updated:
  stage: test
  script:
  - FLASK_APP=uffd flask db upgrade
  - FLASK_APP=uffd flask db migrate 2>&1 | grep -q 'No changes in schema detected'

linter:
  stage: test
  script:
  - pip3 install pylint-gitlab # this force-updates jinja2 and some other packages!
  - python3 -m pylint --exit-zero --rcfile .pylintrc --output-format=pylint_gitlab.GitlabCodeClimateReporter uffd > codeclimate.json
  - python3 -m pylint --exit-zero --rcfile .pylintrc --output-format=pylint_gitlab.GitlabPagesHtmlReporter uffd > pylint.html
  - python3 -m pylint --rcfile .pylintrc --output-format=text uffd
  artifacts:
    when: always
    paths:
    - pylint.html
    reports:
      codequality: codeclimate.json

unittests:
  stage: test
  script:
  - service slapd start
  - UNITTEST_OPENLDAP=1 python3-coverage run --include 'uffd/*.py' -m pytest --junitxml=report.xml || true
  - python3-coverage report -m
  - python3-coverage html
  - python3-coverage xml
  artifacts:
    when: always
    paths:
    - htmlcov/index.html
    - htmlcov
    expose_as: 'Coverage Report'
    reports:
      cobertura: coverage.xml
      junit: report.xml
  coverage: '/^TOTAL.*\s+(\d+\%)$/'

html5validator:
  stage: test
  script:
  - rm -rf pages
  - mkdir -p pages
  - cp -r uffd/static pages/static
  - DUMP_PAGES=pages python3 -m unittest discover tests
  - sed -i -e 's/href="\/static\//href=".\/static\//g' -e 's/src="\/static\//src=".\/static\//g' pages/*.html
  - html5validator --root pages 2>&1 | tee html5validator.log
  artifacts:
    when: on_failure
    paths:
    - pages
    - html5validator.log

trans_de:
  stage: test
  script:
  - ./update_translations.sh de
  coverage: '/^TOTAL.*\s+(\d+\%)$/'

test:package:pip:
  stage: test
  script:
  - pip3 install dist/*.tar.gz
  dependencies:
  - build:pip

test:package:apt:
  stage: test
  script:
  - apt -y install ./build/*.deb
  dependencies:
  - build:apt

publish:pip:
  stage: deploy
  script:
  - pip3 install twine
  - TWINE_USERNAME="${GITLABPKGS_USERNAME}" TWINE_PASSWORD="${GITLABPKGS_PASSWORD}" python3 -m twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/*
  - TWINE_USERNAME="${PYPI_USERNAME}" TWINE_PASSWORD="${PYPI_PASSWORD}" python3 -m twine upload dist/*
  dependencies:
  - build:pip
  rules:
    - if: '$CI_COMMIT_TAG =~ /v[0-9]+[.][0-9]+[.][0-9]+.*/'

publish:apt:
  stage: deploy
  script:
  - exit 0
  dependencies:
  - build:apt
  rules:
    - if: '$CI_COMMIT_TAG =~ /v[0-9]+[.][0-9]+[.][0-9]+.*/'
