From: Eric Ball Date: Mon, 8 Aug 2022 14:59:17 +0000 (-0700) Subject: Fix: Gerrit create_project check results X-Git-Tag: v0.36.2~1 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F62%2F70462%2F1;p=releng%2Flftools.git 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 --- 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: