From 3a7f38b484a9cbae62711f3140b0856ac356292f Mon Sep 17 00:00:00 2001 From: Zhen Qian Date: Thu, 16 Jan 2025 16:53:42 -0500 Subject: [PATCH] issue_567 add github version info to the footer (#602) * issue_567 add github version info to the footer * issue_567 read git info from environment variables instead --- src/docker-entrypoint.sh | 5 +++ src/officehours/settings.py | 4 +++ src/officehours_ui/context_processors.py | 39 ++++++++++++++++++++++++ src/officehours_ui/templates/base.html | 31 +++++++++++++------ 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/docker-entrypoint.sh b/src/docker-entrypoint.sh index f53c8752..a24527ec 100755 --- a/src/docker-entrypoint.sh +++ b/src/docker-entrypoint.sh @@ -2,6 +2,11 @@ set -e +# Load in git version information +source /etc/git.version + +echo "Loaded Git information: ${GIT_REPO} ${GIT_BRANCH} ${GIT_COMMIT}" + python manage.py collectstatic --noinput python manage.py migrate diff --git a/src/officehours/settings.py b/src/officehours/settings.py index 79ed9c0c..5198790e 100644 --- a/src/officehours/settings.py +++ b/src/officehours/settings.py @@ -142,6 +142,9 @@ def str_to_bool(val): ROOT_URLCONF = 'officehours.urls' +# Git info settings +SHA_ABBREV_LENGTH = 7 + TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', @@ -157,6 +160,7 @@ def str_to_bool(val): 'officehours_ui.context_processors.debug', 'officehours_ui.context_processors.login_url', 'officehours_ui.context_processors.spa_globals', + 'officehours_ui.context_processors.get_git_version_info', ], }, }, diff --git a/src/officehours_ui/context_processors.py b/src/officehours_ui/context_processors.py index 6d3e8394..4a08a5a8 100644 --- a/src/officehours_ui/context_processors.py +++ b/src/officehours_ui/context_processors.py @@ -6,6 +6,8 @@ from officehours_api.backends import __all__ as IMPLEMENTED_BACKEND_NAMES from officehours_api.backends.types import BackendDict +import os + def feedback(request): return {'FEEDBACK_EMAIL': getattr(settings, 'FEEDBACK_EMAIL', None)} @@ -44,3 +46,40 @@ def spa_globals(request): 'otp_request_buffer': settings.OTP_REQUEST_BUFFER, } } + +def format_github_url_using_https(github_url: str): + ssh_base = "git@" + https_base = "https://" + # If the URL is formatted for SSH, convert, otherwise, replace .git extension with "" + if ssh_base == github_url[:len(ssh_base)]: + github_url = github_url.replace(":", "/").replace(".git", "").replace(ssh_base, https_base) + else: + github_url = github_url.replace(".git", "") + return github_url + +def get_git_version_info(request): + # read git version info from environment variables + # else, return None + repo = os.getenv("GIT_REPO", None) + branch = os.getenv("GIT_BRANCH", None) + commit = os.getenv("GIT_COMMIT", None) + + if not repo or not branch or not commit: + return None + + # Only include the branch name and not remote info + branch = branch.split('/')[-1] + + commit_abbrev = ( + commit[:settings.SHA_ABBREV_LENGTH] + if len(commit) > settings.SHA_ABBREV_LENGTH else commit + ) + + return { + 'git_version': { + "repo": format_github_url_using_https(repo), + "branch": branch, + "commit": commit, + "commit_abbrev": commit_abbrev + } + } diff --git a/src/officehours_ui/templates/base.html b/src/officehours_ui/templates/base.html index 35f6bd77..187762fb 100644 --- a/src/officehours_ui/templates/base.html +++ b/src/officehours_ui/templates/base.html @@ -119,18 +119,29 @@ {% block scripts %}{% endblock %} - {% get_flatpages '/page-footer/' as flatpages %} - {% if flatpages.first and flatpages.first.content %} -