image: python:3.9

services:
  - mysql

variables:
  MYSQL_DATABASE: "test"
  MYSQL_ROOT_PASSWORD: "mysql"
  MYSQL_USER: "django"
  MYSQL_PASSWORD: "mysql"
  MYSQL_HOST: "mysql"

cache:
  paths:
    - ~/.cache/pip/

.before_script_template:
  before_script:
    - python -V  # Print out python version for debugging
    - apt-get -qq update
    - apt-get -qq install -y python3-virtualenv python3 python3-dev python3-pip gettext default-mysql-client default-libmysqlclient-dev
    - ./Utils/setup.sh --ci
    - mkdir -p public/badges public/lint
    - echo undefined > public/badges/$CI_JOB_NAME.score
    - source venv/bin/activate
    - pip install pylint-gitlab pylint-django
    - mysql --version

check:
  extends: .before_script_template
  script:
    - ./Utils/check.sh --all
    - source venv/bin/activate
    - ./manage.py makemigrations --dry-run --check

test:
  extends: .before_script_template
  script:
    - source venv/bin/activate
    - echo "GRANT ALL on *.* to '${MYSQL_USER}';"| mysql -u root --password="${MYSQL_ROOT_PASSWORD}" -h mysql
    - pip install pytest-cov unittest-xml-reporting
    - coverage run --source='.' manage.py test --settings AKPlanning.settings_ci
  after_script:
    - source venv/bin/activate
    - coverage report
    - coverage xml
  coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
      junit: unit.xml

lint:
  extends: .before_script_template
  stage: test
  script:
    - pylint --load-plugins pylint_django --django-settings-module=AKPlanning.settings_ci --rcfile pylintrc --exit-zero --output-format=text AK* | 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 --load-plugins pylint_django --django-settings-module=AKPlanning.settings_ci --rcfile pylintrc --exit-zero --output-format=pylint_gitlab.GitlabCodeClimateReporter AK* > codeclimate.json
    - pylint --load-plugins pylint_django --django-settings-module=AKPlanning.settings_ci --rcfile pylintrc --exit-zero --output-format=pylint_gitlab.GitlabPagesHtmlReporter AK* > public/lint/index.html
  after_script:
   - |
      echo "Linting score: $(cat public/badges/$CI_JOB_NAME.score)"
  artifacts:
    paths:
      - public
    reports:
      codequality: codeclimate.json
    when: always

doc:
  extends: .before_script_template
  stage: test
  script:
    - cd docs
    - make html
    - cd ..
  artifacts:
    paths:
      - docs/_build/html

pages:
  stage: deploy
  image: alpine:latest
  script:
    - echo
  artifacts:
    paths:
      - public
  only:
    refs:
      - main
