Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: get branch info from GITHUB_REF if available #13

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading