---
fix:standard:
  stage: fix
  image:
    name: megabytelabs/standard:slim
    entrypoint: ['']
  rules:
    - if: $DO_CI == 'false'
      when: never
    - if: $CI_COMMIT_TITLE =~ /^chore.*/
      when: never
    - if: $CI_PIPELINE_SOURCE != 'pipeline' && $CI_PIPELINE_SOURCE != 'web' && $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
      changes:
        - '**/*.js'
    - if: $CI_COMMIT_REF_NAME != $CI_DEFAULT_BRANCH
      changes:
        - '**/*.js'
  variables:
    GIT_DEPTH: 1
  before_script:
    - git config pull.ff only
    - git remote set-url origin "https://root:$GROUP_ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git"
    - git config user.email "$GITLAB_CI_EMAIL"
    - git config user.name "$GITLAB_CI_NAME"
    - git checkout "$CI_COMMIT_REF_NAME"
    - git pull origin "$CI_COMMIT_REF_NAME" --allow-unrelated-histories
  script:
    - set -eo pipefail
    - standard --fix
    - git add --all
    - git diff --cached *
    - |
      if [[ `git status --porcelain` ]]; then
        git commit -m "automation(standard-fix) Fixed formatting issues with Standard on GitLab CI."
        git push origin "$CI_COMMIT_REF_NAME"
      fi
