diff --git a/bin/ci/gitlab_common.py b/bin/ci/gitlab_common.py index d7ba607771f..57f6c696215 100644 --- a/bin/ci/gitlab_common.py +++ b/bin/ci/gitlab_common.py @@ -3,16 +3,17 @@ # Authors: # Tomeu Vizoso # David Heidelberg +# Guilherme Gallo # # SPDX-License-Identifier: MIT '''Shared functions between the scripts.''' import os import time -from typing import Optional - +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): @@ -50,6 +51,26 @@ def get_gitlab_project(glab, name: str): 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: Optional[str]) -> str: """pick token from args or file""" if token_arg: diff --git a/bin/ci/gitlab_gql.py b/bin/ci/gitlab_gql.py index 503a9cc64ce..0e5bc0f5193 100755 --- a/bin/ci/gitlab_gql.py +++ b/bin/ci/gitlab_gql.py @@ -9,7 +9,6 @@ from collections import OrderedDict from copy import deepcopy from dataclasses import dataclass, field from itertools import accumulate -from os import getenv from pathlib import Path from subprocess import check_output from textwrap import dedent @@ -17,6 +16,7 @@ from typing import Any, Iterable, Optional, Pattern, TypedDict, Union import yaml from filecache import DAY, filecache +from gitlab_common import get_token_from_default_dir from gql import Client, gql from gql.transport.requests import RequestsHTTPTransport from graphql import DocumentNode @@ -34,18 +34,6 @@ Dag = dict[str, DagNode] StageSeq = OrderedDict[str, set[str]] -TOKEN_DIR = Path(getenv("XDG_CONFIG_HOME") or Path.home() / ".config") - - -def get_token_from_default_dir() -> str: - 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 get_project_root_dir():