You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

572 lines
14 KiB

Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/setting"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "xorm.io/builder"
  11. "xorm.io/xorm"
  12. )
  13. // Milestone represents a milestone of repository.
  14. type Milestone struct {
  15. ID int64 `xorm:"pk autoincr"`
  16. RepoID int64 `xorm:"INDEX"`
  17. Repo *Repository `xorm:"-"`
  18. Name string
  19. Content string `xorm:"TEXT"`
  20. RenderedContent string `xorm:"-"`
  21. IsClosed bool
  22. NumIssues int
  23. NumClosedIssues int
  24. NumOpenIssues int `xorm:"-"`
  25. Completeness int // Percentage(1-100).
  26. IsOverdue bool `xorm:"-"`
  27. DeadlineString string `xorm:"-"`
  28. DeadlineUnix timeutil.TimeStamp
  29. ClosedDateUnix timeutil.TimeStamp
  30. TotalTrackedTime int64 `xorm:"-"`
  31. }
  32. // BeforeUpdate is invoked from XORM before updating this object.
  33. func (m *Milestone) BeforeUpdate() {
  34. if m.NumIssues > 0 {
  35. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  36. } else {
  37. m.Completeness = 0
  38. }
  39. }
  40. // AfterLoad is invoked from XORM after setting the value of a field of
  41. // this object.
  42. func (m *Milestone) AfterLoad() {
  43. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  44. if m.DeadlineUnix.Year() == 9999 {
  45. return
  46. }
  47. m.DeadlineString = m.DeadlineUnix.Format("2006-01-02")
  48. if timeutil.TimeStampNow() >= m.DeadlineUnix {
  49. m.IsOverdue = true
  50. }
  51. }
  52. // State returns string representation of milestone status.
  53. func (m *Milestone) State() api.StateType {
  54. if m.IsClosed {
  55. return api.StateClosed
  56. }
  57. return api.StateOpen
  58. }
  59. // APIFormat returns this Milestone in API format.
  60. func (m *Milestone) APIFormat() *api.Milestone {
  61. apiMilestone := &api.Milestone{
  62. ID: m.ID,
  63. State: m.State(),
  64. Title: m.Name,
  65. Description: m.Content,
  66. OpenIssues: m.NumOpenIssues,
  67. ClosedIssues: m.NumClosedIssues,
  68. }
  69. if m.IsClosed {
  70. apiMilestone.Closed = m.ClosedDateUnix.AsTimePtr()
  71. }
  72. if m.DeadlineUnix.Year() < 9999 {
  73. apiMilestone.Deadline = m.DeadlineUnix.AsTimePtr()
  74. }
  75. return apiMilestone
  76. }
  77. // NewMilestone creates new milestone of repository.
  78. func NewMilestone(m *Milestone) (err error) {
  79. sess := x.NewSession()
  80. defer sess.Close()
  81. if err = sess.Begin(); err != nil {
  82. return err
  83. }
  84. if _, err = sess.Insert(m); err != nil {
  85. return err
  86. }
  87. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
  88. return err
  89. }
  90. return sess.Commit()
  91. }
  92. func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
  93. m := &Milestone{
  94. ID: id,
  95. RepoID: repoID,
  96. }
  97. has, err := e.Get(m)
  98. if err != nil {
  99. return nil, err
  100. } else if !has {
  101. return nil, ErrMilestoneNotExist{id, repoID}
  102. }
  103. return m, nil
  104. }
  105. // GetMilestoneByRepoID returns the milestone in a repository.
  106. func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
  107. return getMilestoneByRepoID(x, repoID, id)
  108. }
  109. // GetMilestoneByID returns the milestone via id .
  110. func GetMilestoneByID(id int64) (*Milestone, error) {
  111. var m Milestone
  112. has, err := x.ID(id).Get(&m)
  113. if err != nil {
  114. return nil, err
  115. } else if !has {
  116. return nil, ErrMilestoneNotExist{id, 0}
  117. }
  118. return &m, nil
  119. }
  120. // MilestoneList is a list of milestones offering additional functionality
  121. type MilestoneList []*Milestone
  122. func (milestones MilestoneList) loadTotalTrackedTimes(e Engine) error {
  123. type totalTimesByMilestone struct {
  124. MilestoneID int64
  125. Time int64
  126. }
  127. if len(milestones) == 0 {
  128. return nil
  129. }
  130. var trackedTimes = make(map[int64]int64, len(milestones))
  131. // Get total tracked time by milestone_id
  132. rows, err := e.Table("issue").
  133. Join("INNER", "milestone", "issue.milestone_id = milestone.id").
  134. Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
  135. Where("tracked_time.deleted = ?", false).
  136. Select("milestone_id, sum(time) as time").
  137. In("milestone_id", milestones.getMilestoneIDs()).
  138. GroupBy("milestone_id").
  139. Rows(new(totalTimesByMilestone))
  140. if err != nil {
  141. return err
  142. }
  143. defer rows.Close()
  144. for rows.Next() {
  145. var totalTime totalTimesByMilestone
  146. err = rows.Scan(&totalTime)
  147. if err != nil {
  148. return err
  149. }
  150. trackedTimes[totalTime.MilestoneID] = totalTime.Time
  151. }
  152. for _, milestone := range milestones {
  153. milestone.TotalTrackedTime = trackedTimes[milestone.ID]
  154. }
  155. return nil
  156. }
  157. func (m *Milestone) loadTotalTrackedTime(e Engine) error {
  158. type totalTimesByMilestone struct {
  159. MilestoneID int64
  160. Time int64
  161. }
  162. totalTime := &totalTimesByMilestone{MilestoneID: m.ID}
  163. has, err := e.Table("issue").
  164. Join("INNER", "milestone", "issue.milestone_id = milestone.id").
  165. Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
  166. Where("tracked_time.deleted = ?", false).
  167. Select("milestone_id, sum(time) as time").
  168. Where("milestone_id = ?", m.ID).
  169. GroupBy("milestone_id").
  170. Get(totalTime)
  171. if err != nil {
  172. return err
  173. } else if !has {
  174. return nil
  175. }
  176. m.TotalTrackedTime = totalTime.Time
  177. return nil
  178. }
  179. // LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
  180. func (milestones MilestoneList) LoadTotalTrackedTimes() error {
  181. return milestones.loadTotalTrackedTimes(x)
  182. }
  183. // LoadTotalTrackedTime loads the tracked time for the milestone
  184. func (m *Milestone) LoadTotalTrackedTime() error {
  185. return m.loadTotalTrackedTime(x)
  186. }
  187. func (milestones MilestoneList) getMilestoneIDs() []int64 {
  188. var ids = make([]int64, 0, len(milestones))
  189. for _, ms := range milestones {
  190. ids = append(ids, ms.ID)
  191. }
  192. return ids
  193. }
  194. // GetMilestonesByRepoID returns all opened milestones of a repository.
  195. func GetMilestonesByRepoID(repoID int64, state api.StateType) (MilestoneList, error) {
  196. sess := x.Where("repo_id = ?", repoID)
  197. switch state {
  198. case api.StateClosed:
  199. sess = sess.And("is_closed = ?", true)
  200. case api.StateAll:
  201. break
  202. case api.StateOpen:
  203. fallthrough
  204. default:
  205. sess = sess.And("is_closed = ?", false)
  206. }
  207. miles := make([]*Milestone, 0, 10)
  208. return miles, sess.Asc("deadline_unix").Asc("id").Find(&miles)
  209. }
  210. // GetMilestones returns a list of milestones of given repository and status.
  211. func GetMilestones(repoID int64, page int, isClosed bool, sortType string) (MilestoneList, error) {
  212. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  213. sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
  214. if page > 0 {
  215. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  216. }
  217. switch sortType {
  218. case "furthestduedate":
  219. sess.Desc("deadline_unix")
  220. case "leastcomplete":
  221. sess.Asc("completeness")
  222. case "mostcomplete":
  223. sess.Desc("completeness")
  224. case "leastissues":
  225. sess.Asc("num_issues")
  226. case "mostissues":
  227. sess.Desc("num_issues")
  228. default:
  229. sess.Asc("deadline_unix")
  230. }
  231. return miles, sess.Find(&miles)
  232. }
  233. func updateMilestone(e Engine, m *Milestone) error {
  234. _, err := e.ID(m.ID).AllCols().
  235. SetExpr("num_issues", builder.Select("count(*)").From("issue").Where(
  236. builder.Eq{"milestone_id": m.ID},
  237. )).
  238. SetExpr("num_closed_issues", builder.Select("count(*)").From("issue").Where(
  239. builder.Eq{
  240. "milestone_id": m.ID,
  241. "is_closed": true,
  242. },
  243. )).
  244. Update(m)
  245. return err
  246. }
  247. // UpdateMilestone updates information of given milestone.
  248. func UpdateMilestone(m *Milestone) error {
  249. if err := updateMilestone(x, m); err != nil {
  250. return err
  251. }
  252. return updateMilestoneCompleteness(x, m.ID)
  253. }
  254. func updateMilestoneCompleteness(e Engine, milestoneID int64) error {
  255. _, err := e.Exec("UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?",
  256. milestoneID,
  257. )
  258. return err
  259. }
  260. func countRepoMilestones(e Engine, repoID int64) (int64, error) {
  261. return e.
  262. Where("repo_id=?", repoID).
  263. Count(new(Milestone))
  264. }
  265. func countRepoClosedMilestones(e Engine, repoID int64) (int64, error) {
  266. return e.
  267. Where("repo_id=? AND is_closed=?", repoID, true).
  268. Count(new(Milestone))
  269. }
  270. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  271. func CountRepoClosedMilestones(repoID int64) (int64, error) {
  272. return countRepoClosedMilestones(x, repoID)
  273. }
  274. // MilestoneStats returns number of open and closed milestones of given repository.
  275. func MilestoneStats(repoID int64) (open int64, closed int64, err error) {
  276. open, err = x.
  277. Where("repo_id=? AND is_closed=?", repoID, false).
  278. Count(new(Milestone))
  279. if err != nil {
  280. return 0, 0, nil
  281. }
  282. closed, err = CountRepoClosedMilestones(repoID)
  283. return open, closed, err
  284. }
  285. // ChangeMilestoneStatus changes the milestone open/closed status.
  286. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  287. sess := x.NewSession()
  288. defer sess.Close()
  289. if err = sess.Begin(); err != nil {
  290. return err
  291. }
  292. m.IsClosed = isClosed
  293. if isClosed {
  294. m.ClosedDateUnix = timeutil.TimeStampNow()
  295. }
  296. if _, err := sess.ID(m.ID).Cols("is_closed", "closed_date_unix").Update(m); err != nil {
  297. return err
  298. }
  299. if err := updateRepoMilestoneNum(sess, m.RepoID); err != nil {
  300. return err
  301. }
  302. return sess.Commit()
  303. }
  304. func updateRepoMilestoneNum(e Engine, repoID int64) error {
  305. _, 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=?",
  306. repoID,
  307. repoID,
  308. true,
  309. repoID,
  310. )
  311. return err
  312. }
  313. func updateMilestoneTotalNum(e Engine, milestoneID int64) (err error) {
  314. if _, err = e.Exec("UPDATE `milestone` SET num_issues=(SELECT count(*) FROM issue WHERE milestone_id=?) WHERE id=?",
  315. milestoneID,
  316. milestoneID,
  317. ); err != nil {
  318. return
  319. }
  320. return updateMilestoneCompleteness(e, milestoneID)
  321. }
  322. func updateMilestoneClosedNum(e Engine, milestoneID int64) (err error) {
  323. if _, err = e.Exec("UPDATE `milestone` SET num_closed_issues=(SELECT count(*) FROM issue WHERE milestone_id=? AND is_closed=?) WHERE id=?",
  324. milestoneID,
  325. true,
  326. milestoneID,
  327. ); err != nil {
  328. return
  329. }
  330. return updateMilestoneCompleteness(e, milestoneID)
  331. }
  332. func changeMilestoneAssign(e *xorm.Session, doer *User, issue *Issue, oldMilestoneID int64) error {
  333. if err := updateIssueCols(e, issue, "milestone_id"); err != nil {
  334. return err
  335. }
  336. if oldMilestoneID > 0 {
  337. if err := updateMilestoneTotalNum(e, oldMilestoneID); err != nil {
  338. return err
  339. }
  340. if issue.IsClosed {
  341. if err := updateMilestoneClosedNum(e, oldMilestoneID); err != nil {
  342. return err
  343. }
  344. }
  345. }
  346. if issue.MilestoneID > 0 {
  347. if err := updateMilestoneTotalNum(e, issue.MilestoneID); err != nil {
  348. return err
  349. }
  350. if issue.IsClosed {
  351. if err := updateMilestoneClosedNum(e, issue.MilestoneID); err != nil {
  352. return err
  353. }
  354. }
  355. }
  356. if oldMilestoneID > 0 || issue.MilestoneID > 0 {
  357. if err := issue.loadRepo(e); err != nil {
  358. return err
  359. }
  360. var opts = &CreateCommentOptions{
  361. Type: CommentTypeMilestone,
  362. Doer: doer,
  363. Repo: issue.Repo,
  364. Issue: issue,
  365. OldMilestoneID: oldMilestoneID,
  366. MilestoneID: issue.MilestoneID,
  367. }
  368. if _, err := createComment(e, opts); err != nil {
  369. return err
  370. }
  371. }
  372. return nil
  373. }
  374. // ChangeMilestoneAssign changes assignment of milestone for issue.
  375. func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err error) {
  376. sess := x.NewSession()
  377. defer sess.Close()
  378. if err = sess.Begin(); err != nil {
  379. return err
  380. }
  381. if err = changeMilestoneAssign(sess, doer, issue, oldMilestoneID); err != nil {
  382. return err
  383. }
  384. if err = sess.Commit(); err != nil {
  385. return fmt.Errorf("Commit: %v", err)
  386. }
  387. return nil
  388. }
  389. // DeleteMilestoneByRepoID deletes a milestone from a repository.
  390. func DeleteMilestoneByRepoID(repoID, id int64) error {
  391. m, err := GetMilestoneByRepoID(repoID, id)
  392. if err != nil {
  393. if IsErrMilestoneNotExist(err) {
  394. return nil
  395. }
  396. return err
  397. }
  398. repo, err := GetRepositoryByID(m.RepoID)
  399. if err != nil {
  400. return err
  401. }
  402. sess := x.NewSession()
  403. defer sess.Close()
  404. if err = sess.Begin(); err != nil {
  405. return err
  406. }
  407. if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
  408. return err
  409. }
  410. numMilestones, err := countRepoMilestones(sess, repo.ID)
  411. if err != nil {
  412. return err
  413. }
  414. numClosedMilestones, err := countRepoClosedMilestones(sess, repo.ID)
  415. if err != nil {
  416. return err
  417. }
  418. repo.NumMilestones = int(numMilestones)
  419. repo.NumClosedMilestones = int(numClosedMilestones)
  420. if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
  421. return err
  422. }
  423. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  424. return err
  425. }
  426. return sess.Commit()
  427. }
  428. // CountMilestonesByRepoIDs map from repoIDs to number of milestones matching the options`
  429. func CountMilestonesByRepoIDs(repoIDs []int64, isClosed bool) (map[int64]int64, error) {
  430. sess := x.Where("is_closed = ?", isClosed)
  431. sess.In("repo_id", repoIDs)
  432. countsSlice := make([]*struct {
  433. RepoID int64
  434. Count int64
  435. }, 0, 10)
  436. if err := sess.GroupBy("repo_id").
  437. Select("repo_id AS repo_id, COUNT(*) AS count").
  438. Table("milestone").
  439. Find(&countsSlice); err != nil {
  440. return nil, err
  441. }
  442. countMap := make(map[int64]int64, len(countsSlice))
  443. for _, c := range countsSlice {
  444. countMap[c.RepoID] = c.Count
  445. }
  446. return countMap, nil
  447. }
  448. // GetMilestonesByRepoIDs returns a list of milestones of given repositories and status.
  449. func GetMilestonesByRepoIDs(repoIDs []int64, page int, isClosed bool, sortType string) (MilestoneList, error) {
  450. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  451. sess := x.Where("is_closed = ?", isClosed)
  452. sess.In("repo_id", repoIDs)
  453. if page > 0 {
  454. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  455. }
  456. switch sortType {
  457. case "furthestduedate":
  458. sess.Desc("deadline_unix")
  459. case "leastcomplete":
  460. sess.Asc("completeness")
  461. case "mostcomplete":
  462. sess.Desc("completeness")
  463. case "leastissues":
  464. sess.Asc("num_issues")
  465. case "mostissues":
  466. sess.Desc("num_issues")
  467. default:
  468. sess.Asc("deadline_unix")
  469. }
  470. return miles, sess.Find(&miles)
  471. }
  472. // MilestonesStats represents milestone statistic information.
  473. type MilestonesStats struct {
  474. OpenCount, ClosedCount int64
  475. }
  476. // GetMilestonesStats returns milestone statistic information for dashboard by given conditions.
  477. func GetMilestonesStats(userRepoIDs []int64) (*MilestonesStats, error) {
  478. var err error
  479. stats := &MilestonesStats{}
  480. stats.OpenCount, err = x.Where("is_closed = ?", false).
  481. And(builder.In("repo_id", userRepoIDs)).
  482. Count(new(Milestone))
  483. if err != nil {
  484. return nil, err
  485. }
  486. stats.ClosedCount, err = x.Where("is_closed = ?", true).
  487. And(builder.In("repo_id", userRepoIDs)).
  488. Count(new(Milestone))
  489. if err != nil {
  490. return nil, err
  491. }
  492. return stats, nil
  493. }