---
#
# SPDX-License-Identifier: GPL-3.0-or-later

default:
  image: "archlinux:latest"
  interruptible: true
  artifacts:
    expire_in: 2 weeks

stages:
  - check
  - test
  - build
  - publish
  - pages

variables:
  BUILD_PUBLISH_PACKAGES: python-pdm python-tox
  SYSTEM_PACKAGES: pyalpm python-aiofiles python-email-validator python-jinja python-magic python-orjson python-pydantic python-pyxdg python-pyzstd python-subprocess-tee python-tomli
  SYSTEM_INSTALL_PACKAGES: just python-build python-installer python-pdm python-setuptools python-sphinx python-sphinx-argparse python-sphinx_rtd_theme python-sphinxcontrib-programoutput python-wheel
  SYSTEM_TEST_PACKAGES: python-coverage python-pytest python-pytest-asyncio python-pytest-lazy-fixture
  TEST_PACKAGES: base-devel python-pdm python-tox rust cargo gcc python

.check:
  stage: check
  before_script:
    - |
      read -r -a deps <<< "${TEST_PACKAGES}"
      pacman --noconfirm -Syu --needed "${deps[@]}"

linter:
  extends: .check
  script:
    - tox -e linter --force-python=/usr/bin/python

.bump_dependencies_to_latest: &bump_dependencies_to_latest
  - |
    if [[ $TEST_LATEST = true ]]; then
      pdm update --unconstrained
    fi

.test:
  stage: test
  before_script:
    - |
      read -r -a deps <<< "${TEST_PACKAGES}"
      pacman --noconfirm -Syu --needed "${deps[@]}"
    - *bump_dependencies_to_latest

coverage:
  extends: .test
  script:
    - tox -e coverage-linux
  coverage: '/TOTAL.*\s([.\d]+)%/'
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: .tox/coverage.xml
      junit: junit-report.xml

.integration_test:
  stage: test
  before_script:
    - printf "[multilib]\nInclude = /etc/pacman.d/mirrorlist\n" >> /etc/pacman.conf
    - pacman --noconfirm -Fy
    - |
      read -r -a test_deps <<< "${TEST_PACKAGES}"
      pacman --noconfirm -Syu --needed "${test_deps[@]}"
    - *bump_dependencies_to_latest

integration:
  extends: .integration_test
  script:
    - tox -e integration

.system_test:
  stage: test
  before_script:
    - |
      read -r -a system_deps <<< "${SYSTEM_PACKAGES}"
      read -r -a test_deps <<< "${SYSTEM_TEST_PACKAGES}"
      pacman --noconfirm -Syu --needed "${system_deps[@]}" "${test_deps[@]}"

system_coverage:
  extends: .system_test
  script:
    - pytest -vv tests/ -m 'not integration and not regex'

.system_integration_test:
  stage: test
  before_script:
    - printf "[multilib]\nInclude = /etc/pacman.d/mirrorlist\n" >> /etc/pacman.conf
    - pacman --noconfirm -Fy
    - |
      read -r -a install_deps <<< "${SYSTEM_INSTALL_PACKAGES}"
      read -r -a system_deps <<< "${SYSTEM_PACKAGES}"
      read -r -a test_deps <<< "${SYSTEM_TEST_PACKAGES}"
      pacman --noconfirm -Syu --needed "${install_deps[@]}" "${system_deps[@]}" "${test_deps[@]}"
    - just build
    - just install

system_integration:
  extends: .system_integration_test
  script:
    - pytest -vv tests/ -m 'integration'

system_integration_core:
  extends: .system_integration_test
  cache:
    paths:
      - core/
  script:
    - pacman --noconfirm -Syu --needed rsync
    - rsync --archive --copy-links --delete --human-readable --verbose --timeout=600 --contimeout=60 rsync://geo.mirror.pkgbuild.com/packages/core .
    - find "${PWD}/core/os/x86_64/" -type f -name '*.pkg.tar.*' -not -name '*.sig' | xargs --max-args 1 --verbose repod-file package inspect >/dev/null
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule" && $TEST_CORE

.build:
  stage: build
  before_script:
    - |
      read -r -a deps <<< "${BUILD_PUBLISH_PACKAGES}"
      pacman --noconfirm -Syu --needed "${deps[@]}"

build:
  extends: .build
  script:
    - pdm build

docs:
  extends: .build
  script:
    - tox -e docs
  after_script:
    - mv docs/_build/html public
  artifacts:
    paths:
      - public

pages:
  stage: pages
  script:
    - "true"
  artifacts:
    paths:
      - public
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

regex:
  extends: .test
  script:
    - tox -e regex

pypi_publish:
  before_script:
    - |
      read -r -a deps <<< "${BUILD_PUBLISH_PACKAGES}"
      pacman --noconfirm -Syu --needed "${deps[@]}"
  rules:
    - if: '$PDM_PUBLISH_PASSWORD && $CI_COMMIT_TAG && $CI_PROJECT_PATH == "archlinux/repod"'
  script:
    - pdm publish --build
  stage: publish
  tags:
    - secure
  variables:
    PDM_PUBLISH_USERNAME: __token__
