# Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python/tags/
default:
  image: cimg/python:3.13.1-browsers
  cache: # may need to be eliminated so it is not included in build tarball
    paths:
      - .pip-cache/
  before_script:
    - sudo chmod -R +r "$CI_PROJECT_DIR"
    - pip install --upgrade pip
    - python --version ; pip --version  # For debugging
    - pip install hatch


# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.pip-cache"

.release-base:
  # Abstract base job for "release" jobs.
  # Extending jobs must define the following variables:
  # - PYPI_OIDC_AUD: Audience for the ID token that GitLab
  #   issues to the pipeline job
  # - PYPI_OIDC_URL: PyPI endpoint for retrieving a publish
  #   token with GitLab’s ID token
  # - UV_PUBLISH_URL: PyPI endpoint for the actual upload
  stage: 'Publish'
  id_tokens:
    PYPI_ID_TOKEN:
      aud: '$PYPI_OIDC_AUD'
  script:
    - 'ls -lh dist/'  # For debugging
    # Use the GitLab ID token to retrieve an API token from PyPI
    - >-
      resp="$(curl -X POST "${PYPI_OIDC_URL}" -d "{\"token\":\"${PYPI_ID_TOKEN}\"}")"
    # Parse the response and extract the token
    - >-
      publish_token="$(python -c "import json; print(json.loads('${resp}')['token'])")"
    # Upload the files from "dist/"
    - 'uv publish --token "$publish_token"'
    # Print the link to PyPI so we can quickly go there to verify the result:
    - 'version="$(uv run --with hatch-vcs hatchling version)"'
    - 'echo -e "\033[34;1mPackage on PyPI:\033[0m ${CI_ENVIRONMENT_URL}${version}/"'

stages:
  - Test
  - Build
  - Publish
  - Deploy

lint:
  stage: Test
  script:
    - hatch run lint

pytest:
  stage: Test
  script:
    - hatch run test

coverage:
  stage: Test
  script:
    - hatch run cov
  coverage: '/^TOTAL.*\s+(\d+\%)$/'

create-pages:
  stage: Deploy
  pages: true  # specifies a Pages job and publishes default public directory
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - hatch run doc

build:
  stage: Build
  rules:
    # Only run if it's a pipeline for a release branch or a tag:
    - if: $CI_COMMIT_BRANCH =~ /[0-9]+\.[0-9]+\.x/
    - if: $CI_COMMIT_TAG
  script:
    - 'hatch build -c'
    - 'ls -lh dist/'  # For debugging
  artifacts:
    paths:
      - 'dist/'

release-test:
  extends: .release-base
  rules:
    # Only run if it's a pipeline for a release branch or a tag:
    - if: $CI_COMMIT_BRANCH =~ /[0-9]+\.[0-9]+\.x/
    - if: $CI_COMMIT_TAG
  environment:
    name: 'staging'
    url: 'https://test.pypi.org/project/artemis_sg/'
  variables:
    PYPI_OIDC_AUD: 'testpypi'
    PYPI_OIDC_URL: 'https://test.pypi.org/_/oidc/mint-token'
    UV_PUBLISH_URL: 'https://test.pypi.org/legacy/'

release:
  extends: .release-base
  rules:
    # Only run in tag pipelines:
    - if: $CI_COMMIT_TAG
  environment:
    name: 'production'
    url: 'https://pypi.org/project/artemis_sg/'
  variables:
    PYPI_OIDC_AUD: 'pypi'
    PYPI_OIDC_URL: 'https://pypi.org/_/oidc/mint-token'
    UV_PUBLISH_URL: 'https://upload.pypi.org/legacy/'
