variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# https://pip.pypa.io/en/stable/topics/caching/
cache:
  paths:
    - .cache/pip
    - .cache/pre-commit

before_script:
  - python --version ; pip --version
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate

stages:
  - quality
  - test
  - release
  - docs

pre-commit:
  stage: quality
  image: python:3.12
  script:
    - pip install pre-commit
    - pre-commit run --all-files

ruff:
  stage: quality
  image: python:3.12
  script:
    - pip install ruff
    - ruff check --output-format=gitlab src/cigas_ptrms tests > gl-code-quality-report.json
  artifacts:
    reports:
      codequality: gl-code-quality-report.json

mypy:
  stage: quality
  image: python:3.12
  script:
    - pip install .[dev]
    - mypy src/cigas_ptrms tests
  allow_failure: true

test:
  stage: test
  image: python:$PYTHON_VERSION
  needs: ["pre-commit", "ruff"]
  script:
    - pip install -e .[dev]
    - pytest
  coverage: '/\d+\%\s*$/'
  artifacts:
    when: always
    reports:
      junit: coverage.xml
  parallel:
    matrix:
      - PYTHON_VERSION: ["3.11", "3.12"]

deploy:
  stage: release
  image: python:3.12
  needs: ["test"]
  script:
    - pip install build twine
    - python -m build
    - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python -m twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/*
  only:
    - tags
  artifacts:
    paths:
      - dist
    when: on_success
    expire_in: "1 week"

prepare_release:
  stage: release
  image: python:3.12
  needs:
    - deploy
  rules:
    - if: '$CI_COMMIT_TAG =~ /^?\d+\.\d+\.\d+$/'
  script:
    - pip install generate-changelog
    - generate-changelog -o notes -t $CI_COMMIT_TAG > release_notes.md
  artifacts:
    paths:
      - release_notes.md

release_job:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: prepare_release
      artifacts: true
  rules:
    - if: '$CI_COMMIT_TAG =~ /^?\d+\.\d+\.\d+$/'
  before_script:
    - ""
  script:
    - echo "Creating release"
  release:
    name: "Release $CI_COMMIT_TAG"
    description: release_notes.md
    tag_name: "$CI_COMMIT_TAG"
    ref: "$CI_COMMIT_SHA"

pages:
  stage: docs
  image: python:3.12
  needs: ["test"]
  script:
    - pip install .[docs]
    - cd docs
    - make html
    - mv build/html ../public
  artifacts:
    paths:
      - public
  only:
    - main