Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent adding nil label to .AddedLabels or .RemovedLabels #14623

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions models/consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,13 @@ func CountWrongUserType() (int64, error) {
func FixWrongUserType() (int64, error) {
return x.Where(builder.Eq{"type": 0}.And(builder.Neq{"num_teams": 0})).Cols("type").NoAutoTime().Update(&User{Type: 1})
}

// CountCommentTypeLabelWithEmptyLabel count label comments with empty label
func CountCommentTypeLabelWithEmptyLabel() (int64, error) {
return x.Where(builder.Eq{"type": CommentTypeLabel, "label_id": 0}).Count(new(Comment))
}

// FixCommentTypeLabelWithEmptyLabel count label comments with empty label
func FixCommentTypeLabelWithEmptyLabel() (int64, error) {
return x.Where(builder.Eq{"type": CommentTypeLabel, "label_id": 0}).Delete(new(Comment))
}
18 changes: 18 additions & 0 deletions modules/doctor/dbconsistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
}
}

// find label comments with empty labels
count, err = models.CountCommentTypeLabelWithEmptyLabel()
if err != nil {
logger.Critical("Error: %v whilst counting label comments with empty labels")
return err
}
if count > 0 {
if autofix {
updatedCount, err := models.FixCommentTypeLabelWithEmptyLabel()
if err != nil {
logger.Critical("Error: %v whilst removing label comments with empty labels")
return err
}
logger.Info("%d label comments with empty labels removed", updatedCount)
} else {
logger.Warn("%d label comments with empty labels", count)
}
}
// TODO: function to recalc all counters

return nil
Expand Down
4 changes: 4 additions & 0 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ func NewFuncMap() []template.FuncMap {
"RenderLabels": func(labels []*models.Label) template.HTML {
html := `<span class="labels-list">`
for _, label := range labels {
// Protect against nil value in labels - shouldn't happen but would cause a panic if so
if label == nil {
continue
}
html += fmt.Sprintf("<div class='ui label' style='color: %s; background-color: %s'>%s</div> ",
label.ForegroundColor(), label.Color, RenderEmoji(label.Name))
}
Expand Down
12 changes: 7 additions & 5 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,7 @@ func combineLabelComments(issue *models.Issue) {
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.Type == models.CommentTypeLabel && cur.Label != nil {
if cur.Content != "1" {
cur.RemovedLabels = append(cur.RemovedLabels, cur.Label)
} else {
Expand All @@ -2474,10 +2474,12 @@ func combineLabelComments(issue *models.Issue) {
continue
}

if cur.Content != "1" {
prev.RemovedLabels = append(prev.RemovedLabels, cur.Label)
} else {
prev.AddedLabels = append(prev.AddedLabels, cur.Label)
if cur.Label != nil {
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:]...)
Expand Down