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

variables:
  DEBIAN_FRONTEND: noninteractive
  GIT_SUBMODULE_STRATEGY: normal
  PYTHONPATH: deps/ldapalchemy
  APT_API_URL: https://packages.cccv.de
  APT_REPO: uffd
  PYLINT_PIN: pylint~=2.10.0

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 PACKAGE_VERSION="${CI_COMMIT_TAG#v}" || export PACKAGE_VERSION="${CI_COMMIT_SHA}"

.build:
  stage: build

build:pip:
  extends: .build
  script:
  - PACKAGE_VERSION="${PACKAGE_VERSION}" python3 -m build
  artifacts:
    paths:
      - dist/*

build:apt:
  extends: .build
  script:
  - ./debian/create_changelog.py python3-ldapserver > debian/changelog
  - dpkg-buildpackage -us -uc
  - mv ../*.deb ./
  - dpkg-deb -I *.deb
  - dpkg-deb -c *.deb
  artifacts:
    paths:
    - ./*.deb

build:docs:
  extends: .build
  script:
  - pip3 install Sphinx sphinx-rtd-theme sphinx-multiversion
  - sphinx-build docs docs/_build/html
  artifacts:
    paths:
    - docs/_build/html/index.html
    - docs/_build/html
    expose_as: 'Documentation'

linter:buster:
  image: registry.git.cccv.de/uffd/docker-images/buster
  stage: test
  script:
  - pip3 install $PYLINT_PIN pylint-gitlab
  - python3 -m pylint --exit-zero --rcfile .pylintrc --output-format=pylint_gitlab.GitlabCodeClimateReporter ldapserver > codeclimate.json
  - python3 -m pylint --exit-zero --rcfile .pylintrc --output-format=pylint_gitlab.GitlabPagesHtmlReporter ldapserver > pylint.html
  - python3 -m pylint --rcfile .pylintrc --output-format=text ldapserver
  artifacts:
    when: always
    paths:
    - pylint.html
    reports:
      codequality: codeclimate.json

linter:bullseye:
  image: registry.git.cccv.de/uffd/docker-images/bullseye
  stage: test
  script:
  - pip3 install $PYLINT_PIN pylint-gitlab
  - python3 -m pylint --exit-zero --rcfile .pylintrc --output-format=pylint_gitlab.GitlabCodeClimateReporter ldapserver > codeclimate.json
  - python3 -m pylint --exit-zero --rcfile .pylintrc --output-format=pylint_gitlab.GitlabPagesHtmlReporter ldapserver > pylint.html
  - python3 -m pylint --rcfile .pylintrc --output-format=text ldapserver
  artifacts:
    when: always
    paths:
    - pylint.html
    reports:
      codequality: codeclimate.json

unittests:buster:
  image: registry.git.cccv.de/uffd/docker-images/buster
  stage: test
  script:
  - python3-coverage run --include 'ldapserver/*.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+\%)$/'

unittests:bullseye:
  image: registry.git.cccv.de/uffd/docker-images/bullseye
  stage: test
  script:
  - python3-coverage run --include 'ldapserver/*.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+\%)$/'

test:package:pip:buster:
  image: registry.git.cccv.de/uffd/docker-images/buster
  stage: test
  script:
  - pip3 install dist/*.tar.gz
  dependencies:
  - build:pip

test:package:pip:bullseye:
  image: registry.git.cccv.de/uffd/docker-images/bullseye
  stage: test
  script:
  - pip3 install dist/*.tar.gz
  dependencies:
  - build:pip

test:package:apt:buster:
  image: registry.git.cccv.de/uffd/docker-images/buster
  stage: test
  script:
  - apt -y install ./*.deb
  dependencies:
  - build:apt

test:package:apt:bullseye:
  image: registry.git.cccv.de/uffd/docker-images/bullseye
  stage: test
  script:
  - apt -y install ./*.deb
  dependencies:
  - build:apt

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

publish:pip:
  extends: .publish
  script:
  - 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

publish:apt:
  extends: .publish
  script:
  - export DEBPATH="$(echo *.deb)"
  - echo Upload deb file, add it to repo and clean up upload
  - curl --user "${APTLY_API_USER}:${APTLY_API_PW}" -X POST -F file=@"$DEBPATH" "${APT_API_URL}/api/files/${APT_REPO}-ci-upload-${CI_JOB_ID}"
  - curl --user "${APTLY_API_USER}:${APTLY_API_PW}" -X POST "${APT_API_URL}/api/repos/${APT_REPO}/file/${APT_REPO}-ci-upload-${CI_JOB_ID}"
  - curl --user "${APTLY_API_USER}:${APTLY_API_PW}" -X DELETE "${APT_API_URL}/api/files/${APT_REPO}-ci-upload-${CI_JOB_ID}"
  - echo Update published repo for all distros
  - 'curl --user "${APTLY_API_USER}:${APTLY_API_PW}" -X PUT -H "Content-Type: application/json" --data "{ }" "${APT_API_URL}/api/publish/uffd/buster"'
  - 'curl --user "${APTLY_API_USER}:${APTLY_API_PW}" -X PUT -H "Content-Type: application/json" --data "{ }" "${APT_API_URL}/api/publish/uffd/bullseye"'
  dependencies:
  - build:apt

pages:
  stage: deploy
  script:
  - pip3 install Sphinx sphinx-rtd-theme sphinx-multiversion
  - sphinx-multiversion docs public
  - printf '/ /master 302\n/python-ldapserver /python-ldapserver/master 302\n' > public/_redirects
  - du -sch public
  artifacts:
    paths:
    - public
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /v[0-9]+[.][0-9]+[.]x$/ || $CI_COMMIT_BRANCH == "master"'
