Skip to content

Add advanced labels with priority #11669

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions models/fixtures/label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
org_id: 0
name: label1
color: '#abcdef'
priority: high
exclusive: false
num_issues: 2
num_closed_issues: 0
Expand Down
44 changes: 44 additions & 0 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,31 @@ func (issue *Issue) LoadLabels(ctx context.Context) (err error) {
return nil
}

// CalculatePriority calculates priority value of an issue based on its labels
func (issue *Issue) CalculatePriority() {
// If no labels are set then set default priority
if issue.Labels == nil {
issue.Priority = 0
return
}
// Use only the labels that has priority set
p := -10000000
for _, label := range issue.Labels {
if label.Priority.IsEmpty() {
continue
}
if pv := label.Priority.Value(); p < pv {
p = pv
}
}
// If no label has priority use default
if p == -10000000 {
issue.Priority = 0
} else {
issue.Priority = p
}
}

// LoadPoster loads poster
func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
if issue.Poster == nil && issue.PosterID != 0 {
Expand Down Expand Up @@ -521,6 +546,11 @@ func ClearIssueLabels(issue *Issue, doer *user_model.User) (err error) {
return err
}

issue.Priority = 0
if err = UpdateIssueCols(ctx, issue, "priority"); err != nil {
return err
}

if err = committer.Commit(); err != nil {
return fmt.Errorf("Commit: %w", err)
}
Expand Down Expand Up @@ -634,6 +664,11 @@ func ReplaceIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (e
return err
}

issue.CalculatePriority()
if err = UpdateIssueCols(ctx, issue, "priority"); err != nil {
return err
}

return committer.Commit()
}

Expand Down Expand Up @@ -1020,6 +1055,15 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
return fmt.Errorf("addLabel [id: %d]: %w", label.ID, err)
}
}

opts.Issue.Labels = nil
if err = opts.Issue.LoadLabels(ctx); err != nil {
return err
}

if err = UpdateIssueCols(ctx, opts.Issue, "priority"); err != nil {
return err
}
}

if err = NewIssueUsers(ctx, opts.Repo, opts.Issue); err != nil {
Expand Down
31 changes: 28 additions & 3 deletions models/issues/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ type Label struct {
Name string
Exclusive bool
Description string
Color string `xorm:"VARCHAR(7)"`
Color string `xorm:"VARCHAR(7)"`
Priority label.Priority `xorm:"VARCHAR(20)"`
NumIssues int
NumClosedIssues int
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
Expand Down Expand Up @@ -204,6 +205,9 @@ func NewLabel(ctx context.Context, l *Label) error {
if err != nil {
return err
}
if !l.Priority.IsValid() {
return fmt.Errorf("invalid priority: %s", l.Priority)
}
l.Color = color

return db.Insert(ctx, l)
Expand All @@ -222,6 +226,9 @@ func NewLabels(labels ...*Label) error {
if err != nil {
return err
}
if !l.Priority.IsValid() {
return fmt.Errorf("invalid priority: %s", l.Priority)
}
l.Color = color

if err := db.Insert(ctx, l); err != nil {
Expand All @@ -237,9 +244,12 @@ func UpdateLabel(l *Label) error {
if err != nil {
return err
}
if !l.Priority.IsValid() {
return fmt.Errorf("invalid priority: %s", l.Priority)
}
l.Color = color

return updateLabelCols(db.DefaultContext, l, "name", "description", "color", "exclusive")
return updateLabelCols(db.DefaultContext, l, "name", "exclusive", "color", "priority", "description")
}

// DeleteLabel delete a label
Expand Down Expand Up @@ -663,6 +673,11 @@ func NewIssueLabel(issue *Issue, label *Label, doer *user_model.User) (err error
return err
}

issue.CalculatePriority()
if err = UpdateIssueCols(ctx, issue, "priority"); err != nil {
return err
}

return committer.Commit()
}

Expand Down Expand Up @@ -703,6 +718,11 @@ func NewIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (err e
return err
}

issue.CalculatePriority()
if err = UpdateIssueCols(ctx, issue, "priority"); err != nil {
return err
}

return committer.Commit()
}

Expand Down Expand Up @@ -741,7 +761,12 @@ func DeleteIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *use
}

issue.Labels = nil
return issue.LoadLabels(ctx)
if err := issue.LoadLabels(ctx); err != nil {
return err
}

issue.CalculatePriority()
return UpdateIssueCols(ctx, issue, "priority")
}

// DeleteLabelsByRepoID deletes labels of some repository
Expand Down
Loading