# yamllint disable rule:line-length
---
# YAML Linting [https://yamllint.readthedocs.io/en/latest/]
yamllint:
  stage: lint
  image: registry.gitlab.com/pipeline-components/yamllint:latest
  tags:
    - docker
  rules:
    - if: "$CODE_QUALITY_DISABLED"
      when: never
    - if: "$CI_COMMIT_TAG || $CI_COMMIT_BRANCH"
  script:
    - yamllint .

# JSON Linting [https://github.com/zaach/jsonlint]
jsonlint:
  stage: lint
  image: registry.gitlab.com/pipeline-components/jsonlint:latest
  tags:
    - docker
  rules:
    - if: "$CODE_QUALITY_DISABLED"
      when: never
    - if: "$CI_COMMIT_TAG || $CI_COMMIT_BRANCH"
  script:
    - |
      find . -not -path './.git/*' -name '*.json' -type f -print0 |
      parallel --will-cite -k -0 -n1 jsonlint -q

# Code Format Checking [https://black.readthedocs.io/en/stable/]
black:
  stage: lint
  image: registry.gitlab.com/pipeline-components/black:latest
  tags:
    - docker
  rules:
    - if: "$CODE_QUALITY_DISABLED"
      when: never
    - if: "$CI_COMMIT_TAG || $CI_COMMIT_BRANCH"
  script:
    - black --check --verbose -- .

# Code Climate/Quality Checking [https://pylint.pycqa.org/en/latest/]
pylint:
  stage: lint
  image: registry.gitlab.com/pipeline-components/pylint:latest
  tags:
    - docker
  rules:
    - if: "$CODE_QUALITY_DISABLED"
      when: never
    - if: "$CI_COMMIT_TAG || $CI_COMMIT_BRANCH"
  before_script:
    - mkdir -p public/badges public/lint
    - echo undefined > public/badges/$CI_JOB_NAME.score
  script:
    - pylint --exit-zero --output-format=text $(find -type f -name "*.py" ! -path "**/.venv/**" ! -path "**/app/migrations/**") | 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 $(find -type f -name "*.py" ! -path "**/.venv/**" ! -path "**/app/migrations/**") > codeclimate.json
  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