From 97ec8e60dc3a7c2616690f0b78eba8a53bf49cdd Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Wed, 21 Feb 2024 15:18:25 -0500 Subject: [PATCH] Experiment with determining GitHub branch name --- example_docs/docs/conf.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/example_docs/docs/conf.py b/example_docs/docs/conf.py index 9f466940..c522f0da 100644 --- a/example_docs/docs/conf.py +++ b/example_docs/docs/conf.py @@ -11,6 +11,7 @@ # that they have been altered from the originals. import os +import re import sys # This allows autodoc to find the `api_example` folder. @@ -100,3 +101,31 @@ # Default image for thumbnails. "**": "_static/images/logo.png", } + + +def determine_github_branch() -> str: + """Determine the GitHub branch name to use for source code links. + + We need to decide whether to use `stable/` vs. `main` for dev builds. Refer to https://docs.github.com/en/actions/learn-github-actions/variables for how we determine this with GitHub Actions. + """ + # If not `GITHUB_REF_NAME` is not set, default to `main`. This + # is relevant for local builds. + if "GITHUB_REF_NAME" not in os.environ: + return "main" + + # PR workflows set the branch they're merging into. + if base_ref := os.environ.get("GITHUB_BASE_REF"): + return base_ref + + ref_name = os.environ["GITHUB_REF_NAME"] + if os.environ["GITHUB_REF_TYPE"] == "branch": + return ref_name + + # Else, the ref_name is a tag like `1.0.0` or `1.0.0rc1`. We need + # to transform this to a Git branch like `stable/1.0`. + version_without_patch = re.match(r"(\d+\.\d+)", ref_name).group() + return f"stable/{version_without_patch}" + + +GITHUB_BRANCH = determine_github_branch() +raise ValueError(GITHUB_BRANCH)