From c293521fb00a5041c1cf09b3e420471ca323d22b Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen.haas@lakedrops.com>
Date: Thu, 7 Jul 2022 12:00:05 +0200
Subject: [PATCH] gitlab-ci-cd/drupal#30 Improve handling of pipeline trigger
 tokens Ignore archived projects

---
 bin/download | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/bin/download b/bin/download
index f682838..9972ef5 100755
--- a/bin/download
+++ b/bin/download
@@ -90,7 +90,7 @@ else
   echo "Find project ID from remote URL ..."
   # shellcheck disable=SC2001
   path="$(echo "$path" | sed -e 's,\..*,,g')"
-  gitlab 200 GET "projects?search_namespaces=1&search=$path"
+  gitlab 200 GET "projects?archived=0&search_namespaces=1&search=$path"
   PRJID=$(echo "$result"  | jq -r .[0]."id")
   if [[ "a${PRJID}a" == "aa" ]]; then
     echo "No project found"
@@ -104,10 +104,28 @@ fi
 
 echo "Find pipeline trigger token ..."
 gitlab 200 GET projects/"$PRJID"/triggers
-TRIGGERTOKEN=$(echo "$result"  | jq -r .[0]."token")
-if [[ "a${TRIGGERTOKEN}a" == "aa" ]]; then
-  echo "No trigger token found"
-  exit 7
+TRIGGERTOKEN=none
+i=0
+while [ $i -ge 0 ]
+do
+  TOKEN=$(echo "$result" | jq -r .[$i]."token")
+  if [[ "$TOKEN" == "null" ]]; then
+    i=-1
+  elif [[ ${#TOKEN} -eq 4 ]]; then
+    ((i=i+1))
+  else
+    TRIGGERTOKEN=$TOKEN
+    i=-1
+  fi
+done
+if [[ "$TRIGGERTOKEN" == "none" ]] || [[ "a${TRIGGERTOKEN}a" == "aa" ]]; then
+  echo "No token found, creating one ..."
+  gitlab 201 POST projects/"$PRJID"/triggers --data "description=Created by L3D"
+  TRIGGERTOKEN=$(echo "$result"  | jq -r ."token")
+  if [[ "$TRIGGERTOKEN" == "none" ]] || [[ "a${TRIGGERTOKEN}a" == "aa" ]]; then
+    echo "No trigger token found and can not create one"
+    exit 7
+  fi
 fi
 
 echo "Trigger pipeline ..."
-- 
GitLab