2017-02-09 06:39:26 +00:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 18:20:29 +00:00
// SPDX-License-Identifier: MIT
2017-02-09 06:39:26 +00:00
2022-04-08 09:11:15 +00:00
package issues
2017-02-09 06:39:26 +00:00
import (
2021-11-19 13:39:57 +00:00
"context"
2018-05-16 14:01:55 +00:00
"fmt"
2020-01-27 16:23:21 +00:00
"strings"
2018-05-16 14:01:55 +00:00
2021-09-19 11:49:59 +00:00
"code.gitea.io/gitea/models/db"
2021-12-10 01:27:50 +00:00
repo_model "code.gitea.io/gitea/models/repo"
2017-02-09 06:39:26 +00:00
"code.gitea.io/gitea/modules/setting"
2019-05-11 10:21:34 +00:00
api "code.gitea.io/gitea/modules/structs"
2019-08-15 14:46:21 +00:00
"code.gitea.io/gitea/modules/timeutil"
2022-10-18 05:50:37 +00:00
"code.gitea.io/gitea/modules/util"
2019-08-15 14:46:21 +00:00
2019-12-27 20:30:58 +00:00
"xorm.io/builder"
2017-02-09 06:39:26 +00:00
)
2022-04-08 09:11:15 +00:00
// ErrMilestoneNotExist represents a "MilestoneNotExist" kind of error.
type ErrMilestoneNotExist struct {
ID int64
RepoID int64
Name string
}
// IsErrMilestoneNotExist checks if an error is a ErrMilestoneNotExist.
func IsErrMilestoneNotExist ( err error ) bool {
_ , ok := err . ( ErrMilestoneNotExist )
return ok
}
func ( err ErrMilestoneNotExist ) Error ( ) string {
if len ( err . Name ) > 0 {
return fmt . Sprintf ( "milestone does not exist [name: %s, repo_id: %d]" , err . Name , err . RepoID )
}
return fmt . Sprintf ( "milestone does not exist [id: %d, repo_id: %d]" , err . ID , err . RepoID )
}
2022-10-18 05:50:37 +00:00
func ( err ErrMilestoneNotExist ) Unwrap ( ) error {
return util . ErrNotExist
}
2017-02-09 06:39:26 +00:00
// Milestone represents a milestone of repository.
type Milestone struct {
2021-12-10 01:27:50 +00:00
ID int64 ` xorm:"pk autoincr" `
RepoID int64 ` xorm:"INDEX" `
Repo * repo_model . Repository ` xorm:"-" `
2017-02-09 06:39:26 +00:00
Name string
Content string ` xorm:"TEXT" `
RenderedContent string ` xorm:"-" `
IsClosed bool
NumIssues int
NumClosedIssues int
NumOpenIssues int ` xorm:"-" `
Completeness int // Percentage(1-100).
2018-05-07 09:50:27 +00:00
IsOverdue bool ` xorm:"-" `
2017-02-09 06:39:26 +00:00
2020-09-05 17:38:54 +00:00
CreatedUnix timeutil . TimeStamp ` xorm:"INDEX created" `
UpdatedUnix timeutil . TimeStamp ` xorm:"INDEX updated" `
2019-08-15 14:46:21 +00:00
DeadlineUnix timeutil . TimeStamp
ClosedDateUnix timeutil . TimeStamp
2020-09-05 17:38:54 +00:00
DeadlineString string ` xorm:"-" `
2018-04-29 05:58:47 +00:00
TotalTrackedTime int64 ` xorm:"-" `
2017-02-09 06:39:26 +00:00
}
2021-09-19 11:49:59 +00:00
func init ( ) {
db . RegisterModel ( new ( Milestone ) )
}
2017-02-09 06:39:26 +00:00
// BeforeUpdate is invoked from XORM before updating this object.
func ( m * Milestone ) BeforeUpdate ( ) {
if m . NumIssues > 0 {
m . Completeness = m . NumClosedIssues * 100 / m . NumIssues
} else {
m . Completeness = 0
}
}
2017-10-01 16:52:35 +00:00
// AfterLoad is invoked from XORM after setting the value of a field of
2017-02-09 06:39:26 +00:00
// this object.
2017-10-01 16:52:35 +00:00
func ( m * Milestone ) AfterLoad ( ) {
m . NumOpenIssues = m . NumIssues - m . NumClosedIssues
2017-12-11 04:37:04 +00:00
if m . DeadlineUnix . Year ( ) == 9999 {
2017-10-01 16:52:35 +00:00
return
}
2017-02-09 06:39:26 +00:00
2017-12-11 04:37:04 +00:00
m . DeadlineString = m . DeadlineUnix . Format ( "2006-01-02" )
2021-03-08 01:55:57 +00:00
if m . IsClosed {
m . IsOverdue = m . ClosedDateUnix >= m . DeadlineUnix
} else {
m . IsOverdue = timeutil . TimeStampNow ( ) >= m . DeadlineUnix
2017-02-09 06:39:26 +00:00
}
}
// State returns string representation of milestone status.
func ( m * Milestone ) State ( ) api . StateType {
if m . IsClosed {
return api . StateClosed
}
return api . StateOpen
}
// NewMilestone creates new milestone of repository.
func NewMilestone ( m * Milestone ) ( err error ) {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 15:41:00 +00:00
if err != nil {
2017-02-09 06:39:26 +00:00
return err
}
2021-11-21 15:41:00 +00:00
defer committer . Close ( )
2017-02-09 06:39:26 +00:00
2020-01-27 16:23:21 +00:00
m . Name = strings . TrimSpace ( m . Name )
2021-11-21 15:41:00 +00:00
if err = db . Insert ( ctx , m ) ; err != nil {
2017-02-09 06:39:26 +00:00
return err
}
2021-11-21 15:41:00 +00:00
if _ , err = db . Exec ( ctx , "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?" , m . RepoID ) ; err != nil {
2017-02-09 06:39:26 +00:00
return err
}
2021-11-21 15:41:00 +00:00
return committer . Commit ( )
2017-02-09 06:39:26 +00:00
}
2022-06-30 15:55:08 +00:00
// HasMilestoneByRepoID returns if the milestone exists in the repository.
func HasMilestoneByRepoID ( ctx context . Context , repoID , id int64 ) ( bool , error ) {
return db . GetEngine ( ctx ) . ID ( id ) . Where ( "repo_id=?" , repoID ) . Exist ( new ( Milestone ) )
}
2022-04-08 09:11:15 +00:00
// GetMilestoneByRepoID returns the milestone in a repository.
func GetMilestoneByRepoID ( ctx context . Context , repoID , id int64 ) ( * Milestone , error ) {
2020-04-30 04:15:39 +00:00
m := new ( Milestone )
2022-04-08 09:11:15 +00:00
has , err := db . GetEngine ( ctx ) . ID ( id ) . Where ( "repo_id=?" , repoID ) . Get ( m )
2017-02-09 06:39:26 +00:00
if err != nil {
return nil , err
} else if ! has {
2020-04-30 04:15:39 +00:00
return nil , ErrMilestoneNotExist { ID : id , RepoID : repoID }
2017-02-09 06:39:26 +00:00
}
return m , nil
}
2020-04-30 04:15:39 +00:00
// GetMilestoneByRepoIDANDName return a milestone if one exist by name and repo
func GetMilestoneByRepoIDANDName ( repoID int64 , name string ) ( * Milestone , error ) {
var mile Milestone
2021-09-23 15:45:36 +00:00
has , err := db . GetEngine ( db . DefaultContext ) . Where ( "repo_id=? AND name=?" , repoID , name ) . Get ( & mile )
2020-04-30 04:15:39 +00:00
if err != nil {
return nil , err
}
if ! has {
return nil , ErrMilestoneNotExist { Name : name , RepoID : repoID }
}
return & mile , nil
}
2017-02-09 06:39:26 +00:00
// UpdateMilestone updates information of given milestone.
2020-01-29 06:36:32 +00:00
func UpdateMilestone ( m * Milestone , oldIsClosed bool ) error {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 15:41:00 +00:00
if err != nil {
2020-01-29 06:36:32 +00:00
return err
}
2021-11-21 15:41:00 +00:00
defer committer . Close ( )
2020-01-29 06:36:32 +00:00
if m . IsClosed && ! oldIsClosed {
m . ClosedDateUnix = timeutil . TimeStampNow ( )
}
2022-01-17 18:31:58 +00:00
if err := updateMilestone ( ctx , m ) ; err != nil {
2019-10-07 21:44:58 +00:00
return err
}
2020-01-29 06:36:32 +00:00
// if IsClosed changed, update milestone numbers of repository
if oldIsClosed != m . IsClosed {
2022-01-17 18:31:58 +00:00
if err := updateRepoMilestoneNum ( ctx , m . RepoID ) ; err != nil {
2020-01-29 06:36:32 +00:00
return err
}
}
2021-11-21 15:41:00 +00:00
return committer . Commit ( )
2019-10-07 21:44:58 +00:00
}
2022-01-17 18:31:58 +00:00
func updateMilestone ( ctx context . Context , m * Milestone ) error {
2020-05-12 21:54:35 +00:00
m . Name = strings . TrimSpace ( m . Name )
2022-01-17 18:31:58 +00:00
_ , err := db . GetEngine ( ctx ) . ID ( m . ID ) . AllCols ( ) . Update ( m )
2021-06-21 18:34:58 +00:00
if err != nil {
return err
}
2022-04-08 09:11:15 +00:00
return UpdateMilestoneCounters ( ctx , m . ID )
2021-06-21 18:34:58 +00:00
}
[FEAT] allow setting the update date on issues and comments
This field adds the possibility to set the update date when modifying
an issue through the API.
A 'NoAutoDate' in-memory field is added in the Issue struct.
If the update_at field is set, NoAutoDate is set to true and the
Issue's UpdatedUnix field is filled.
That information is passed down to the functions that actually updates
the database, which have been modified to not auto update dates if
requested.
A guard is added to the 'EditIssue' API call, to checks that the
udpate_at date is between the issue's creation date and the current
date (to avoid 'malicious' changes). It also limits the new feature
to project's owners and admins.
(cherry picked from commit c524d33402c76bc4cccea2806f289e08a009baae)
Add a SetIssueUpdateDate() function in services/issue.go
That function is used by some API calls to set the NoAutoDate and
UpdatedUnix fields of an Issue if an updated_at date is provided.
(cherry picked from commit f061caa6555e0c9e922ee1e73dd2e4337360e9fe)
Add an updated_at field to the API calls related to Issue's Labels.
The update date is applied to the issue's comment created to inform
about the modification of the issue's labels.
(cherry picked from commit ea36cf80f58f0ab20c565a8f5d063b90fd741f97)
Add an updated_at field to the API call for issue's attachment creation
The update date is applied to the issue's comment created to inform
about the modification of the issue's content, and is set as the
asset creation date.
(cherry picked from commit 96150971ca31b97e97e84d5f5eb95a177cc44e2e)
Checking Issue changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPIEditIssueWithAutoDate
- TestAPIEditIssueWithNoAutoDate
- TestAPIAddIssueLabelsWithAutoDate
- TestAPIAddIssueLabelsWithNoAutoDate
- TestAPICreateIssueAttachmentWithAutoDate
- TestAPICreateIssueAttachmentWithNoAutoDate
(cherry picked from commit 4926a5d7a28581003545256632213bf4136b193d)
Add an updated_at field to the API call for issue's comment creation
The update date is used as the comment creation date, and is applied to
the issue as the update creation date.
(cherry picked from commit 76c8faecdc6cba48ca4fe07d1a916d1f1a4b37b4)
Add an updated_at field to the API call for issue's comment edition
The update date is used as the comment update date, and is applied to
the issue as an update date.
(cherry picked from commit cf787ad7fdb8e6273fdc35d7b5cc164b400207e9)
Add an updated_at field to the API call for comment's attachment creation
The update date is applied to the comment, and is set as the asset
creation date.
(cherry picked from commit 1e4ff424d39db7a4256cd9abf9c58b8d3e1b5c14)
Checking Comment changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPICreateCommentWithAutoDate
- TestAPICreateCommentWithNoAutoDate
- TestAPIEditCommentWithAutoDate
- TestAPIEditCommentWithNoAutoDate
- TestAPICreateCommentAttachmentWithAutoDate
- TestAPICreateCommentAttachmentWithNoAutoDate
(cherry picked from commit da932152f1deb3039a399516a51c8b6757059c91)
Pettier code to set the update time of comments
Now uses sess.AllCols().NoAutoToime().SetExpr("updated_unix", ...)
XORM is smart enough to compose one single SQL UPDATE which all
columns + updated_unix.
(cherry picked from commit 1f6a42808dd739c0c2e49e6b7ae2967f120f43c2)
Issue edition: Keep the max of the milestone and issue update dates.
When editing an issue via the API, an updated_at date can be provided.
If the EditIssue call changes the issue's milestone, the milestone's
update date is to be changed accordingly, but only with a greater
value.
This ensures that a milestone's update date is the max of all issue's
update dates.
(cherry picked from commit 8f22ea182e6b49e933dc6534040160dd739ff18a)
Rewrite the 'AutoDate' tests using subtests
Also add a test to check the permissions to set a date, and a test
to check update dates on milestones.
The tests related to 'AutoDate' are:
- TestAPIEditIssueAutoDate
- TestAPIAddIssueLabelsAutoDate
- TestAPIEditIssueMilestoneAutoDate
- TestAPICreateIssueAttachmentAutoDate
- TestAPICreateCommentAutoDate
- TestAPIEditCommentWithDate
- TestAPICreateCommentAttachmentAutoDate
(cherry picked from commit 961fd13c551b3e50040acb7c914a00ead92de63f)
2023-05-30 16:42:58 +00:00
func updateMilestoneCounters ( ctx context . Context , id int64 , noAutoTime bool , updatedUnix timeutil . TimeStamp ) error {
2022-01-17 18:31:58 +00:00
e := db . GetEngine ( ctx )
[FEAT] allow setting the update date on issues and comments
This field adds the possibility to set the update date when modifying
an issue through the API.
A 'NoAutoDate' in-memory field is added in the Issue struct.
If the update_at field is set, NoAutoDate is set to true and the
Issue's UpdatedUnix field is filled.
That information is passed down to the functions that actually updates
the database, which have been modified to not auto update dates if
requested.
A guard is added to the 'EditIssue' API call, to checks that the
udpate_at date is between the issue's creation date and the current
date (to avoid 'malicious' changes). It also limits the new feature
to project's owners and admins.
(cherry picked from commit c524d33402c76bc4cccea2806f289e08a009baae)
Add a SetIssueUpdateDate() function in services/issue.go
That function is used by some API calls to set the NoAutoDate and
UpdatedUnix fields of an Issue if an updated_at date is provided.
(cherry picked from commit f061caa6555e0c9e922ee1e73dd2e4337360e9fe)
Add an updated_at field to the API calls related to Issue's Labels.
The update date is applied to the issue's comment created to inform
about the modification of the issue's labels.
(cherry picked from commit ea36cf80f58f0ab20c565a8f5d063b90fd741f97)
Add an updated_at field to the API call for issue's attachment creation
The update date is applied to the issue's comment created to inform
about the modification of the issue's content, and is set as the
asset creation date.
(cherry picked from commit 96150971ca31b97e97e84d5f5eb95a177cc44e2e)
Checking Issue changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPIEditIssueWithAutoDate
- TestAPIEditIssueWithNoAutoDate
- TestAPIAddIssueLabelsWithAutoDate
- TestAPIAddIssueLabelsWithNoAutoDate
- TestAPICreateIssueAttachmentWithAutoDate
- TestAPICreateIssueAttachmentWithNoAutoDate
(cherry picked from commit 4926a5d7a28581003545256632213bf4136b193d)
Add an updated_at field to the API call for issue's comment creation
The update date is used as the comment creation date, and is applied to
the issue as the update creation date.
(cherry picked from commit 76c8faecdc6cba48ca4fe07d1a916d1f1a4b37b4)
Add an updated_at field to the API call for issue's comment edition
The update date is used as the comment update date, and is applied to
the issue as an update date.
(cherry picked from commit cf787ad7fdb8e6273fdc35d7b5cc164b400207e9)
Add an updated_at field to the API call for comment's attachment creation
The update date is applied to the comment, and is set as the asset
creation date.
(cherry picked from commit 1e4ff424d39db7a4256cd9abf9c58b8d3e1b5c14)
Checking Comment changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPICreateCommentWithAutoDate
- TestAPICreateCommentWithNoAutoDate
- TestAPIEditCommentWithAutoDate
- TestAPIEditCommentWithNoAutoDate
- TestAPICreateCommentAttachmentWithAutoDate
- TestAPICreateCommentAttachmentWithNoAutoDate
(cherry picked from commit da932152f1deb3039a399516a51c8b6757059c91)
Pettier code to set the update time of comments
Now uses sess.AllCols().NoAutoToime().SetExpr("updated_unix", ...)
XORM is smart enough to compose one single SQL UPDATE which all
columns + updated_unix.
(cherry picked from commit 1f6a42808dd739c0c2e49e6b7ae2967f120f43c2)
Issue edition: Keep the max of the milestone and issue update dates.
When editing an issue via the API, an updated_at date can be provided.
If the EditIssue call changes the issue's milestone, the milestone's
update date is to be changed accordingly, but only with a greater
value.
This ensures that a milestone's update date is the max of all issue's
update dates.
(cherry picked from commit 8f22ea182e6b49e933dc6534040160dd739ff18a)
Rewrite the 'AutoDate' tests using subtests
Also add a test to check the permissions to set a date, and a test
to check update dates on milestones.
The tests related to 'AutoDate' are:
- TestAPIEditIssueAutoDate
- TestAPIAddIssueLabelsAutoDate
- TestAPIEditIssueMilestoneAutoDate
- TestAPICreateIssueAttachmentAutoDate
- TestAPICreateCommentAutoDate
- TestAPIEditCommentWithDate
- TestAPICreateCommentAttachmentAutoDate
(cherry picked from commit 961fd13c551b3e50040acb7c914a00ead92de63f)
2023-05-30 16:42:58 +00:00
sess := e . ID ( id ) .
2020-05-12 21:54:35 +00:00
SetExpr ( "num_issues" , builder . Select ( "count(*)" ) . From ( "issue" ) . Where (
2021-06-21 18:34:58 +00:00
builder . Eq { "milestone_id" : id } ,
2020-05-12 21:54:35 +00:00
) ) .
SetExpr ( "num_closed_issues" , builder . Select ( "count(*)" ) . From ( "issue" ) . Where (
builder . Eq {
2021-06-21 18:34:58 +00:00
"milestone_id" : id ,
2020-05-12 21:54:35 +00:00
"is_closed" : true ,
} ,
[FEAT] allow setting the update date on issues and comments
This field adds the possibility to set the update date when modifying
an issue through the API.
A 'NoAutoDate' in-memory field is added in the Issue struct.
If the update_at field is set, NoAutoDate is set to true and the
Issue's UpdatedUnix field is filled.
That information is passed down to the functions that actually updates
the database, which have been modified to not auto update dates if
requested.
A guard is added to the 'EditIssue' API call, to checks that the
udpate_at date is between the issue's creation date and the current
date (to avoid 'malicious' changes). It also limits the new feature
to project's owners and admins.
(cherry picked from commit c524d33402c76bc4cccea2806f289e08a009baae)
Add a SetIssueUpdateDate() function in services/issue.go
That function is used by some API calls to set the NoAutoDate and
UpdatedUnix fields of an Issue if an updated_at date is provided.
(cherry picked from commit f061caa6555e0c9e922ee1e73dd2e4337360e9fe)
Add an updated_at field to the API calls related to Issue's Labels.
The update date is applied to the issue's comment created to inform
about the modification of the issue's labels.
(cherry picked from commit ea36cf80f58f0ab20c565a8f5d063b90fd741f97)
Add an updated_at field to the API call for issue's attachment creation
The update date is applied to the issue's comment created to inform
about the modification of the issue's content, and is set as the
asset creation date.
(cherry picked from commit 96150971ca31b97e97e84d5f5eb95a177cc44e2e)
Checking Issue changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPIEditIssueWithAutoDate
- TestAPIEditIssueWithNoAutoDate
- TestAPIAddIssueLabelsWithAutoDate
- TestAPIAddIssueLabelsWithNoAutoDate
- TestAPICreateIssueAttachmentWithAutoDate
- TestAPICreateIssueAttachmentWithNoAutoDate
(cherry picked from commit 4926a5d7a28581003545256632213bf4136b193d)
Add an updated_at field to the API call for issue's comment creation
The update date is used as the comment creation date, and is applied to
the issue as the update creation date.
(cherry picked from commit 76c8faecdc6cba48ca4fe07d1a916d1f1a4b37b4)
Add an updated_at field to the API call for issue's comment edition
The update date is used as the comment update date, and is applied to
the issue as an update date.
(cherry picked from commit cf787ad7fdb8e6273fdc35d7b5cc164b400207e9)
Add an updated_at field to the API call for comment's attachment creation
The update date is applied to the comment, and is set as the asset
creation date.
(cherry picked from commit 1e4ff424d39db7a4256cd9abf9c58b8d3e1b5c14)
Checking Comment changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPICreateCommentWithAutoDate
- TestAPICreateCommentWithNoAutoDate
- TestAPIEditCommentWithAutoDate
- TestAPIEditCommentWithNoAutoDate
- TestAPICreateCommentAttachmentWithAutoDate
- TestAPICreateCommentAttachmentWithNoAutoDate
(cherry picked from commit da932152f1deb3039a399516a51c8b6757059c91)
Pettier code to set the update time of comments
Now uses sess.AllCols().NoAutoToime().SetExpr("updated_unix", ...)
XORM is smart enough to compose one single SQL UPDATE which all
columns + updated_unix.
(cherry picked from commit 1f6a42808dd739c0c2e49e6b7ae2967f120f43c2)
Issue edition: Keep the max of the milestone and issue update dates.
When editing an issue via the API, an updated_at date can be provided.
If the EditIssue call changes the issue's milestone, the milestone's
update date is to be changed accordingly, but only with a greater
value.
This ensures that a milestone's update date is the max of all issue's
update dates.
(cherry picked from commit 8f22ea182e6b49e933dc6534040160dd739ff18a)
Rewrite the 'AutoDate' tests using subtests
Also add a test to check the permissions to set a date, and a test
to check update dates on milestones.
The tests related to 'AutoDate' are:
- TestAPIEditIssueAutoDate
- TestAPIAddIssueLabelsAutoDate
- TestAPIEditIssueMilestoneAutoDate
- TestAPICreateIssueAttachmentAutoDate
- TestAPICreateCommentAutoDate
- TestAPIEditCommentWithDate
- TestAPICreateCommentAttachmentAutoDate
(cherry picked from commit 961fd13c551b3e50040acb7c914a00ead92de63f)
2023-05-30 16:42:58 +00:00
) )
if noAutoTime {
sess . SetExpr ( "updated_unix" , updatedUnix ) . NoAutoTime ( )
}
_ , err := sess . Update ( & Milestone { } )
2021-06-21 18:34:58 +00:00
if err != nil {
return err
}
_ , err = e . Exec ( "UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?" ,
id ,
2019-10-07 21:44:58 +00:00
)
return err
2017-02-09 06:39:26 +00:00
}
[FEAT] allow setting the update date on issues and comments
This field adds the possibility to set the update date when modifying
an issue through the API.
A 'NoAutoDate' in-memory field is added in the Issue struct.
If the update_at field is set, NoAutoDate is set to true and the
Issue's UpdatedUnix field is filled.
That information is passed down to the functions that actually updates
the database, which have been modified to not auto update dates if
requested.
A guard is added to the 'EditIssue' API call, to checks that the
udpate_at date is between the issue's creation date and the current
date (to avoid 'malicious' changes). It also limits the new feature
to project's owners and admins.
(cherry picked from commit c524d33402c76bc4cccea2806f289e08a009baae)
Add a SetIssueUpdateDate() function in services/issue.go
That function is used by some API calls to set the NoAutoDate and
UpdatedUnix fields of an Issue if an updated_at date is provided.
(cherry picked from commit f061caa6555e0c9e922ee1e73dd2e4337360e9fe)
Add an updated_at field to the API calls related to Issue's Labels.
The update date is applied to the issue's comment created to inform
about the modification of the issue's labels.
(cherry picked from commit ea36cf80f58f0ab20c565a8f5d063b90fd741f97)
Add an updated_at field to the API call for issue's attachment creation
The update date is applied to the issue's comment created to inform
about the modification of the issue's content, and is set as the
asset creation date.
(cherry picked from commit 96150971ca31b97e97e84d5f5eb95a177cc44e2e)
Checking Issue changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPIEditIssueWithAutoDate
- TestAPIEditIssueWithNoAutoDate
- TestAPIAddIssueLabelsWithAutoDate
- TestAPIAddIssueLabelsWithNoAutoDate
- TestAPICreateIssueAttachmentWithAutoDate
- TestAPICreateIssueAttachmentWithNoAutoDate
(cherry picked from commit 4926a5d7a28581003545256632213bf4136b193d)
Add an updated_at field to the API call for issue's comment creation
The update date is used as the comment creation date, and is applied to
the issue as the update creation date.
(cherry picked from commit 76c8faecdc6cba48ca4fe07d1a916d1f1a4b37b4)
Add an updated_at field to the API call for issue's comment edition
The update date is used as the comment update date, and is applied to
the issue as an update date.
(cherry picked from commit cf787ad7fdb8e6273fdc35d7b5cc164b400207e9)
Add an updated_at field to the API call for comment's attachment creation
The update date is applied to the comment, and is set as the asset
creation date.
(cherry picked from commit 1e4ff424d39db7a4256cd9abf9c58b8d3e1b5c14)
Checking Comment changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPICreateCommentWithAutoDate
- TestAPICreateCommentWithNoAutoDate
- TestAPIEditCommentWithAutoDate
- TestAPIEditCommentWithNoAutoDate
- TestAPICreateCommentAttachmentWithAutoDate
- TestAPICreateCommentAttachmentWithNoAutoDate
(cherry picked from commit da932152f1deb3039a399516a51c8b6757059c91)
Pettier code to set the update time of comments
Now uses sess.AllCols().NoAutoToime().SetExpr("updated_unix", ...)
XORM is smart enough to compose one single SQL UPDATE which all
columns + updated_unix.
(cherry picked from commit 1f6a42808dd739c0c2e49e6b7ae2967f120f43c2)
Issue edition: Keep the max of the milestone and issue update dates.
When editing an issue via the API, an updated_at date can be provided.
If the EditIssue call changes the issue's milestone, the milestone's
update date is to be changed accordingly, but only with a greater
value.
This ensures that a milestone's update date is the max of all issue's
update dates.
(cherry picked from commit 8f22ea182e6b49e933dc6534040160dd739ff18a)
Rewrite the 'AutoDate' tests using subtests
Also add a test to check the permissions to set a date, and a test
to check update dates on milestones.
The tests related to 'AutoDate' are:
- TestAPIEditIssueAutoDate
- TestAPIAddIssueLabelsAutoDate
- TestAPIEditIssueMilestoneAutoDate
- TestAPICreateIssueAttachmentAutoDate
- TestAPICreateCommentAutoDate
- TestAPIEditCommentWithDate
- TestAPICreateCommentAttachmentAutoDate
(cherry picked from commit 961fd13c551b3e50040acb7c914a00ead92de63f)
2023-05-30 16:42:58 +00:00
// UpdateMilestoneCounters calculates NumIssues, NumClosesIssues and Completeness
func UpdateMilestoneCounters ( ctx context . Context , id int64 ) error {
return updateMilestoneCounters ( ctx , id , false , 0 )
}
// UpdateMilestoneCountersWithDate calculates NumIssues, NumClosesIssues and Completeness and set the UpdatedUnix date
func UpdateMilestoneCountersWithDate ( ctx context . Context , id int64 , updatedUnix timeutil . TimeStamp ) error {
return updateMilestoneCounters ( ctx , id , true , updatedUnix )
}
2020-08-17 03:07:38 +00:00
// ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo.
func ChangeMilestoneStatusByRepoIDAndID ( repoID , milestoneID int64 , isClosed bool ) error {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 15:41:00 +00:00
if err != nil {
2020-08-17 03:07:38 +00:00
return err
}
2021-11-21 15:41:00 +00:00
defer committer . Close ( )
2020-08-17 03:07:38 +00:00
m := & Milestone {
ID : milestoneID ,
RepoID : repoID ,
}
2022-01-17 18:31:58 +00:00
has , err := db . GetEngine ( ctx ) . ID ( milestoneID ) . Where ( "repo_id = ?" , repoID ) . Get ( m )
2020-08-17 03:07:38 +00:00
if err != nil {
return err
} else if ! has {
return ErrMilestoneNotExist { ID : milestoneID , RepoID : repoID }
}
2022-01-17 18:31:58 +00:00
if err := changeMilestoneStatus ( ctx , m , isClosed ) ; err != nil {
2020-08-17 03:07:38 +00:00
return err
}
2021-11-21 15:41:00 +00:00
return committer . Commit ( )
2020-08-17 03:07:38 +00:00
}
2017-02-09 06:39:26 +00:00
// ChangeMilestoneStatus changes the milestone open/closed status.
func ChangeMilestoneStatus ( m * Milestone , isClosed bool ) ( err error ) {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 15:41:00 +00:00
if err != nil {
2017-02-09 06:39:26 +00:00
return err
}
2021-11-21 15:41:00 +00:00
defer committer . Close ( )
2017-02-09 06:39:26 +00:00
2022-01-17 18:31:58 +00:00
if err := changeMilestoneStatus ( ctx , m , isClosed ) ; err != nil {
2020-08-17 03:07:38 +00:00
return err
}
2021-11-21 15:41:00 +00:00
return committer . Commit ( )
2020-08-17 03:07:38 +00:00
}
2022-01-17 18:31:58 +00:00
func changeMilestoneStatus ( ctx context . Context , m * Milestone , isClosed bool ) error {
2017-02-09 06:39:26 +00:00
m . IsClosed = isClosed
2019-10-29 02:35:50 +00:00
if isClosed {
m . ClosedDateUnix = timeutil . TimeStampNow ( )
}
2022-01-17 18:31:58 +00:00
count , err := db . GetEngine ( ctx ) . ID ( m . ID ) . Where ( "repo_id = ? AND is_closed = ?" , m . RepoID , ! isClosed ) . Cols ( "is_closed" , "closed_date_unix" ) . Update ( m )
2020-08-17 03:07:38 +00:00
if err != nil {
2017-02-09 06:39:26 +00:00
return err
}
2020-08-17 03:07:38 +00:00
if count < 1 {
return nil
2017-12-18 14:06:51 +00:00
}
2022-01-17 18:31:58 +00:00
return updateRepoMilestoneNum ( ctx , m . RepoID )
2017-02-09 06:39:26 +00:00
}
// DeleteMilestoneByRepoID deletes a milestone from a repository.
func DeleteMilestoneByRepoID ( repoID , id int64 ) error {
2022-04-08 09:11:15 +00:00
m , err := GetMilestoneByRepoID ( db . DefaultContext , repoID , id )
2017-02-09 06:39:26 +00:00
if err != nil {
if IsErrMilestoneNotExist ( err ) {
return nil
}
return err
}
2022-12-03 02:48:26 +00:00
repo , err := repo_model . GetRepositoryByID ( db . DefaultContext , m . RepoID )
2017-02-09 06:39:26 +00:00
if err != nil {
return err
}
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 15:41:00 +00:00
if err != nil {
2017-02-09 06:39:26 +00:00
return err
}
2021-11-21 15:41:00 +00:00
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2017-02-09 06:39:26 +00:00
2017-10-05 04:43:04 +00:00
if _ , err = sess . ID ( m . ID ) . Delete ( new ( Milestone ) ) ; err != nil {
2017-02-09 06:39:26 +00:00
return err
}
2022-06-13 09:37:59 +00:00
numMilestones , err := CountMilestones ( ctx , GetMilestonesOption {
RepoID : repo . ID ,
State : api . StateAll ,
} )
2017-12-18 14:06:51 +00:00
if err != nil {
return err
}
2022-06-13 09:37:59 +00:00
numClosedMilestones , err := CountMilestones ( ctx , GetMilestonesOption {
RepoID : repo . ID ,
State : api . StateClosed ,
} )
2017-12-18 14:06:51 +00:00
if err != nil {
return err
}
repo . NumMilestones = int ( numMilestones )
repo . NumClosedMilestones = int ( numClosedMilestones )
2017-10-05 04:43:04 +00:00
if _ , err = sess . ID ( repo . ID ) . Cols ( "num_milestones, num_closed_milestones" ) . Update ( repo ) ; err != nil {
2017-02-09 06:39:26 +00:00
return err
}
2021-11-21 15:41:00 +00:00
if _ , err = db . Exec ( ctx , "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?" , m . ID ) ; err != nil {
2017-02-09 06:39:26 +00:00
return err
}
2021-11-21 15:41:00 +00:00
return committer . Commit ( )
2017-02-09 06:39:26 +00:00
}
2019-12-15 14:20:08 +00:00
2020-05-12 21:54:35 +00:00
// MilestoneList is a list of milestones offering additional functionality
type MilestoneList [ ] * Milestone
func ( milestones MilestoneList ) getMilestoneIDs ( ) [ ] int64 {
2021-03-14 18:52:12 +00:00
ids := make ( [ ] int64 , 0 , len ( milestones ) )
2020-05-12 21:54:35 +00:00
for _ , ms := range milestones {
ids = append ( ids , ms . ID )
2020-03-31 07:47:00 +00:00
}
2020-05-12 21:54:35 +00:00
return ids
}
2019-12-15 14:20:08 +00:00
2020-07-28 11:30:40 +00:00
// GetMilestonesOption contain options to get milestones
type GetMilestonesOption struct {
2021-09-24 11:32:56 +00:00
db . ListOptions
2020-07-28 11:30:40 +00:00
RepoID int64
State api . StateType
Name string
SortType string
}
2021-08-12 12:43:08 +00:00
func ( opts GetMilestonesOption ) toCond ( ) builder . Cond {
cond := builder . NewCond ( )
if opts . RepoID != 0 {
cond = cond . And ( builder . Eq { "repo_id" : opts . RepoID } )
}
2020-05-12 21:54:35 +00:00
2020-07-28 11:30:40 +00:00
switch opts . State {
2020-05-12 21:54:35 +00:00
case api . StateClosed :
2021-08-12 12:43:08 +00:00
cond = cond . And ( builder . Eq { "is_closed" : true } )
2020-05-12 21:54:35 +00:00
case api . StateAll :
break
2020-07-28 11:30:40 +00:00
// api.StateOpen:
2020-05-12 21:54:35 +00:00
default :
2021-08-12 12:43:08 +00:00
cond = cond . And ( builder . Eq { "is_closed" : false } )
2019-12-15 14:20:08 +00:00
}
2020-07-28 11:30:40 +00:00
if len ( opts . Name ) != 0 {
2022-07-28 16:58:04 +00:00
cond = cond . And ( db . BuildCaseInsensitiveLike ( "name" , opts . Name ) )
2019-12-15 14:20:08 +00:00
}
2020-05-12 21:54:35 +00:00
2021-08-12 12:43:08 +00:00
return cond
}
// GetMilestones returns milestones filtered by GetMilestonesOption's
func GetMilestones ( opts GetMilestonesOption ) ( MilestoneList , int64 , error ) {
2021-09-23 15:45:36 +00:00
sess := db . GetEngine ( db . DefaultContext ) . Where ( opts . toCond ( ) )
2021-08-12 12:43:08 +00:00
2020-07-28 11:30:40 +00:00
if opts . Page != 0 {
2021-09-24 11:32:56 +00:00
sess = db . SetSessionPagination ( sess , & opts )
2020-05-12 21:54:35 +00:00
}
2020-07-28 11:30:40 +00:00
switch opts . SortType {
2020-05-12 21:54:35 +00:00
case "furthestduedate" :
sess . Desc ( "deadline_unix" )
case "leastcomplete" :
sess . Asc ( "completeness" )
case "mostcomplete" :
sess . Desc ( "completeness" )
case "leastissues" :
sess . Asc ( "num_issues" )
case "mostissues" :
sess . Desc ( "num_issues" )
2020-07-28 11:30:40 +00:00
case "id" :
sess . Asc ( "id" )
2020-05-12 21:54:35 +00:00
default :
2020-07-28 11:30:40 +00:00
sess . Asc ( "deadline_unix" ) . Asc ( "id" )
2020-05-12 21:54:35 +00:00
}
2020-07-28 11:30:40 +00:00
miles := make ( [ ] * Milestone , 0 , opts . PageSize )
2021-08-12 12:43:08 +00:00
total , err := sess . FindAndCount ( & miles )
return miles , total , err
2020-03-31 07:47:00 +00:00
}
Refactor and enhance issue indexer to support both searching, filtering and paging (#26012)
Fix #24662.
Replace #24822 and #25708 (although it has been merged)
## Background
In the past, Gitea supported issue searching with a keyword and
conditions in a less efficient way. It worked by searching for issues
with the keyword and obtaining limited IDs (as it is heavy to get all)
on the indexer (bleve/elasticsearch/meilisearch), and then querying with
conditions on the database to find a subset of the found IDs. This is
why the results could be incomplete.
To solve this issue, we need to store all fields that could be used as
conditions in the indexer and support both keyword and additional
conditions when searching with the indexer.
## Major changes
- Redefine `IndexerData` to include all fields that could be used as
filter conditions.
- Refactor `Search(ctx context.Context, kw string, repoIDs []int64,
limit, start int, state string)` to `Search(ctx context.Context, options
*SearchOptions)`, so it supports more conditions now.
- Change the data type stored in `issueIndexerQueue`. Use
`IndexerMetadata` instead of `IndexerData` in case the data has been
updated while it is in the queue. This also reduces the storage size of
the queue.
- Enhance searching with Bleve/Elasticsearch/Meilisearch, make them
fully support `SearchOptions`. Also, update the data versions.
- Keep most logic of database indexer, but remove
`issues.SearchIssueIDsByKeyword` in `models` to avoid confusion where is
the entry point to search issues.
- Start a Meilisearch instance to test it in unit tests.
- Add unit tests with almost full coverage to test
Bleve/Elasticsearch/Meilisearch indexer.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-07-31 06:28:53 +00:00
// GetMilestoneIDsByNames returns a list of milestone ids by given names.
// It doesn't filter them by repo, so it could return milestones belonging to different repos.
// It's used for filtering issues via indexer, otherwise it would be useless.
// Since it could return milestones with the same name, so the length of returned ids could be more than the length of names.
func GetMilestoneIDsByNames ( ctx context . Context , names [ ] string ) ( [ ] int64 , error ) {
var ids [ ] int64
return ids , db . GetEngine ( ctx ) . Table ( "milestone" ) .
Where ( db . BuildCaseInsensitiveIn ( "name" , names ) ) .
Cols ( "id" ) .
Find ( & ids )
}
2020-03-31 07:47:00 +00:00
// SearchMilestones search milestones
2021-12-20 04:41:31 +00:00
func SearchMilestones ( repoCond builder . Cond , page int , isClosed bool , sortType , keyword string ) ( MilestoneList , error ) {
2019-12-15 14:20:08 +00:00
miles := make ( [ ] * Milestone , 0 , setting . UI . IssuePagingNum )
2021-09-23 15:45:36 +00:00
sess := db . GetEngine ( db . DefaultContext ) . Where ( "is_closed = ?" , isClosed )
2021-04-08 11:53:59 +00:00
if len ( keyword ) > 0 {
sess = sess . And ( builder . Like { "UPPER(name)" , strings . ToUpper ( keyword ) } )
}
2020-03-31 07:47:00 +00:00
if repoCond . IsValid ( ) {
sess . In ( "repo_id" , builder . Select ( "id" ) . From ( "repository" ) . Where ( repoCond ) )
}
2019-12-15 14:20:08 +00:00
if page > 0 {
sess = sess . Limit ( setting . UI . IssuePagingNum , ( page - 1 ) * setting . UI . IssuePagingNum )
}
switch sortType {
case "furthestduedate" :
sess . Desc ( "deadline_unix" )
case "leastcomplete" :
sess . Asc ( "completeness" )
case "mostcomplete" :
sess . Desc ( "completeness" )
case "leastissues" :
sess . Asc ( "num_issues" )
case "mostissues" :
sess . Desc ( "num_issues" )
default :
sess . Asc ( "deadline_unix" )
}
return miles , sess . Find ( & miles )
}
2020-03-31 07:47:00 +00:00
// GetMilestonesByRepoIDs returns a list of milestones of given repositories and status.
func GetMilestonesByRepoIDs ( repoIDs [ ] int64 , page int , isClosed bool , sortType string ) ( MilestoneList , error ) {
return SearchMilestones (
builder . In ( "repo_id" , repoIDs ) ,
page ,
isClosed ,
sortType ,
2021-04-08 11:53:59 +00:00
"" ,
2020-03-31 07:47:00 +00:00
)
}
2019-12-15 14:20:08 +00:00
// MilestonesStats represents milestone statistic information.
type MilestonesStats struct {
OpenCount , ClosedCount int64
}
2020-03-31 07:47:00 +00:00
// Total returns the total counts of milestones
func ( m MilestonesStats ) Total ( ) int64 {
return m . OpenCount + m . ClosedCount
}
2020-05-12 21:54:35 +00:00
// GetMilestonesStatsByRepoCond returns milestone statistic information for dashboard by given conditions.
func GetMilestonesStatsByRepoCond ( repoCond builder . Cond ) ( * MilestonesStats , error ) {
2019-12-15 14:20:08 +00:00
var err error
stats := & MilestonesStats { }
2021-09-23 15:45:36 +00:00
sess := db . GetEngine ( db . DefaultContext ) . Where ( "is_closed = ?" , false )
2020-03-31 07:47:00 +00:00
if repoCond . IsValid ( ) {
sess . And ( builder . In ( "repo_id" , builder . Select ( "id" ) . From ( "repository" ) . Where ( repoCond ) ) )
}
stats . OpenCount , err = sess . Count ( new ( Milestone ) )
2019-12-15 14:20:08 +00:00
if err != nil {
return nil , err
}
2020-03-31 07:47:00 +00:00
2021-09-23 15:45:36 +00:00
sess = db . GetEngine ( db . DefaultContext ) . Where ( "is_closed = ?" , true )
2020-03-31 07:47:00 +00:00
if repoCond . IsValid ( ) {
sess . And ( builder . In ( "repo_id" , builder . Select ( "id" ) . From ( "repository" ) . Where ( repoCond ) ) )
}
stats . ClosedCount , err = sess . Count ( new ( Milestone ) )
2019-12-15 14:20:08 +00:00
if err != nil {
return nil , err
}
return stats , nil
}
2020-05-12 21:54:35 +00:00
2021-04-08 11:53:59 +00:00
// GetMilestonesStatsByRepoCondAndKw returns milestone statistic information for dashboard by given repo conditions and name keyword.
func GetMilestonesStatsByRepoCondAndKw ( repoCond builder . Cond , keyword string ) ( * MilestonesStats , error ) {
var err error
stats := & MilestonesStats { }
2021-09-23 15:45:36 +00:00
sess := db . GetEngine ( db . DefaultContext ) . Where ( "is_closed = ?" , false )
2021-04-08 11:53:59 +00:00
if len ( keyword ) > 0 {
sess = sess . And ( builder . Like { "UPPER(name)" , strings . ToUpper ( keyword ) } )
}
if repoCond . IsValid ( ) {
sess . And ( builder . In ( "repo_id" , builder . Select ( "id" ) . From ( "repository" ) . Where ( repoCond ) ) )
}
stats . OpenCount , err = sess . Count ( new ( Milestone ) )
if err != nil {
return nil , err
}
2021-09-23 15:45:36 +00:00
sess = db . GetEngine ( db . DefaultContext ) . Where ( "is_closed = ?" , true )
2021-04-08 11:53:59 +00:00
if len ( keyword ) > 0 {
sess = sess . And ( builder . Like { "UPPER(name)" , strings . ToUpper ( keyword ) } )
}
if repoCond . IsValid ( ) {
sess . And ( builder . In ( "repo_id" , builder . Select ( "id" ) . From ( "repository" ) . Where ( repoCond ) ) )
}
stats . ClosedCount , err = sess . Count ( new ( Milestone ) )
if err != nil {
return nil , err
}
return stats , nil
}
2022-06-13 09:37:59 +00:00
// CountMilestones returns number of milestones in given repository with other options
func CountMilestones ( ctx context . Context , opts GetMilestonesOption ) ( int64 , error ) {
2022-05-20 14:08:52 +00:00
return db . GetEngine ( ctx ) .
2022-06-13 09:37:59 +00:00
Where ( opts . toCond ( ) ) .
2020-05-12 21:54:35 +00:00
Count ( new ( Milestone ) )
}
// CountMilestonesByRepoCond map from repo conditions to number of milestones matching the options`
func CountMilestonesByRepoCond ( repoCond builder . Cond , isClosed bool ) ( map [ int64 ] int64 , error ) {
2021-09-23 15:45:36 +00:00
sess := db . GetEngine ( db . DefaultContext ) . Where ( "is_closed = ?" , isClosed )
2020-05-12 21:54:35 +00:00
if repoCond . IsValid ( ) {
sess . In ( "repo_id" , builder . Select ( "id" ) . From ( "repository" ) . Where ( repoCond ) )
}
countsSlice := make ( [ ] * struct {
RepoID int64
Count int64
} , 0 , 10 )
if err := sess . GroupBy ( "repo_id" ) .
Select ( "repo_id AS repo_id, COUNT(*) AS count" ) .
Table ( "milestone" ) .
Find ( & countsSlice ) ; err != nil {
return nil , err
}
countMap := make ( map [ int64 ] int64 , len ( countsSlice ) )
for _ , c := range countsSlice {
countMap [ c . RepoID ] = c . Count
}
return countMap , nil
}
2021-04-08 11:53:59 +00:00
// CountMilestonesByRepoCondAndKw map from repo conditions and the keyword of milestones' name to number of milestones matching the options`
func CountMilestonesByRepoCondAndKw ( repoCond builder . Cond , keyword string , isClosed bool ) ( map [ int64 ] int64 , error ) {
2021-09-23 15:45:36 +00:00
sess := db . GetEngine ( db . DefaultContext ) . Where ( "is_closed = ?" , isClosed )
2021-04-08 11:53:59 +00:00
if len ( keyword ) > 0 {
sess = sess . And ( builder . Like { "UPPER(name)" , strings . ToUpper ( keyword ) } )
}
if repoCond . IsValid ( ) {
sess . In ( "repo_id" , builder . Select ( "id" ) . From ( "repository" ) . Where ( repoCond ) )
}
countsSlice := make ( [ ] * struct {
RepoID int64
Count int64
} , 0 , 10 )
if err := sess . GroupBy ( "repo_id" ) .
Select ( "repo_id AS repo_id, COUNT(*) AS count" ) .
Table ( "milestone" ) .
Find ( & countsSlice ) ; err != nil {
return nil , err
}
countMap := make ( map [ int64 ] int64 , len ( countsSlice ) )
for _ , c := range countsSlice {
countMap [ c . RepoID ] = c . Count
}
return countMap , nil
}
2022-01-17 18:31:58 +00:00
func updateRepoMilestoneNum ( ctx context . Context , repoID int64 ) error {
_ , err := db . GetEngine ( ctx ) . Exec ( "UPDATE `repository` SET num_milestones=(SELECT count(*) FROM milestone WHERE repo_id=?),num_closed_milestones=(SELECT count(*) FROM milestone WHERE repo_id=? AND is_closed=?) WHERE id=?" ,
2020-05-12 21:54:35 +00:00
repoID ,
repoID ,
true ,
repoID ,
)
return err
}
// _____ _ _ _____ _
// |_ _| __ __ _ ___| | _____ __| |_ _(_)_ __ ___ ___ ___
// | || '__/ _` |/ __| |/ / _ \/ _` | | | | | '_ ` _ \ / _ \/ __|
// | || | | (_| | (__| < __/ (_| | | | | | | | | | | __/\__ \
// |_||_| \__,_|\___|_|\_\___|\__,_| |_| |_|_| |_| |_|\___||___/
//
2022-05-20 14:08:52 +00:00
func ( milestones MilestoneList ) loadTotalTrackedTimes ( ctx context . Context ) error {
2020-05-12 21:54:35 +00:00
type totalTimesByMilestone struct {
MilestoneID int64
Time int64
}
if len ( milestones ) == 0 {
return nil
}
2021-03-14 18:52:12 +00:00
trackedTimes := make ( map [ int64 ] int64 , len ( milestones ) )
2020-05-12 21:54:35 +00:00
// Get total tracked time by milestone_id
2022-05-20 14:08:52 +00:00
rows , err := db . GetEngine ( ctx ) . Table ( "issue" ) .
2020-05-12 21:54:35 +00:00
Join ( "INNER" , "milestone" , "issue.milestone_id = milestone.id" ) .
Join ( "LEFT" , "tracked_time" , "tracked_time.issue_id = issue.id" ) .
Where ( "tracked_time.deleted = ?" , false ) .
Select ( "milestone_id, sum(time) as time" ) .
In ( "milestone_id" , milestones . getMilestoneIDs ( ) ) .
GroupBy ( "milestone_id" ) .
Rows ( new ( totalTimesByMilestone ) )
if err != nil {
return err
}
defer rows . Close ( )
for rows . Next ( ) {
var totalTime totalTimesByMilestone
err = rows . Scan ( & totalTime )
if err != nil {
return err
}
trackedTimes [ totalTime . MilestoneID ] = totalTime . Time
}
for _ , milestone := range milestones {
milestone . TotalTrackedTime = trackedTimes [ milestone . ID ]
}
return nil
}
2022-05-20 14:08:52 +00:00
func ( m * Milestone ) loadTotalTrackedTime ( ctx context . Context ) error {
2020-05-12 21:54:35 +00:00
type totalTimesByMilestone struct {
MilestoneID int64
Time int64
}
totalTime := & totalTimesByMilestone { MilestoneID : m . ID }
2022-05-20 14:08:52 +00:00
has , err := db . GetEngine ( ctx ) . Table ( "issue" ) .
2020-05-12 21:54:35 +00:00
Join ( "INNER" , "milestone" , "issue.milestone_id = milestone.id" ) .
Join ( "LEFT" , "tracked_time" , "tracked_time.issue_id = issue.id" ) .
Where ( "tracked_time.deleted = ?" , false ) .
Select ( "milestone_id, sum(time) as time" ) .
Where ( "milestone_id = ?" , m . ID ) .
GroupBy ( "milestone_id" ) .
Get ( totalTime )
if err != nil {
return err
} else if ! has {
return nil
}
m . TotalTrackedTime = totalTime . Time
return nil
}
// LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
func ( milestones MilestoneList ) LoadTotalTrackedTimes ( ) error {
2022-05-20 14:08:52 +00:00
return milestones . loadTotalTrackedTimes ( db . DefaultContext )
2020-05-12 21:54:35 +00:00
}
// LoadTotalTrackedTime loads the tracked time for the milestone
func ( m * Milestone ) LoadTotalTrackedTime ( ) error {
2022-05-20 14:08:52 +00:00
return m . loadTotalTrackedTime ( db . DefaultContext )
2020-05-12 21:54:35 +00:00
}