0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-25 08:54:17 -05:00

[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

(cherry picked from commit 7a97c05206)
This commit is contained in:
Gusted 2024-04-12 14:31:44 +02:00 committed by GitHub
parent 89d751ef1b
commit 0c71afea31
4 changed files with 50 additions and 23 deletions

View file

@ -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)
}

View file

@ -239,15 +239,20 @@ func RenderMarkdownToHtml(ctx context.Context, input string) template.HTML { //n
return output 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 := `<span class="labels-list">` htmlCode := `<span class="labels-list">`
for _, label := range labels { for _, label := range labels {
// Protect against nil value in labels - shouldn't happen but would cause a panic if so // Protect against nil value in labels - shouldn't happen but would cause a panic if so
if label == nil { if label == nil {
continue continue
} }
htmlCode += fmt.Sprintf("<a href='%s/issues?labels=%d'>%s</a> ",
repoLink, label.ID, RenderLabel(ctx, locale, label)) issuesOrPull := "issues"
if isPull {
issuesOrPull = "pulls"
}
htmlCode += fmt.Sprintf("<a href='%s/%s?labels=%d'>%s</a> ",
repoLink, issuesOrPull, label.ID, RenderLabel(ctx, locale, label))
} }
htmlCode += "</span>" htmlCode += "</span>"
return template.HTML(htmlCode) return template.HTML(htmlCode)

View file

@ -6,13 +6,12 @@ package templates
import ( import (
"context" "context"
"html/template" "html/template"
"os"
"testing" "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/models/unittest"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -46,19 +45,6 @@ var testMetas = map[string]string{
"mode": "comment", "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) { func TestApostrophesInMentions(t *testing.T) {
rendered := RenderMarkdownToHtml(context.Background(), "@mention-user's comment") rendered := RenderMarkdownToHtml(context.Background(), "@mention-user's comment")
assert.EqualValues(t, template.HTML("<p><a href=\"/mention-user\" rel=\"nofollow\">@mention-user</a>&#39;s comment</p>\n"), rendered) assert.EqualValues(t, template.HTML("<p><a href=\"/mention-user\" rel=\"nofollow\">@mention-user</a>&#39;s comment</p>\n"), rendered)
@ -190,3 +176,15 @@ space</p>
` `
assert.EqualValues(t, expected, RenderMarkdownToHtml(context.Background(), testInput)) 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")
}

View file

@ -177,11 +177,11 @@
<span class="text grey muted-links"> <span class="text grey muted-links">
{{template "shared/user/authorlink" .Poster}} {{template "shared/user/authorlink" .Poster}}
{{if and .AddedLabels (not .RemovedLabels)}} {{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}} {{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}} {{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}} {{end}}
</span> </span>
</div> </div>