From b985ba6950e2abb50e78b4b83d6ab479060e5610 Mon Sep 17 00:00:00 2001 From: Melech Lapson Date: Wed, 21 Feb 2024 15:07:48 -0600 Subject: [PATCH] Added a method to determine the GitHub branch Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> --- docs/conf.py | 33 ++++++++++++++++++++++++++++++--- tox.ini | 2 +- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index c02714188de0..8a7217b4dbce 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -19,6 +19,8 @@ import datetime import doctest import inspect +import os +import re import sys from pathlib import PurePath @@ -163,6 +165,33 @@ # ---------------------------------------------------------------------------------- # Source code links # ---------------------------------------------------------------------------------- +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. + """ + print(os.environ) + # 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() + + def linkcode_resolve(domain, info): if domain != "py": return None @@ -194,6 +223,4 @@ def linkcode_resolve(domain, info): ending_lineno = lineno + len(source) - 1 linespec = f"#L{lineno}-L{ending_lineno}" - git_branch = "main" - - return f"https://github.com/Qiskit/qiskit/tree/{git_branch}/{file_name}{linespec}" + return f"https://github.com/Qiskit/qiskit/tree/{GITHUB_BRANCH}/{file_name}{linespec}" diff --git a/tox.ini b/tox.ini index b9001f6db5d4..d555431debb3 100644 --- a/tox.ini +++ b/tox.ini @@ -15,7 +15,7 @@ setenv = QISKIT_SUPRESS_PACKAGING_WARNINGS=Y QISKIT_TEST_CAPTURE_STREAMS=1 QISKIT_PARALLEL=FALSE -passenv = RAYON_NUM_THREADS, OMP_NUM_THREADS, QISKIT_PARALLEL, RUST_BACKTRACE, SETUPTOOLS_ENABLE_FEATURES, QISKIT_TESTS, QISKIT_IN_PARALLEL +passenv = RAYON_NUM_THREADS, OMP_NUM_THREADS, QISKIT_PARALLEL, RUST_BACKTRACE, SETUPTOOLS_ENABLE_FEATURES, QISKIT_TESTS, QISKIT_IN_PARALLEL, GITHUB_REF_NAME, GITHUB_REF_TYPE, GITHUB_BASE_REF deps = setuptools_rust # This is work around for the bug of tox 3 (see #8606 for more details.) -r{toxinidir}/requirements.txt