Verified Commit 0d734eb0 authored by Kevin Morris's avatar Kevin Morris
Browse files

feat(fastapi): add configurable commit hash display



Two new options have been added:

- [devel] commit_url
    - URL including an %s format specifier that can be used to link
      to a webpage for the commit.
- [devel] commit_hash
    - HEAD's commit hash (produced via `git rev-parse HEAD`)

If a `[devel] commit_hash` is configured, a link to the commit based on
`[devel] commit_url` will be displayed in the aurweb footer in
the form: `HEAD@<commit_hash>`. If no `[devel] commit_url` is
configured, a non-linked hash will be displayed.
Signed-off-by: Kevin Morris's avatarKevin Morris <kevr@0cost.org>
parent da55aa64
Pipeline #12561 passed with stage
in 6 minutes and 28 seconds
......@@ -88,9 +88,17 @@ def register_function(name: str) -> Callable:
def make_context(request: Request, title: str, next: str = None):
""" Create a context for a jinja2 TemplateResponse. """
commit_url = aurweb.config.get_with_fallback("devel", "commit_url", None)
commit_hash = aurweb.config.get_with_fallback("devel", "commit_hash", None)
if commit_hash:
# Shorten commit_hash to a short Git hash.
commit_hash = commit_hash[:7]
timezone = time.get_request_timezone(request)
return {
"request": request,
"commit_url": commit_url,
"commit_hash": commit_hash,
"language": l10n.get_request_language(request),
"languages": l10n.SUPPORTED_LANGUAGES,
"timezone": timezone,
......
......@@ -20,9 +20,14 @@ class Client:
host = "127.0.0.1"
class URL:
path = "/"
class Request:
""" A fake Request object which mimics a FastAPI Request for tests. """
client = Client()
cookies = dict()
headers = dict()
user = User()
url = URL()
......@@ -104,3 +104,15 @@ server = ftp://mirrors.kernel.org/archlinux/%s/os/x86_64
packagesfile = /srv/http/aurweb/web/html/packages.gz
pkgbasefile = /srv/http/aurweb/web/html/pkgbase.gz
userfile = /srv/http/aurweb/web/html/users.gz
[devel]
; commit_url is a format string used to produce a link to a commit hash.
commit_url = https://gitlab.archlinux.org/archlinux/aurweb/-/commits/%s
; If commit_hash is configured, a link to the commit based on commit_url
; will be displayed in aurweb's footer with the release version.
; This allows us to diagnose which commit a particular instance is on
; during testing of development branches.
; Example deployment configuration step:
; sed -r "s/^;?(commit_hash) =.*$/\1 = $(git rev-parse HEAD)/" config
;commit_hash = 1234567
......@@ -62,3 +62,6 @@ bind_address = 127.0.0.1:8082
; Passphrase FastAPI uses to sign client-side sessions.
session_secret = secret
[devel]
;commit_hash = 1234567
<div id="footer">
<p>aurweb <a href="https://git.archlinux.org/aurweb.git/log/?h={{ config.AURWEB_VERSION }}">{{ config.AURWEB_VERSION }}</a></p>
<p>
aurweb <a href="https://git.archlinux.org/aurweb.git/log/?h={{ config.AURWEB_VERSION }}">{{ config.AURWEB_VERSION }}</a>
{% if commit_hash %}
-
{% if commit_url %}
<a href="{{ commit_url | format(commit_hash) }}">
HEAD@{{ commit_hash }}
</a>
{% else %}
HEAD@{{ commit_hash }}
{% endif %}
{% endif %}
</p>
<p>Copyright &copy; 2004-{{ now.strftime("%Y") }} aurweb Development Team.</p>
<p>{% trans %}AUR packages are user produced content. Any use of the provided files is at your own risk.{% endtrans %}</p>
</div>
import pytest
from aurweb import config, templates
from aurweb.templates import register_filter, register_function
from aurweb.testing.requests import Request
@register_filter("func")
......@@ -29,3 +31,42 @@ def test_register_function_exists_key_error():
@register_function("function")
def some_func():
pass
def test_commit_hash():
# Hashes we'll use for this test. long_commit_hash should be
# shortened to commit_hash for rendering.
commit_hash = "abcdefg"
long_commit_hash = commit_hash + "1234567"
def config_get_with_fallback(section: str, option: str,
fallback: str = None) -> str:
if section == "devel" and option == "commit_hash":
return long_commit_hash
return config.original_get_with_fallback(section, option, fallback)
# Fake config.get_with_fallback.
config.original_get_with_fallback = config.get_with_fallback
config.get_with_fallback = config_get_with_fallback
request = Request()
context = templates.make_context(request, "Test Context")
render = templates.render_raw_template(request, "index.html", context)
# We've faked config.get_with_fallback to return a "valid" commit_hash
# when queried. Test that the expected render occurs.
commit_url = config.get("devel", "commit_url")
expected = commit_url % commit_hash
assert expected in render
assert f"HEAD@{commit_hash}" in render
assert long_commit_hash not in render
# Restore config.get_with_fallback.
config.get_with_fallback = config.original_get_with_fallback
config.original_get_with_fallback = None
# Now, we no longer fake the commit_hash option: no commit
# is displayed in the footer. Assert this expectation.
context = templates.make_context(request, "Test Context")
render = templates.render_raw_template(request, "index.html", context)
assert commit_hash not in render
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment