From 7a97c05206b9f9d936c10608c3b7e4fd1ebb5f08 Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 12 Apr 2024 14:31:44 +0200 Subject: [PATCH] [BUG] Render correct label link - Render the correct label for pull requests, it should link to the pull requests list and not the issue list. - Add unit test. - Resolves https://codeberg.org/forgejo/forgejo/issues/3183 --- modules/templates/main_test.go | 24 ++++++++++++++ modules/templates/util_render.go | 11 +++++-- modules/templates/util_render_test.go | 32 +++++++++---------- .../repo/issue/view_content/comments.tmpl | 6 ++-- 4 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 modules/templates/main_test.go diff --git a/modules/templates/main_test.go b/modules/templates/main_test.go new file mode 100644 index 0000000000..bbdf5d2f99 --- /dev/null +++ b/modules/templates/main_test.go @@ -0,0 +1,24 @@ +// Copyright 2024 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package templates_test + +import ( + "context" + "testing" + + "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/markup" + + _ "code.gitea.io/gitea/models" + _ "code.gitea.io/gitea/models/issues" +) + +func TestMain(m *testing.M) { + markup.Init(&markup.ProcessorHelper{ + IsUsernameMentionable: func(ctx context.Context, username string) bool { + return username == "mention-user" + }, + }) + unittest.MainTest(m) +} diff --git a/modules/templates/util_render.go b/modules/templates/util_render.go index f593e1b897..5446741287 100644 --- a/modules/templates/util_render.go +++ b/modules/templates/util_render.go @@ -239,15 +239,20 @@ func RenderMarkdownToHtml(ctx context.Context, input string) template.HTML { //n return output } -func RenderLabels(ctx context.Context, locale translation.Locale, labels []*issues_model.Label, repoLink string) template.HTML { +func RenderLabels(ctx context.Context, locale translation.Locale, labels []*issues_model.Label, repoLink string, isPull bool) template.HTML { htmlCode := `` for _, label := range labels { // Protect against nil value in labels - shouldn't happen but would cause a panic if so if label == nil { continue } - htmlCode += fmt.Sprintf("%s ", - repoLink, label.ID, RenderLabel(ctx, locale, label)) + + issuesOrPull := "issues" + if isPull { + issuesOrPull = "pulls" + } + htmlCode += fmt.Sprintf("%s ", + repoLink, issuesOrPull, label.ID, RenderLabel(ctx, locale, label)) } htmlCode += "" return template.HTML(htmlCode) diff --git a/modules/templates/util_render_test.go b/modules/templates/util_render_test.go index 5fea4d9a16..c1d5e26f62 100644 --- a/modules/templates/util_render_test.go +++ b/modules/templates/util_render_test.go @@ -6,13 +6,12 @@ package templates import ( "context" "html/template" - "os" "testing" + "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/unittest" - "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/markup" + "code.gitea.io/gitea/modules/translation" "github.com/stretchr/testify/assert" ) @@ -46,19 +45,6 @@ var testMetas = map[string]string{ "mode": "comment", } -func TestMain(m *testing.M) { - unittest.InitSettings() - if err := git.InitSimple(context.Background()); err != nil { - log.Fatal("git init failed, err: %v", err) - } - markup.Init(&markup.ProcessorHelper{ - IsUsernameMentionable: func(ctx context.Context, username string) bool { - return username == "mention-user" - }, - }) - os.Exit(m.Run()) -} - func TestApostrophesInMentions(t *testing.T) { rendered := RenderMarkdownToHtml(context.Background(), "@mention-user's comment") assert.EqualValues(t, template.HTML("

@mention-user's comment

\n"), rendered) @@ -190,3 +176,15 @@ space

` assert.EqualValues(t, expected, RenderMarkdownToHtml(context.Background(), testInput)) } + +func TestRenderLabels(t *testing.T) { + unittest.PrepareTestEnv(t) + + tr := &translation.MockLocale{} + label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1}) + + assert.Contains(t, RenderLabels(db.DefaultContext, tr, []*issues_model.Label{label}, "user2/repo1", false), + "user2/repo1/issues?labels=1") + assert.Contains(t, RenderLabels(db.DefaultContext, tr, []*issues_model.Label{label}, "user2/repo1", true), + "user2/repo1/pulls?labels=1") +} diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index a5fd02c190..e9e92db07d 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -177,11 +177,11 @@ {{template "shared/user/authorlink" .Poster}} {{if and .AddedLabels (not .RemovedLabels)}} - {{ctx.Locale.TrN (len .AddedLabels) "repo.issues.add_label" "repo.issues.add_labels" (RenderLabels $.Context ctx.Locale .AddedLabels $.RepoLink) $createdStr}} + {{ctx.Locale.TrN (len .AddedLabels) "repo.issues.add_label" "repo.issues.add_labels" (RenderLabels $.Context ctx.Locale .AddedLabels $.RepoLink .Issue.IsPull) $createdStr}} {{else if and (not .AddedLabels) .RemovedLabels}} - {{ctx.Locale.TrN (len .RemovedLabels) "repo.issues.remove_label" "repo.issues.remove_labels" (RenderLabels $.Context ctx.Locale .RemovedLabels $.RepoLink) $createdStr}} + {{ctx.Locale.TrN (len .RemovedLabels) "repo.issues.remove_label" "repo.issues.remove_labels" (RenderLabels $.Context ctx.Locale .RemovedLabels $.RepoLink .Issue.IsPull) $createdStr}} {{else}} - {{ctx.Locale.Tr "repo.issues.add_remove_labels" (RenderLabels $.Context ctx.Locale .AddedLabels $.RepoLink) (RenderLabels $.Context ctx.Locale .RemovedLabels $.RepoLink) $createdStr}} + {{ctx.Locale.Tr "repo.issues.add_remove_labels" (RenderLabels $.Context ctx.Locale .AddedLabels $.RepoLink .Issue.IsPull) (RenderLabels $.Context ctx.Locale .RemovedLabels $.RepoLink .Issue.IsPull) $createdStr}} {{end}}