bin/ci: Refactor read_token function

Make `read_token` utilize the `get_gitlab_pipeline_from_url` to reuse
code from `gitlab_gql.py`.

Signed-off-by: Guilherme Gallo <guilherme.gallo@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/27206>
This commit is contained in:
Guilherme Gallo
2024-01-22 20:16:43 -03:00
parent 708a26c607
commit 8eabdb3909
2 changed files with 27 additions and 12 deletions

View File

@@ -26,12 +26,14 @@ from typing import TYPE_CHECKING, Iterable, Literal, Optional
import gitlab
from colorama import Fore, Style
from gitlab_common import (
get_gitlab_project,
GITLAB_URL,
TOKEN_DIR,
get_gitlab_pipeline_from_url,
get_gitlab_project,
get_token_from_default_dir,
pretty_duration,
read_token,
wait_for_pipeline,
pretty_duration,
GITLAB_URL,
)
from gitlab_gql import GitlabGQL, create_job_needs_dag, filter_dag, print_dag
@@ -274,7 +276,10 @@ def parse_args() -> None:
parser.add_argument(
"--token",
metavar="token",
help="force GitLab token, otherwise it's read from ~/.config/gitlab-token",
type=str,
default=get_token_from_default_dir(),
help="Use the provided GitLab token or token file, "
f"otherwise it's read from {TOKEN_DIR / 'gitlab-token'}",
)
parser.add_argument(
"--force-manual", action="store_true", help="Force jobs marked as manual"

View File

@@ -71,15 +71,25 @@ def get_token_from_default_dir() -> str:
raise ex
def read_token(token_arg: Optional[str]) -> str:
"""pick token from args or file"""
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:
return token_arg
return (
open(os.path.expanduser("~/.config/gitlab-token"), encoding="utf-8")
.readline()
.rstrip()
)
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):