Skip to content

Commit

Permalink
Experiment with determining GitHub branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Arellano committed Feb 21, 2024
1 parent d916732 commit 97ec8e6
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions example_docs/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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/<version>` 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)

0 comments on commit 97ec8e6

Please sign in to comment.