From 2faf56bd9563e86fd9295b8ada9ee5198712cd2f Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Mon, 22 Feb 2021 14:32:38 -0500 Subject: scripts/ci/gitlab-pipeline-status: split utlity function for HTTP GET MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This simply splits out the code that does an HTTP GET so that it can be used for other API requests. Signed-off-by: Cleber Rosa Reviewed-by: Alex Bennée Reviewed-by: Wainer dos Santos Moschetta Message-Id: <20210222193240.921250-2-crosa@redhat.com> Signed-off-by: Thomas Huth --- scripts/ci/gitlab-pipeline-status | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'scripts/ci/gitlab-pipeline-status') diff --git a/scripts/ci/gitlab-pipeline-status b/scripts/ci/gitlab-pipeline-status index 78e72f6008..0c1e8bd8a7 100755 --- a/scripts/ci/gitlab-pipeline-status +++ b/scripts/ci/gitlab-pipeline-status @@ -48,18 +48,25 @@ def get_local_branch_commit(branch): return result -def get_pipeline_status(project_id, commit_sha1): +def get_json_http_response(url): """ - Returns the JSON content of the pipeline status API response + Returns the JSON content of an HTTP GET request to gitlab.com """ - url = '/api/v4/projects/{}/pipelines?sha={}'.format(project_id, - commit_sha1) connection = http.client.HTTPSConnection('gitlab.com') connection.request('GET', url=url) response = connection.getresponse() if response.code != http.HTTPStatus.OK: raise CommunicationFailure("Failed to receive a successful response") - json_response = json.loads(response.read()) + return json.loads(response.read()) + + +def get_pipeline_status(project_id, commit_sha1): + """ + Returns the JSON content of the pipeline status API response + """ + url = '/api/v4/projects/{}/pipelines?sha={}'.format(project_id, + commit_sha1) + json_response = get_json_http_response(url) # As far as I can tell, there should be only one pipeline for the same # project + commit. If this assumption is false, we can add further -- cgit v1.2.3 From 861d1d509b111f59b294c975eee59f2a23bc783a Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Mon, 22 Feb 2021 14:32:39 -0500 Subject: scripts/ci/gitlab-pipeline-status: give more information on failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When an HTTP GET request fails, it's useful to go beyond the "not successful" message, and show the code returned by the server. Signed-off-by: Cleber Rosa Reviewed-by: Alex Bennée Reviewed-by: Wainer dos Santos Moschetta Message-Id: <20210222193240.921250-3-crosa@redhat.com> Signed-off-by: Thomas Huth --- scripts/ci/gitlab-pipeline-status | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'scripts/ci/gitlab-pipeline-status') diff --git a/scripts/ci/gitlab-pipeline-status b/scripts/ci/gitlab-pipeline-status index 0c1e8bd8a7..ad62ab3cfc 100755 --- a/scripts/ci/gitlab-pipeline-status +++ b/scripts/ci/gitlab-pipeline-status @@ -56,7 +56,9 @@ def get_json_http_response(url): connection.request('GET', url=url) response = connection.getresponse() if response.code != http.HTTPStatus.OK: - raise CommunicationFailure("Failed to receive a successful response") + msg = "Received unsuccessful response: %s (%s)" % (response.code, + response.reason) + raise CommunicationFailure(msg) return json.loads(response.read()) -- cgit v1.2.3 From 6179f32eeb6b13574ef65c85f836c681d213e577 Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Mon, 22 Feb 2021 14:32:40 -0500 Subject: scripts/ci/gitlab-pipeline-status: give more info when pipeline not found This includes both input parameters (project id and commit) in the message so to make it easier to debug returned API calls. Signed-off-by: Cleber Rosa Reviewed-by: Wainer dos Santos Moschetta Message-Id: <20210222193240.921250-4-crosa@redhat.com> Signed-off-by: Thomas Huth --- scripts/ci/gitlab-pipeline-status | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'scripts/ci/gitlab-pipeline-status') diff --git a/scripts/ci/gitlab-pipeline-status b/scripts/ci/gitlab-pipeline-status index ad62ab3cfc..924db327ff 100755 --- a/scripts/ci/gitlab-pipeline-status +++ b/scripts/ci/gitlab-pipeline-status @@ -74,7 +74,9 @@ def get_pipeline_status(project_id, commit_sha1): # project + commit. If this assumption is false, we can add further # filters to the url, such as username, and order_by. if not json_response: - raise NoPipelineFound("No pipeline found") + msg = "No pipeline found for project %s and commit %s" % (project_id, + commit_sha1) + raise NoPipelineFound(msg) return json_response[0] -- cgit v1.2.3