Skip to contentSkip to conversation

gitlab-pipeline-status script: provide more information on errors

PATCH In review Cleber Rosa 5 participants 13 messages Feb 22, 2021 – Mar 3, 2021

lore
Share & tools
lore
#0 lore raw

cover gitlab-pipeline-status script: provide more information on errors

When things go wrong with the GitLab API requests, it's useful to give users more information about the possible causes.

Cleber Rosa (3):
scripts/ci/gitlab-pipeline-status: split utlity function for HTTP GET scripts/ci/gitlab-pipeline-status: give more information on failures scripts/ci/gitlab-pipeline-status: give more info when pipeline not
found

 scripts/ci/gitlab-pipeline-status | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
Signature · 1 line
2.25.4
#1 lore raw

PATCH 1/3 scripts/ci/gitlab-pipeline-status: split utlity function for HTTP GET

This simply splits out the code that does an HTTP GET so that it can be used for other API requests.

---
 scripts/ci/gitlab-pipeline-status | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
scripts/ci/gitlab-pipeline-status +12 -5
index 78e72f6008..0c1e8bd8a7 100755
@@ -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
Signature · 1 line
2.25.4
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] scripts/ci/gitlab-pipeline-status: split utlity function for HTTP GET

Hi,

Quoted 7 lines · Cleber Rosa

This simply splits out the code that does an HTTP GET so that it can be used for other API requests.

---
  scripts/ci/gitlab-pipeline-status | 17 ++++++++++++-----
  1 file changed, 12 insertions(+), 5 deletions(-)

Only fix the "utlity" typo in $SUBJECT:

Quoted 36 lines
scripts/ci/gitlab-pipeline-status +12 -5
index 78e72f6008..0c1e8bd8a7 100755
@@ -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
Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] scripts/ci/gitlab-pipeline-status: split utlity function for HTTP GET

In reply to Cleber Rosa
Quoted 4 lines · Cleber Rosa

This simply splits out the code that does an HTTP GET so that it can be used for other API requests.

fix sp as per Wainer, otherwise:

Quoted 39 lines
---
 scripts/ci/gitlab-pipeline-status | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
scripts/ci/gitlab-pipeline-status +12 -5
index 78e72f6008..0c1e8bd8a7 100755
@@ -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
Signature · 1 line
Alex Bennée
#4 lore raw

PATCH 2/3 scripts/ci/gitlab-pipeline-status: give more information on failures

In reply to Cleber Rosa

When an HTTP GET request fails, it's useful to go beyond the "not successful" message, and show the code returned by the server.

---
 scripts/ci/gitlab-pipeline-status | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/ci/gitlab-pipeline-status +3 -1
index 0c1e8bd8a7..ad62ab3cfc 100755
@@ -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())
 
 
Signature · 1 line
2.25.4
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] scripts/ci/gitlab-pipeline-status: give more information on failures

Quoted 7 lines · Cleber Rosa

When an HTTP GET request fails, it's useful to go beyond the "not successful" message, and show the code returned by the server.

---
  scripts/ci/gitlab-pipeline-status | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 16 lines
scripts/ci/gitlab-pipeline-status +3 -1
index 0c1e8bd8a7..ad62ab3cfc 100755
@@ -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())
 
 
Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] scripts/ci/gitlab-pipeline-status: give more information on failures

In reply to Cleber Rosa
Quoted 4 lines · Cleber Rosa

When an HTTP GET request fails, it's useful to go beyond the "not successful" message, and show the code returned by the server.

Quoted 17 lines
---
 scripts/ci/gitlab-pipeline-status | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/ci/gitlab-pipeline-status +3 -1
index 0c1e8bd8a7..ad62ab3cfc 100755
@@ -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())
Signature · 1 line
Alex Bennée
#7 lore raw

PATCH 3/3 scripts/ci/gitlab-pipeline-status: give more info when pipeline not found

In reply to Cleber Rosa

This includes both input parameters (project id and commit) in the message so to make it easier to debug returned API calls.

---
 scripts/ci/gitlab-pipeline-status | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/ci/gitlab-pipeline-status +3 -1
index ad62ab3cfc..924db327ff 100755
@@ -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]
 
 
Signature · 1 line
2.25.4
Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] scripts/ci/gitlab-pipeline-status: give more info when pipeline not found

Quoted 7 lines · Cleber Rosa

This includes both input parameters (project id and commit) in the message so to make it easier to debug returned API calls.

---
  scripts/ci/gitlab-pipeline-status | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 16 lines
scripts/ci/gitlab-pipeline-status +3 -1
index ad62ab3cfc..924db327ff 100755
@@ -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]
 
 
#9 lore raw
In reply to Cleber Rosa

Hi Cleber,

In case you need to send a v2, mind to add the following patch together?

