Commit 268d4f2e authored by Federico Falconieri's avatar Federico Falconieri
Browse files

Merge branch 'settings' into 'master'

feat: gitlab settings

Closes #59

See merge request ci/templates!51
parents 930a5da4 c72f56af
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ variables:

include:
  - local: "alm/semantic-release.yml"
  - local: "other/gitlab/project-settings.yml"

  - local: "other/badge.yml"

@@ -90,6 +91,10 @@ c:flawfinder:
    ERROR_LEVEL: "6"
    CHECK_PATH: templates_tests/c

gitlab:project-settings:
  variables:
    AUTO_FIX: "true"

# We keep this downstream, as we don't expect many MRs for this specific test
downstream:pages:
  stage: test
+57 −0
Original line number Diff line number Diff line
---
# 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 AUTO_FIX to "true"

gitlab:project-settings:
  stage: test
  image: registry.gitlab.com/notno/python-tests
  variables:
    GIT_STRATEGY: none
    AUTO_FIX: "false"
  script:
    - |
      if [ -z ${GL_TOKEN} ]; then
        echo "[!] You need to set a variable called 'GL_TOKEN' with a Project Access Token with either 'read_api' or 'api' scope access."
        exit 1
      fi
    - |
      curl -s "https://gitlab.com/notno/python-tests/-/raw/master/.settings.yml" > .settings.yml
    - FILE=project_${CI_PROJECT_ID}_details
    - |
      curl -s --header "PRIVATE-TOKEN: ${GL_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}" > ${FILE}.json
    - dasel -f ${FILE}.json -r json -w yaml > ${FILE}.yml
    - COUNT=0
    - |
      for KEY in $(dasel -f .settings.yml -m '.-'); do
        printf "[*] Checking ${KEY}..."
        CURRENT=$(dasel -f ${FILE}.yml ${KEY})
        RECOMMENDED=$(dasel -f .settings.yml "${KEY}".recommended)
        HINT=$(dasel -f .settings.yml ${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 [ "${AUTO_FIX}" == "true" ]; then
            printf "[*] Automatically fixing the problem..."
            curl --fail -s --header "PRIVATE-TOKEN: ${GL_TOKEN}" -X PUT "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}" --data "${KEY}=${RECOMMENDED}" > /dev/null
            echo -e " \e[32mdone\e[0m."
          else
            ((++COUNT))
          fi
        else
          echo -e " \e[32mrecommended value set\e[0m: ${RECOMMENDED}."
        fi
      done
    - |
      if [ ${COUNT} -gt 0 ]; then
        echo "[!] There are $COUNT settings to check. See above. To resolve these automatically, add the following to your .gitlab-ci.yml:"
        echo -e 'gitlab:project-settings:\n  variables:\n    AUTO_FIX: "true"'
        exit 1
      else
        echo "[+] All good!"
      fi
  needs: []
  allow_failure: true