Skip to content

Commit

Permalink
issue_567 add github version info to the footer (#602)
Browse files Browse the repository at this point in the history
* issue_567 add github version info to the footer

* issue_567 read git info from environment variables instead
  • Loading branch information
zqian authored Jan 16, 2025
1 parent fdc5e60 commit 3a7f38b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions src/officehours/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
],
},
},
Expand Down
39 changes: 39 additions & 0 deletions src/officehours_ui/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down Expand Up @@ -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
}
}
31 changes: 21 additions & 10 deletions src/officehours_ui/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,29 @@
</script>
{% block scripts %}{% endblock %}


{% get_flatpages '/page-footer/' as flatpages %}
{% if flatpages.first and flatpages.first.content %}
<footer class="sticky-footer">
<div class="alert alert-info alert-footer" role="alert">
<div class="alert-footer-text">
{{flatpages.first.content|safe}}
</div>
<footer class="sticky-footer">
<div class="alert alert-info alert-footer" role="alert">
<div class="alert-footer-text">
<table cellspacing="8" style="width: 100%; padding: 8px">
<tr>
{% if flatpages.first and flatpages.first.content %}
<td style="text-align: center; vertical-align: middle;">
{{ flatpages.first.content|safe }}
</td>
{% endif %}
{% if user.is_staff and git_version %}
<td style="text-align: center; vertical-align: middle; color: white;">
Git version: <a style="color: white" href="{{ git_version.repo }}/commit/{{ git_version.commit }}" target="_blank">{{ git_version.commit_abbrev }}</a>
(commit)
<a style="color: white" href="{{ git_version.repo }}/tree/{{ git_version.branch }}" target="_blank"> {{ git_version.branch }}</a> (branch)
</td>
{% endif %}
</tr>
</table>
</div>
</footer>
{% endif %}

</div>
</footer>
</body>
</html>

Expand Down

0 comments on commit 3a7f38b

Please sign in to comment.