#!/usr/bin/env bash
 
# https://python-gitlab.readthedocs.io/en/stable/cli-examples.html
# https://docs.gitlab.com/ee/ci/variables/#list-all-environment-variables
 
set -Eeuo pipefail
 
# shellcheck disable=SC2154
trap 'declare rc=$?;
      >&2 echo "Unexpected error (exit-code $rc) executing $BASH_COMMAND at ${BASH_SOURCE[0]} line $LINENO";
      exit $rc' ERR
 
gitlab () {
  command gitlab -o json "$@"
}
 
getGroupProjects () {
  declare groupId=""
  groupId="$(gitlab -o json group get --id "$1" | jq -r .id)"
  gitlab group-project list --all --include-subgroups true --group-id "$groupId" \
    | jq -r '.[]|[.id, .path, .namespace.full_path, .path_with_namespace, .default_branch]|join(" ")'
}
 
getFile () {
   gitlab project-file get --project-id "$1" --file-path "$2" --ref "${3:-main}" | jq -r '.content|@base64d'
}
 
main () {
  if [[ -z "${CI_PROJECT_ROOT_NAMESPACE:+defined}" ]] ; then
    CI_PROJECT_ROOT_NAMESPACE="yourFallBackDefaultGroupName"
  fi
 
  declare -i max_jobs=6 rc=0
  while read -r id _ group path branch ; do
    (
      declare ciYaml=""
      if ciYaml="$(getFile "$id" ".gitlab-ci.yml" "$branch" 2>/dev/null)" ; then
        mkdir -pv "$group"
        echo "$ciYaml" > "$path.yaml"
        printf "%-10s %s\n" "OK" "$path"
      else
        printf "%-10s %s\n" "MISSING" "$path"
      fi
    ) &
 
    declare -a jobs=(); mapfile -t jobs < <(jobs -r -p)
    if [[ ${#jobs[@]} -ge $max_jobs ]] ; then
      wait -n || rc=$?
    fi
  done < <(getGroupProjects "$CI_PROJECT_ROOT_NAMESPACE")
 
  wait || rc=$?
}
 
main "$@"