---
# Here we check your GitLab project settings and provide recommendations on which
# settings you should change, or how to decide what to choose.
# You need to have a variable called GL_TOKEN set, which contains a Project
# Access Token with read_api or api access.

# If you want problems to be automatically resolved, set the variable GITLAB_RECOMMENDED_AUTO_FIX to "true"
include:
  - remote: https://jobs.just-ci.dev/main/project-automation/generic.yaml

gitlab:recommended:
  stage: test
  image:
    name: ghcr.io/tomwright/dasel:2-alpine
    entrypoint: [""]
  variables:
    GIT_STRATEGY: none
    RECOMMENDED_SETTINGS: |
      merge_requests_enabled:
        recommended: true
        hint: Enable merge requests.
      merge_method:
        recommended: merge
        hint:
          To ensure a full history of what happened with your code, never use fast
          forward. Set 'Merge method' to 'Merge commit'
      only_allow_merge_if_pipeline_succeeds:
        recommended: true
        hint:
          Only allow merging when the pipeline succeeds, to ensure no broken code can
          be merged.
      only_allow_merge_if_all_discussions_are_resolved:
        recommended: true
        hint:
          Ensure all discussions are resolved before merging. Tedious perhaps, but
          reduces mistakes.
      allow_merge_on_skipped_pipeline:
        recommended: false
        hint: Do not allow merging on skipped pipelines.
      remove_source_branch_after_merge:
        recommended: true
        hint:
          To keep your repo clean, remove source branches when merge requests are
          merged.
      autoclose_referenced_issues:
        recommended: true
        hint: Set auto close referenced issues.
  script:
    - apk add -q curl
    - dasel --version
    - echo "${RECOMMENDED_SETTINGS}" > settings.yaml
    - >
      curl -sSLf -H "PRIVATE-TOKEN: ${GL_TOKEN}"
      "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}" -o project.json
    - let COUNT=0 || true
    - KEYS="$(dasel -f settings.yaml "keys().all()" -w -)"
    - |
      for KEY in ${KEYS}; do
        printf "[*] Checking ${KEY}..."
        CURRENT=$(dasel -f project.json -r json "${KEY}" -w - || echo "error")
        RECOMMENDED=$(dasel -f settings.yaml "${KEY}.recommended")
        HINT=$(dasel -f settings.yaml "${KEY}.hint")
        if [ "${CURRENT}" != "${RECOMMENDED}" ]; then
          echo -e " \e[31mrecommended value not set\e[0m. Current value is: ${CURRENT}. Expected value is: ${RECOMMENDED}."
          echo "${HINT}"
          if [ "${GITLAB_RECOMMENDED_AUTO_FIX}" = "true" ]; then
            printf "[*] Automatically fixing the problem..."
            curl -sSLf -H "PRIVATE-TOKEN: ${GL_TOKEN}" -X PUT "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}" -d "${KEY}=${RECOMMENDED}" > /dev/null
            echo -e " \e[32mdone\e[0m."
          else
            let COUNT++ || true
          fi
        else
          echo -e " \e[32mrecommended value set\e[0m: ${RECOMMENDED}."
        fi
      done
    - |
      if test ${COUNT} -gt 0; then
        echo "[!] There are $COUNT settings to check. See above. To resolve these automatically, add the following variable to your .gitlab-ci.yml:"
        echo -e 'variables:\n  GITLAB_RECOMMENDED_AUTO_FIX: "true"'
        exit 1
      else
        echo "[+] All good!"
      fi
  rules:
    - if: $GL_TOKEN
  needs: []

gitlab:recommended:GL_TOKEN:
  extends: .fix:GL_TOKEN
  variables:
    JOB_NAME: gitlab:recommended
