Skip to content

Commit

Permalink
ENH: get branch info from GITHUB_REF if available
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Dec 9, 2023
1 parent ab1ff4d commit d1c674d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
29 changes: 23 additions & 6 deletions src/sphinx_api_relink/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
10 changes: 3 additions & 7 deletions src/sphinx_api_relink/linkcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit d1c674d

Please sign in to comment.