Add detection of curroent commit / branch (#7745)

this ensures runs are reproducible/replacable and doc contents stay up
This commit is contained in:
Matthias Mair 2024-07-30 04:28:36 +02:00 committed by GitHub
parent 06a95f944e
commit 413cc0ce20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,7 +2,6 @@
import os import os
import subprocess import subprocess
import textwrap
import requests import requests
import yaml import yaml
@ -54,11 +53,23 @@ def check_link(url) -> bool:
return False 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): def define_env(env):
"""Define custom environment variables for the documentation build process.""" """Define custom environment variables for the documentation build process."""
@env.macro @env.macro
def sourcedir(dirname, branch='master'): def sourcedir(dirname, branch=None):
"""Return a link to a directory within the source code repository. """Return a link to a directory within the source code repository.
Arguments: Arguments:
@ -70,6 +81,9 @@ def define_env(env):
Raises: Raises:
- FileNotFoundError: If the directory does not exist, or the generated URL is invalid - FileNotFoundError: If the directory does not exist, or the generated URL is invalid
""" """
if branch == None:
branch = get_build_enviroment()
if dirname.startswith('/'): if dirname.startswith('/'):
dirname = dirname[1:] dirname = dirname[1:]
@ -94,7 +108,7 @@ def define_env(env):
return url return url
@env.macro @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. """Return a link to a file within the source code repository.
Arguments: Arguments:
@ -106,6 +120,9 @@ def define_env(env):
Raises: Raises:
- FileNotFoundError: If the file does not exist, or the generated URL is invalid - FileNotFoundError: If the file does not exist, or the generated URL is invalid
""" """
if branch == None:
branch = get_build_enviroment()
if filename.startswith('/'): if filename.startswith('/'):
filename = filename[1:] filename = filename[1:]