# 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:0.21.1
  tags:
    - docker
  rules:
    - if: "$CODE_QUALITY_DISABLED"
      when: never
    - if: "$CI_COMMIT_TAG || $CI_COMMIT_BRANCH"
  script:
    - pylint --exit-zero --load-plugins=pylint_gitlab --output-format=gitlab-codeclimate:codeclimate.json $(find -type f -name "*.py" ! -path "**/.venv/**" ! -path "**/app/migrations/**")
  artifacts:
    reports:
      codequality: codeclimate.json
    when: always

# SonarQube/SonarCloud - Code Climate & QA [https://www.sonarsource.com]
sonarcloud-check:
  stage: lint
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  tags:
    - docker
  rules:
    - if: "$SONAR_TOKEN == null"
      when: never
    - if: "$CODE_QUALITY_DISABLED"
      when: never
    - if: "$CI_COMMIT_TAG || $CI_COMMIT_BRANCH"
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
    GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - sonar-scanner

# Lang file checking
lang-check:
  stage: lint
  image: alpine:latest
  tags:
    - docker
  rules:
    - if: "$CODE_QUALITY_DISABLED"
      when: never
    - if: "$CI_COMMIT_TAG || $CI_COMMIT_BRANCH"
  allow_failure: true
  before_script:
    - apk add --no-cache jq bash
  script:
    - chmod +x .gitlab/scripts/lang_sort.sh
    - bash .gitlab/scripts/lang_sort.sh ./app/translations/
  after_script:
    - if [ -f .gitlab/scripts/lang_sort_log.txt ]; then cat .gitlab/scripts/lang_sort_log.txt; fi
  artifacts:
    paths:
      - .gitlab/scripts/lang_sort_log.txt
    expire_in: 1 week