@@ -10,6 +10,7 @@ import (
1010 "code.gitea.io/gitea/modules/setting"
1111 api "code.gitea.io/gitea/modules/structs"
1212 "code.gitea.io/gitea/modules/timeutil"
13+ "xorm.io/builder"
1314
1415 "github.com/go-xorm/xorm"
1516)
@@ -191,7 +192,6 @@ func (milestones MilestoneList) getMilestoneIDs() []int64 {
191192
192193// GetMilestonesByRepoID returns all opened milestones of a repository.
193194func GetMilestonesByRepoID (repoID int64 , state api.StateType ) (MilestoneList , error ) {
194-
195195 sess := x .Where ("repo_id = ?" , repoID )
196196
197197 switch state {
@@ -238,13 +238,34 @@ func GetMilestones(repoID int64, page int, isClosed bool, sortType string) (Mile
238238}
239239
240240func updateMilestone (e Engine , m * Milestone ) error {
241- _ , err := e .ID (m .ID ).AllCols ().Update (m )
241+ _ , err := e .ID (m .ID ).AllCols ().
242+ SetExpr ("num_issues" , builder .Select ("count(*)" ).From ("issue" ).Where (
243+ builder.Eq {"milestone_id" : m .ID },
244+ )).
245+ SetExpr ("num_closed_issues" , builder .Select ("count(*)" ).From ("issue" ).Where (
246+ builder.Eq {
247+ "milestone_id" : m .ID ,
248+ "is_closed" : true ,
249+ },
250+ )).
251+ Update (m )
242252 return err
243253}
244254
245255// UpdateMilestone updates information of given milestone.
246256func UpdateMilestone (m * Milestone ) error {
247- return updateMilestone (x , m )
257+ if err := updateMilestone (x , m ); err != nil {
258+ return err
259+ }
260+
261+ return updateMilestoneCompleteness (x , m .ID )
262+ }
263+
264+ func updateMilestoneCompleteness (e Engine , milestoneID int64 ) error {
265+ _ , err := e .Exec ("UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?" ,
266+ milestoneID ,
267+ )
268+ return err
248269}
249270
250271func countRepoMilestones (e Engine , repoID int64 ) (int64 , error ) {
@@ -278,39 +299,34 @@ func MilestoneStats(repoID int64) (open int64, closed int64, err error) {
278299
279300// ChangeMilestoneStatus changes the milestone open/closed status.
280301func ChangeMilestoneStatus (m * Milestone , isClosed bool ) (err error ) {
281- repo , err := GetRepositoryByID (m .RepoID )
282- if err != nil {
283- return err
284- }
285-
286302 sess := x .NewSession ()
287303 defer sess .Close ()
288304 if err = sess .Begin (); err != nil {
289305 return err
290306 }
291307
292308 m .IsClosed = isClosed
293- if err = updateMilestone ( sess , m ); err != nil {
309+ if _ , err := sess . ID ( m . ID ). Cols ( "is_closed" ). Update ( m ); err != nil {
294310 return err
295311 }
296312
297- numMilestones , err := countRepoMilestones (sess , repo .ID )
298- if err != nil {
313+ if err := updateRepoMilestoneNum (sess , m .RepoID ); err != nil {
299314 return err
300315 }
301- numClosedMilestones , err := countRepoClosedMilestones (sess , repo .ID )
302- if err != nil {
303- return err
304- }
305- repo .NumMilestones = int (numMilestones )
306- repo .NumClosedMilestones = int (numClosedMilestones )
307316
308- if _ , err = sess .ID (repo .ID ).Cols ("num_milestones, num_closed_milestones" ).Update (repo ); err != nil {
309- return err
310- }
311317 return sess .Commit ()
312318}
313319
320+ func updateRepoMilestoneNum (e Engine , repoID int64 ) error {
321+ _ , 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=?" ,
322+ repoID ,
323+ repoID ,
324+ true ,
325+ repoID ,
326+ )
327+ return err
328+ }
329+
314330func updateMilestoneTotalNum (e Engine , milestoneID int64 ) (err error ) {
315331 if _ , err = e .Exec ("UPDATE `milestone` SET num_issues=(SELECT count(*) FROM issue WHERE milestone_id=?) WHERE id=?" ,
316332 milestoneID ,
@@ -319,11 +335,7 @@ func updateMilestoneTotalNum(e Engine, milestoneID int64) (err error) {
319335 return
320336 }
321337
322- _ , err = e .Exec ("UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?" ,
323- milestoneID ,
324- )
325-
326- return
338+ return updateMilestoneCompleteness (e , milestoneID )
327339}
328340
329341func updateMilestoneClosedNum (e Engine , milestoneID int64 ) (err error ) {
@@ -335,10 +347,7 @@ func updateMilestoneClosedNum(e Engine, milestoneID int64) (err error) {
335347 return
336348 }
337349
338- _ , err = e .Exec ("UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?" ,
339- milestoneID ,
340- )
341- return
350+ return updateMilestoneCompleteness (e , milestoneID )
342351}
343352
344353func changeMilestoneAssign (e * xorm.Session , doer * User , issue * Issue , oldMilestoneID int64 ) error {
0 commit comments