image: python:3.9

stages:
  - test
  - deploy
  - release

.before_script_template:
  before_script:
    - apt-get -y update && apt-get -y --no-install-recommends install libsuitesparse-dev	
    - pip install -r requirements.txt
    - pip install . 

pylint:
  extends: .before_script_template
  stage: test
  script:
    - pip install pylint pylint-gitlab anybadge
    - mkdir -p public/badges public/lint
    - echo undefined > public/badges/$CI_JOB_NAME.score
    - echo $CI_JOB_NAME
    - pylint --exit-zero --output-format=text src | tee /tmp/pylint.txt
    - sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' /tmp/pylint.txt > public/badges/$CI_JOB_NAME.score
    - pylint --exit-zero --output-format=pylint_gitlab.GitlabCodeClimateReporter src > codeclimate.json
    - pylint --exit-zero --output-format=pylint_gitlab.GitlabPagesHtmlReporter src > public/lint/index.html
  after_script:
    - anybadge --overwrite --label $CI_JOB_NAME --value=$(cat public/badges/$CI_JOB_NAME.score) --file=public/badges/$CI_JOB_NAME.svg 4=red 6=orange 8=yellow 10=green
    - |
      echo "Your score is: $(cat public/badges/$CI_JOB_NAME.score)"
  artifacts:
    paths:
      - public
    reports:
      codequality: codeclimate.json
    when: always

pytest:
  extends: .before_script_template
  stage: test
  script:
    - pip install pytest pytest-cov
    - mkdir -p public/cov
    - pytest --cov=src --cov-report term --cov-report xml:coverage.xml --junitxml=report.xml --cov-report html:public/cov
  coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
  artifacts:
    when: always
    paths:
      - report.xml
      - public
    reports:
      junit: report.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
    when: always


sphinx:
  extends: .before_script_template
  stage: test
  script:
  - pip3 install --no-cache-dir sphinx sphinx_rtd_theme recommonmark>=0.5.0
  - cd doc
  - ln -s ../README.md
  - sphinx-apidoc -s txt -e -o ./ ../src
  - sphinx-build -b html . ../public
  artifacts:
    paths:
    - public
  only:
    - main
    
pages:
  stage: deploy
  image: alpine:latest
  script:
    - echo
  artifacts:
    paths:
      - public

pypi:
  stage: release
  image: python:3.9-slim
  script:
    - pip install twine build
    - python -m build --sdist --outdir dist
    - twine upload --verbose --non-interactive --repository pypi dist/* -u __token__ -p $TWINE
  rules:
    - if: $CI_COMMIT_TAG
