Skip to content

Commit c40df54

Browse files
pta2002zeripath
andauthored
Group Label Changed Comments in timeline (#13304)
* Create function to group label comments * Combine multiple label additions into one * Group removed and added labels in the same comment * Fix indentation on comments.tmpl Co-authored-by: zeripath <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 756c090 commit c40df54

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

models/issue_comment.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ type Comment struct {
124124
IssueID int64 `xorm:"INDEX"`
125125
Issue *Issue `xorm:"-"`
126126
LabelID int64
127-
Label *Label `xorm:"-"`
127+
Label *Label `xorm:"-"`
128+
AddedLabels []*Label `xorm:"-"`
129+
RemovedLabels []*Label `xorm:"-"`
128130
OldProjectID int64
129131
ProjectID int64
130132
OldProject *Project `xorm:"-"`

modules/templates/helper.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,16 @@ func NewFuncMap() []template.FuncMap {
343343
// the table is NOT sorted with this header
344344
return ""
345345
},
346+
"RenderLabels": func(labels []*models.Label) template.HTML {
347+
html := ""
348+
349+
for _, label := range labels {
350+
html = fmt.Sprintf("%s<div class='ui label' style='color: %s; background-color: %s'>%s</div>",
351+
html, label.ForegroundColor(), label.Color, RenderEmoji(label.Name))
352+
}
353+
354+
return template.HTML(html)
355+
},
346356
}}
347357
}
348358

options/locale/locale_en-US.ini

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,8 +979,11 @@ issues.label_templates.info = No labels exist yet. Create a label with 'New Labe
979979
issues.label_templates.helper = Select a label set
980980
issues.label_templates.use = Use Label Set
981981
issues.label_templates.fail_to_load_file = Failed to load label template file '%s': %v
982-
issues.add_label_at = added the <div class="ui label" style="color: %s\; background-color: %s">%s</div> label %s
983-
issues.remove_label_at = removed the <div class="ui label" style="color: %s\; background-color: %s">%s</div> label %s
982+
issues.add_label = added the %s label %s
983+
issues.add_labels = added the %s labels %s
984+
issues.remove_label = removed the %s label %s
985+
issues.remove_labels = removed the %s labels %s
986+
issues.add_remove_labels = added %s and removed %s labels %s
984987
issues.add_milestone_at = `added this to the <b>%s</b> milestone %s`
985988
issues.add_project_at = `added this to the <b>%s</b> project %s`
986989
issues.change_milestone_at = `modified the milestone from <b>%s</b> to <b>%s</b> %s`

routers/repo/issue.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,9 @@ func ViewIssue(ctx *context.Context) {
13541354
}
13551355
}
13561356

1357+
// Combine multiple label assignments into a single comment
1358+
combineLabelComments(issue)
1359+
13571360
getBranchData(ctx, issue)
13581361
if issue.IsPull {
13591362
pull := issue.PullRequest
@@ -2385,3 +2388,46 @@ func attachmentsHTML(ctx *context.Context, attachments []*models.Attachment) str
23852388
}
23862389
return attachHTML
23872390
}
2391+
2392+
func combineLabelComments(issue *models.Issue) {
2393+
for i := 0; i < len(issue.Comments); {
2394+
c := issue.Comments[i]
2395+
var shouldMerge bool
2396+
var removingCur bool
2397+
var prev *models.Comment
2398+
2399+
if i == 0 {
2400+
shouldMerge = false
2401+
} else {
2402+
prev = issue.Comments[i-1]
2403+
removingCur = c.Content != "1"
2404+
2405+
shouldMerge = prev.PosterID == c.PosterID && c.CreatedUnix-prev.CreatedUnix < 60 &&
2406+
c.Type == prev.Type
2407+
}
2408+
2409+
if c.Type == models.CommentTypeLabel {
2410+
if !shouldMerge {
2411+
if removingCur {
2412+
c.RemovedLabels = make([]*models.Label, 1)
2413+
c.AddedLabels = make([]*models.Label, 0)
2414+
c.RemovedLabels[0] = c.Label
2415+
} else {
2416+
c.RemovedLabels = make([]*models.Label, 0)
2417+
c.AddedLabels = make([]*models.Label, 1)
2418+
c.AddedLabels[0] = c.Label
2419+
}
2420+
} else {
2421+
if removingCur {
2422+
prev.RemovedLabels = append(prev.RemovedLabels, c.Label)
2423+
} else {
2424+
prev.AddedLabels = append(prev.AddedLabels, c.Label)
2425+
}
2426+
prev.CreatedUnix = c.CreatedUnix
2427+
issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)
2428+
continue
2429+
}
2430+
}
2431+
i++
2432+
}
2433+
}

templates/repo/issue/view_content/comments.tmpl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,21 @@
161161
</div>
162162
</div>
163163
{{else if eq .Type 7}}
164-
{{if .Label}}
164+
{{if or .AddedLabels .RemovedLabels}}
165165
<div class="timeline-item event" id="{{.HashTag}}">
166166
<span class="badge">{{svg "octicon-tag"}}</span>
167167
<a class="ui avatar image" href="{{.Poster.HomeLink}}">
168168
<img src="{{.Poster.RelAvatarLink}}">
169169
</a>
170170
<span class="text grey">
171171
<a class="author" href="{{.Poster.HomeLink}}">{{.Poster.GetDisplayName}}</a>
172-
{{if .Content}}{{$.i18n.Tr "repo.issues.add_label_at" .Label.ForegroundColor .Label.Color (.Label.Name|RenderEmoji) $createdStr | Safe}}{{else}}{{$.i18n.Tr "repo.issues.remove_label_at" .Label.ForegroundColor .Label.Color (.Label.Name|RenderEmoji) $createdStr | Safe}}{{end}}
172+
{{if and .AddedLabels (not .RemovedLabels)}}
173+
{{$.i18n.Tr (TrN $.i18n.Lang (len .AddedLabels) "repo.issues.add_label" "repo.issues.add_labels") (RenderLabels .AddedLabels) $createdStr | Safe}}
174+
{{else if and (not .AddedLabels) .RemovedLabels}}
175+
{{$.i18n.Tr (TrN $.i18n.Lang (len .RemovedLabels) "repo.issues.remove_label" "repo.issues.remove_labels") (RenderLabels .RemovedLabels) $createdStr | Safe}}
176+
{{else}}
177+
{{$.i18n.Tr "repo.issues.add_remove_labels" (RenderLabels .AddedLabels) (RenderLabels .RemovedLabels) $createdStr | Safe}}
178+
{{end}}
173179
</span>
174180
</div>
175181
{{end}}

0 commit comments

Comments
 (0)