Merge pull request '[v7.0/forgejo] [BUG] Render correct label link' (#3196) from bp-v7.0/forgejo-7a97c05 into v7.0/forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3196 Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
commit
9656408102
4 changed files with 50 additions and 23 deletions
24
modules/templates/main_test.go
Normal file
24
modules/templates/main_test.go
Normal 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)
|
||||||
|
}
|
|
@ -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)
|
||||||
|
|
|
@ -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>'s comment</p>\n"), rendered)
|
assert.EqualValues(t, template.HTML("<p><a href=\"/mention-user\" rel=\"nofollow\">@mention-user</a>'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")
|
||||||
|
}
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Add table
Reference in a new issue