diff --git a/aurweb/templates.py b/aurweb/templates.py index 83ce0fbd..42e335b1 100644 --- a/aurweb/templates.py +++ b/aurweb/templates.py @@ -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, diff --git a/aurweb/testing/requests.py b/aurweb/testing/requests.py index 9976b6fb..a8c077db 100644 --- a/aurweb/testing/requests.py +++ b/aurweb/testing/requests.py @@ -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() diff --git a/conf/config.defaults b/conf/config.defaults index da969343..b078e57c 100644 --- a/conf/config.defaults +++ b/conf/config.defaults @@ -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 diff --git a/conf/config.dev b/conf/config.dev index b8bd5bdb..fb43612e 100644 --- a/conf/config.dev +++ b/conf/config.dev @@ -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 diff --git a/templates/partials/footer.html b/templates/partials/footer.html index 0ac4d089..dde03c96 100644 --- a/templates/partials/footer.html +++ b/templates/partials/footer.html @@ -1,5 +1,17 @@ diff --git a/test/test_templates.py b/test/test_templates.py index 86fbf611..a483cb51 100644 --- a/test/test_templates.py +++ b/test/test_templates.py @@ -1,6 +1,8 @@ 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