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

WIP: Weighted Issues #32086

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions models/fixtures/issue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
created_unix: 946684800
updated_unix: 978307200
is_locked: false
weight: 10

-
id: 2
Expand All @@ -31,6 +32,7 @@
created_unix: 946684810
updated_unix: 978307190
is_locked: false
weight: 12

-
id: 3
Expand All @@ -48,6 +50,7 @@
created_unix: 946684820
updated_unix: 978307180
is_locked: false
weight: 5

-
id: 4
Expand All @@ -65,6 +68,7 @@
created_unix: 946684830
updated_unix: 978307200
is_locked: false
weight: 10

-
id: 5
Expand All @@ -82,6 +86,7 @@
created_unix: 946684840
updated_unix: 978307200
is_locked: false
weight: 20

-
id: 6
Expand All @@ -99,6 +104,7 @@
created_unix: 946684850
updated_unix: 978307200
is_locked: false
weight: 0

-
id: 7
Expand All @@ -116,6 +122,7 @@
created_unix: 946684830
updated_unix: 978307200
is_locked: false
weight: 10

-
id: 8
Expand All @@ -133,6 +140,7 @@
created_unix: 946684820
updated_unix: 978307180
is_locked: false
weight: 0

-
id: 9
Expand All @@ -150,6 +158,7 @@
created_unix: 946684820
updated_unix: 978307180
is_locked: false
weight: 10

-
id: 10
Expand All @@ -168,6 +177,7 @@
created_unix: 946684830
updated_unix: 999307200
is_locked: false
weight: 4

-
id: 11
Expand All @@ -185,6 +195,7 @@
created_unix: 1579194806
updated_unix: 1579194806
is_locked: false
weight: 0

-
id: 12
Expand All @@ -202,6 +213,7 @@
created_unix: 1602935696
updated_unix: 1602935696
is_locked: false
weight: 22

-
id: 13
Expand All @@ -219,6 +231,7 @@
created_unix: 1602935696
updated_unix: 1602935696
is_locked: false
weight: 13

-
id: 14
Expand All @@ -236,6 +249,7 @@
created_unix: 1602935696
updated_unix: 1602935696
is_locked: false
weight: 8

-
id: 15
Expand All @@ -253,6 +267,7 @@
created_unix: 1602935696
updated_unix: 1602935696
is_locked: false
weight: 52

-
id: 16
Expand All @@ -270,6 +285,7 @@
created_unix: 1602935696
updated_unix: 1602935696
is_locked: false
weight: 10

-
id: 17
Expand All @@ -287,6 +303,7 @@
created_unix: 1602935696
updated_unix: 1602935696
is_locked: false
weight: 0

-
id: 18
Expand All @@ -304,6 +321,7 @@
created_unix: 946684830
updated_unix: 978307200
is_locked: false
weight: 20

-
id: 19
Expand All @@ -321,6 +339,7 @@
created_unix: 946684830
updated_unix: 978307200
is_locked: false
weight: 10

-
id: 20
Expand All @@ -338,6 +357,7 @@
created_unix: 978307210
updated_unix: 978307210
is_locked: false
weight: 24

-
id: 21
Expand All @@ -355,6 +375,7 @@
created_unix: 1707270422
updated_unix: 1707270422
is_locked: false
weight: 40

-
id: 22
Expand All @@ -372,3 +393,4 @@
created_unix: 1707270422
updated_unix: 1707270422
is_locked: false
weight: 2
38 changes: 38 additions & 0 deletions models/issues/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ const (

CommentTypePin // 36 pin Issue
CommentTypeUnpin // 37 unpin Issue

CommentTypeAddedWeight // 38 Added a weight
CommentTypeModifiedWeight // 39 Modified the weight
CommentTypeRemovedWeight // 40 Removed a weight
)

var commentStrings = []string{
Expand Down Expand Up @@ -960,6 +964,40 @@ func createDeadlineComment(ctx context.Context, doer *user_model.User, issue *Is
return comment, nil
}

func CreateWeightComment(ctx context.Context, doer *user_model.User, issue *Issue, newWeight int) (*Comment, error) {
var content string
var commentType CommentType

// weight = 0 means deleting
if newWeight == 0 {
commentType = CommentTypeRemovedWeight
content = fmt.Sprintf("%d", issue.Weight)
} else if issue.Weight == 0 || issue.Weight == newWeight {
commentType = CommentTypeAddedWeight
content = fmt.Sprintf("%d", newWeight)
} else { // Otherwise modified
commentType = CommentTypeModifiedWeight
content = fmt.Sprintf("%d|%d", newWeight, issue.Weight)
}

if err := issue.LoadRepo(ctx); err != nil {
return nil, err
}

opts := &CreateCommentOptions{
Type: commentType,
Doer: doer,
Repo: issue.Repo,
Issue: issue,
Content: content,
}
comment, err := CreateComment(ctx, opts)
if err != nil {
return nil, err
}
return comment, nil
}

// Creates issue dependency comment
func createIssueDependencyComment(ctx context.Context, doer *user_model.User, issue, dependentIssue *Issue, add bool) (err error) {
cType := CommentTypeAddDependency
Expand Down
1 change: 1 addition & 0 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type Issue struct {
NumComments int
Ref string
PinOrder int `xorm:"DEFAULT 0"`
Weight int

DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"`

Expand Down
20 changes: 20 additions & 0 deletions models/issues/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,26 @@ func TestLoadTotalTrackedTime(t *testing.T) {
assert.Equal(t, int64(3682), milestone.TotalTrackedTime)
}

func TestMilestoneList_LoadTotalWeight(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
miles := issues_model.MilestoneList{
unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}),
}

assert.NoError(t, miles.LoadTotalWeight(db.DefaultContext))

assert.Equal(t, 12, miles[0].TotalWeight)
}

func TestLoadTotalWeight(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1})

