From 8c6cfd50ac058b32a710a5dc5f2091a696edceeb Mon Sep 17 00:00:00 2001 From: Tim Jaacks <tim.jaacks@garz-fricke.com> Date: Mon, 18 Jan 2021 17:55:19 +0100 Subject: [PATCH] Make retrigger script only retrigger certain job instead of complete pipeline --- get_merge_requests.py | 9 +++- ...ipeline.py => retrigger_mr_pipeline_job.py | 46 ++++++++++++------- ...elines.py => retrigger_mr_pipeline_jobs.py | 10 +++- 3 files changed, 45 insertions(+), 20 deletions(-) rename retrigger_mr_pipeline.py => retrigger_mr_pipeline_job.py (56%) rename retrigger_mr_pipelines.py => retrigger_mr_pipeline_jobs.py (87%) diff --git a/get_merge_requests.py b/get_merge_requests.py index 71aa9d6e..f0b7cdd7 100755 --- a/get_merge_requests.py +++ b/get_merge_requests.py @@ -30,7 +30,14 @@ def get_merge_requests( merge_requests.append(mr) elif all_merge_requests: merge_requests = all_merge_requests - return merge_requests + + # Get complete objects + full_merge_requests = [] + for mr in merge_requests: + mr = project.mergerequests.get(mr.iid, retry_transient_errors=True) + full_merge_requests.append(mr) + + return full_merge_requests def main(): diff --git a/retrigger_mr_pipeline.py b/retrigger_mr_pipeline_job.py similarity index 56% rename from retrigger_mr_pipeline.py rename to retrigger_mr_pipeline_job.py index c46dc608..5357ba67 100755 --- a/retrigger_mr_pipeline.py +++ b/retrigger_mr_pipeline_job.py @@ -3,28 +3,34 @@ import common import argparse import sys -from gitlab import Gitlab, GitlabGetError, GitlabHttpError +from gitlab import Gitlab, GitlabGetError -def retrigger_mr_pipeline(project, mr): - # Creating a merge request pipeline is not implemented in python-gitlab, so we - # have to construct the HTTP request manually. - # https://github.com/python-gitlab/python-gitlab/issues/1239 - # https://docs.gitlab.com/ce/api/merge_requests.html#create-mr-pipeline - gitlab = mr.manager.gitlab - path = "%s/%s/pipelines" % (mr.manager.path, mr.iid) - try: - pipeline = gitlab.http_post(path) - except GitlabHttpError as e: +def retrigger_mr_pipeline_job(project, mr, job_name): + # Get pipeline + pipeline = project.pipelines.get(mr.pipeline.get("id"), retry_transient_errors=True) + + # Find job + job = None + for pipelinejob in pipeline.jobs.list(): + if pipelinejob.name == job_name: + job = project.jobs.get(pipelinejob.id, retry_transient_errors=True) + + if not job: sys.exit( - "ERROR: could not create pipeline for merge request %s!%s: %s" - % (project.name, mr.iid, e) + "ERROR: could not find job '%s' in pipeline of !%s" % (job_name, mr.iid) ) - print("Created new pipeline for %s :" % mr.web_url) - print(pipeline.get("web_url")) + # Retrigger if not already failed + if job.status not in ["failed", "skipped"]: + job.retry() + print("Retrigger job '%s' of pipeline #%s:" % (job_name, pipeline.id)) + job = project.jobs.get(job.id, retry_transient_errors=True) + print(job.web_url) + else: + job = None - return pipeline + return job def main(): @@ -53,6 +59,12 @@ def main(): dest="iid", required=True, ) + parser.add_argument( + "--job", + help="""name of the job""", + dest="job", + required=True, + ) args, _ = parser.parse_known_args() @@ -68,7 +80,7 @@ def main(): if not mr: sys.exit("ERROR: could not find merge request %s!%s" % (project.name, args.iid)) - retrigger_mr_pipeline(project, mr) + retrigger_mr_pipeline_job(project, mr, args.job) if __name__ == "__main__": diff --git a/retrigger_mr_pipelines.py b/retrigger_mr_pipeline_jobs.py similarity index 87% rename from retrigger_mr_pipelines.py rename to retrigger_mr_pipeline_jobs.py index 4e1ade2b..95e40da9 100755 --- a/retrigger_mr_pipelines.py +++ b/retrigger_mr_pipeline_jobs.py @@ -5,7 +5,7 @@ import argparse from gitlab import Gitlab from get_merge_requests import get_merge_requests -from retrigger_mr_pipeline import retrigger_mr_pipeline +from retrigger_mr_pipeline_job import retrigger_mr_pipeline_job def main(): @@ -52,6 +52,12 @@ def main(): dest="commit", required=False, ) + parser.add_argument( + "--job", + help="""job to retrigger""", + dest="job", + required=True, + ) args, _ = parser.parse_known_args() @@ -67,7 +73,7 @@ def main(): ) for mr in mrs: - retrigger_mr_pipeline(project, mr) + retrigger_mr_pipeline_job(project, mr, args.job) if __name__ == "__main__": -- GitLab