From 413cc0ce20a95e665b89829fa7655d1c289a1601 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Tue, 30 Jul 2024 04:28:36 +0200 Subject: [PATCH] Add detection of curroent commit / branch (#7745) this ensures runs are reproducible/replacable and doc contents stay up --- docs/main.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/main.py b/docs/main.py index 78a4339315..4dcac9fcf8 100644 --- a/docs/main.py +++ b/docs/main.py @@ -2,7 +2,6 @@ import os import subprocess -import textwrap import requests import yaml @@ -54,11 +53,23 @@ def check_link(url) -> bool: return False +def get_build_enviroment() -> str: + """Returns the branch we are currently building on, based on the environment variables of the various CI platforms.""" + # Check if we are in ReadTheDocs + if os.environ.get('READTHEDOCS') == 'True': + return os.environ.get('READTHEDOCS_GIT_IDENTIFIER') + # We are in GitHub Actions + elif os.environ.get('GITHUB_ACTIONS') == 'true': + return os.environ.get('GITHUB_REF') + else: + return 'master' + + def define_env(env): """Define custom environment variables for the documentation build process.""" @env.macro - def sourcedir(dirname, branch='master'): + def sourcedir(dirname, branch=None): """Return a link to a directory within the source code repository. Arguments: @@ -70,6 +81,9 @@ def define_env(env): Raises: - FileNotFoundError: If the directory does not exist, or the generated URL is invalid """ + if branch == None: + branch = get_build_enviroment() + if dirname.startswith('/'): dirname = dirname[1:] @@ -94,7 +108,7 @@ def define_env(env): return url @env.macro - def sourcefile(filename, branch='master', raw=False): + def sourcefile(filename, branch=None, raw=False): """Return a link to a file within the source code repository. Arguments: @@ -106,6 +120,9 @@ def define_env(env): Raises: - FileNotFoundError: If the file does not exist, or the generated URL is invalid """ + if branch == None: + branch = get_build_enviroment() + if filename.startswith('/'): filename = filename[1:]