summaryrefslogtreecommitdiffstats
path: root/routers/repo/issue.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-11-21 06:29:09 +0800
committerGitHub <noreply@github.com>2020-11-20 17:29:09 -0500
commitc5f6f8f2f19d6893d5fe32e61214fc3d457a1a39 (patch)
tree4d763c0be3ee60394a2feca256f9350dbd9cb002 /routers/repo/issue.go
parentf915161a2f1f4a1c436e7a96cfc88f61ae8fbce6 (diff)
downloadgitea-c5f6f8f2f19d6893d5fe32e61214fc3d457a1a39.tar.gz
gitea-c5f6f8f2f19d6893d5fe32e61214fc3d457a1a39.zip
Refactor combine label comments with tests (#13619)
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'routers/repo/issue.go')
-rw-r--r--routers/repo/issue.go79
1 files changed, 23 insertions, 56 deletions
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index c55a0ce693..159cc5b9f0 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -2392,67 +2392,34 @@ func attachmentsHTML(ctx *context.Context, attachments []*models.Attachment) str
}
func combineLabelComments(issue *models.Issue) {
- for i := 0; i < len(issue.Comments); {
- c := issue.Comments[i]
- var shouldMerge bool
- var removingCur bool
- var prev *models.Comment
-
- if i == 0 {
- shouldMerge = false
- } else {
+ for i := 0; i < len(issue.Comments); i++ {
+ var (
+ prev *models.Comment
+ cur = issue.Comments[i]
+ )
+ if i > 0 {
prev = issue.Comments[i-1]
- removingCur = c.Content != "1"
-
- shouldMerge = prev.PosterID == c.PosterID && c.CreatedUnix-prev.CreatedUnix < 60 &&
- c.Type == prev.Type
}
-
- if c.Type == models.CommentTypeLabel {
- if !shouldMerge {
- if removingCur {
- c.RemovedLabels = make([]*models.Label, 1)
- c.AddedLabels = make([]*models.Label, 0)
- c.RemovedLabels[0] = c.Label
+ if i == 0 || cur.Type != models.CommentTypeLabel ||
+ (prev != nil && prev.PosterID != cur.PosterID) ||
+ (prev != nil && cur.CreatedUnix-prev.CreatedUnix >= 60) {
+ if cur.Type == models.CommentTypeLabel {
+ if cur.Content != "1" {
+ cur.RemovedLabels = append(cur.RemovedLabels, cur.Label)
} else {
- c.RemovedLabels = make([]*models.Label, 0)
- c.AddedLabels = make([]*models.Label, 1)
- c.AddedLabels[0] = c.Label
+ cur.AddedLabels = append(cur.AddedLabels, cur.Label)
}
- } else {
- // Remove duplicated "added" and "removed" labels
- // This way, adding and immediately removing a label won't generate a comment.
- var appendingTo *[]*models.Label
- var other *[]*models.Label
-
- if removingCur {
- appendingTo = &prev.RemovedLabels
- other = &prev.AddedLabels
- } else {
- appendingTo = &prev.AddedLabels
- other = &prev.RemovedLabels
- }
-
- appending := true
-
- for i := 0; i < len(*other); i++ {
- l := (*other)[i]
- if l.ID == c.Label.ID {
- *other = append((*other)[:i], (*other)[i+1:]...)
- appending = false
- break
- }
- }
-
- if appending {
- *appendingTo = append(*appendingTo, c.Label)
- }
-
- prev.CreatedUnix = c.CreatedUnix
- issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)
- continue
}
+ continue
}
- i++
+
+ if cur.Content != "1" {
+ prev.RemovedLabels = append(prev.RemovedLabels, cur.Label)
+ } else {
+ prev.AddedLabels = append(prev.AddedLabels, cur.Label)
+ }
+ prev.CreatedUnix = cur.CreatedUnix
+ issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)
+ i--
}
}