image: mambaorg/micromamba:1.4.3

before_script:
  - mkdir -p micromamba
  - export MAMBA_ROOT_PREFIX=micromamba
  - micromamba create -yf docs/environment.yml
  - eval "$(micromamba shell hook --shell bash)"
  - micromamba activate meanfi-docs

run tests:
  script:
    - pip install pytest-cov pytest-randomly pytest-repeat pytest-ruff pytest-regressions
    - py.test
  coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
  artifacts:
    paths:
      - htmlcov
    reports:
      junit: junit.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml

run profiler:
  script:
    - pip install memray pyinstrument
    - python profiling/graphene.py
    - memray stats memoryProfile.bin
    - memray summary memoryProfile.bin
  artifacts:
    paths:
      - timeProfile.html
      - memoryProfile.bin

run docs:
  script:
    - make -C docs/ html SPHINXOPTS="-WT --keep-going -n"
  artifacts:
    paths:
      - docs/build

pages:
  needs:
    - run tests
    - run profiler
    - run docs
  script:
    - mkdir public
    - cp -r htmlcov public/
    - cp -r docs/build public/
    - cp -r timeProfile.html public/
  artifacts:
    paths:
      - public

run pre-commit:
  variables:
    PRE_COMMIT_HOME: $CI_PROJECT_DIR/.pre-commit-cache
  script:
    - micromamba install -c conda-forge -y mamba pip pre-commit git
    - git config --global --add safe.directory $CI_PROJECT_DIR
    - pre-commit run --all-files
  cache:
    key: "$CI_JOB_NAME"
    paths:
      - .pre-commit-cache
      - micromamba

prepare zips:
  image: gitlab.kwant-project.org:5005/qt/research-docker
  before_script: []
  when: manual
  script:
    - zip -r zenodo.zip *
  artifacts:
    paths:
      - zenodo.zip

publish to test pypi:
  needs:
    - run tests
    - run docs
  rules:
    - if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+.*\+test$/' # vX.Y.Z.post1+test
  script:
    - micromamba install -c conda-forge -y hatch hatch-vcs
    - hatch build
    - hatch publish -u __token__ -a $PYPI_TEST_TOKEN -r test

publish to pypi:
  needs:
    - run tests
    - run docs
  rules:
    - if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+[^+]*$/' # No +test
  script:
    - micromamba install -c conda-forge -y hatch hatch-vcs
    - hatch build
    - hatch publish -u __token__ -a $PYPI_TOKEN