assert.NoError(t, milestone.LoadTotalWeight(db.DefaultContext))

assert.Equal(t, 12, milestone.TotalWeight)
}

func TestCountIssues(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
count, err := issues_model.CountIssues(db.DefaultContext, &issues_model.IssuesOptions{})
Expand Down
25 changes: 25 additions & 0 deletions models/issues/issue_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,31 @@ func UpdateIssueDeadline(ctx context.Context, issue *Issue, deadlineUnix timeuti
return committer.Commit()
}

// UpdateIssueDeadline updates an issue deadline and adds comments. Setting a deadline to 0 means deleting it.
func UpdateIssueWeight(ctx context.Context, issue *Issue, weight int, doer *user_model.User) (err error) {
// if the weight hasn't changed do nothing
if issue.Weight == weight {
return nil
}
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
}
defer committer.Close()

// Update the weight
if err = UpdateIssueCols(ctx, &Issue{ID: issue.ID, Weight: weight}, "weight"); err != nil {
return err
}

// Make the comment
if _, err = CreateWeightComment(ctx, doer, issue, weight); err != nil {
return fmt.Errorf("createWeightComment: %w", err)
}

return committer.Commit()
}

// FindAndUpdateIssueMentions finds users mentioned in the given content string, and saves them in the database.
func FindAndUpdateIssueMentions(ctx context.Context, issue *Issue, doer *user_model.User, content string) (mentions []*user_model.User, err error) {
rawMentions := references.FindAllMentionsMarkdown(content)
Expand Down
31 changes: 30 additions & 1 deletion models/issues/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ type Milestone struct {
ClosedDateUnix timeutil.TimeStamp
DeadlineString string `xorm:"-"`

TotalTrackedTime int64 `xorm:"-"`
TotalTrackedTime int64 `xorm:"-"`
TotalWeight int `xorm:"-"`
WeightedCompleteness int `xorm:"-"`
}

func init() {
Expand Down Expand Up @@ -355,6 +357,33 @@ func (m *Milestone) LoadTotalTrackedTime(ctx context.Context) error {
return nil
}

// LoadTotalWeight loads the total weight for the milestone
func (m *Milestone) LoadTotalWeight(ctx context.Context) error {
type totalWeightByMilestone struct {
MilestoneID int64
Weight int
WeightClosed int
}

totalWeight := &totalWeightByMilestone{MilestoneID: m.ID}
has, err := db.GetEngine(ctx).Table("issue").
Join("INNER", "milestone", "issue.milestone_id = milestone.id").
Select("milestone_id, sum(weight) as weight, sum(CASE WHEN is_closed THEN 0 ELSE weight END) as weight_closed").
Where("milestone_id = ?", m.ID).
GroupBy("milestone_id").
Get(totalWeight)
if err != nil {
return err
} else if !has {
return nil
}

m.TotalWeight = totalWeight.Weight
m.WeightedCompleteness = totalWeight.WeightClosed * 100 / totalWeight.Weight

return nil
}

// InsertMilestones creates milestones of repository.
func InsertMilestones(ctx context.Context, ms ...*Milestone) (err error) {
if len(ms) == 0 {
Expand Down
43 changes: 43 additions & 0 deletions models/issues/milestone_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,49 @@ func (milestones MilestoneList) LoadTotalTrackedTimes(ctx context.Context) error
return nil
}

// LoadTotalWeight loads for every milestone in the list the TotalWeight by a batch request
func (milestones MilestoneList) LoadTotalWeight(ctx context.Context) error {
type totalWeightByMilestone struct {
MilestoneID int64
Weight int
WeightClosed int
}
if len(milestones) == 0 {
return nil
}

weight := make(map[int64]int, len(milestones))
closedWeight := make(map[int64]int, len(milestones))

rows, err := db.GetEngine(ctx).Table("issue").
Join("INNER", "milestone", "issue.milestone_id = milestone.id").
Select("milestone_id, sum(weight) as weight, sum(CASE WHEN is_closed THEN 0 ELSE weight END) as weight_closed").
In("milestone_id", milestones.getMilestoneIDs()).
GroupBy("milestone_id").
Rows(new(totalWeightByMilestone))
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
var totalWeight totalWeightByMilestone
err = rows.Scan(&totalWeight)
if err != nil {
return err
}
weight[totalWeight.MilestoneID] = totalWeight.Weight
closedWeight[totalWeight.MilestoneID] = totalWeight.WeightClosed
}

for _, milestone := range milestones {
milestone.TotalWeight = weight[milestone.ID]
milestone.WeightedCompleteness = closedWeight[milestone.ID] * 100 / milestone.TotalWeight
}

return nil
}

// CountMilestonesByRepoCondAndKw map from repo conditions and the keyword of milestones' name to number of milestones matching the options`
func CountMilestonesMap(ctx context.Context, opts FindMilestoneOptions) (map[int64]int64, error) {
sess := db.GetEngine(ctx).Where(opts.ToConds())
Expand Down
Loading
Loading