From 9fad87b7762d073c6d44066a6fd7000870f6b9f8 Mon Sep 17 00:00:00 2001 From: Eric Ball Date: Mon, 8 Aug 2022 07:59:17 -0700 Subject: [PATCH] Fix: Gerrit create_project check results 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 --- lftools/api/endpoints/gerrit.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/lftools/api/endpoints/gerrit.py b/lftools/api/endpoints/gerrit.py index fffa2265..b6fc2766 100644 --- a/lftools/api/endpoints/gerrit.py +++ b/lftools/api/endpoints/gerrit.py @@ -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: -- 2.16.6