From 5943c91c30e8657bc0a8cb5061bc5697583de839 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen.haas@lakedrops.com>
Date: Mon, 15 Aug 2022 13:44:02 +0200
Subject: [PATCH] Fix finding project ID from project namespace to avoid
 conflicts with matching substrings

---
 bin/download | 31 ++++++++++++++++++++++---------
 bin/merge    | 31 ++++++++++++++++++++++---------
 2 files changed, 44 insertions(+), 18 deletions(-)

diff --git a/bin/download b/bin/download
index 9972ef5..cf710e9 100755
--- a/bin/download
+++ b/bin/download
@@ -91,15 +91,28 @@ else
   # shellcheck disable=SC2001
   path="$(echo "$path" | sed -e 's,\..*,,g')"
   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"
-    exit 6
-  fi
-  if [[ $(isNumeric "$PRJID") -eq 1 ]]; then
-    echo "Can not find project ID"
-    exit 6
-  fi
+  i=0
+  while true
+  do
+    NAMESPACE=$(echo "$result"  | jq -r .[$i]."path_with_namespace")
+    if [[ "$NAMESPACE" == "$path" ]]; then
+      PRJID=$(echo "$result"  | jq -r .[$i]."id")
+      if [[ "a${PRJID}a" == "aa" ]]; then
+        echo "No project found"
+        exit 6
+      fi
+      if [[ $(isNumeric "$PRJID") -eq 1 ]]; then
+        echo "Can not find project ID"
+        exit 6
+      fi
+      break
+    fi
+    if [[ "a${NAMESPACE}a" == "aa" ]]; then
+      echo "No project found"
+      exit 6
+    fi
+    ((i=i+1))
+  done
 fi
 
 echo "Find pipeline trigger token ..."
diff --git a/bin/merge b/bin/merge
index 013985a..a08c23c 100755
--- a/bin/merge
+++ b/bin/merge
@@ -94,15 +94,28 @@ else
   # shellcheck disable=SC2001
   path="$(echo "$path" | sed -e 's,\..*,,g')"
   gitlab 200 GET "projects?search_namespaces=1&search=$path"
-  PRJID=$(echo "$result"  | jq -r .[0]."id")
-  if [[ "a${PRJID}a" == "aa" ]]; then
-    echo "No project found"
-    exit 6
-  fi
-  if [[ $(isNumeric "$PRJID") -eq 1 ]]; then
-    echo "Can not find project ID"
-    exit 6
-  fi
+  i=0
+  while true
+  do
+    NAMESPACE=$(echo "$result"  | jq -r .[$i]."path_with_namespace")
+    if [[ "$NAMESPACE" == "$path" ]]; then
+      PRJID=$(echo "$result"  | jq -r .[$i]."id")
+      if [[ "a${PRJID}a" == "aa" ]]; then
+        echo "No project found"
+        exit 6
+      fi
+      if [[ $(isNumeric "$PRJID") -eq 1 ]]; then
+        echo "Can not find project ID"
+        exit 6
+      fi
+      break
+    fi
+    if [[ "a${NAMESPACE}a" == "aa" ]]; then
+      echo "No project found"
+      exit 6
+    fi
+    ((i=i+1))
+  done
 fi
 
 echo "Create merge request ..."
-- 
GitLab