image: python:3.7

cache:
    key: "project-${CI_JOB_NAME}"
    paths:
      - .venv
    key:
      files:
        - poetry.lock

variables:
  TRIGGER_PYPI_NAME: ""

stages:
  - test
  - check
  - release

before_script:
  - test -e $HOME/.poetry/bin/ || curl -sSL https://install.python-poetry.org | python3 -
  - export PATH="$PATH:$HOME/.local/bin/"
  - poetry --version
  - poetry config virtualenvs.in-project true
  - pip install --upgrade pip
  - git remote rm origin && git remote add origin https://${ACCESS_TOKEN_NAME}:${ACCESS_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git
  - git config pull.rebase false
  - git pull origin HEAD:master
  - rm -rf ~/.cache/pypoetry
  - if [ ${var+TRIGGER_PYPI_NAME} ]; then echo "Pipeline triggered by ${TRIGGER_PYPI_NAME}"; poetry add ${TRIGGER_PYPI_NAME}@latest; fi
  - poetry install -vv

Unit test:
  stage: test
  script:
    - apt update && apt install ffmpeg libsm6 libxext6  -y
    - poetry run pytest ./tests/

Python Code Lint:
  stage: check
  script:
    - poetry run black .

Static Type:
  stage: check
  allow_failure: true
  script:
    - poetry run mypy . --exclude 'setup\.py$'
    # We can remove the flag once this is resolved https://github.com/pypa/setuptools/issues/2345

Bump_release:
  stage: release
  script:
    - git config --global user.email ${GITLAB_USER_EMAIL}
    - git config --global user.name ${GITLAB_USER_NAME}
    - git pull origin HEAD:MASTER && poetry version ${BUMP_RULE} && git add pypoetry add pyproject.toml poetry.lock && git commit -m "Bump version" && git push -o ci.skip origin HEAD:master && poetry publish --build --username ${PYPI_USER} --password ${PYPI_PASSWORD}

    - echo "TRIGGER_PYPI_NAME=$(cat pyproject.toml | grep '^name =' | head -n 1 | cut -f3 -d' ' | tr -d \")" >> build.env
    - echo "Exporting TRIGGER_PYPI_NAME as ${TRIGGER_PYPI_NAME}"
  only:
    - master
  except:
    changes:
      - tests/
      - .*

  needs:
    job: Unit test

## Custom stages ##