From 1a55e92824d56a34a677c1f764a5393038e66955 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 22 Aug 2020 01:40:55 +0200 Subject: Add a context processor that outputs the git SHA. This will make the SHA available in all templates. --- pydis_site/context_processors.py | 10 ++++++++++ pydis_site/settings.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 pydis_site/context_processors.py (limited to 'pydis_site') diff --git a/pydis_site/context_processors.py b/pydis_site/context_processors.py new file mode 100644 index 00000000..4ae0dbb3 --- /dev/null +++ b/pydis_site/context_processors.py @@ -0,0 +1,10 @@ +import git +from django.template import RequestContext + +REPO = git.Repo(search_parent_directories=True) +SHA = REPO.head.object.hexsha + + +def git_sha_processor(_: RequestContext) -> dict: + """Expose the git SHA for this repo to all views.""" + return {'git_sha': SHA} diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 2c87007c..afa097be 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -157,8 +157,8 @@ TEMPLATES = [ 'django.template.context_processors.static', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', - "sekizai.context_processors.sekizai", + "pydis_site.context_processors.git_sha_processor" ], }, }, -- cgit v1.2.3 From 75181a55977abe2eb6f3ac782b51bf13b8945024 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 22 Aug 2020 01:41:20 +0200 Subject: Add a comment with the git SHA to base templates. --- pydis_site/templates/base/base.html | 1 + pydis_site/templates/wiki/base.html | 1 + 2 files changed, 2 insertions(+) (limited to 'pydis_site') diff --git a/pydis_site/templates/base/base.html b/pydis_site/templates/base/base.html index 4c70d778..70426dc1 100644 --- a/pydis_site/templates/base/base.html +++ b/pydis_site/templates/base/base.html @@ -37,6 +37,7 @@ {% render_block "css" %} +
{% if messages %} diff --git a/pydis_site/templates/wiki/base.html b/pydis_site/templates/wiki/base.html index 846492ab..c9faf914 100644 --- a/pydis_site/templates/wiki/base.html +++ b/pydis_site/templates/wiki/base.html @@ -19,6 +19,7 @@ {% endblock %} {% block content %} + {% block site_navbar %} {% include "base/navbar.html" %} {% endblock %} -- cgit v1.2.3 From 60bc529e4b1cfa59e82ede102b88fca08f8f93b4 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 22 Aug 2020 02:04:44 +0200 Subject: Move git SHA initialization to __init__.py. This will make it easier to use in multiple places. --- pydis_site/__init__.py | 5 +++++ pydis_site/context_processors.py | 6 ++---- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'pydis_site') diff --git a/pydis_site/__init__.py b/pydis_site/__init__.py index df67cf71..f0702577 100644 --- a/pydis_site/__init__.py +++ b/pydis_site/__init__.py @@ -1,3 +1,4 @@ +import git from wiki.plugins.macros.mdx import toc # Remove the toc header prefix. There's no option for this, so we gotta monkey patch it. @@ -7,3 +8,7 @@ toc.HEADER_ID_PREFIX = '' # by a string because Allauth won't let us just give it a list _there_, we have to point # at a list _somewhere else_ instead. VALIDATORS = [] + +# Git SHA +repo = git.Repo(search_parent_directories=True) +GIT_SHA = repo.head.object.hexsha diff --git a/pydis_site/context_processors.py b/pydis_site/context_processors.py index 4ae0dbb3..bb66f21d 100644 --- a/pydis_site/context_processors.py +++ b/pydis_site/context_processors.py @@ -1,10 +1,8 @@ -import git from django.template import RequestContext -REPO = git.Repo(search_parent_directories=True) -SHA = REPO.head.object.hexsha +from pydis_site import GIT_SHA def git_sha_processor(_: RequestContext) -> dict: """Expose the git SHA for this repo to all views.""" - return {'git_sha': SHA} + return {'git_sha': GIT_SHA} -- cgit v1.2.3 From 212a06f400bfce95281e4c8d11baf6cb09e70861 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 22 Aug 2020 02:05:24 +0200 Subject: Set the sentry_sdk.init release to git_sha. This is the first step in implementing releases for Sentry. --- pydis_site/settings.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'pydis_site') diff --git a/pydis_site/settings.py b/pydis_site/settings.py index afa097be..ff2942f0 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -20,6 +20,7 @@ import sentry_sdk from django.contrib.messages import constants as messages from sentry_sdk.integrations.django import DjangoIntegration +from pydis_site import GIT_SHA if typing.TYPE_CHECKING: from django.contrib.auth.models import User @@ -33,7 +34,8 @@ env = environ.Env( sentry_sdk.init( dsn=env('SITE_SENTRY_DSN'), integrations=[DjangoIntegration()], - send_default_pii=True + send_default_pii=True, + release=f"pydis-site@{GIT_SHA}" ) # Build paths inside the project like this: os.path.join(BASE_DIR, ...) -- cgit v1.2.3 From 416a0874187fdf96346e3f504d6542b3214957db Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 22 Aug 2020 02:49:28 +0200 Subject: Move git SHA fetcher into utils. --- pydis_site/__init__.py | 5 ----- pydis_site/context_processors.py | 4 ++-- pydis_site/settings.py | 4 ++-- pydis_site/utils/resources.py | 10 ++++++++++ 4 files changed, 14 insertions(+), 9 deletions(-) (limited to 'pydis_site') diff --git a/pydis_site/__init__.py b/pydis_site/__init__.py index f0702577..df67cf71 100644 --- a/pydis_site/__init__.py +++ b/pydis_site/__init__.py @@ -1,4 +1,3 @@ -import git from wiki.plugins.macros.mdx import toc # Remove the toc header prefix. There's no option for this, so we gotta monkey patch it. @@ -8,7 +7,3 @@ toc.HEADER_ID_PREFIX = '' # by a string because Allauth won't let us just give it a list _there_, we have to point # at a list _somewhere else_ instead. VALIDATORS = [] - -# Git SHA -repo = git.Repo(search_parent_directories=True) -GIT_SHA = repo.head.object.hexsha diff --git a/pydis_site/context_processors.py b/pydis_site/context_processors.py index bb66f21d..e905d9c7 100644 --- a/pydis_site/context_processors.py +++ b/pydis_site/context_processors.py @@ -1,8 +1,8 @@ from django.template import RequestContext -from pydis_site import GIT_SHA +from pydis_site.utils.resources import get_git_sha def git_sha_processor(_: RequestContext) -> dict: """Expose the git SHA for this repo to all views.""" - return {'git_sha': GIT_SHA} + return {'git_sha': get_git_sha()} diff --git a/pydis_site/settings.py b/pydis_site/settings.py index ff2942f0..e707a526 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -20,7 +20,7 @@ import sentry_sdk from django.contrib.messages import constants as messages from sentry_sdk.integrations.django import DjangoIntegration -from pydis_site import GIT_SHA +from pydis_site.utils.resources import get_git_sha if typing.TYPE_CHECKING: from django.contrib.auth.models import User @@ -35,7 +35,7 @@ sentry_sdk.init( dsn=env('SITE_SENTRY_DSN'), integrations=[DjangoIntegration()], send_default_pii=True, - release=f"pydis-site@{GIT_SHA}" + release=f"pydis-site@{get_git_sha()}" ) # Build paths inside the project like this: os.path.join(BASE_DIR, ...) diff --git a/pydis_site/utils/resources.py b/pydis_site/utils/resources.py index 637fd785..d36c4b77 100644 --- a/pydis_site/utils/resources.py +++ b/pydis_site/utils/resources.py @@ -4,8 +4,13 @@ import glob import typing from dataclasses import dataclass +import git import yaml +# Git SHA +repo = git.Repo(search_parent_directories=True) +GIT_SHA = repo.head.object.hexsha + @dataclass class URL: @@ -89,3 +94,8 @@ def load_categories(order: typing.List[str]) -> typing.List[Category]: categories.append(Category.construct_from_directory(direc)) return categories + + +def get_git_sha() -> str: + """Get the Git SHA for this repo.""" + return GIT_SHA -- cgit v1.2.3 From 503bfba6855d3019a2739bb1adce69c457a8b26e Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 22 Aug 2020 03:18:25 +0200 Subject: Move git SHA fetcher into its own file. Fix tests. --- pydis_site/context_processors.py | 2 +- pydis_site/settings.py | 2 +- pydis_site/tests/test_utils.py | 11 +++++++++++ pydis_site/utils/__init__.py | 3 +++ pydis_site/utils/resources.py | 10 ---------- pydis_site/utils/utils.py | 10 ++++++++++ 6 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 pydis_site/tests/test_utils.py create mode 100644 pydis_site/utils/__init__.py create mode 100644 pydis_site/utils/utils.py (limited to 'pydis_site') diff --git a/pydis_site/context_processors.py b/pydis_site/context_processors.py index e905d9c7..ab5a4168 100644 --- a/pydis_site/context_processors.py +++ b/pydis_site/context_processors.py @@ -1,6 +1,6 @@ from django.template import RequestContext -from pydis_site.utils.resources import get_git_sha +from pydis_site.utils import get_git_sha def git_sha_processor(_: RequestContext) -> dict: diff --git a/pydis_site/settings.py b/pydis_site/settings.py index e707a526..0a5b0eed 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -20,7 +20,7 @@ import sentry_sdk from django.contrib.messages import constants as messages from sentry_sdk.integrations.django import DjangoIntegration -from pydis_site.utils.resources import get_git_sha +from pydis_site.utils import get_git_sha if typing.TYPE_CHECKING: from django.contrib.auth.models import User diff --git a/pydis_site/tests/test_utils.py b/pydis_site/tests/test_utils.py new file mode 100644 index 00000000..f1419860 --- /dev/null +++ b/pydis_site/tests/test_utils.py @@ -0,0 +1,11 @@ +from django.test import TestCase + +from pydis_site.utils import get_git_sha +from pydis_site.utils.utils import GIT_SHA + + +class UtilsTests(TestCase): + + def test_git_sha(self): + """Test that the get_git_sha returns the correct SHA.""" + self.assertEqual(get_git_sha(), GIT_SHA) diff --git a/pydis_site/utils/__init__.py b/pydis_site/utils/__init__.py new file mode 100644 index 00000000..bb91b3d8 --- /dev/null +++ b/pydis_site/utils/__init__.py @@ -0,0 +1,3 @@ +from .utils import get_git_sha + +__all__ = ['get_git_sha'] diff --git a/pydis_site/utils/resources.py b/pydis_site/utils/resources.py index d36c4b77..637fd785 100644 --- a/pydis_site/utils/resources.py +++ b/pydis_site/utils/resources.py @@ -4,13 +4,8 @@ import glob import typing from dataclasses import dataclass -import git import yaml -# Git SHA -repo = git.Repo(search_parent_directories=True) -GIT_SHA = repo.head.object.hexsha - @dataclass class URL: @@ -94,8 +89,3 @@ def load_categories(order: typing.List[str]) -> typing.List[Category]: categories.append(Category.construct_from_directory(direc)) return categories - - -def get_git_sha() -> str: - """Get the Git SHA for this repo.""" - return GIT_SHA diff --git a/pydis_site/utils/utils.py b/pydis_site/utils/utils.py new file mode 100644 index 00000000..2033ea19 --- /dev/null +++ b/pydis_site/utils/utils.py @@ -0,0 +1,10 @@ +import git + +# Git SHA +repo = git.Repo(search_parent_directories=True) +GIT_SHA = repo.head.object.hexsha + + +def get_git_sha() -> str: + """Get the Git SHA for this repo.""" + return GIT_SHA -- cgit v1.2.3 From 88d364bfc809d20c6c4645abba75b889a526c804 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 22 Aug 2020 03:46:08 +0200 Subject: Remove the SHA from the wiki base.html This extends the regular base.html, so this would cause wiki pages to have two SHA's. --- pydis_site/templates/wiki/base.html | 1 - 1 file changed, 1 deletion(-) (limited to 'pydis_site') diff --git a/pydis_site/templates/wiki/base.html b/pydis_site/templates/wiki/base.html index c9faf914..846492ab 100644 --- a/pydis_site/templates/wiki/base.html +++ b/pydis_site/templates/wiki/base.html @@ -19,7 +19,6 @@ {% endblock %} {% block content %} - {% block site_navbar %} {% include "base/navbar.html" %} {% endblock %} -- cgit v1.2.3 From a71c38f07bba0191ba28eef12eea56e8a9265349 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Sat, 22 Aug 2020 03:54:51 +0200 Subject: Move the SHA into constants.py. The util was redundant. Thanks @MarkKoz --- pydis_site/constants.py | 5 +++++ pydis_site/context_processors.py | 4 ++-- pydis_site/settings.py | 4 ++-- pydis_site/tests/test_utils.py | 11 ----------- pydis_site/utils/__init__.py | 3 --- pydis_site/utils/utils.py | 10 ---------- 6 files changed, 9 insertions(+), 28 deletions(-) create mode 100644 pydis_site/constants.py delete mode 100644 pydis_site/tests/test_utils.py delete mode 100644 pydis_site/utils/__init__.py delete mode 100644 pydis_site/utils/utils.py (limited to 'pydis_site') diff --git a/pydis_site/constants.py b/pydis_site/constants.py new file mode 100644 index 00000000..0b76694a --- /dev/null +++ b/pydis_site/constants.py @@ -0,0 +1,5 @@ +import git + +# Git SHA +repo = git.Repo(search_parent_directories=True) +GIT_SHA = repo.head.object.hexsha diff --git a/pydis_site/context_processors.py b/pydis_site/context_processors.py index ab5a4168..6937a3db 100644 --- a/pydis_site/context_processors.py +++ b/pydis_site/context_processors.py @@ -1,8 +1,8 @@ from django.template import RequestContext -from pydis_site.utils import get_git_sha +from pydis_site.constants import GIT_SHA def git_sha_processor(_: RequestContext) -> dict: """Expose the git SHA for this repo to all views.""" - return {'git_sha': get_git_sha()} + return {'git_sha': GIT_SHA} diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 0a5b0eed..1f042c1b 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -20,7 +20,7 @@ import sentry_sdk from django.contrib.messages import constants as messages from sentry_sdk.integrations.django import DjangoIntegration -from pydis_site.utils import get_git_sha +from pydis_site.constants import GIT_SHA if typing.TYPE_CHECKING: from django.contrib.auth.models import User @@ -35,7 +35,7 @@ sentry_sdk.init( dsn=env('SITE_SENTRY_DSN'), integrations=[DjangoIntegration()], send_default_pii=True, - release=f"pydis-site@{get_git_sha()}" + release=f"pydis-site@{GIT_SHA}" ) # Build paths inside the project like this: os.path.join(BASE_DIR, ...) diff --git a/pydis_site/tests/test_utils.py b/pydis_site/tests/test_utils.py deleted file mode 100644 index f1419860..00000000 --- a/pydis_site/tests/test_utils.py +++ /dev/null @@ -1,11 +0,0 @@ -from django.test import TestCase - -from pydis_site.utils import get_git_sha -from pydis_site.utils.utils import GIT_SHA - - -class UtilsTests(TestCase): - - def test_git_sha(self): - """Test that the get_git_sha returns the correct SHA.""" - self.assertEqual(get_git_sha(), GIT_SHA) diff --git a/pydis_site/utils/__init__.py b/pydis_site/utils/__init__.py deleted file mode 100644 index bb91b3d8..00000000 --- a/pydis_site/utils/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from .utils import get_git_sha - -__all__ = ['get_git_sha'] diff --git a/pydis_site/utils/utils.py b/pydis_site/utils/utils.py deleted file mode 100644 index 2033ea19..00000000 --- a/pydis_site/utils/utils.py +++ /dev/null @@ -1,10 +0,0 @@ -import git - -# Git SHA -repo = git.Repo(search_parent_directories=True) -GIT_SHA = repo.head.object.hexsha - - -def get_git_sha() -> str: - """Get the Git SHA for this repo.""" - return GIT_SHA -- cgit v1.2.3