Skip to content

Commit 05a2a82

Browse files
committed
Add advanced labels with priority
* Calculate issue priority when adding/removing labels
1 parent df00cca commit 05a2a82

File tree

29 files changed

+396
-88
lines changed

29 files changed

+396
-88
lines changed

models/fixtures/label.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
org_id: 0
55
name: label1
66
color: '#abcdef'
7+
priority: high
78
exclusive: false
89
num_issues: 2
910
num_closed_issues: 0

models/issues/issue.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,31 @@ func (issue *Issue) LoadLabels(ctx context.Context) (err error) {
232232
return nil
233233
}
234234

235+
// CalculatePriority calculates priority value of an issue based on its labels
236+
func (issue *Issue) CalculatePriority() {
237+
// If no labels are set then set default priority
238+
if issue.Labels == nil {
239+
issue.Priority = 0
240+
return
241+
}
242+
// Use only the labels that has priority set
243+
p := -10000000
244+
for _, label := range issue.Labels {
245+
if label.Priority.IsEmpty() {
246+
continue
247+
}
248+
if pv := label.Priority.Value(); p < pv {
249+
p = pv
250+
}
251+
}
252+
// If no label has priority use default
253+
if p == -10000000 {
254+
issue.Priority = 0
255+
} else {
256+
issue.Priority = p
257+
}
258+
}
259+
235260
// LoadPoster loads poster
236261
func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
237262
if issue.Poster == nil && issue.PosterID != 0 {
@@ -521,6 +546,11 @@ func ClearIssueLabels(issue *Issue, doer *user_model.User) (err error) {
521546
return err
522547
}
523548

549+
issue.Priority = 0
550+
if err = UpdateIssueCols(ctx, issue, "priority"); err != nil {
551+
return err
552+
}
553+
524554
if err = committer.Commit(); err != nil {
525555
return fmt.Errorf("Commit: %w", err)
526556
}
@@ -634,6 +664,11 @@ func ReplaceIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (e
634664
return err
635665
}
636666

667+
issue.CalculatePriority()
668+
if err = UpdateIssueCols(ctx, issue, "priority"); err != nil {
669+
return err
670+
}
671+
637672
return committer.Commit()
638673
}
639674

@@ -1020,6 +1055,15 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
10201055
return fmt.Errorf("addLabel [id: %d]: %w", label.ID, err)
10211056
}
10221057
}
1058+
1059+
opts.Issue.Labels = nil
1060+
if err = opts.Issue.LoadLabels(ctx); err != nil {
1061+
return err
1062+
}
1063+
1064+
if err = UpdateIssueCols(ctx, opts.Issue, "priority"); err != nil {
1065+
return err
1066+
}
10231067
}
10241068

10251069
if err = NewIssueUsers(ctx, opts.Repo, opts.Issue); err != nil {

models/issues/label.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ type Label struct {
8686
Name string
8787
Exclusive bool
8888
Description string
89-
Color string `xorm:"VARCHAR(7)"`
89+
Color string `xorm:"VARCHAR(7)"`
90+
Priority label.Priority `xorm:"VARCHAR(20)"`
9091
NumIssues int
9192
NumClosedIssues int
9293
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
@@ -204,6 +205,9 @@ func NewLabel(ctx context.Context, l *Label) error {
204205
if err != nil {
205206
return err
206207
}
208+
if !l.Priority.IsValid() {
209+
return fmt.Errorf("invalid priority: %s", l.Priority)
210+
}
207211
l.Color = color
208212

209213
return db.Insert(ctx, l)
@@ -222,6 +226,9 @@ func NewLabels(labels ...*Label) error {
222226
if err != nil {
223227
return err
224228
}
229+
if !l.Priority.IsValid() {
230+
return fmt.Errorf("invalid priority: %s", l.Priority)
231+
}
225232
l.Color = color
226233

227234
if err := db.Insert(ctx, l); err != nil {
@@ -237,9 +244,12 @@ func UpdateLabel(l *Label) error {
237244
if err != nil {
238245
return err
239246
}
247+
if !l.Priority.IsValid() {
248+
return fmt.Errorf("invalid priority: %s", l.Priority)
249+
}
240250
l.Color = color
241251

242-
return updateLabelCols(db.DefaultContext, l, "name", "description", "color", "exclusive")
252+
return updateLabelCols(db.DefaultContext, l, "name", "exclusive", "color", "priority", "description")
243253
}
244254

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

676+
issue.CalculatePriority()
677+
if err = UpdateIssueCols(ctx, issue, "priority"); err != nil {
678+
return err
679+
}
680+
666681
return committer.Commit()
667682
}
668683

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

721+
issue.CalculatePriority()
722+
if err = UpdateIssueCols(ctx, issue, "priority"); err != nil {
723+
return err
724+
}
725+
706726
return committer.Commit()
707727
}
708728

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

743763
issue.Labels = nil
744-
return issue.LoadLabels(ctx)
764+
if err := issue.LoadLabels(ctx); err != nil {
765+
return err
766+
}
767+
768+
issue.CalculatePriority()
769+
return UpdateIssueCols(ctx, issue, "priority")
745770
}
746771

747772
// DeleteLabelsByRepoID deletes labels of some repository

0 commit comments

Comments
 (0)