From b473a44a2bb59591f3e24bfcdeed1d8fbb0f9204 Mon Sep 17 00:00:00 2001 From: Gusted Date: Thu, 4 Jan 2024 23:29:28 +0100 Subject: [PATCH] [GITEA] Check for Commit in opengraph - Backport of #2094 - It's possible that `PageIsDiff` is set but not `Commit` resulting in a NPE in the template. This can happen when the requested commit doesn't exist. - Regression of c802c46a9beeaed44d41f50de31a4db146cdd8f7 & 5743d7cb5bcd85c88ad7d128e0162893a074418b - Added 'hacky' integration test. (cherry picked from commit 8db2d5e4a76f05b34e4f889e7a00ecd6578d3639) --- templates/base/head_opengraph.tmpl | 2 +- tests/integration/repo_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/templates/base/head_opengraph.tmpl b/templates/base/head_opengraph.tmpl index 19d924610c..f1f38999d2 100644 --- a/templates/base/head_opengraph.tmpl +++ b/templates/base/head_opengraph.tmpl @@ -17,7 +17,7 @@ {{else if or .PageIsDiff .IsViewFile}} - {{if .PageIsDiff}} + {{if and .PageIsDiff .Commit}} {{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}} {{- $commitMessageBody := index $commitMessageParts 1 -}} {{- if $commitMessageBody -}} diff --git a/tests/integration/repo_test.go b/tests/integration/repo_test.go index 5d3908eb7e..78e8634b2a 100644 --- a/tests/integration/repo_test.go +++ b/tests/integration/repo_test.go @@ -729,3 +729,20 @@ func TestArchiveRequest(t *testing.T) { req := NewRequest(t, "GET", "/user2/repo1/archive/a480fe666d6f550787b6cc85047b966d1f8d6bbf.zip") session.MakeRequest(t, req, http.StatusNotFound) } + +func TestCommitView(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + t.Run("Non-existent commit", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + req := NewRequest(t, "GET", "/user2/repo1/commit/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") + MakeRequest(t, req, http.StatusNotFound) + req.Header.Add("Accept", "text/html") + resp := MakeRequest(t, req, http.StatusNotFound) + + // Really ensure that 404 is being sent back. + doc := NewHTMLParser(t, resp.Body) + doc.AssertElement(t, `[aria-label="Page Not Found"]`, true) + }) +}