commit 3c4ed8a78e096e4d7df0398c29887a9d468ae120 (HEAD -> gitlab_runners)
Author: Wainer dos Santos Moschetta <[email protected]> Date:   Tue Feb 23 11:26:08 2021 -0300

    scripts/ci/gitlab-pipeline-status: Handle ValueError exceptions nicely

    With this change, when getting the local branch, it will handle nicely any threw ValueError exception instead of print the stack trace.

scripts/ci/gitlab-pipeline-status +0 -0
index 924db327ff..6177df973a 100755
@@ -160,7 +160,11 @@ def main():

     args = parser.parse_args()

     if not args.commit:
-        args.commit = get_local_branch_commit(args.branch) +        try:
+            args.commit = get_local_branch_commit(args.branch) +        except ValueError as error:
+            print("ERROR: %s" % error)
+            sys.exit(1)

     success = False
     try:

Quoted 12 lines · Cleber Rosa

When things go wrong with the GitLab API requests, it's useful to give users more information about the possible causes.

Cleber Rosa (3):
scripts/ci/gitlab-pipeline-status: split utlity function for HTTP GET scripts/ci/gitlab-pipeline-status: give more information on failures scripts/ci/gitlab-pipeline-status: give more info when pipeline not
found

  scripts/ci/gitlab-pipeline-status | 25 ++++++++++++++++++-------
  1 file changed, 18 insertions(+), 7 deletions(-)
#10 lore raw
Quoted 23 lines · Wainer dos Santos Moschetta

Hi Cleber,

In case you need to send a v2, mind to add the following patch together?

commit 3c4ed8a78e096e4d7df0398c29887a9d468ae120 (HEAD -> gitlab_runners)
Author: Wainer dos Santos Moschetta <[email protected]> Date:   Tue Feb 23 11:26:08 2021 -0300

    scripts/ci/gitlab-pipeline-status: Handle ValueError exceptions nicely

    With this change, when getting the local branch, it will handle nicely any threw ValueError exception instead of print the stack trace.

scripts/ci/gitlab-pipeline-status +0 -0
index 924db327ff..6177df973a 100755
@@ -160,7 +160,11 @@ def main():
      args = parser.parse_args()
 
Quoted 7 lines

     if not args.commit:
-        args.commit = get_local_branch_commit(args.branch) +        try:
+            args.commit = get_local_branch_commit(args.branch) +        except ValueError as error:
+            print("ERROR: %s" % error)
+            sys.exit(1)

1 is the default error code, so you should pass the error message to sys.exit directly without the print. If you don't want that, at least redirect the print to sys.stderr.

Erik

#11 lore raw

Hi,

Quoted 24 lines · Erik Skultety
Quoted 23 lines · Wainer dos Santos Moschetta

Hi Cleber,

In case you need to send a v2, mind to add the following patch together?

commit 3c4ed8a78e096e4d7df0398c29887a9d468ae120 (HEAD -> gitlab_runners)
Author: Wainer dos Santos Moschetta <[email protected]> Date:   Tue Feb 23 11:26:08 2021 -0300

    scripts/ci/gitlab-pipeline-status: Handle ValueError exceptions nicely

    With this change, when getting the local branch, it will handle nicely any threw ValueError exception instead of print the stack trace.

scripts/ci/gitlab-pipeline-status +0 -0
index 924db327ff..6177df973a 100755
@@ -160,7 +160,11 @@ def main():
      args = parser.parse_args()
 
Quoted 10 lines

scripts/ci/gitlab-pipeline-status def main(): · line 167

Quoted 7 lines
scripts/ci/gitlab-pipeline-status +5 -1
      if not args.commit:
-        args.commit = get_local_branch_commit(args.branch)
+        try:
+            args.commit = get_local_branch_commit(args.branch)
+        except ValueError as error:
+            print("ERROR: %s" % error)
+            sys.exit(1)

1 is the default error code, so you should pass the error message to sys.exit directly without the print. If you don't want that, at least redirect the print to sys.stderr.

There are occurrences of the same pattern on the script, so I better send a separate series to change them all. Thanks for pointing it out Erik!

- Wainer

Quoted 2 lines

Erik

#12 lore raw
In reply to Cleber Rosa
Quoted 12 lines · Cleber Rosa

When things go wrong with the GitLab API requests, it's useful to give users more information about the possible causes.

Cleber Rosa (3):
scripts/ci/gitlab-pipeline-status: split utlity function for HTTP GET scripts/ci/gitlab-pipeline-status: give more information on failures scripts/ci/gitlab-pipeline-status: give more info when pipeline not
found

  scripts/ci/gitlab-pipeline-status | 25 ++++++++++++++++++-------
  1 file changed, 18 insertions(+), 7 deletions(-)

Thanks, I've queued the three patches now to my testing-next branch:

https://gitlab.com/thuth/qemu/-/commits/testing-next/

Thomas