feat(ui): create a comment aggregator to reduce noise in issues (#6523)

Closes: https://codeberg.org/forgejo/forgejo/issues/6042
Continuation of: https://codeberg.org/forgejo/forgejo/pulls/6284
Replaces: https://codeberg.org/forgejo/forgejo/pulls/6285
Context: https://codeberg.org/forgejo/forgejo/pulls/6284#issuecomment-2518599

Create a new type of comment: `CommentTypeAggregator`

Replaces the grouping of labels and review request in a single place: the comment aggregator

The whole list of comments is "scanned", if they can get aggregated (diff of time < 60secs, same poster, open / close issue, add / del labels, add /del review req), they are added to the aggregator.
Once needed, the list of all the aggregated comments are replaced with a single aggregated comment containing all the data required.

In templates, have a specific HTML rendering part for the comment aggregator, reuse the same rendering as with the other types of comments.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6523
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
Reviewed-by: Otto <otto@codeberg.org>
Co-authored-by: Litchi Pi <litchi.pi@proton.me>
Co-committed-by: Litchi Pi <litchi.pi@proton.me>
This commit is contained in:
Litchi Pi 2025-03-05 17:24:51 +00:00 committed by 0ko
parent 2c27a0f727
commit dc7f5d6b84
8 changed files with 1264 additions and 1006 deletions

View file

@ -0,0 +1,800 @@
// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package repo
import (
"strings"
"testing"
issue_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/timeutil"
"github.com/stretchr/testify/assert"
)
// *************** Helper functions for the tests ***************
func testComment(t int64) *issue_model.Comment {
return &issue_model.Comment{PosterID: 1, CreatedUnix: timeutil.TimeStamp(t)}
}
func nameToID(name string) int64 {
var id int64
for c, letter := range name {
id += int64((c+1)*1000) * int64(letter)
}
return id
}
func createReqReviewTarget(name string) issue_model.RequestReviewTarget {
if strings.HasSuffix(name, "-team") {
team := createTeam(name)
return issue_model.RequestReviewTarget{Team: &team}
}
user := createUser(name)
return issue_model.RequestReviewTarget{User: &user}
}
func createUser(name string) user_model.User {
return user_model.User{Name: name, ID: nameToID(name)}
}
func createTeam(name string) organization.Team {
return organization.Team{Name: name, ID: nameToID(name)}
}
func createLabel(name string) issue_model.Label {
return issue_model.Label{Name: name, ID: nameToID(name)}
}
func addLabel(t int64, name string) *issue_model.Comment {
c := testComment(t)
c.Type = issue_model.CommentTypeLabel
c.Content = "1"
lbl := createLabel(name)
c.Label = &lbl
c.AddedLabels = []*issue_model.Label{&lbl}
return c
}
func delLabel(t int64, name string) *issue_model.Comment {
c := addLabel(t, name)
c.Content = ""
c.RemovedLabels = c.AddedLabels
c.AddedLabels = nil
return c
}
func openOrClose(t int64, close bool) *issue_model.Comment {
c := testComment(t)
if close {
c.Type = issue_model.CommentTypeClose
} else {
c.Type = issue_model.CommentTypeReopen
}
return c
}
func reqReview(t int64, name string, delReq bool) *issue_model.Comment {
c := testComment(t)
c.Type = issue_model.CommentTypeReviewRequest
if strings.HasSuffix(name, "-team") {
team := createTeam(name)
c.AssigneeTeam = &team
c.AssigneeTeamID = team.ID
} else {
user := createUser(name)
c.Assignee = &user
c.AssigneeID = user.ID
}
c.RemovedAssignee = delReq
return c
}
func reqReviewList(t int64, del bool, names ...string) *issue_model.Comment {
req := []issue_model.RequestReviewTarget{}
for _, name := range names {
req = append(req, createReqReviewTarget(name))
}
cmnt := testComment(t)
cmnt.Type = issue_model.CommentTypeReviewRequest
if del {
cmnt.RemovedRequestReview = req
} else {
cmnt.AddedRequestReview = req
}
return cmnt
}
func aggregatedComment(t int64,
closed bool,
addLabels []*issue_model.Label,
delLabels []*issue_model.Label,
addReqReview []issue_model.RequestReviewTarget,
delReqReview []issue_model.RequestReviewTarget,
) *issue_model.Comment {
cmnt := testComment(t)
cmnt.Type = issue_model.CommentTypeAggregator
cmnt.Aggregator = &issue_model.ActionAggregator{
IsClosed: closed,
AddedLabels: addLabels,
RemovedLabels: delLabels,
AddedRequestReview: addReqReview,
RemovedRequestReview: delReqReview,
}
if len(addLabels) > 0 {
cmnt.AddedLabels = addLabels
}
if len(delLabels) > 0 {
cmnt.RemovedLabels = delLabels
}
if len(addReqReview) > 0 {
cmnt.AddedRequestReview = addReqReview
}
if len(delReqReview) > 0 {
cmnt.RemovedRequestReview = delReqReview
}
return cmnt
}
func commentText(t int64, text string) *issue_model.Comment {
c := testComment(t)
c.Type = issue_model.CommentTypeComment
c.Content = text
return c
}
// ****************************************************************
type testCase struct {
name string
beforeCombined []*issue_model.Comment
afterCombined []*issue_model.Comment
sameAfter bool
timestampCombination int64
}
func (kase *testCase) doTest(t *testing.T) {
issue := issue_model.Issue{Comments: kase.beforeCombined}
var now int64 = -9223372036854775808
for c := 0; c < len(kase.beforeCombined); c++ {
assert.Greater(t, int64(kase.beforeCombined[c].CreatedUnix), now)
now = int64(kase.beforeCombined[c].CreatedUnix)
}
if kase.timestampCombination != 0 {
now = kase.timestampCombination
}
issue_model.CombineCommentsHistory(&issue, now)
after := kase.afterCombined
if kase.sameAfter {
after = kase.beforeCombined
}
if len(after) != len(issue.Comments) {
t.Logf("Expected %v comments, got %v", len(after), len(issue.Comments))
t.Logf("Comments got after combination:")
for c := 0; c < len(issue.Comments); c++ {
cmt := issue.Comments[c]
t.Logf("%v %v %v\n", cmt.Type, cmt.CreatedUnix, cmt.Content)
}
assert.EqualValues(t, len(after), len(issue.Comments))
t.Fail()
return
}
for c := 0; c < len(after); c++ {
l := (after)[c]
r := issue.Comments[c]
// Ignore some inner data of the aggregator to facilitate testing
if l.Type == issue_model.CommentTypeAggregator {
r.Aggregator.StartUnix = 0
r.Aggregator.PrevClosed = false
r.Aggregator.PosterID = 0
r.Aggregator.StartInd = 0
r.Aggregator.EndInd = 0
r.Aggregator.AggAge = 0
}
// We can safely ignore this if the rest matches
if l.Type == issue_model.CommentTypeLabel {
l.Label = nil
l.Content = ""
} else if l.Type == issue_model.CommentTypeReviewRequest {
l.Assignee = nil
l.AssigneeID = 0
l.AssigneeTeam = nil
l.AssigneeTeamID = 0
}
assert.EqualValues(t, (after)[c], issue.Comments[c],
"Comment %v is not equal", c,
)
}
}
// **************** Start of the tests ******************
func TestCombineLabelComments(t *testing.T) {
var tmon int64 = 60 * 60 * 24 * 30
var tday int64 = 60 * 60 * 24
var thour int64 = 60 * 60
kases := []testCase{
// ADD single = normal label comment
{
name: "add_single_label",
beforeCombined: []*issue_model.Comment{
addLabel(0, "a"),
commentText(10, "I'm a salmon"),
},
sameAfter: true,
},
// ADD then REMOVE = Nothing
{
name: "add_label_then_remove",
beforeCombined: []*issue_model.Comment{
addLabel(0, "a"),
delLabel(1, "a"),
commentText(65, "I'm a salmon"),
},
afterCombined: []*issue_model.Comment{
commentText(65, "I'm a salmon"),
},
},
// ADD 1 then comment then REMOVE = separate comments
{
name: "add_label_then_comment_then_remove",
beforeCombined: []*issue_model.Comment{
addLabel(0, "a"),
commentText(10, "I'm a salmon"),
delLabel(20, "a"),
},
sameAfter: true,
},
// ADD 2 = Combined labels
{
name: "combine_labels",
beforeCombined: []*issue_model.Comment{
addLabel(0, "a"),
addLabel(10, "b"),
commentText(20, "I'm a salmon"),
addLabel(30, "c"),
addLabel(80, "d"),
addLabel(85, "e"),
delLabel(90, "c"),
},
afterCombined: []*issue_model.Comment{
{
PosterID: 1,
Type: issue_model.CommentTypeLabel,
CreatedUnix: timeutil.TimeStamp(0),
AddedLabels: []*issue_model.Label{
{Name: "a", ID: nameToID("a")},
{Name: "b", ID: nameToID("b")},
},
},
commentText(20, "I'm a salmon"),
{
PosterID: 1,
Type: issue_model.CommentTypeLabel,
CreatedUnix: timeutil.TimeStamp(30),
AddedLabels: []*issue_model.Label{
{Name: "d", ID: nameToID("d")},
{Name: "e", ID: nameToID("e")},
},
},
},
},
// ADD 1, then 1 later = 2 separate comments
{
name: "add_then_later_label",
beforeCombined: []*issue_model.Comment{
addLabel(0, "a"),
addLabel(60, "b"),
addLabel(121, "c"),
},
afterCombined: []*issue_model.Comment{
{
PosterID: 1,
Type: issue_model.CommentTypeLabel,
CreatedUnix: timeutil.TimeStamp(0),
AddedLabels: []*issue_model.Label{
{Name: "a", ID: nameToID("a")},
{Name: "b", ID: nameToID("b")},
},
},
addLabel(121, "c"),
},
},
// ADD 2 then REMOVE 1 = label
{
name: "add_2_remove_1",
beforeCombined: []*issue_model.Comment{
addLabel(0, "a"),
addLabel(10, "b"),
delLabel(20, "a"),
},
afterCombined: []*issue_model.Comment{
// The timestamp will be the one of the first aggregated comment
addLabel(0, "b"),
},
},
// ADD then REMOVE multiple = nothing
{
name: "add_multiple_remove_all",
beforeCombined: []*issue_model.Comment{
addLabel(0, "a"),
addLabel(1, "b"),
addLabel(2, "c"),
addLabel(3, "d"),
addLabel(4, "e"),
delLabel(5, "d"),
delLabel(6, "a"),
delLabel(7, "e"),
delLabel(8, "c"),
delLabel(9, "b"),
},
afterCombined: nil,
},
// ADD 2, wait, REMOVE 2 = +2 then -2 comments
{
name: "add2_wait_rm2_labels",
beforeCombined: []*issue_model.Comment{
addLabel(0, "a"),
addLabel(1, "b"),
delLabel(120, "a"),
delLabel(121, "b"),
},
afterCombined: []*issue_model.Comment{
{
PosterID: 1,
Type: issue_model.CommentTypeLabel,
CreatedUnix: timeutil.TimeStamp(0),
AddedLabels: []*issue_model.Label{
{Name: "a", ID: nameToID("a")},
{Name: "b", ID: nameToID("b")},
},
},
{
PosterID: 1,
Type: issue_model.CommentTypeLabel,
CreatedUnix: timeutil.TimeStamp(120),
RemovedLabels: []*issue_model.Label{
{Name: "a", ID: nameToID("a")},
{Name: "b", ID: nameToID("b")},
},
},
},
},
// Regression check on edge case
{
name: "regression_edgecase_finalagg",
beforeCombined: []*issue_model.Comment{
commentText(0, "hey"),
commentText(1, "ho"),
addLabel(2, "a"),
addLabel(3, "b"),
delLabel(4, "a"),
delLabel(5, "b"),
addLabel(120, "a"),
addLabel(220, "c"),
addLabel(221, "d"),
addLabel(222, "e"),
delLabel(223, "d"),
delLabel(400, "a"),
},
afterCombined: []*issue_model.Comment{
commentText(0, "hey"),
commentText(1, "ho"),
addLabel(120, "a"),
{
PosterID: 1,
Type: issue_model.CommentTypeLabel,
CreatedUnix: timeutil.TimeStamp(220),
AddedLabels: []*issue_model.Label{
{Name: "c", ID: nameToID("c")},
{Name: "e", ID: nameToID("e")},
},
},
delLabel(400, "a"),
},
},
{
name: "combine_label_high_timestamp_separated",
timestampCombination: tmon + 1,
beforeCombined: []*issue_model.Comment{
// 1 month old, comments separated by 1 Day + 1 sec (not agg)
addLabel(0, "d"),
delLabel(tday+1, "d"),
// 1 day old, comments separated by 1 hour + 1 sec (not agg)
addLabel((tmon-tday)-thour, "c"),
delLabel((tmon-tday)+1, "c"),
// 1 hour old, comments separated by 10 mins + 1 sec (not agg)
addLabel(tmon-thour, "b"),
delLabel((tmon-(50*60))+1, "b"),
// Else, aggregate by minute
addLabel(tmon-61, "a"),
delLabel(tmon, "a"),
},
sameAfter: true,
},
// Test higher timestamp diff
{
name: "combine_label_high_timestamp_merged",
timestampCombination: tmon + 1,
beforeCombined: []*issue_model.Comment{
// 1 month old, comments separated by 1 Day (aggregated)
addLabel(0, "d"),
delLabel(tday, "d"),
// 1 day old, comments separated by 1 hour (aggregated)
addLabel((tmon-tday)-thour, "c"),
delLabel(tmon-tday, "c"),
// 1 hour old, comments separated by 10 mins (aggregated)
addLabel(tmon-thour, "b"),
delLabel(tmon-(50*60), "b"),
addLabel(tmon-60, "a"),
delLabel(tmon, "a"),
},
},
}
for _, kase := range kases {
t.Run(kase.name, kase.doTest)
}
}
func TestCombineReviewRequests(t *testing.T) {
kases := []testCase{
// ADD single = normal request review comment
{
name: "add_single_review",
beforeCombined: []*issue_model.Comment{
reqReview(0, "toto", false),
commentText(10, "I'm a salmon"),
reqReview(20, "toto-team", false),
},
sameAfter: true,
},
// ADD then REMOVE = Nothing
{
name: "add_then_remove_review",
beforeCombined: []*issue_model.Comment{
reqReview(0, "toto", false),
reqReview(5, "toto", true),
commentText(10, "I'm a salmon"),
},
afterCombined: []*issue_model.Comment{
commentText(10, "I'm a salmon"),
},
},
// ADD 1 then comment then REMOVE = separate comments
{
name: "add_comment_del_review",
beforeCombined: []*issue_model.Comment{
reqReview(0, "toto", false),
commentText(5, "I'm a salmon"),
reqReview(10, "toto", true),
},
sameAfter: true,
},
// ADD 2 = Combined request reviews
{
name: "combine_reviews",
beforeCombined: []*issue_model.Comment{
reqReview(0, "toto", false),
reqReview(10, "tutu-team", false),
commentText(20, "I'm a salmon"),
reqReview(30, "titi", false),
reqReview(80, "tata", false),
reqReview(85, "tyty-team", false),
reqReview(90, "titi", true),
},
afterCombined: []*issue_model.Comment{
reqReviewList(0, false, "toto", "tutu-team"),
commentText(20, "I'm a salmon"),
reqReviewList(30, false, "tata", "tyty-team"),
},
},
// ADD 1, then 1 later = 2 separate comments
{
name: "add_then_later_review",
beforeCombined: []*issue_model.Comment{
reqReview(0, "titi", false),
reqReview(60, "toto-team", false),
reqReview(121, "tutu", false),
},
afterCombined: []*issue_model.Comment{
reqReviewList(0, false, "titi", "toto-team"),
reqReviewList(121, false, "tutu"),
},
},
// ADD 2 then REMOVE 1 = single request review
{
name: "add_2_then_remove_review",
beforeCombined: []*issue_model.Comment{
reqReview(0, "titi-team", false),
reqReview(59, "toto", false),
reqReview(60, "titi-team", true),
},
afterCombined: []*issue_model.Comment{
reqReviewList(0, false, "toto"),
},
},
// ADD then REMOVE multiple = nothing
{
name: "add_multiple_then_remove_all_review",
beforeCombined: []*issue_model.Comment{
reqReview(0, "titi0-team", false),
reqReview(1, "toto1", false),
reqReview(2, "titi2", false),
reqReview(3, "titi3-team", false),
reqReview(4, "titi4", false),
reqReview(5, "titi5", false),
reqReview(6, "titi6-team", false),
reqReview(10, "titi0-team", true),
reqReview(11, "toto1", true),
reqReview(12, "titi2", true),
reqReview(13, "titi3-team", true),
reqReview(14, "titi4", true),
reqReview(15, "titi5", true),
reqReview(16, "titi6-team", true),
},
afterCombined: nil,
},
// ADD 2, wait, REMOVE 2 = +2 then -2 comments
{
name: "add2_wait_rm2_requests",
beforeCombined: []*issue_model.Comment{
reqReview(1, "titi", false),
reqReview(2, "toto-team", false),
reqReview(121, "titi", true),
reqReview(122, "toto-team", true),
},
afterCombined: []*issue_model.Comment{
reqReviewList(1, false, "titi", "toto-team"),
reqReviewList(121, true, "titi", "toto-team"),
},
},
}
for _, kase := range kases {
t.Run(kase.name, kase.doTest)
}
}
func TestCombineOpenClose(t *testing.T) {
kases := []testCase{
// Close then open = nullified
{
name: "close_open_nullified",
beforeCombined: []*issue_model.Comment{
openOrClose(0, true),
openOrClose(10, false),
},
afterCombined: nil,
},
// Close then open later = separate comments
{
name: "close_open_later",
beforeCombined: []*issue_model.Comment{
openOrClose(0, true),
openOrClose(61, false),
},
sameAfter: true,
},
// Close then comment then open = separate comments
{
name: "close_comment_open",
beforeCombined: []*issue_model.Comment{
openOrClose(0, true),
commentText(1, "I'm a salmon"),
openOrClose(2, false),
},
sameAfter: true,
},
}
for _, kase := range kases {
t.Run(kase.name, kase.doTest)
}
}
func TestCombineMultipleDifferentComments(t *testing.T) {
lblA := createLabel("a")
kases := []testCase{
// Add Label + Close + ReqReview = Combined
{
name: "label_close_reqreview_combined",
beforeCombined: []*issue_model.Comment{
reqReview(1, "toto", false),
addLabel(2, "a"),
openOrClose(3, true),
reqReview(101, "toto", true),
openOrClose(102, false),
delLabel(103, "a"),
},
afterCombined: []*issue_model.Comment{
aggregatedComment(1,
true,
[]*issue_model.Label{&lblA},
[]*issue_model.Label{},
[]issue_model.RequestReviewTarget{createReqReviewTarget("toto")},
[]issue_model.RequestReviewTarget{},
),
aggregatedComment(101,
false,
[]*issue_model.Label{},
[]*issue_model.Label{&lblA},
[]issue_model.RequestReviewTarget{},
[]issue_model.RequestReviewTarget{createReqReviewTarget("toto")},
),
},
},
// Add Req + Add Label + Close + Del Req + Del Label = Close only
{
name: "req_label_close_dellabel_delreq",
beforeCombined: []*issue_model.Comment{
addLabel(2, "a"),
reqReview(3, "titi", false),
openOrClose(4, true),
delLabel(5, "a"),
reqReview(6, "titi", true),
},
afterCombined: []*issue_model.Comment{
openOrClose(2, true),
},
},
// Close + Add Req + Add Label + Del Req + Open = Label only
{
name: "close_req_label_open_delreq",
beforeCombined: []*issue_model.Comment{
openOrClose(2, true),
reqReview(4, "titi", false),
addLabel(5, "a"),
reqReview(6, "titi", true),
openOrClose(8, false),
},
afterCombined: []*issue_model.Comment{
addLabel(2, "a"),
},
},
// Add Label + Close + Add ReqReview + Del Label + Open = ReqReview only
{
name: "label_close_req_dellabel_open",
beforeCombined: []*issue_model.Comment{
addLabel(1, "a"),
openOrClose(2, true),
reqReview(4, "titi", false),
openOrClose(7, false),
delLabel(8, "a"),
},
afterCombined: []*issue_model.Comment{
reqReviewList(1, false, "titi"),
},
},
// Add Label + Close + ReqReview, then delete everything = nothing
{
name: "add_multiple_delete_everything",
beforeCombined: []*issue_model.Comment{
addLabel(1, "a"),
openOrClose(2, true),
reqReview(4, "titi", false),
openOrClose(7, false),
delLabel(8, "a"),
reqReview(10, "titi", true),
},
afterCombined: nil,
},
// Add multiple, then comment, then delete everything = separate aggregation
{
name: "add_multiple_comment_delete_everything",
beforeCombined: []*issue_model.Comment{
addLabel(1, "a"),
openOrClose(2, true),
reqReview(4, "titi", false),
commentText(6, "I'm a salmon"),
openOrClose(7, false),
delLabel(8, "a"),
reqReview(10, "titi", true),
},
afterCombined: []*issue_model.Comment{
aggregatedComment(1,
true,
[]*issue_model.Label{&lblA},
[]*issue_model.Label{},
[]issue_model.RequestReviewTarget{createReqReviewTarget("titi")},
[]issue_model.RequestReviewTarget{},
),
commentText(6, "I'm a salmon"),
aggregatedComment(7,
false,
[]*issue_model.Label{},
[]*issue_model.Label{&lblA},
[]issue_model.RequestReviewTarget{},
[]issue_model.RequestReviewTarget{createReqReviewTarget("titi")},
),
},
},
{
name: "regression_edgecase_finalagg",
beforeCombined: []*issue_model.Comment{
commentText(0, "hey"),
commentText(1, "ho"),
addLabel(2, "a"),
reqReview(3, "titi", false),
delLabel(4, "a"),
reqReview(5, "titi", true),
addLabel(120, "a"),
openOrClose(220, true),
addLabel(221, "d"),
reqReview(222, "toto-team", false),
delLabel(223, "d"),
delLabel(400, "a"),
},
afterCombined: []*issue_model.Comment{
commentText(0, "hey"),
commentText(1, "ho"),
addLabel(120, "a"),
aggregatedComment(220,
true,
[]*issue_model.Label{},
[]*issue_model.Label{},
[]issue_model.RequestReviewTarget{createReqReviewTarget("toto-team")},
[]issue_model.RequestReviewTarget{},
),
delLabel(400, "a"),
},
},
}
for _, kase := range kases {
t.Run(kase.name, kase.doTest)
}
}

View file

@ -1834,8 +1834,7 @@ func ViewIssue(ctx *context.Context) {
ctx.Data["LatestCloseCommentID"] = latestCloseCommentID
// Combine multiple label assignments into a single comment
combineLabelComments(issue)
combineRequestReviewComments(issue)
issues_model.CombineCommentsHistory(issue, time.Now().Unix())
getBranchData(ctx, issue)
if issue.IsPull {
@ -3710,194 +3709,6 @@ func attachmentsHTML(ctx *context.Context, attachments []*repo_model.Attachment,
return attachHTML
}
type RequestReviewTarget struct {
user *user_model.User
team *organization.Team
}
func (t *RequestReviewTarget) ID() int64 {
if t.user != nil {
return t.user.ID
}
return t.team.ID
}
func (t *RequestReviewTarget) Name() string {
if t.user != nil {
return t.user.GetDisplayName()
}
return t.team.Name
}
func (t *RequestReviewTarget) Type() string {
if t.user != nil {
return "user"
}
return "team"
}
// combineRequestReviewComments combine the nearby request review comments as one.
func combineRequestReviewComments(issue *issues_model.Issue) {
var prev, cur *issues_model.Comment
for i := 0; i < len(issue.Comments); i++ {
cur = issue.Comments[i]
if i > 0 {
prev = issue.Comments[i-1]
}
if i == 0 || cur.Type != issues_model.CommentTypeReviewRequest ||
(prev != nil && prev.PosterID != cur.PosterID) ||
(prev != nil && cur.CreatedUnix-prev.CreatedUnix >= 60) {
if cur.Type == issues_model.CommentTypeReviewRequest && (cur.Assignee != nil || cur.AssigneeTeam != nil) {
if cur.RemovedAssignee {
if cur.AssigneeTeam != nil {
cur.RemovedRequestReview = append(cur.RemovedRequestReview, &RequestReviewTarget{team: cur.AssigneeTeam})
} else {
cur.RemovedRequestReview = append(cur.RemovedRequestReview, &RequestReviewTarget{user: cur.Assignee})
}
} else {
if cur.AssigneeTeam != nil {
cur.AddedRequestReview = append(cur.AddedRequestReview, &RequestReviewTarget{team: cur.AssigneeTeam})
} else {
cur.AddedRequestReview = append(cur.AddedRequestReview, &RequestReviewTarget{user: cur.Assignee})
}
}
}
continue
}
// Previous comment is not a review request, so cannot group. Start a new group.
if prev.Type != issues_model.CommentTypeReviewRequest {
if cur.RemovedAssignee {
if cur.AssigneeTeam != nil {
cur.RemovedRequestReview = append(cur.RemovedRequestReview, &RequestReviewTarget{team: cur.AssigneeTeam})
} else {
cur.RemovedRequestReview = append(cur.RemovedRequestReview, &RequestReviewTarget{user: cur.Assignee})
}
} else {
if cur.AssigneeTeam != nil {
cur.AddedRequestReview = append(cur.AddedRequestReview, &RequestReviewTarget{team: cur.AssigneeTeam})
} else {
cur.AddedRequestReview = append(cur.AddedRequestReview, &RequestReviewTarget{user: cur.Assignee})
}
}
continue
}
// Start grouping.
if cur.RemovedAssignee {
addedIndex := slices.IndexFunc(prev.AddedRequestReview, func(t issues_model.RequestReviewTarget) bool {
if cur.AssigneeTeam != nil {
return cur.AssigneeTeam.ID == t.ID() && t.Type() == "team"
}
return cur.Assignee.ID == t.ID() && t.Type() == "user"
})
// If for this target a AddedRequestReview, then we remove that entry. If it's not found, then add it to the RemovedRequestReview.
if addedIndex == -1 {
if cur.AssigneeTeam != nil {
prev.RemovedRequestReview = append(prev.RemovedRequestReview, &RequestReviewTarget{team: cur.AssigneeTeam})
} else {
prev.RemovedRequestReview = append(prev.RemovedRequestReview, &RequestReviewTarget{user: cur.Assignee})
}
} else {
prev.AddedRequestReview = slices.Delete(prev.AddedRequestReview, addedIndex, addedIndex+1)
}
} else {
removedIndex := slices.IndexFunc(prev.RemovedRequestReview, func(t issues_model.RequestReviewTarget) bool {
if cur.AssigneeTeam != nil {
return cur.AssigneeTeam.ID == t.ID() && t.Type() == "team"
}
return cur.Assignee.ID == t.ID() && t.Type() == "user"
})
// If for this target a RemovedRequestReview, then we remove that entry. If it's not found, then add it to the AddedRequestReview.
if removedIndex == -1 {
if cur.AssigneeTeam != nil {
prev.AddedRequestReview = append(prev.AddedRequestReview, &RequestReviewTarget{team: cur.AssigneeTeam})
} else {
prev.AddedRequestReview = append(prev.AddedRequestReview, &RequestReviewTarget{user: cur.Assignee})
}
} else {
prev.RemovedRequestReview = slices.Delete(prev.RemovedRequestReview, removedIndex, removedIndex+1)
}
}
// Propagate creation time.
prev.CreatedUnix = cur.CreatedUnix
// Remove the current comment since it has been combined to prev comment
issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)
i--
}
}
// combineLabelComments combine the nearby label comments as one.
func combineLabelComments(issue *issues_model.Issue) {
var prev, cur *issues_model.Comment
for i := 0; i < len(issue.Comments); i++ {
cur = issue.Comments[i]
if i > 0 {
prev = issue.Comments[i-1]
}
if i == 0 || cur.Type != issues_model.CommentTypeLabel ||
(prev != nil && prev.PosterID != cur.PosterID) ||
(prev != nil && cur.CreatedUnix-prev.CreatedUnix >= 60) {
if cur.Type == issues_model.CommentTypeLabel && cur.Label != nil {
if cur.Content != "1" {
cur.RemovedLabels = append(cur.RemovedLabels, cur.Label)
} else {
cur.AddedLabels = append(cur.AddedLabels, cur.Label)
}
}
continue
}
if cur.Label != nil { // now cur MUST be label comment
if prev.Type == issues_model.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 {
prev.AddedLabels = append(prev.AddedLabels[:i], prev.AddedLabels[i+1:]...)
addedAndRemoved = true
break
}
}
if !addedAndRemoved {
prev.RemovedLabels = append(prev.RemovedLabels, cur.Label)
}
} else {
// 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:]...)
removedAndAdded = true
break
}
}
if !removedAndAdded {
prev.AddedLabels = append(prev.AddedLabels, cur.Label)
}
}
prev.CreatedUnix = cur.CreatedUnix
// remove the current comment since it has been combined to prev comment
issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)
i--
} else { // if prev is not a label comment, start a new group
if cur.Content != "1" {
cur.RemovedLabels = append(cur.RemovedLabels, cur.Label)
} else {
cur.AddedLabels = append(cur.AddedLabels, cur.Label)
}
}
}
}
}
// get all teams that current user can mention
func handleTeamMentions(ctx *context.Context) {
if ctx.Doer == nil || !ctx.Repo.Owner.IsOrganization() {

View file

@ -1,806 +0,0 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"testing"
issues_model "code.gitea.io/gitea/models/issues"
org_model "code.gitea.io/gitea/models/organization"
user_model "code.gitea.io/gitea/models/user"
"github.com/stretchr/testify/assert"
)
func TestCombineLabelComments(t *testing.T) {
kases := []struct {
name string
beforeCombined []*issues_model.Comment
afterCombined []*issues_model.Comment
}{
{
name: "kase 1",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
CreatedUnix: 0,
AddedLabels: []*issues_model.Label{},
Label: &issues_model.Label{
Name: "kind/bug",
},
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
},
},
{
name: "kase 2",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 70,
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
CreatedUnix: 0,
AddedLabels: []*issues_model.Label{
{
Name: "kind/bug",
},
},
Label: &issues_model.Label{
Name: "kind/bug",
},
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "",
CreatedUnix: 70,
RemovedLabels: []*issues_model.Label{
{
Name: "kind/bug",
},
},
Label: &issues_model.Label{
Name: "kind/bug",
},
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
},
},
{
name: "kase 3",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 2,
Content: "",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
CreatedUnix: 0,
AddedLabels: []*issues_model.Label{
{
Name: "kind/bug",
},
},
Label: &issues_model.Label{
Name: "kind/bug",
},
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 2,
Content: "",
CreatedUnix: 0,
RemovedLabels: []*issues_model.Label{
{
Name: "kind/bug",
},
},
Label: &issues_model.Label{
Name: "kind/bug",
},
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
},
},
{
name: "kase 4",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/backport",
},
CreatedUnix: 10,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
CreatedUnix: 10,
AddedLabels: []*issues_model.Label{
{
Name: "kind/bug",
},
{
Name: "kind/backport",
},
},
Label: &issues_model.Label{
Name: "kind/bug",
},
},
},
},
{
name: "kase 5",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeComment,
PosterID: 2,
Content: "testtest",
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/bug",
},
AddedLabels: []*issues_model.Label{
{
Name: "kind/bug",
},
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeComment,
PosterID: 2,
Content: "testtest",
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "",
RemovedLabels: []*issues_model.Label{
{
Name: "kind/bug",
},
},
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
},
},
{
name: "kase 6",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "reviewed/confirmed",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "",
Label: &issues_model.Label{
Name: "kind/bug",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/feature",
},
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeLabel,
PosterID: 1,
Content: "1",
Label: &issues_model.Label{
Name: "kind/bug",
},
AddedLabels: []*issues_model.Label{
{
Name: "reviewed/confirmed",
},
{
Name: "kind/feature",
},
},
CreatedUnix: 0,
},
},
},
}
for _, kase := range kases {
t.Run(kase.name, func(t *testing.T) {
issue := issues_model.Issue{
Comments: kase.beforeCombined,
}
combineLabelComments(&issue)
assert.EqualValues(t, kase.afterCombined, issue.Comments)
})
}
}
func TestCombineReviewRequests(t *testing.T) {
testCases := []struct {
name string
beforeCombined []*issues_model.Comment
afterCombined []*issues_model.Comment
}{
{
name: "case 1",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
RemovedAssignee: true,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
CreatedUnix: 0,
AddedRequestReview: []issues_model.RequestReviewTarget{},
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
},
},
{
name: "case 2",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
Assignee: &user_model.User{
ID: 2,
Name: "Ghost 2",
},
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
CreatedUnix: 0,
AddedRequestReview: []issues_model.RequestReviewTarget{
&RequestReviewTarget{
user: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
&RequestReviewTarget{
user: &user_model.User{
ID: 2,
Name: "Ghost 2",
},
},
},
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
},
},
{
name: "case 3",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
RemovedAssignee: true,
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
CreatedUnix: 0,
AddedRequestReview: []issues_model.RequestReviewTarget{
&RequestReviewTarget{
user: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
},
RemovedRequestReview: []issues_model.RequestReviewTarget{
&RequestReviewTarget{
team: &org_model.Team{
ID: 1,
Name: "Team 1",
},
},
},
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
},
},
{
name: "case 4",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
RemovedAssignee: true,
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
CreatedUnix: 0,
AddedRequestReview: []issues_model.RequestReviewTarget{
&RequestReviewTarget{
user: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
},
RemovedRequestReview: []issues_model.RequestReviewTarget{},
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
},
},
{
name: "case 5",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
RemovedAssignee: true,
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
RemovedAssignee: true,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
CreatedUnix: 0,
AddedRequestReview: []issues_model.RequestReviewTarget{},
RemovedRequestReview: []issues_model.RequestReviewTarget{},
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
},
},
{
name: "case 6",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
RemovedAssignee: true,
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
RemovedAssignee: true,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
CreatedUnix: 0,
RemovedRequestReview: []issues_model.RequestReviewTarget{&RequestReviewTarget{
team: &org_model.Team{
ID: 1,
Name: "Team 1",
},
}},
AddedRequestReview: []issues_model.RequestReviewTarget{&RequestReviewTarget{
user: &user_model.User{
ID: 1,
Name: "Ghost",
},
}},
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
{
Type: issues_model.CommentTypeComment,
PosterID: 1,
Content: "test",
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
CreatedUnix: 0,
AddedRequestReview: []issues_model.RequestReviewTarget{&RequestReviewTarget{
team: &org_model.Team{
ID: 1,
Name: "Team 1",
},
}},
RemovedRequestReview: []issues_model.RequestReviewTarget{&RequestReviewTarget{
user: &user_model.User{
ID: 1,
Name: "Ghost",
},
}},
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
},
},
},
{
name: "case 7",
beforeCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
CreatedUnix: 0,
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
CreatedUnix: 61,
},
},
afterCombined: []*issues_model.Comment{
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
CreatedUnix: 0,
AddedRequestReview: []issues_model.RequestReviewTarget{&RequestReviewTarget{
user: &user_model.User{
ID: 1,
Name: "Ghost",
},
}},
Assignee: &user_model.User{
ID: 1,
Name: "Ghost",
},
},
{
Type: issues_model.CommentTypeReviewRequest,
PosterID: 1,
CreatedUnix: 0,
RemovedRequestReview: []issues_model.RequestReviewTarget{&RequestReviewTarget{
team: &org_model.Team{
ID: 1,
Name: "Team 1",
},
}},
AssigneeTeam: &org_model.Team{
ID: 1,
Name: "Team 1",
},
},
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
issue := issues_model.Issue{
Comments: testCase.beforeCombined,
}
combineRequestReviewComments(&issue)
assert.EqualValues(t, testCase.afterCombined[0], issue.Comments[0])
})
}
}