2017-02-09 12:09:26 +05:30
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
2018-05-16 19:31:55 +05:30
"fmt"
2017-02-09 12:09:26 +05:30
"code.gitea.io/gitea/modules/setting"
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"
2019-10-08 03:14:58 +05:30
"xorm.io/builder"
2019-08-15 20:16:21 +05:30
2019-10-17 14:56:49 +05:30
"xorm.io/xorm"
2017-02-09 12:09:26 +05:30
)
// Milestone represents a milestone of repository.
type Milestone struct {
ID int64 ` xorm:"pk autoincr" `
RepoID int64 ` xorm:"INDEX" `
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
2017-12-11 10:07:04 +05:30
DeadlineString string ` xorm:"-" `
2019-08-15 20:16:21 +05:30
DeadlineUnix timeutil . TimeStamp
ClosedDateUnix timeutil . TimeStamp
2018-04-29 11:28:47 +05:30
TotalTrackedTime int64 ` xorm:"-" `
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
2017-12-11 10:07:04 +05:30
m . DeadlineString = m . DeadlineUnix . Format ( "2006-01-02" )
2019-08-15 20:16:21 +05:30
if timeutil . TimeStampNow ( ) >= m . DeadlineUnix {
2018-05-07 15:20:27 +05:30
m . IsOverdue = true
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
}
// APIFormat returns this Milestone in API format.
func ( m * Milestone ) APIFormat ( ) * api . Milestone {
apiMilestone := & api . Milestone {
ID : m . ID ,
State : m . State ( ) ,
Title : m . Name ,
Description : m . Content ,
OpenIssues : m . NumOpenIssues ,
ClosedIssues : m . NumClosedIssues ,
}
if m . IsClosed {
2017-12-11 10:07:04 +05:30
apiMilestone . Closed = m . ClosedDateUnix . AsTimePtr ( )
2017-02-09 12:09:26 +05:30
}
2017-12-11 10:07:04 +05:30
if m . DeadlineUnix . Year ( ) < 9999 {
apiMilestone . Deadline = m . DeadlineUnix . AsTimePtr ( )
2017-02-09 12:09:26 +05:30
}
return apiMilestone
}
// NewMilestone creates new milestone of repository.
func NewMilestone ( m * Milestone ) ( err error ) {
sess := x . NewSession ( )
2017-06-21 06:27:05 +05:30
defer sess . Close ( )
2017-02-09 12:09:26 +05:30
if err = sess . Begin ( ) ; err != nil {
return err
}
if _ , err = sess . Insert ( m ) ; err != nil {
return err
}
if _ , err = sess . Exec ( "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?" , m . RepoID ) ; err != nil {
return err
}
return sess . Commit ( )
}
func getMilestoneByRepoID ( e Engine , repoID , id int64 ) ( * Milestone , error ) {
m := & Milestone {
ID : id ,
RepoID : repoID ,
}
has , err := e . Get ( m )
if err != nil {
return nil , err
} else if ! has {
return nil , ErrMilestoneNotExist { id , repoID }
}
return m , nil
}
// GetMilestoneByRepoID returns the milestone in a repository.
func GetMilestoneByRepoID ( repoID , id int64 ) ( * Milestone , error ) {
return getMilestoneByRepoID ( x , repoID , id )
}
2018-11-29 07:16:30 +05:30
// GetMilestoneByID returns the milestone via id .
func GetMilestoneByID ( id int64 ) ( * Milestone , error ) {
var m Milestone
has , err := x . ID ( id ) . Get ( & m )
if err != nil {
return nil , err
} else if ! has {
return nil , ErrMilestoneNotExist { id , 0 }
}
return & m , nil
}
2018-04-29 11:28:47 +05:30
// MilestoneList is a list of milestones offering additional functionality
type MilestoneList [ ] * Milestone
func ( milestones MilestoneList ) loadTotalTrackedTimes ( e Engine ) error {
type totalTimesByMilestone struct {
MilestoneID int64
Time int64
}
if len ( milestones ) == 0 {
return nil
}
var trackedTimes = make ( map [ int64 ] int64 , len ( milestones ) )
// Get total tracked time by milestone_id
rows , err := e . Table ( "issue" ) .
Join ( "INNER" , "milestone" , "issue.milestone_id = milestone.id" ) .
Join ( "LEFT" , "tracked_time" , "tracked_time.issue_id = issue.id" ) .
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
}
// LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
func ( milestones MilestoneList ) LoadTotalTrackedTimes ( ) error {
return milestones . loadTotalTrackedTimes ( x )
}
func ( milestones MilestoneList ) getMilestoneIDs ( ) [ ] int64 {
var ids = make ( [ ] int64 , 0 , len ( milestones ) )
for _ , ms := range milestones {
ids = append ( ids , ms . ID )
}
return ids
}
2018-11-26 14:15:42 +05:30
// GetMilestonesByRepoID returns all opened milestones of a repository.
2019-06-06 06:07:45 +05:30
func GetMilestonesByRepoID ( repoID int64 , state api . StateType ) ( MilestoneList , error ) {
sess := x . Where ( "repo_id = ?" , repoID )
switch state {
case api . StateClosed :
sess = sess . And ( "is_closed = ?" , true )
case api . StateAll :
break
case api . StateOpen :
fallthrough
default :
sess = sess . And ( "is_closed = ?" , false )
}
2017-02-09 12:09:26 +05:30
miles := make ( [ ] * Milestone , 0 , 10 )
2019-06-06 06:07:45 +05:30
return miles , sess . Asc ( "deadline_unix" ) . Asc ( "id" ) . Find ( & miles )
2017-02-09 12:09:26 +05:30
}
// GetMilestones returns a list of milestones of given repository and status.
2018-04-29 11:28:47 +05:30
func GetMilestones ( repoID int64 , page int , isClosed bool , sortType string ) ( MilestoneList , error ) {
2017-02-09 12:09:26 +05:30
miles := make ( [ ] * Milestone , 0 , setting . UI . IssuePagingNum )
sess := x . Where ( "repo_id = ? AND is_closed = ?" , repoID , isClosed )
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 )
}
func updateMilestone ( e Engine , m * Milestone ) error {
2019-10-08 03:14:58 +05:30
_ , err := e . ID ( m . ID ) . AllCols ( ) .
SetExpr ( "num_issues" , builder . Select ( "count(*)" ) . From ( "issue" ) . Where (
builder . Eq { "milestone_id" : m . ID } ,
) ) .
SetExpr ( "num_closed_issues" , builder . Select ( "count(*)" ) . From ( "issue" ) . Where (
builder . Eq {
"milestone_id" : m . ID ,
"is_closed" : true ,
} ,
) ) .
Update ( m )
2017-02-09 12:09:26 +05:30
return err
}
// UpdateMilestone updates information of given milestone.
func UpdateMilestone ( m * Milestone ) error {
2019-10-08 03:14:58 +05:30
if err := updateMilestone ( x , m ) ; err != nil {
return err
}
return updateMilestoneCompleteness ( x , m . ID )
}
func updateMilestoneCompleteness ( e Engine , milestoneID int64 ) error {
_ , err := e . Exec ( "UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?" ,
milestoneID ,
)
return err
2017-02-09 12:09:26 +05:30
}
2017-12-18 19:36:51 +05:30
func countRepoMilestones ( e Engine , repoID int64 ) ( int64 , error ) {
return e .
2017-02-09 12:09:26 +05:30
Where ( "repo_id=?" , repoID ) .
Count ( new ( Milestone ) )
}
2017-12-18 19:36:51 +05:30
func countRepoClosedMilestones ( e Engine , repoID int64 ) ( int64 , error ) {
return e .
2017-02-09 12:09:26 +05:30
Where ( "repo_id=? AND is_closed=?" , repoID , true ) .
Count ( new ( Milestone ) )
}
// CountRepoClosedMilestones returns number of closed milestones in given repository.
2017-12-18 19:36:51 +05:30
func CountRepoClosedMilestones ( repoID int64 ) ( int64 , error ) {
2017-02-09 12:09:26 +05:30
return countRepoClosedMilestones ( x , repoID )
}
// MilestoneStats returns number of open and closed milestones of given repository.
2017-12-18 19:36:51 +05:30
func MilestoneStats ( repoID int64 ) ( open int64 , closed int64 , err error ) {
open , err = x .
2017-02-09 12:09:26 +05:30
Where ( "repo_id=? AND is_closed=?" , repoID , false ) .
Count ( new ( Milestone ) )
2017-12-18 19:36:51 +05:30
if err != nil {
return 0 , 0 , nil
}
closed , err = CountRepoClosedMilestones ( repoID )
return open , closed , err
2017-02-09 12:09:26 +05:30
}
// ChangeMilestoneStatus changes the milestone open/closed status.
func ChangeMilestoneStatus ( m * Milestone , isClosed bool ) ( err error ) {
sess := x . NewSession ( )
2017-06-21 06:27:05 +05:30
defer sess . Close ( )
2017-02-09 12:09:26 +05:30
if err = sess . Begin ( ) ; err != nil {
return err
}
m . IsClosed = isClosed
2019-10-29 08:05:50 +05:30
if isClosed {
m . ClosedDateUnix = timeutil . TimeStampNow ( )
}
if _ , err := sess . ID ( m . ID ) . Cols ( "is_closed" , "closed_date_unix" ) . Update ( m ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2019-10-08 03:14:58 +05:30
if err := updateRepoMilestoneNum ( sess , m . RepoID ) ; err != nil {
2017-12-18 19:36:51 +05:30
return err
}
2017-02-09 12:09:26 +05:30
return sess . Commit ( )
}
2019-10-08 03:14:58 +05:30
func updateRepoMilestoneNum ( e Engine , repoID int64 ) error {
_ , err := e . 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=?" ,
repoID ,
repoID ,
true ,
repoID ,
)
return err
}
2019-10-07 02:56:19 +05:30
func updateMilestoneTotalNum ( e Engine , milestoneID int64 ) ( err error ) {
if _ , err = e . Exec ( "UPDATE `milestone` SET num_issues=(SELECT count(*) FROM issue WHERE milestone_id=?) WHERE id=?" ,
milestoneID ,
milestoneID ,
) ; err != nil {
return
2017-02-09 12:09:26 +05:30
}
2019-10-08 03:14:58 +05:30
return updateMilestoneCompleteness ( e , milestoneID )
2019-10-07 02:56:19 +05:30
}
func updateMilestoneClosedNum ( e Engine , milestoneID int64 ) ( err error ) {
if _ , err = e . Exec ( "UPDATE `milestone` SET num_closed_issues=(SELECT count(*) FROM issue WHERE milestone_id=? AND is_closed=?) WHERE id=?" ,
milestoneID ,
true ,
milestoneID ,
) ; err != nil {
return
2017-02-09 12:09:26 +05:30
}
2019-10-08 03:14:58 +05:30
return updateMilestoneCompleteness ( e , milestoneID )
2017-02-09 12:09:26 +05:30
}
func changeMilestoneAssign ( e * xorm . Session , doer * User , issue * Issue , oldMilestoneID int64 ) error {
2019-10-07 02:56:19 +05:30
if err := updateIssueCols ( e , issue , "milestone_id" ) ; err != nil {
return err
}
2017-02-09 12:09:26 +05:30
if oldMilestoneID > 0 {
2019-10-07 02:56:19 +05:30
if err := updateMilestoneTotalNum ( e , oldMilestoneID ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
if issue . IsClosed {
2019-10-07 02:56:19 +05:30
if err := updateMilestoneClosedNum ( e , oldMilestoneID ) ; err != nil {
return err
}
2017-02-09 12:09:26 +05:30
}
}
if issue . MilestoneID > 0 {
2019-10-07 02:56:19 +05:30
if err := updateMilestoneTotalNum ( e , issue . MilestoneID ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
if issue . IsClosed {
2019-10-07 02:56:19 +05:30
if err := updateMilestoneClosedNum ( e , issue . MilestoneID ) ; err != nil {
return err
}
2017-02-09 12:09:26 +05:30
}
2019-10-07 02:56:19 +05:30
}
2017-02-09 12:09:26 +05:30
2019-10-07 02:56:19 +05:30
if oldMilestoneID > 0 || issue . MilestoneID > 0 {
if err := issue . loadRepo ( e ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2019-12-01 08:14:39 +05:30
var opts = & CreateCommentOptions {
Type : CommentTypeMilestone ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
OldMilestoneID : oldMilestoneID ,
MilestoneID : issue . MilestoneID ,
}
2019-12-02 20:13:39 +05:30
if _ , err := createCommentWithNoAction ( e , opts ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
}
2019-10-07 02:56:19 +05:30
return nil
2017-02-09 12:09:26 +05:30
}
// ChangeMilestoneAssign changes assignment of milestone for issue.
func ChangeMilestoneAssign ( issue * Issue , doer * User , oldMilestoneID int64 ) ( err error ) {
sess := x . NewSession ( )
defer sess . Close ( )
if err = sess . Begin ( ) ; err != nil {
return err
}
if err = changeMilestoneAssign ( sess , doer , issue , oldMilestoneID ) ; err != nil {
return err
}
2018-05-16 19:31:55 +05:30
if err = sess . Commit ( ) ; err != nil {
return fmt . Errorf ( "Commit: %v" , err )
}
return nil
2017-02-09 12:09:26 +05:30
}
// DeleteMilestoneByRepoID deletes a milestone from a repository.
func DeleteMilestoneByRepoID ( repoID , id int64 ) error {
m , err := GetMilestoneByRepoID ( repoID , id )
if err != nil {
if IsErrMilestoneNotExist ( err ) {
return nil
}
return err
}
repo , err := GetRepositoryByID ( m . RepoID )
if err != nil {
return err
}
sess := x . NewSession ( )
2017-06-21 06:27:05 +05:30
defer sess . Close ( )
2017-02-09 12:09:26 +05:30
if err = sess . Begin ( ) ; err != nil {
return err
}
2017-10-05 10:13:04 +05:30
if _ , err = sess . ID ( m . ID ) . Delete ( new ( Milestone ) ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
2017-12-18 19:36:51 +05:30
numMilestones , err := countRepoMilestones ( sess , repo . ID )
if err != nil {
return err
}
numClosedMilestones , err := countRepoClosedMilestones ( sess , repo . ID )
if err != nil {
return err
}
repo . NumMilestones = int ( numMilestones )
repo . NumClosedMilestones = int ( numClosedMilestones )
2017-10-05 10:13:04 +05:30
if _ , err = sess . ID ( repo . ID ) . Cols ( "num_milestones, num_closed_milestones" ) . Update ( repo ) ; err != nil {
2017-02-09 12:09:26 +05:30
return err
}
if _ , err = sess . Exec ( "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?" , m . ID ) ; err != nil {
return err
}
return sess . Commit ( )
}