gitlab-pipeline-status script: provide more information on errors
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
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
@@ -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
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.
Signed-off-by Cleber Rosa <[email protected]>--- 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@@ -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
Re: [PATCH 1/3] scripts/ci/gitlab-pipeline-status: split utlity function for HTTP GET
In reply to Cleber RosaQuoted 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.
Signed-off-by Cleber Rosa <[email protected]>
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@@ -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
PATCH 2/3 scripts/ci/gitlab-pipeline-status: give more information on failures
In reply to Cleber RosaWhen 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
@@ -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
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.
Signed-off-by Cleber Rosa <[email protected]>--- scripts/ci/gitlab-pipeline-status | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 16 lines
scripts/ci/gitlab-pipeline-status +3
-1@@ -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())
Re: [PATCH 2/3] scripts/ci/gitlab-pipeline-status: give more information on failures
In reply to Cleber RosaQuoted 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.
Signed-off-by Cleber Rosa <[email protected]>
Quoted 17 lines
--- scripts/ci/gitlab-pipeline-status | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)scripts/ci/gitlab-pipeline-status +3
-1@@ -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
PATCH 3/3 scripts/ci/gitlab-pipeline-status: give more info when pipeline not found
In reply to Cleber RosaThis 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
@@ -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
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.
Signed-off-by Cleber Rosa <[email protected]>--- scripts/ci/gitlab-pipeline-status | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 16 lines
scripts/ci/gitlab-pipeline-status +3
-1@@ -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]
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
@@ -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 GETscripts/ci/gitlab-pipeline-status: give more information on failuresscripts/ci/gitlab-pipeline-status: give more info when pipeline not
foundscripts/ci/gitlab-pipeline-status | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)
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 nicelyWith this change, when getting the local branch, it will handle nicely any threw ValueError exception instead of print the stack trace.
Signed-off-by Wainer dos Santos Moschetta <[email protected]>scripts/ci/gitlab-pipeline-status +0
-0@@ -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
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 nicelyWith this change, when getting the local branch, it will handle nicely any threw ValueError exception instead of print the stack trace.
Signed-off-by Wainer dos Santos Moschetta <[email protected]>scripts/ci/gitlab-pipeline-status +0
-0@@ -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
-1if 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
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 GETscripts/ci/gitlab-pipeline-status: give more information on failuresscripts/ci/gitlab-pipeline-status: give more info when pipeline not
foundscripts/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