Skip to content

Commit

Permalink
Group Label Changed Comments in timeline (#13304)
Browse files Browse the repository at this point in the history
* 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 <art27@cantab.net>

Co-authored-by: zeripath <art27@cantab.net>
  • Loading branch information
pta2002 and zeripath authored Oct 25, 2020
1 parent 756c090 commit c40df54
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
4 changes: 3 additions & 1 deletion models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ type Comment struct {
IssueID int64 `xorm:"INDEX"`
Issue *Issue `xorm:"-"`
LabelID int64
Label *Label `xorm:"-"`
Label *Label `xorm:"-"`
AddedLabels []*Label `xorm:"-"`
RemovedLabels []*Label `xorm:"-"`
OldProjectID int64
ProjectID int64
OldProject *Project `xorm:"-"`
Expand Down
10 changes: 10 additions & 0 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ func NewFuncMap() []template.FuncMap {
// the table is NOT sorted with this header
return ""
},
"RenderLabels": func(labels []*models.Label) template.HTML {
html := ""

for _, label := range labels {
html = fmt.Sprintf("%s<div class='ui label' style='color: %s; background-color: %s'>%s</div>",
html, label.ForegroundColor(), label.Color, RenderEmoji(label.Name))
}

return template.HTML(html)
},
}}
}

Expand Down
7 changes: 5 additions & 2 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,11 @@ issues.label_templates.info = No labels exist yet. Create a label with 'New Labe
issues.label_templates.helper = Select a label set
issues.label_templates.use = Use Label Set
issues.label_templates.fail_to_load_file = Failed to load label template file '%s': %v
issues.add_label_at = added the <div class="ui label" style="color: %s\; background-color: %s">%s</div> label %s
issues.remove_label_at = removed the <div class="ui label" style="color: %s\; background-color: %s">%s</div> label %s
issues.add_label = added the %s label %s
issues.add_labels = added the %s labels %s
issues.remove_label = removed the %s label %s
issues.remove_labels = removed the %s labels %s
issues.add_remove_labels = added %s and removed %s labels %s
issues.add_milestone_at = `added this to the <b>%s</b> milestone %s`
issues.add_project_at = `added this to the <b>%s</b> project %s`
issues.change_milestone_at = `modified the milestone from <b>%s</b> to <b>%s</b> %s`
Expand Down
46 changes: 46 additions & 0 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,9 @@ func ViewIssue(ctx *context.Context) {
}
}

// Combine multiple label assignments into a single comment
combineLabelComments(issue)

getBranchData(ctx, issue)
if issue.IsPull {
pull := issue.PullRequest
Expand Down Expand Up @@ -2385,3 +2388,46 @@ func attachmentsHTML(ctx *context.Context, attachments []*models.Attachment) str
}
return attachHTML
}

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 {
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
} else {
c.RemovedLabels = make([]*models.Label, 0)
c.AddedLabels = make([]*models.Label, 1)
c.AddedLabels[0] = c.Label
}
} else {
if removingCur {
prev.RemovedLabels = append(prev.RemovedLabels, c.Label)
} else {
prev.AddedLabels = append(prev.AddedLabels, c.Label)
}
prev.CreatedUnix = c.CreatedUnix
issue.Comments = append(issue.Comments[:i], issue.Comments[i+1:]...)
continue
}
}
i++
}
}
10 changes: 8 additions & 2 deletions templates/repo/issue/view_content/comments.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,21 @@
</div>
</div>
{{else if eq .Type 7}}
{{if .Label}}
{{if or .AddedLabels .RemovedLabels}}
<div class="timeline-item event" id="{{.HashTag}}">
<span class="badge">{{svg "octicon-tag"}}</span>
<a class="ui avatar image" href="{{.Poster.HomeLink}}">
<img src="{{.Poster.RelAvatarLink}}">
</a>
<span class="text grey">
<a class="author" href="{{.Poster.HomeLink}}">{{.Poster.GetDisplayName}}</a>
{{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}}
{{if and .AddedLabels (not .RemovedLabels)}}
{{$.i18n.Tr (TrN $.i18n.Lang (len .AddedLabels) "repo.issues.add_label" "repo.issues.add_labels") (RenderLabels .AddedLabels) $createdStr | Safe}}
{{else if and (not .AddedLabels) .RemovedLabels}}
{{$.i18n.Tr (TrN $.i18n.Lang (len .RemovedLabels) "repo.issues.remove_label" "repo.issues.remove_labels") (RenderLabels .RemovedLabels) $createdStr | Safe}}
{{else}}
{{$.i18n.Tr "repo.issues.add_remove_labels" (RenderLabels .AddedLabels) (RenderLabels .RemovedLabels) $createdStr | Safe}}
{{end}}
</span>
</div>
{{end}}
Expand Down

0 comments on commit c40df54

Please sign in to comment.