2017-02-09 12:09:26 +05:30
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 23:50:29 +05:30
// SPDX-License-Identifier: MIT
2017-02-09 12:09:26 +05:30
2022-04-08 14:41:15 +05:30
package issues
2017-02-09 12:09:26 +05:30
import (
2021-11-19 19:09:57 +05:30
"context"
2018-05-16 19:31:55 +05:30
"fmt"
2020-01-27 21:53:21 +05:30
"strings"
2018-05-16 19:31:55 +05:30
2021-09-19 17:19:59 +05:30
"code.gitea.io/gitea/models/db"
2021-12-10 06:57:50 +05:30
repo_model "code.gitea.io/gitea/models/repo"
2019-05-11 15:51:34 +05:30
api "code.gitea.io/gitea/modules/structs"
2019-08-15 20:16:21 +05:30
"code.gitea.io/gitea/modules/timeutil"
2022-10-18 11:20:37 +05:30
"code.gitea.io/gitea/modules/util"
2019-08-15 20:16:21 +05:30
2019-12-28 02:00:58 +05:30
"xorm.io/builder"
2017-02-09 12:09:26 +05:30
)
2022-04-08 14:41:15 +05:30
// 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 11:20:37 +05:30
func ( err ErrMilestoneNotExist ) Unwrap ( ) error {
return util . ErrNotExist
}
2017-02-09 12:09:26 +05:30
// Milestone represents a milestone of repository.
type Milestone struct {
2021-12-10 06:57:50 +05:30
ID int64 ` xorm:"pk autoincr" `
RepoID int64 ` xorm:"INDEX" `
Repo * repo_model . Repository ` xorm:"-" `
2017-02-09 12:09:26 +05:30
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 15:20:27 +05:30
IsOverdue bool ` xorm:"-" `
2017-02-09 12:09:26 +05:30
2020-09-05 23:08:54 +05:30
CreatedUnix timeutil . TimeStamp ` xorm:"INDEX created" `
UpdatedUnix timeutil . TimeStamp ` xorm:"INDEX updated" `
2019-08-15 20:16:21 +05:30
DeadlineUnix timeutil . TimeStamp
ClosedDateUnix timeutil . TimeStamp
2020-09-05 23:08:54 +05:30
DeadlineString string ` xorm:"-" `
2018-04-29 11:28:47 +05:30
TotalTrackedTime int64 ` xorm:"-" `
2017-02-09 12:09:26 +05:30
}
2021-09-19 17:19:59 +05:30
func init ( ) {
db . RegisterModel ( new ( Milestone ) )
}
2017-02-09 12:09:26 +05:30
// 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 22:22:35 +05:30
// AfterLoad is invoked from XORM after setting the value of a field of
2017-02-09 12:09:26 +05:30
// this object.
2017-10-01 22:22:35 +05:30
func ( m * Milestone ) AfterLoad ( ) {
m . NumOpenIssues = m . NumIssues - m . NumClosedIssues
2017-12-11 10:07:04 +05:30
if m . DeadlineUnix . Year ( ) == 9999 {
2017-10-01 22:22:35 +05:30
return
}
2017-02-09 12:09:26 +05:30
2023-12-28 15:39:57 +05:30
m . DeadlineString = m . DeadlineUnix . FormatDate ( )
2021-03-08 07:25:57 +05:30
if m . IsClosed {
m . IsOverdue = m . ClosedDateUnix >= m . DeadlineUnix
} else {
m . IsOverdue = timeutil . TimeStampNow ( ) >= m . DeadlineUnix
2017-02-09 12:09:26 +05:30
}
}
// 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.
2023-09-16 20:09:12 +05:30
func NewMilestone ( ctx context . Context , m * Milestone ) ( err error ) {
ctx , committer , err := db . TxContext ( ctx )
2021-11-21 21:11:00 +05:30
if err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2017-02-09 12:09:26 +05:30
2020-01-27 21:53:21 +05:30
m . Name = strings . TrimSpace ( m . Name )
2021-11-21 21:11:00 +05:30
if err = db . Insert ( ctx , m ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2021-11-21 21:11:00 +05:30
if _ , err = db . Exec ( ctx , "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?" , m . RepoID ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2017-02-09 12:09:26 +05:30
}
2022-06-30 21:25:08 +05:30
// 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 14:41:15 +05:30
// GetMilestoneByRepoID returns the milestone in a repository.
func GetMilestoneByRepoID ( ctx context . Context , repoID , id int64 ) ( * Milestone , error ) {
2020-04-30 09:45:39 +05:30
m := new ( Milestone )
2022-04-08 14:41:15 +05:30
has , err := db . GetEngine ( ctx ) . ID ( id ) . Where ( "repo_id=?" , repoID ) . Get ( m )
2017-02-09 12:09:26 +05:30
if err != nil {
return nil , err
} else if ! has {
2020-04-30 09:45:39 +05:30
return nil , ErrMilestoneNotExist { ID : id , RepoID : repoID }
2017-02-09 12:09:26 +05:30
}
return m , nil
}
2020-04-30 09:45:39 +05:30
// GetMilestoneByRepoIDANDName return a milestone if one exist by name and repo
2023-09-16 20:09:12 +05:30
func GetMilestoneByRepoIDANDName ( ctx context . Context , repoID int64 , name string ) ( * Milestone , error ) {
2020-04-30 09:45:39 +05:30
var mile Milestone
2023-09-16 20:09:12 +05:30
has , err := db . GetEngine ( ctx ) . Where ( "repo_id=? AND name=?" , repoID , name ) . Get ( & mile )
2020-04-30 09:45:39 +05:30
if err != nil {
return nil , err
}
if ! has {
return nil , ErrMilestoneNotExist { Name : name , RepoID : repoID }
}
return & mile , nil
}
2017-02-09 12:09:26 +05:30
// UpdateMilestone updates information of given milestone.
2023-09-16 20:09:12 +05:30
func UpdateMilestone ( ctx context . Context , m * Milestone , oldIsClosed bool ) error {
ctx , committer , err := db . TxContext ( ctx )
2021-11-21 21:11:00 +05:30
if err != nil {
2020-01-29 12:06:32 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2020-01-29 12:06:32 +05:30
if m . IsClosed && ! oldIsClosed {
m . ClosedDateUnix = timeutil . TimeStampNow ( )
}
2022-01-18 00:01:58 +05:30
if err := updateMilestone ( ctx , m ) ; err != nil {
2019-10-08 03:14:58 +05:30
return err
}
2020-01-29 12:06:32 +05:30
// if IsClosed changed, update milestone numbers of repository
if oldIsClosed != m . IsClosed {
2022-01-18 00:01:58 +05:30
if err := updateRepoMilestoneNum ( ctx , m . RepoID ) ; err != nil {
2020-01-29 12:06:32 +05:30
return err
}
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2019-10-08 03:14:58 +05:30
}
2022-01-18 00:01:58 +05:30
func updateMilestone ( ctx context . Context , m * Milestone ) error {
2020-05-13 03:24:35 +05:30
m . Name = strings . TrimSpace ( m . Name )
2022-01-18 00:01:58 +05:30
_ , err := db . GetEngine ( ctx ) . ID ( m . ID ) . AllCols ( ) . Update ( m )
2021-06-22 00:04:58 +05:30
if err != nil {
return err
}
2022-04-08 14:41:15 +05:30
return UpdateMilestoneCounters ( ctx , m . ID )
2021-06-22 00:04:58 +05:30
}
2022-04-08 14:41:15 +05:30
// UpdateMilestoneCounters calculates NumIssues, NumClosesIssues and Completeness
func UpdateMilestoneCounters ( ctx context . Context , id int64 ) error {
2022-01-18 00:01:58 +05:30
e := db . GetEngine ( ctx )
2021-06-22 00:04:58 +05:30
_ , err := e . ID ( id ) .
2020-05-13 03:24:35 +05:30
SetExpr ( "num_issues" , builder . Select ( "count(*)" ) . From ( "issue" ) . Where (
2021-06-22 00:04:58 +05:30
builder . Eq { "milestone_id" : id } ,
2020-05-13 03:24:35 +05:30
) ) .
SetExpr ( "num_closed_issues" , builder . Select ( "count(*)" ) . From ( "issue" ) . Where (
builder . Eq {
2021-06-22 00:04:58 +05:30
"milestone_id" : id ,
2020-05-13 03:24:35 +05:30
"is_closed" : true ,
} ,
) ) .
2021-06-22 00:04:58 +05:30
Update ( & Milestone { } )
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-08 03:14:58 +05:30
)
return err
2017-02-09 12:09:26 +05:30
}
2020-08-17 08:37:38 +05:30
// ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo.
2023-09-16 20:09:12 +05:30
func ChangeMilestoneStatusByRepoIDAndID ( ctx context . Context , repoID , milestoneID int64 , isClosed bool ) error {
ctx , committer , err := db . TxContext ( ctx )
2021-11-21 21:11:00 +05:30
if err != nil {
2020-08-17 08:37:38 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2020-08-17 08:37:38 +05:30
m := & Milestone {
ID : milestoneID ,
RepoID : repoID ,
}
2022-01-18 00:01:58 +05:30
has , err := db . GetEngine ( ctx ) . ID ( milestoneID ) . Where ( "repo_id = ?" , repoID ) . Get ( m )
2020-08-17 08:37:38 +05:30
if err != nil {
return err
} else if ! has {
return ErrMilestoneNotExist { ID : milestoneID , RepoID : repoID }
}
2022-01-18 00:01:58 +05:30
if err := changeMilestoneStatus ( ctx , m , isClosed ) ; err != nil {
2020-08-17 08:37:38 +05:30
return err
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2020-08-17 08:37:38 +05:30
}
2017-02-09 12:09:26 +05:30
// ChangeMilestoneStatus changes the milestone open/closed status.
2023-09-16 20:09:12 +05:30
func ChangeMilestoneStatus ( ctx context . Context , m * Milestone , isClosed bool ) ( err error ) {
ctx , committer , err := db . TxContext ( ctx )
2021-11-21 21:11:00 +05:30
if err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2017-02-09 12:09:26 +05:30
2022-01-18 00:01:58 +05:30
if err := changeMilestoneStatus ( ctx , m , isClosed ) ; err != nil {
2020-08-17 08:37:38 +05:30
return err
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2020-08-17 08:37:38 +05:30
}
2022-01-18 00:01:58 +05:30
func changeMilestoneStatus ( ctx context . Context , m * Milestone , isClosed bool ) error {
2017-02-09 12:09:26 +05:30
m . IsClosed = isClosed
2019-10-29 08:05:50 +05:30
if isClosed {
m . ClosedDateUnix = timeutil . TimeStampNow ( )
}
2022-01-18 00:01:58 +05:30
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 08:37:38 +05:30
if err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2020-08-17 08:37:38 +05:30
if count < 1 {
return nil
2017-12-18 19:36:51 +05:30
}
2022-01-18 00:01:58 +05:30
return updateRepoMilestoneNum ( ctx , m . RepoID )
2017-02-09 12:09:26 +05:30
}
// DeleteMilestoneByRepoID deletes a milestone from a repository.
2023-09-16 20:09:12 +05:30
func DeleteMilestoneByRepoID ( ctx context . Context , repoID , id int64 ) error {
m , err := GetMilestoneByRepoID ( ctx , repoID , id )
2017-02-09 12:09:26 +05:30
if err != nil {
if IsErrMilestoneNotExist ( err ) {
return nil
}
return err
}
2023-09-16 20:09:12 +05:30
repo , err := repo_model . GetRepositoryByID ( ctx , m . RepoID )
2017-02-09 12:09:26 +05:30
if err != nil {
return err
}
2023-09-16 20:09:12 +05:30
ctx , committer , err := db . TxContext ( ctx )
2021-11-21 21:11:00 +05:30
if err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2023-12-26 01:55:29 +05:30
if _ , err = db . DeleteByID [ Milestone ] ( ctx , m . ID ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2023-12-11 14:26:48 +05:30
numMilestones , err := db . Count [ Milestone ] ( ctx , FindMilestoneOptions {
2022-06-13 15:07:59 +05:30
RepoID : repo . ID ,
} )
2017-12-18 19:36:51 +05:30
if err != nil {
return err
}
2023-12-11 14:26:48 +05:30
numClosedMilestones , err := db . Count [ Milestone ] ( ctx , FindMilestoneOptions {
RepoID : repo . ID ,
IsClosed : util . OptionalBoolTrue ,
2022-06-13 15:07:59 +05:30
} )
2017-12-18 19:36:51 +05:30
if err != nil {
return err
}
repo . NumMilestones = int ( numMilestones )
repo . NumClosedMilestones = int ( numClosedMilestones )
2023-12-26 01:55:29 +05:30
if _ , err = db . GetEngine ( ctx ) . ID ( repo . ID ) . Cols ( "num_milestones, num_closed_milestones" ) . Update ( repo ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2021-11-21 21:11:00 +05:30
if _ , err = db . Exec ( ctx , "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?" , m . ID ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2017-02-09 12:09:26 +05:30
}
2019-12-15 19:50:08 +05:30
2022-01-18 00:01:58 +05:30
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-13 03:24:35 +05:30
repoID ,
repoID ,
true ,
repoID ,
)
return err
}
2023-09-16 20:09:12 +05:30
// LoadTotalTrackedTime loads the tracked time for the milestone
func ( m * Milestone ) LoadTotalTrackedTime ( ctx context . Context ) error {
2020-05-13 03:24:35 +05:30
type totalTimesByMilestone struct {
MilestoneID int64
Time int64
}
totalTime := & totalTimesByMilestone { MilestoneID : m . ID }
2022-05-20 19:38:52 +05:30
has , err := db . GetEngine ( ctx ) . Table ( "issue" ) .
2020-05-13 03:24:35 +05:30
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
}
2023-09-09 02:39:23 +05:30
// InsertMilestones creates milestones of repository.
2023-09-16 20:09:12 +05:30
func InsertMilestones ( ctx context . Context , ms ... * Milestone ) ( err error ) {
2023-09-09 02:39:23 +05:30
if len ( ms ) == 0 {
return nil
}
2023-09-16 20:09:12 +05:30
ctx , committer , err := db . TxContext ( ctx )
2023-09-09 02:39:23 +05:30
if err != nil {
return err
}
defer committer . Close ( )
sess := db . GetEngine ( ctx )
// to return the id, so we should not use batch insert
for _ , m := range ms {
if _ , err = sess . NoAutoTime ( ) . Insert ( m ) ; err != nil {
return err
}
}
if _ , err = db . Exec ( ctx , "UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?" , len ( ms ) , ms [ 0 ] . RepoID ) ; err != nil {
return err
}
return committer . Commit ( )
}