From 1aafcbbac0e7f555cf070817d69540b1e959d10d Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Wed, 27 Oct 2021 18:26:01 +0200 Subject: [PATCH 1/4] Hide label comments if labels were added and removed immediately --- routers/web/repo/issue.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 173cb49e45127..aa9d838e11cda 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -2577,9 +2577,29 @@ func combineLabelComments(issue *models.Issue) { if cur.Label != nil { // now cur MUST be label comment if prev.Type == models.CommentTypeLabel { // we can combine them only prev is a label comment if cur.Content != "1" { - prev.RemovedLabels = append(prev.RemovedLabels, cur.Label) + addedAndRemoved := false + for i, label := range prev.AddedLabels { + if cur.Label.ID == label.ID { + prev.AddedLabels = append(prev.AddedLabels[:i], prev.AddedLabels[i+1:]...) + addedAndRemoved = true + break + } + } + if !addedAndRemoved { + prev.RemovedLabels = append(prev.RemovedLabels, cur.Label) + } } else { - prev.AddedLabels = append(prev.AddedLabels, cur.Label) + addedAndRemoved := false + for i, label := range prev.RemovedLabels { + if cur.Label.ID == label.ID { + prev.RemovedLabels = append(prev.RemovedLabels[:i], prev.RemovedLabels[i+1:]...) + addedAndRemoved = true + break + } + } + if !addedAndRemoved { + prev.AddedLabels = append(prev.AddedLabels, cur.Label) + } } prev.CreatedUnix = cur.CreatedUnix // remove the current comment since it has been combined to prev comment From 0b16001e72177c6cc6203d5c7e3e38083a8b5131 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Thu, 28 Oct 2021 18:56:51 +0200 Subject: [PATCH 2/4] Add comment and rename var --- routers/web/repo/issue.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index aa9d838e11cda..0816d413400c5 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -2577,6 +2577,8 @@ func combineLabelComments(issue *models.Issue) { if cur.Label != nil { // now cur MUST be label comment if prev.Type == models.CommentTypeLabel { // we can combine them only prev is a label comment if cur.Content != "1" { + // remove labels from the AddedLabels list if the label that was removed is already + // in this list, and if it's not in this list, add the label to RemovedLabels addedAndRemoved := false for i, label := range prev.AddedLabels { if cur.Label.ID == label.ID { @@ -2589,15 +2591,17 @@ func combineLabelComments(issue *models.Issue) { prev.RemovedLabels = append(prev.RemovedLabels, cur.Label) } } else { - addedAndRemoved := false + // remove labels from the RemovedLabels list if the label that was added is already + // in this list, and if it's not in this list, add the label to AddedLabels + removedAndAdded := false for i, label := range prev.RemovedLabels { if cur.Label.ID == label.ID { prev.RemovedLabels = append(prev.RemovedLabels[:i], prev.RemovedLabels[i+1:]...) - addedAndRemoved = true + removedAndAdded = true break } } - if !addedAndRemoved { + if !removedAndAdded { prev.AddedLabels = append(prev.AddedLabels, cur.Label) } } From d40db76bfe7281c5f33c088fb716fea91578b8dc Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Fri, 29 Oct 2021 18:04:44 +0200 Subject: [PATCH 3/4] Fix unit test --- routers/web/repo/issue_test.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/routers/web/repo/issue_test.go b/routers/web/repo/issue_test.go index 7fb837fa124e2..4066ddab665c3 100644 --- a/routers/web/repo/issue_test.go +++ b/routers/web/repo/issue_test.go @@ -51,16 +51,7 @@ func TestCombineLabelComments(t *testing.T) { PosterID: 1, Content: "1", CreatedUnix: 0, - AddedLabels: []*models.Label{ - { - Name: "kind/bug", - }, - }, - RemovedLabels: []*models.Label{ - { - Name: "kind/bug", - }, - }, + AddedLabels: []*models.Label{}, Label: &models.Label{ Name: "kind/bug", }, From 16650331838aaf286f40453eae3ce92a842227f9 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Wed, 3 Nov 2021 17:52:54 +0100 Subject: [PATCH 4/4] Add test case --- routers/web/repo/issue_test.go | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/routers/web/repo/issue_test.go b/routers/web/repo/issue_test.go index 4066ddab665c3..6638baf34a16d 100644 --- a/routers/web/repo/issue_test.go +++ b/routers/web/repo/issue_test.go @@ -301,6 +301,66 @@ func TestCombineLabelComments(t *testing.T) { }, }, }, + { + name: "kase 6", + beforeCombined: []*models.Comment{ + { + Type: models.CommentTypeLabel, + PosterID: 1, + Content: "1", + Label: &models.Label{ + Name: "kind/bug", + }, + CreatedUnix: 0, + }, + { + Type: models.CommentTypeLabel, + PosterID: 1, + Content: "1", + Label: &models.Label{ + Name: "reviewed/confirmed", + }, + CreatedUnix: 0, + }, + { + Type: models.CommentTypeLabel, + PosterID: 1, + Content: "", + Label: &models.Label{ + Name: "kind/bug", + }, + CreatedUnix: 0, + }, + { + Type: models.CommentTypeLabel, + PosterID: 1, + Content: "1", + Label: &models.Label{ + Name: "kind/feature", + }, + CreatedUnix: 0, + }, + }, + afterCombined: []*models.Comment{ + { + Type: models.CommentTypeLabel, + PosterID: 1, + Content: "1", + Label: &models.Label{ + Name: "kind/bug", + }, + AddedLabels: []*models.Label{ + { + Name: "reviewed/confirmed", + }, + { + Name: "kind/feature", + }, + }, + CreatedUnix: 0, + }, + }, + }, } for _, kase := range kases {