diff --git a/.github/workflows/deploy-pr-preview.yaml b/.github/workflows/deploy-pr-preview.yaml index 96faac0c9..3f25c868d 100644 --- a/.github/workflows/deploy-pr-preview.yaml +++ b/.github/workflows/deploy-pr-preview.yaml @@ -57,6 +57,7 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 with: + fetch-depth: 0 # Full commit history is needed for the 'Last updated / Created on' feature to display correctly on pages. submodules: true - name: Pull latest from submodules diff --git a/.github/workflows/deploy-production.yaml b/.github/workflows/deploy-production.yaml index 8bf86618f..dd53b650f 100644 --- a/.github/workflows/deploy-production.yaml +++ b/.github/workflows/deploy-production.yaml @@ -23,6 +23,7 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 with: + fetch-depth: 0 # Full commit history is needed for the 'Last updated / Created on' feature to display correctly on pages. submodules: true - name: Pull latest from submodules diff --git a/docs/_extensions/page_version_history_metadata.py b/docs/_extensions/page_version_history_metadata.py new file mode 100644 index 000000000..6899db2d1 --- /dev/null +++ b/docs/_extensions/page_version_history_metadata.py @@ -0,0 +1,66 @@ +# Adds metadata to pages based on the Git version history. It sources information from the 'last updated' commit and the 'created at' commit for each page. Metadata includes the date of the commit and the name of its author. This metadata can be used in layouts and templates, for example: {{ page_version_history_metadata.last_updated_date }} + +import os +import subprocess +import traceback + +def run_git_log(command): + result = subprocess.run( + command, + check=True, + encoding="UTF-8", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + + return result.stdout.strip().split('|') + +def handle_exception(error): + print(error) + traceback.print_exc() + pass + +def page_version_history_metadata(app, pagename, templatename, context, doctree): + page_filepath_absolute_with_extension_glob = os.path.join(app.srcdir, pagename + ".md") + + context["page_version_history_metadata"] = { + "last_updated_hash": None, + "last_updated_date": None, + "last_updated_author_name": None, + "last_updated_subject": None, + } + + try: + git_log_results = run_git_log( + [ + "git", + "log", + "-1", + "--date=format:%-d %b %Y", + "--pretty=format:%H|%ad|%an|%s", + "--", + # page_filepath_absolute_with_extension_glob, + "/home/runner/work/dea-knowledge-hub/dea-knowledge-hub/", # TODO This is a test + ] + ) + + print(page_filepath_absolute_with_extension_glob) # TODO Remove this after testing + print(git_log_results) # TODO Remove this after testing + + context["page_version_history_metadata"]["last_updated_hash"] = git_log_results[0] + + context["page_version_history_metadata"]["last_updated_date"] = git_log_results[1] + + context["page_version_history_metadata"]["last_updated_author_name"] = git_log_results[2] + + context["page_version_history_metadata"]["last_updated_subject"] = git_log_results[3] + + except (ValueError, Exception) as e: + handle_exception(e) + +def setup(app): + app.connect('html-page-context', page_version_history_metadata) + + return { + "version": "1" + } diff --git a/docs/_layouts/layout.html b/docs/_layouts/layout.html index 9dd212077..94ab14a03 100644 --- a/docs/_layouts/layout.html +++ b/docs/_layouts/layout.html @@ -34,3 +34,15 @@ {{ super() }} {% endblock %} +{% block body %} +{{ super() }} + +{% if page_version_history_metadata.last_updated_hash %} +
+{% endif %} +{% endblock %} + diff --git a/docs/conf.py b/docs/conf.py index 9da0b831e..8bd901f50 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -78,6 +78,7 @@ "sphinx_sitemap", "notfound.extension", "sphinx_copybutton", + "page_version_history_metadata", ] myst_enable_extensions = [