|
| 1 | +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 2 | + |
| 3 | +# SPDX-License-Identifier: CC0-1.0 |
| 4 | + |
| 5 | +import re |
| 6 | +import os |
| 7 | +import requests |
| 8 | +from gitlab_api import GitLabAPI |
| 9 | + |
| 10 | +def cancel_branch_pipelines(gitlab_api): |
| 11 | + url = f"{gitlab_api.gitlab_api_url}/projects/{gitlab_api.ci_project_id}/pipelines?ref={gitlab_api.ci_commit_ref_name}&status=running" |
| 12 | + headers = {"PRIVATE-TOKEN": gitlab_api.gitlab_token} |
| 13 | + response = requests.get(url, headers=headers) |
| 14 | + |
| 15 | + if response.status_code == 200: |
| 16 | + running_pipelines = response.json() |
| 17 | + |
| 18 | + for pipeline in running_pipelines: |
| 19 | + pipeline_id = pipeline.get('id') |
| 20 | + source = pipeline.get('source') |
| 21 | + ref = pipeline.get('ref') |
| 22 | + if pipeline_id != int(gitlab_api.ci_pipeline_id) and source != 'merge_request_event': |
| 23 | + print(f"Cancelling branch pipeline with pipeline ID: {pipeline_id}, Source:{source}, Ref: {ref}") |
| 24 | + gitlab_api.cancel_pipeline(pipeline_id) |
| 25 | + else: |
| 26 | + print(f"Skipping current branch pipeline with pipeline ID: {gitlab_api.ci_pipeline_id}") |
| 27 | + else: |
| 28 | + print(f"Failed to fetch branch pipelines, Status Code: {response.status_code}") |
| 29 | + |
| 30 | +def cancel_merge_request_pipelines(gitlab_api): |
| 31 | + url = f"{gitlab_api.gitlab_api_url}/projects/{gitlab_api.ci_project_id}/pipelines?status=running" |
| 32 | + headers = {"PRIVATE-TOKEN": gitlab_api.gitlab_token} |
| 33 | + response = requests.get(url, headers=headers) |
| 34 | + |
| 35 | + if response.status_code == 200: |
| 36 | + running_pipelines = response.json() |
| 37 | + |
| 38 | + for pipeline in running_pipelines: |
| 39 | + pipeline_id = pipeline.get('id') |
| 40 | + source = pipeline.get('source') |
| 41 | + ref = pipeline.get('ref') |
| 42 | + merge_request_id_pattern = re.match(r"refs/merge-requests/(\d+)/head", ref) |
| 43 | + if merge_request_id_pattern: |
| 44 | + print(f"MR pipeline id: {merge_request_id_pattern.group(1)}") |
| 45 | + if source == "merge_request_event" and gitlab_api.ci_merge_request_iid == merge_request_id_pattern.group(1): |
| 46 | + if pipeline_id == int(gitlab_api.ci_pipeline_id): |
| 47 | + print(f"Skipping current MR pipeline with pipeline id: {pipeline_id} ref: {ref} source: {source}") |
| 48 | + else: |
| 49 | + print(f"Cancelling MR pipeline with pipeline id: {pipeline_id} ref:{ref} source: {source}") |
| 50 | + gitlab_api.cancel_pipeline(pipeline_id) |
| 51 | + else: |
| 52 | + print(f"Skipping pipeline with pipeline ID: {pipeline_id} (ref: {ref}, source: {source})") |
| 53 | + |
| 54 | + else: |
| 55 | + print(f"Failed to fetch pipelines, Status: {response.status_code}") |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + gitlab_api = GitLabAPI() |
| 60 | + cancel_branch_pipelines(gitlab_api) |
| 61 | + cancel_merge_request_pipelines(gitlab_api) |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
| 65 | + |
0 commit comments