Skip to content
Snippets Groups Projects
Commit 7bb66f91 authored by Dylan Aïssi's avatar Dylan Aïssi
Browse files

apertis-pkg-merge-upstream-to-downstreams: cancel pipelines created through...

apertis-pkg-merge-upstream-to-downstreams: cancel pipelines created through the creation of a branch

Due to GitLab limitation (https://gitlab.com/gitlab-org/gitlab/-/issues/26422

),
it's currently impossible to avoid to trigger a pipeline when creating a
branch with the API. Since these useless pipelines are wasting
GitLab and OBS ressources (and devs' time), as a workaround for now
we can cancel them.

Signed-off-by: default avatarDylan Aïssi <dylan.aissi@collabora.com>
parent 4284035e
No related branches found
No related tags found
1 merge request!86apertis-pkg-merge-upstream-to-downstreams: cancel pipelines created through...
......@@ -17,6 +17,8 @@ import urllib.request
import urllib.parse
import dataclasses
import sys
import json
import time
@dataclasses.dataclass
......@@ -74,6 +76,48 @@ def is_minor_change(upstream, downstream):
o = git('diff', '--exit-code', upstream, downstream, ':!debian/changelog', ':!debian/apertis/*', _ok_code=[0,1])
return o.exit_code == 0
def cancel_branch_context_pipeline(project_id, branch, token, project_url):
project_url = urllib.parse.urlsplit(project_url)
latest_branch_url = project_url._replace(
path=f"/api/v4/projects/{project_id}/pipelines/latest?ref={branch}",
).geturl()
get_id_req = urllib.request.Request(
url=latest_branch_url,
headers={"PRIVATE-TOKEN": token},
)
n_wait = 2
print(f'Waiting {n_wait} seconds to ensure GitLab had enough time to trigger a pipeline')
time.sleep(n_wait)
try:
res = urllib.request.urlopen(get_id_req)
json_res = json.load(res)
print(json_res)
latest_job_id = json_res["id"]
except urllib.error.HTTPError as e:
print("ERROR:", e.read().decode())
return
cancel_url = project_url._replace(
path=f"/api/v4/projects/{project_id}/pipelines/{latest_job_id}/cancel",
).geturl()
cancel_req = urllib.request.Request(
url=cancel_url,
method="POST",
headers={"PRIVATE-TOKEN": token},
)
print(f"Canceling useless pipeline on {branch}")
try:
res = urllib.request.urlopen(cancel_req).read().decode('utf-8')
print(res)
except urllib.error.HTTPError as e:
print("ERROR:", e.read().decode())
def ensure_downstream_branch(project_url, downstream, dry_run):
if parse_ref(f"origin/{downstream}"):
return
......@@ -91,6 +135,9 @@ def ensure_downstream_branch(project_url, downstream, dry_run):
project_branches_url = url.geturl()
# TODO: add `ci.skip` once it is available from the API
# https://gitlab.com/gitlab-org/gitlab/-/issues/26422
# For now, let's cancel the pipeline newly created
# to avoid useless pipelines wasting GitLab and OBS ressources. This is only
# a workaround until #26422 is fixed.
data = {"branch": urllib.parse.quote(downstream), "ref": downstream_commit}
print(f"POST'ing to {project_branches_url}: {data}")
req = urllib.request.Request(
......@@ -103,6 +150,8 @@ def ensure_downstream_branch(project_url, downstream, dry_run):
if not dry_run:
res = urllib.request.urlopen(req).read().decode('utf-8')
print(res)
# TODO: remove me once #26422 is fixed
cancel_branch_context_pipeline(project_id, downstream, token, project_branches_url)
except urllib.error.HTTPError as e:
print("ERROR:", e.read().decode())
raise
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment