Skip to content

Commit 9db590f

Browse files
authored
Fix bug when combine label comments (#14894)
* Fix bug when combine label comments * Added some code comments * More comments
1 parent 144cfe5 commit 9db590f

File tree

2 files changed

+97
-17
lines changed

2 files changed

+97
-17
lines changed

routers/repo/issue.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,12 +2481,11 @@ func attachmentsHTML(ctx *context.Context, attachments []*models.Attachment, con
24812481
return attachHTML
24822482
}
24832483

2484+
// combineLabelComments combine the nearby label comments as one.
24842485
func combineLabelComments(issue *models.Issue) {
2486+
var prev, cur *models.Comment
24852487
for i := 0; i < len(issue.Comments); i++ {
2486-
var (
2487-
prev *models.Comment
2488-
cur = issue.Comments[i]
2489-
)
2488+
cur = issue.Comments[i]
24902489
if i > 0 {
24912490
prev = issue.Comments[i-1]
24922491
}
@@ -2503,16 +2502,25 @@ func combineLabelComments(issue *models.Issue) {
25032502
continue
25042503
}
25052504

2506-
if cur.Label != nil {
2507-
if cur.Content != "1" {
2508-
prev.RemovedLabels = append(prev.RemovedLabels, cur.Label)
2509-
} else {
2510-
prev.AddedLabels = append(prev.AddedLabels, cur.Label)
2505+
if cur.Label != nil { // now cur MUST be label comment
2506+
if prev.Type == models.CommentTypeLabel { // we can combine them only prev is a label comment
2507+
if cur.Content != "1" {
2508+
prev.RemovedLabels = append(prev.RemovedLabels, cur.Label)
2509+
} else {
2510+
prev.AddedLabels = append(prev.AddedLabels, cur.Label)
2511+
}
2512+
prev.CreatedUnix = cur.CreatedUnix
2513+
// remove the current comment since it has been combined to prev comment
2514+
issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)
2515+
i--
2516+
} else { // if prev is not a label comment, start a new group
2517+
if cur.Content != "1" {
2518+
cur.RemovedLabels = append(cur.RemovedLabels, cur.Label)
2519+
} else {
2520+
cur.AddedLabels = append(cur.AddedLabels, cur.Label)
2521+
}
25112522
}
25122523
}
2513-
prev.CreatedUnix = cur.CreatedUnix
2514-
issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)
2515-
i--
25162524
}
25172525
}
25182526

routers/repo/issue_test.go

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import (
1313

1414
func TestCombineLabelComments(t *testing.T) {
1515
var kases = []struct {
16+
name string
1617
beforeCombined []*models.Comment
1718
afterCombined []*models.Comment
1819
}{
1920
{
21+
name: "kase 1",
2022
beforeCombined: []*models.Comment{
2123
{
2224
Type: models.CommentTypeLabel,
@@ -72,6 +74,7 @@ func TestCombineLabelComments(t *testing.T) {
7274
},
7375
},
7476
{
77+
name: "kase 2",
7578
beforeCombined: []*models.Comment{
7679
{
7780
Type: models.CommentTypeLabel,
@@ -136,6 +139,7 @@ func TestCombineLabelComments(t *testing.T) {
136139
},
137140
},
138141
{
142+
name: "kase 3",
139143
beforeCombined: []*models.Comment{
140144
{
141145
Type: models.CommentTypeLabel,
@@ -200,6 +204,7 @@ func TestCombineLabelComments(t *testing.T) {
200204
},
201205
},
202206
{
207+
name: "kase 4",
203208
beforeCombined: []*models.Comment{
204209
{
205210
Type: models.CommentTypeLabel,
@@ -240,13 +245,80 @@ func TestCombineLabelComments(t *testing.T) {
240245
},
241246
},
242247
},
248+
{
249+
name: "kase 5",
250+
beforeCombined: []*models.Comment{
251+
{
252+
Type: models.CommentTypeLabel,
253+
PosterID: 1,
254+
Content: "1",
255+
Label: &models.Label{
256+
Name: "kind/bug",
257+
},
258+
CreatedUnix: 0,
259+
},
260+
{
261+
Type: models.CommentTypeComment,
262+
PosterID: 2,
263+
Content: "testtest",
264+
CreatedUnix: 0,
265+
},
266+
{
267+
Type: models.CommentTypeLabel,
268+
PosterID: 1,
269+
Content: "",
270+
Label: &models.Label{
271+
Name: "kind/bug",
272+
},
273+
CreatedUnix: 0,
274+
},
275+
},
276+
afterCombined: []*models.Comment{
277+
{
278+
Type: models.CommentTypeLabel,
279+
PosterID: 1,
280+
Content: "1",
281+
Label: &models.Label{
282+
Name: "kind/bug",
283+
},
284+
AddedLabels: []*models.Label{
285+
{
286+
Name: "kind/bug",
287+
},
288+
},
289+
CreatedUnix: 0,
290+
},
291+
{
292+
Type: models.CommentTypeComment,
293+
PosterID: 2,
294+
Content: "testtest",
295+
CreatedUnix: 0,
296+
},
297+
{
298+
Type: models.CommentTypeLabel,
299+
PosterID: 1,
300+
Content: "",
301+
RemovedLabels: []*models.Label{
302+
{
303+
Name: "kind/bug",
304+
},
305+
},
306+
Label: &models.Label{
307+
Name: "kind/bug",
308+
},
309+
CreatedUnix: 0,
310+
},
311+
},
312+
},
243313
}
244314

245315
for _, kase := range kases {
246-
var issue = models.Issue{
247-
Comments: kase.beforeCombined,
248-
}
249-
combineLabelComments(&issue)
250-
assert.EqualValues(t, kase.afterCombined, issue.Comments)
316+
t.Run(kase.name, func(t *testing.T) {
317+
var issue = models.Issue{
318+
Comments: kase.beforeCombined,
319+
}
320+
combineLabelComments(&issue)
321+
assert.EqualValues(t, kase.afterCombined, issue.Comments)
322+
})
251323
}
252324
}

0 commit comments

Comments
 (0)