From b24dd1fa1c34414799eb9a297efbcc64601f538e Mon Sep 17 00:00:00 2001 From: Sergi Blanch Torne Date: Tue, 11 Jun 2024 22:53:01 +0200 Subject: [PATCH] ci: Fix parse GitLab pipeline url When the namespace have a dash, this method cannot recogniza properly the fields in a url. Better to use a regular expression quickly defining the fields. The exception raised, when the pattern is not recognized would help more the handler. Signed-off-by: Sergi Blanch Torne Part-of: --- bin/ci/gitlab_common.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/bin/ci/gitlab_common.py b/bin/ci/gitlab_common.py index 54e0cc79207..5111b237a30 100644 --- a/bin/ci/gitlab_common.py +++ b/bin/ci/gitlab_common.py @@ -43,15 +43,19 @@ def pretty_duration(seconds): return f"{seconds:0.0f}s" -def get_gitlab_pipeline_from_url(gl, pipeline_url): - assert pipeline_url.startswith(GITLAB_URL) - url_path = pipeline_url[len(GITLAB_URL) :] - url_path_components = url_path.split("/") - project_name = "/".join(url_path_components[1:3]) - assert url_path_components[3] == "-" - assert url_path_components[4] == "pipelines" - pipeline_id = int(url_path_components[5]) - cur_project = gl.projects.get(project_name) +def get_gitlab_pipeline_from_url(gl, pipeline_url) -> tuple: + """ + Extract the project and pipeline object from the url string + :param gl: Gitlab object + :param pipeline_url: string with a url to a pipeline + :return: ProjectPipeline, Project objects + """ + pattern = rf"^{GITLAB_URL}/(.*)/-/pipelines/([0-9]+)$" + match = re.match(pattern, pipeline_url) + if not match: + raise AssertionError(f"url {pipeline_url} doesn't follow the pattern {pattern}") + namespace_with_project, pipeline_id = match.groups() + cur_project = gl.projects.get(namespace_with_project) pipe = cur_project.pipelines.get(pipeline_id) return pipe, cur_project