
This validation code has 2 bugs, the main one being that it is wrong and is refusing perfectly valid codes. Let's remove this until we come up with a valid check. This reverts commitcd8b546205
. Fixes:cd8b546205
("bin/ci: Add GitLab basic token validation") Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/27312>
111 lines
3.4 KiB
Python
111 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
# Copyright © 2020 - 2022 Collabora Ltd.
|
|
# Authors:
|
|
# Tomeu Vizoso <tomeu.vizoso@collabora.com>
|
|
# David Heidelberg <david.heidelberg@collabora.com>
|
|
# Guilherme Gallo <guilherme.gallo@collabora.com>
|
|
#
|
|
# SPDX-License-Identifier: MIT
|
|
'''Shared functions between the scripts.'''
|
|
|
|
import os
|
|
import time
|
|
from pathlib import Path
|
|
|
|
GITLAB_URL = "https://gitlab.freedesktop.org"
|
|
TOKEN_DIR = Path(os.getenv("XDG_CONFIG_HOME") or Path.home() / ".config")
|
|
|
|
|
|
def pretty_duration(seconds):
|
|
"""Pretty print duration"""
|
|
hours, rem = divmod(seconds, 3600)
|
|
minutes, seconds = divmod(rem, 60)
|
|
if hours:
|
|
return f"{hours:0.0f}h{minutes:0.0f}m{seconds:0.0f}s"
|
|
if minutes:
|
|
return f"{minutes:0.0f}m{seconds:0.0f}s"
|
|
return f"{seconds:0.0f}s"
|
|
|
|
|
|
def get_gitlab_pipeline_from_url(gl, pipeline_url):
|
|
assert pipeline_url.startswith(GITLAB_URL)
|
|
url_path = pipeline_url[len(GITLAB_URL) :]
|
|
url_path_components = url_path.split("/")
|
|
project_name = "/".join(url_path_components[1:3])
|
|
assert url_path_components[3] == "-"
|
|
assert url_path_components[4] == "pipelines"
|
|
pipeline_id = int(url_path_components[5])
|
|
cur_project = gl.projects.get(project_name)
|
|
pipe = cur_project.pipelines.get(pipeline_id)
|
|
return pipe, cur_project
|
|
|
|
|
|
def get_gitlab_project(glab, name: str):
|
|
"""Finds a specified gitlab project for given user"""
|
|
if "/" in name:
|
|
project_path = name
|
|
else:
|
|
glab.auth()
|
|
username = glab.user.username
|
|
project_path = f"{username}/{name}"
|
|
return glab.projects.get(project_path)
|
|
|
|
|
|
def get_token_from_default_dir() -> str:
|
|
"""
|
|
Retrieves the GitLab token from the default directory.
|
|
|
|
Returns:
|
|
str: The path to the GitLab token file.
|
|
|
|
Raises:
|
|
FileNotFoundError: If the token file is not found.
|
|
"""
|
|
token_file = TOKEN_DIR / "gitlab-token"
|
|
try:
|
|
return str(token_file.resolve())
|
|
except FileNotFoundError as ex:
|
|
print(
|
|
f"Could not find {token_file}, please provide a token file as an argument"
|
|
)
|
|
raise ex
|
|
|
|
|
|
def read_token(token_arg: str | Path | None) -> str | None:
|
|
"""
|
|
Reads the token from the given file path or returns the token argument if it is not a file.
|
|
|
|
Args:
|
|
token_arg (str | Path | None): The file path or the token itself.
|
|
|
|
Returns:
|
|
str | None: The token string or None if the token is not provided.
|
|
"""
|
|
if token_arg:
|
|
token_path = Path(token_arg)
|
|
if token_path.is_file():
|
|
# if is a file, read it
|
|
return token_path.read_text().strip()
|
|
return str(token_arg)
|
|
|
|
# if the token is not provided neither its file, return None
|
|
return None
|
|
|
|
|
|
def wait_for_pipeline(projects, sha: str, timeout=None):
|
|
"""await until pipeline appears in Gitlab"""
|
|
project_names = [project.path_with_namespace for project in projects]
|
|
print(f"⏲ for the pipeline to appear in {project_names}..", end="")
|
|
start_time = time.time()
|
|
while True:
|
|
for project in projects:
|
|
pipelines = project.pipelines.list(sha=sha)
|
|
if pipelines:
|
|
print("", flush=True)
|
|
return (pipelines[0], project)
|
|
print("", end=".", flush=True)
|
|
if timeout and time.time() - start_time > timeout:
|
|
print(" not found", flush=True)
|
|
return (None, None)
|
|
time.sleep(1)
|