Skip to content

Commit ef3a3a6

Browse files
committed
fix
1 parent b4d8691 commit ef3a3a6

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

modules/templates/util_render.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,16 @@ func RenderMarkdownToHtml(ctx context.Context, input string) template.HTML { //n
216216
return output
217217
}
218218

219-
func RenderLabels(ctx context.Context, locale translation.Locale, labels []*issues_model.Label, repoLink string) template.HTML {
219+
func RenderLabels(ctx context.Context, locale translation.Locale, labels []*issues_model.Label, repoLink string, issue *issues_model.Issue) template.HTML {
220+
isPullRequest := issue != nil && issue.IsPull
221+
baseLink := fmt.Sprintf("%s/%s", repoLink, util.Iif(isPullRequest, "pulls", "issues"))
220222
htmlCode := `<span class="labels-list">`
221223
for _, label := range labels {
222224
// Protect against nil value in labels - shouldn't happen but would cause a panic if so
223225
if label == nil {
224226
continue
225227
}
226-
htmlCode += fmt.Sprintf("<a href='%s/issues?labels=%d'>%s</a> ",
227-
repoLink, label.ID, RenderLabel(ctx, locale, label))
228+
htmlCode += fmt.Sprintf(`<a href="%s?labels=%d">%s</a>`, baseLink, label.ID, RenderLabel(ctx, locale, label))
228229
}
229230
htmlCode += "</span>"
230231
return template.HTML(htmlCode)

modules/templates/util_render_test.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ import (
77
"context"
88
"html/template"
99
"os"
10+
"strings"
1011
"testing"
1112

13+
"code.gitea.io/gitea/models/issues"
1214
"code.gitea.io/gitea/models/unittest"
1315
"code.gitea.io/gitea/modules/git"
1416
"code.gitea.io/gitea/modules/log"
1517
"code.gitea.io/gitea/modules/markup"
18+
"code.gitea.io/gitea/modules/translation"
1619

1720
"github.com/stretchr/testify/assert"
1821
)
1922

20-
const testInput = ` space @mention-user
23+
func testInput() string {
24+
s := ` space @mention-user<SPACE><SPACE>
2125
/just/a/path.bin
2226
https://example.com/file.bin
2327
[local link](file.bin)
@@ -36,8 +40,10 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
3640
3741
@mention-user test
3842
#123
39-
space
43+
space<SPACE><SPACE>
4044
`
45+
return strings.ReplaceAll(s, "<SPACE>", " ")
46+
}
4147

4248
var testMetas = map[string]string{
4349
"user": "user13",
@@ -121,23 +127,23 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
121127
<a href="/user13/repo11/issues/123" class="ref-issue">#123</a>
122128
space`
123129

124-
assert.EqualValues(t, expected, RenderCommitBody(context.Background(), testInput, testMetas))
130+
assert.EqualValues(t, expected, RenderCommitBody(context.Background(), testInput(), testMetas))
125131
}
126132

127133
func TestRenderCommitMessage(t *testing.T) {
128134
expected := `space <a href="/mention-user" class="mention">@mention-user</a> `
129135

130-
assert.EqualValues(t, expected, RenderCommitMessage(context.Background(), testInput, testMetas))
136+
assert.EqualValues(t, expected, RenderCommitMessage(context.Background(), testInput(), testMetas))
131137
}
132138

133139
func TestRenderCommitMessageLinkSubject(t *testing.T) {
134140
expected := `<a href="https://example.com/link" class="default-link muted">space </a><a href="/mention-user" class="mention">@mention-user</a>`
135141

136-
assert.EqualValues(t, expected, RenderCommitMessageLinkSubject(context.Background(), testInput, "https://example.com/link", testMetas))
142+
assert.EqualValues(t, expected, RenderCommitMessageLinkSubject(context.Background(), testInput(), "https://example.com/link", testMetas))
137143
}
138144

139145
func TestRenderIssueTitle(t *testing.T) {
140-
expected := ` space @mention-user
146+
expected := ` space @mention-user<SPACE><SPACE>
141147
/just/a/path.bin
142148
https://example.com/file.bin
143149
[local link](file.bin)
@@ -156,9 +162,10 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
156162
157163
@mention-user test
158164
<a href="/user13/repo11/issues/123" class="ref-issue">#123</a>
159-
space
165+
space<SPACE><SPACE>
160166
`
161-
assert.EqualValues(t, expected, RenderIssueTitle(context.Background(), testInput, testMetas))
167+
expected = strings.ReplaceAll(expected, "<SPACE>", " ")
168+
assert.EqualValues(t, expected, RenderIssueTitle(context.Background(), testInput(), testMetas))
162169
}
163170

164171
func TestRenderMarkdownToHtml(t *testing.T) {
@@ -183,5 +190,20 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
183190
#123
184191
space</p>
185192
`
186-
assert.EqualValues(t, expected, RenderMarkdownToHtml(context.Background(), testInput))
193+
assert.EqualValues(t, expected, RenderMarkdownToHtml(context.Background(), testInput()))
194+
}
195+
196+
func TestRenderLabels(t *testing.T) {
197+
ctx := context.Background()
198+
locale := &translation.MockLocale{}
199+
200+
label := &issues.Label{ID: 123, Name: "label-name", Color: "label-color"}
201+
issue := &issues.Issue{}
202+
expected := `/owner/repo/issues?labels=123`
203+
assert.Contains(t, RenderLabels(ctx, locale, []*issues.Label{label}, "/owner/repo", issue), expected)
204+
205+
label = &issues.Label{ID: 123, Name: "label-name", Color: "label-color"}
206+
issue = &issues.Issue{IsPull: true}
207+
expected = `/owner/repo/pulls?labels=123`
208+
assert.Contains(t, RenderLabels(ctx, locale, []*issues.Label{label}, "/owner/repo", issue), expected)
187209
}

templates/repo/issue/view_content/comments.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@
173173
<span class="text grey muted-links">
174174
{{template "shared/user/authorlink" .Poster}}
175175
{{if and .AddedLabels (not .RemovedLabels)}}
176-
{{ctx.Locale.TrN (len .AddedLabels) "repo.issues.add_label" "repo.issues.add_labels" (RenderLabels $.Context ctx.Locale .AddedLabels $.RepoLink) $createdStr}}
176+
{{ctx.Locale.TrN (len .AddedLabels) "repo.issues.add_label" "repo.issues.add_labels" (RenderLabels ctx ctx.Locale .AddedLabels $.RepoLink .Issue) $createdStr}}
177177
{{else if and (not .AddedLabels) .RemovedLabels}}
178-
{{ctx.Locale.TrN (len .RemovedLabels) "repo.issues.remove_label" "repo.issues.remove_labels" (RenderLabels $.Context ctx.Locale .RemovedLabels $.RepoLink) $createdStr}}
178+
{{ctx.Locale.TrN (len .RemovedLabels) "repo.issues.remove_label" "repo.issues.remove_labels" (RenderLabels ctx ctx.Locale .RemovedLabels $.RepoLink .Issue) $createdStr}}
179179
{{else}}
180-
{{ctx.Locale.Tr "repo.issues.add_remove_labels" (RenderLabels $.Context ctx.Locale .AddedLabels $.RepoLink) (RenderLabels $.Context ctx.Locale .RemovedLabels $.RepoLink) $createdStr}}
180+
{{ctx.Locale.Tr "repo.issues.add_remove_labels" (RenderLabels ctx ctx.Locale .AddedLabels $.RepoLink .Issue) (RenderLabels ctx ctx.Locale .RemovedLabels $.RepoLink .Issue) $createdStr}}
181181
{{end}}
182182
</span>
183183
</div>

0 commit comments

Comments
 (0)