[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 c802c46a9b &
5743d7cb5b
- Added 'hacky' integration test.

(cherry picked from commit 8db2d5e4a76f05b34e4f889e7a00ecd6578d3639)
This commit is contained in:
Gusted 2024-01-04 23:29:28 +01:00
parent 0fbf761d19
commit b473a44a2b
No known key found for this signature in database
GPG key ID: FD821B732837125F
2 changed files with 18 additions and 1 deletions

View file

@ -17,7 +17,7 @@
{{else if or .PageIsDiff .IsViewFile}} {{else if or .PageIsDiff .IsViewFile}}
<meta property="og:title" content="{{.Title}}"> <meta property="og:title" content="{{.Title}}">
<meta property="og:url" content="{{AppUrl}}{{.Link}}"> <meta property="og:url" content="{{AppUrl}}{{.Link}}">
{{if .PageIsDiff}} {{if and .PageIsDiff .Commit}}
{{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}} {{- $commitMessageParts := StringUtils.Cut .Commit.Message "\n" -}}
{{- $commitMessageBody := index $commitMessageParts 1 -}} {{- $commitMessageBody := index $commitMessageParts 1 -}}
{{- if $commitMessageBody -}} {{- if $commitMessageBody -}}

View file

@ -729,3 +729,20 @@ func TestArchiveRequest(t *testing.T) {
req := NewRequest(t, "GET", "/user2/repo1/archive/a480fe666d6f550787b6cc85047b966d1f8d6bbf.zip") req := NewRequest(t, "GET", "/user2/repo1/archive/a480fe666d6f550787b6cc85047b966d1f8d6bbf.zip")
session.MakeRequest(t, req, http.StatusNotFound) 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)
})
}