From d1c674d6e98a645459b5ca808bee5cb778cb7a1a Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Sat, 9 Dec 2023 16:50:36 +0100 Subject: [PATCH] ENH: get branch info from `GITHUB_REF` if available --- src/sphinx_api_relink/helpers.py | 29 +++++++++++++++++++++++------ src/sphinx_api_relink/linkcode.py | 10 +++------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/sphinx_api_relink/helpers.py b/src/sphinx_api_relink/helpers.py index f9a97c0..f51ce60 100644 --- a/src/sphinx_api_relink/helpers.py +++ b/src/sphinx_api_relink/helpers.py @@ -6,25 +6,36 @@ import os import re import sys +from functools import lru_cache + +from colorama import Fore, Style if sys.version_info < (3, 8): from importlib_metadata import version else: from importlib.metadata import version +__DEFAULT_BRANCH = "main" __VERSION_REMAPPING: dict[str, dict[str, str]] = {} def get_branch_name() -> str: - """Get the branch name from the environment for Read the Docs. + """Get the branch name from the environment for GitHub or Read the Docs. - See https://docs.readthedocs.io/en/stable/builds.html. + See https://docs.readthedocs.io/en/stable/builds.html and + https://docs.github.com/en/actions/learn-github-actions/variables. """ - branch_name = os.environ.get("READTHEDOCS_VERSION", "stable") + branch_name = os.environ.get("READTHEDOCS_VERSION") if branch_name == "latest": - return "main" - if re.match(r"^\d+$", branch_name): # PR preview - return "stable" + return __DEFAULT_BRANCH + if branch_name is None: + branch_name = os.environ.get("GITHUB_REF", __DEFAULT_BRANCH) + branch_name = branch_name.replace("refs/heads/", "") + branch_name = branch_name.replace("refs/pull/", "") + branch_name = branch_name.replace("refs/tags/", "") + if re.match(r"^\d+/[a-z]+$", branch_name) is not None: + branch_name = __DEFAULT_BRANCH # PR preview + print_once(f"Linking pages to this Git ref: {branch_name}", color=Fore.MAGENTA) return branch_name @@ -128,3 +139,9 @@ def set_intersphinx_version_remapping( ) raise TypeError(msg) __VERSION_REMAPPING.update(version_remapping) + + +@lru_cache(maxsize=None) +def print_once(message: str, *, color: str = Fore.RED) -> None: + colored_text = f"{color}{message}{Style.RESET_ALL}" + print(colored_text) # noqa: T201 diff --git a/src/sphinx_api_relink/linkcode.py b/src/sphinx_api_relink/linkcode.py index d269a0f..b9681e8 100644 --- a/src/sphinx_api_relink/linkcode.py +++ b/src/sphinx_api_relink/linkcode.py @@ -11,7 +11,9 @@ from urllib.parse import quote import requests -from colorama import Fore, Style +from colorama import Fore + +from sphinx_api_relink.helpers import print_once if TYPE_CHECKING: from types import ModuleType @@ -166,9 +168,3 @@ def _url_exists(url: str) -> bool: return _url_exists(redirect_url) except requests.RequestException: return False - - -@lru_cache(maxsize=None) -def print_once(message: str, *, color: str = Fore.RED) -> None: - colored_text = f"{color}{message}{Style.RESET_ALL}" - print(colored_text) # noqa: T201