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 <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-24 18:07:56 -07:00
parent da55aa6491
commit 0d734eb07d
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
6 changed files with 82 additions and 1 deletions

View file

@ -88,9 +88,17 @@ def register_function(name: str) -> Callable:
def make_context(request: Request, title: str, next: str = None): def make_context(request: Request, title: str, next: str = None):
""" Create a context for a jinja2 TemplateResponse. """ """ 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) timezone = time.get_request_timezone(request)
return { return {
"request": request, "request": request,
"commit_url": commit_url,
"commit_hash": commit_hash,
"language": l10n.get_request_language(request), "language": l10n.get_request_language(request),
"languages": l10n.SUPPORTED_LANGUAGES, "languages": l10n.SUPPORTED_LANGUAGES,
"timezone": timezone, "timezone": timezone,

View file

@ -20,9 +20,14 @@ class Client:
host = "127.0.0.1" host = "127.0.0.1"
class URL:
path = "/"
class Request: class Request:
""" A fake Request object which mimics a FastAPI Request for tests. """ """ A fake Request object which mimics a FastAPI Request for tests. """
client = Client() client = Client()
cookies = dict() cookies = dict()
headers = dict() headers = dict()
user = User() user = User()
url = URL()

View file

@ -104,3 +104,15 @@ server = ftp://mirrors.kernel.org/archlinux/%s/os/x86_64
packagesfile = /srv/http/aurweb/web/html/packages.gz packagesfile = /srv/http/aurweb/web/html/packages.gz
pkgbasefile = /srv/http/aurweb/web/html/pkgbase.gz pkgbasefile = /srv/http/aurweb/web/html/pkgbase.gz
userfile = /srv/http/aurweb/web/html/users.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

View file

@ -62,3 +62,6 @@ bind_address = 127.0.0.1:8082
; Passphrase FastAPI uses to sign client-side sessions. ; Passphrase FastAPI uses to sign client-side sessions.
session_secret = secret session_secret = secret
[devel]
;commit_hash = 1234567

View file

@ -1,5 +1,17 @@
<div id="footer"> <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>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> <p>{% trans %}AUR packages are user produced content. Any use of the provided files is at your own risk.{% endtrans %}</p>
</div> </div>

View file

@ -1,6 +1,8 @@
import pytest import pytest
from aurweb import config, templates
from aurweb.templates import register_filter, register_function from aurweb.templates import register_filter, register_function
from aurweb.testing.requests import Request
@register_filter("func") @register_filter("func")
@ -29,3 +31,42 @@ def test_register_function_exists_key_error():
@register_function("function") @register_function("function")
def some_func(): def some_func():
pass 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