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:
@@ -26,12 +26,14 @@ from typing import TYPE_CHECKING, Iterable, Literal, Optional
|
|||||||
import gitlab
|
import gitlab
|
||||||
from colorama import Fore, Style
|
from colorama import Fore, Style
|
||||||
from gitlab_common import (
|
from gitlab_common import (
|
||||||
get_gitlab_project,
|
GITLAB_URL,
|
||||||
|
TOKEN_DIR,
|
||||||
get_gitlab_pipeline_from_url,
|
get_gitlab_pipeline_from_url,
|
||||||
|
get_gitlab_project,
|
||||||
|
get_token_from_default_dir,
|
||||||
|
pretty_duration,
|
||||||
read_token,
|
read_token,
|
||||||
wait_for_pipeline,
|
wait_for_pipeline,
|
||||||
pretty_duration,
|
|
||||||
GITLAB_URL,
|
|
||||||
)
|
)
|
||||||
from gitlab_gql import GitlabGQL, create_job_needs_dag, filter_dag, print_dag
|
from gitlab_gql import GitlabGQL, create_job_needs_dag, filter_dag, print_dag
|
||||||
|
|
||||||
@@ -274,7 +276,10 @@ def parse_args() -> None:
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--token",
|
"--token",
|
||||||
metavar="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(
|
parser.add_argument(
|
||||||
"--force-manual", action="store_true", help="Force jobs marked as manual"
|
"--force-manual", action="store_true", help="Force jobs marked as manual"
|
||||||
|
@@ -71,15 +71,25 @@ def get_token_from_default_dir() -> str:
|
|||||||
raise ex
|
raise ex
|
||||||
|
|
||||||
|
|
||||||
def read_token(token_arg: Optional[str]) -> str:
|
def read_token(token_arg: str | Path | None) -> str | None:
|
||||||
"""pick token from args or file"""
|
"""
|
||||||
|
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:
|
if token_arg:
|
||||||
return token_arg
|
token_path = Path(token_arg)
|
||||||
return (
|
if token_path.is_file():
|
||||||
open(os.path.expanduser("~/.config/gitlab-token"), encoding="utf-8")
|
# if is a file, read it
|
||||||
.readline()
|
return token_path.read_text().strip()
|
||||||
.rstrip()
|
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):
|
def wait_for_pipeline(projects, sha: str, timeout=None):
|
||||||
|
Reference in New Issue
Block a user