From cb831c1a4f1d590a66ed9033f72a24bb8ac8d661 Mon Sep 17 00:00:00 2001 From: Tim Jaacks <tim.jaacks@garz-fricke.com> Date: Tue, 14 Dec 2021 00:07:22 +0100 Subject: [PATCH] check_pipeline_status: add parameter for ref Remove the trigger source parameter again because it did not serve the use case. Add ref parameter instead. --- check_pipeline_status.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/check_pipeline_status.py b/check_pipeline_status.py index 8c0ebe98..fa3b8175 100755 --- a/check_pipeline_status.py +++ b/check_pipeline_status.py @@ -9,15 +9,29 @@ from gitlab import Gitlab, GitlabGetError from gitlab.v4.objects import Project -def check_pipeline_status(project: Project, commit, source): - """Get latest pipeline status for a given commit""" +def check_pipeline_status(project: Project, commit, ref: str): + """ + Get latest pipeline status for a given commit and ref. + The ref can be negated with a preceding '^', e.g. '^master' finds a pipeline on any + ref different from 'master'. + """ # Find pipeline for commit - pipelines = project.pipelines.list( - sha=commit, source=source, retry_transient_errors=True - ) + pipelines = project.pipelines.list(sha=commit, retry_transient_errors=True) + + # Remove pipelines not matching the ref condition + if ref: + for p in pipelines[:]: + if ( + ref.startswith("^") + and p.ref == ref[1:] + or not ref.startswith("^") + and p.ref != ref + ): + pipelines.remove(p) + if not pipelines: - print("ERROR: no pipeline for commit '%s' found" % commit) + print("ERROR: no pipeline for commit '%s' with ref '%s' found" % (commit, ref)) sys.exit(1) pipeline = pipelines[0] @@ -61,11 +75,10 @@ def main(): required=True, ) parser.add_argument( - "--source", - help="""source that triggered the pipeline""", - choices=["push", "merge_request_event"], - dest="source", - default="push", + "--ref", + help="""ref the pipeline ran on (can be negated with preceding '^')""", + dest="ref", + default="", required=False, ) @@ -74,7 +87,7 @@ def main(): gitlab = Gitlab(args.gitlab_url, private_token=args.token) project = common.get_project(gitlab, args.project) - status = check_pipeline_status(project, args.commit, args.source) + status = check_pipeline_status(project, args.commit, args.ref) # If we are running in a job environment and the upstream status is canceled, # explicitly cancel this job as well -- GitLab