From 6464083a6b056ba87d8b613fd0a6dd465863994b Mon Sep 17 00:00:00 2001 From: Guilherme Gallo Date: Thu, 27 Feb 2025 14:05:54 -0300 Subject: [PATCH] ci: Extract target job handling in CI monitor script Extract the target job processing logic from the monitor_pipeline function into a dedicated run_target_job helper. This improves readability and maintainability by reducing the complexity of the monitor_pipeline function. Also add type casting to ensure proper typing for the job objects and import the necessary Callable type. Signed-off-by: Guilherme Gallo Reviewed-by: Sergi Blanch Torne Part-of: --- bin/ci/ci_run_n_monitor.py | 54 +++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/bin/ci/ci_run_n_monitor.py b/bin/ci/ci_run_n_monitor.py index 5cd732be4fe..ce548ece844 100755 --- a/bin/ci/ci_run_n_monitor.py +++ b/bin/ci/ci_run_n_monitor.py @@ -21,7 +21,7 @@ from concurrent.futures import ThreadPoolExecutor from functools import partial from itertools import chain from subprocess import check_output, CalledProcessError -from typing import Dict, TYPE_CHECKING, Iterable, Literal, Optional, Tuple +from typing import Callable, Dict, TYPE_CHECKING, Iterable, Literal, Optional, Tuple, cast import gitlab import gitlab.v4.objects @@ -110,6 +110,31 @@ def pretty_wait(sec: int) -> None: time.sleep(1) +def run_target_job( + job: gitlab.v4.objects.ProjectPipelineJob, + enable_job_fn: Callable, + stress: int, + stress_status_counter: dict, + execution_times: dict, + target_statuses: dict, + name_field_pad: int, +) -> None: + if stress and job.status in COMPLETED_STATUSES: + if ( + stress < 0 + or sum(stress_status_counter[job.name].values()) < stress + ): + stress_status_counter[job.name][job.status] += 1 + execution_times[job.name][job.id] = (job_duration(job), job.status, job.web_url) + job = enable_job_fn(job=job, action_type="retry") + else: + execution_times[job.name][job.id] = (job_duration(job), job.status, job.web_url) + job = enable_job_fn(job=job, action_type="target") + + print_job_status(job, job.status not in target_statuses[job.name], name_field_pad) + target_statuses[job.name] = job.status + + def monitor_pipeline( project: gitlab.v4.objects.Project, pipeline: gitlab.v4.objects.ProjectPipeline, @@ -156,28 +181,21 @@ def monitor_pipeline( to_cancel = [] jobs_waiting.clear() for job in sorted(pipeline.jobs.list(all=True), key=lambda j: j.name): + job = cast(gitlab.v4.objects.ProjectPipelineJob, job) if target_jobs_regex.fullmatch(job.name) and \ include_stage_regex.fullmatch(job.stage) and \ not exclude_stage_regex.fullmatch(job.stage): + run_target_job( + job, + enable_job_fn, + stress, + stress_status_counter, + execution_times, + target_statuses, + name_field_pad, + ) target_id = job.id - target_status = job.status - - if stress and target_status in COMPLETED_STATUSES: - if ( - stress < 0 - or sum(stress_status_counter[job.name].values()) < stress - ): - stress_status_counter[job.name][target_status] += 1 - execution_times[job.name][job.id] = (job_duration(job), target_status, job.web_url) - job = enable_job_fn(job=job, action_type="retry") - else: - execution_times[job.name][job.id] = (job_duration(job), target_status, job.web_url) - job = enable_job_fn(job=job, action_type="target") - - print_job_status(job, target_status not in target_statuses[job.name], name_field_pad) - target_statuses[job.name] = target_status continue - # all other non-target jobs if job.status != statuses[job.name]: print_job_status(job, True, name_field_pad)