Fix: Gerrit create_project check results 62/70462/1
authorEric Ball <eball@linuxfoundation.org>
Mon, 8 Aug 2022 14:59:17 +0000 (07:59 -0700)
committerEric Ball <eball@linuxfoundation.org>
Mon, 8 Aug 2022 14:59:17 +0000 (07:59 -0700)
The create_project method is not hitting the correct URL, but will
still often receive HTML from the server and a 200 status. We need to
check the validity of the returned data, and use the status of a bad
return as the exit code so that scripts can properly react to the
error encountered.

Issue: RELENG-4355
Change-Id: Icc3a769f0fa9aba61142bef11bd81821e1fa5356
Signed-off-by: Eric Ball <eball@linuxfoundation.org>
lftools/api/endpoints/gerrit.py

index fffa226..b6fc276 100644 (file)
@@ -377,27 +377,21 @@ class Gerrit(client.RestApi):
         --description="This is a demo project"
 
         """
-        gerrit_project = urllib.parse.quote(gerrit_project, safe="", encoding=None, errors=None)
-
-        access_str = "projects/{}".format(gerrit_project)
+        gerrit_project = urllib.parse.quote(gerrit_project, encoding=None, errors=None)
 
+        access_str = "projects/?query=name:{}".format(gerrit_project)
         result = self.get(access_str)[0]
-        if result.status_code == 404:
-            log.info(result)
-            log.info("Project not found.")
-            projectexists = False
+        jsonText = result.text.replace(")]}'\n", "").strip()
 
-        elif result.status_code == 401:
-            log.info(result)
-            log.info("Unauthorized.")
-            exit(1)
-
-        else:
-            log.info("found {}".format(access_str))
+        try:
+            resultsDict = json.loads(jsonText)
+        except json.decoder.JSONDecodeError:
             log.info(result)
-            projectexists = True
+            log.info("A problem was encountered while querying the Gerrit API.")
+            log.debug(result.text)
+            exit(result.status_code)
 
-        if projectexists:
+        if resultsDict:
             log.info("Project already exists")
             exit(1)
         if check: