本站源代码
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.

460 lines
11KB

  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. Name string
  18. Content string `xorm:"TEXT"`
  19. RenderedContent string `xorm:"-"`
  20. IsClosed bool
  21. NumIssues int
  22. NumClosedIssues int
  23. NumOpenIssues int `xorm:"-"`
  24. Completeness int // Percentage(1-100).
  25. IsOverdue bool `xorm:"-"`
  26. DeadlineString string `xorm:"-"`
  27. DeadlineUnix timeutil.TimeStamp
  28. ClosedDateUnix timeutil.TimeStamp
  29. TotalTrackedTime int64 `xorm:"-"`
  30. }
  31. // BeforeUpdate is invoked from XORM before updating this object.
  32. func (m *Milestone) BeforeUpdate() {
  33. if m.NumIssues > 0 {
  34. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  35. } else {
  36. m.Completeness = 0
  37. }
  38. }
  39. // AfterLoad is invoked from XORM after setting the value of a field of
  40. // this object.
  41. func (m *Milestone) AfterLoad() {
  42. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  43. if m.DeadlineUnix.Year() == 9999 {
  44. return
  45. }
  46. m.DeadlineString = m.DeadlineUnix.Format("2006-01-02")
  47. if timeutil.TimeStampNow() >= m.DeadlineUnix {
  48. m.IsOverdue = true
  49. }
  50. }
  51. // State returns string representation of milestone status.
  52. func (m *Milestone) State() api.StateType {
  53. if m.IsClosed {
  54. return api.StateClosed
  55. }
  56. return api.StateOpen
  57. }
  58. // APIFormat returns this Milestone in API format.
  59. func (m *Milestone) APIFormat() *api.Milestone {
  60. apiMilestone := &api.Milestone{
  61. ID: m.ID,
  62. State: m.State(),
  63. Title: m.Name,
  64. Description: m.Content,
  65. OpenIssues: m.NumOpenIssues,
  66. ClosedIssues: m.NumClosedIssues,
  67. }
  68. if m.IsClosed {
  69. apiMilestone.Closed = m.ClosedDateUnix.AsTimePtr()
  70. }
  71. if m.DeadlineUnix.Year() < 9999 {
  72. apiMilestone.Deadline = m.DeadlineUnix.AsTimePtr()
  73. }
  74. return apiMilestone
  75. }
  76. // NewMilestone creates new milestone of repository.
  77. func NewMilestone(m *Milestone) (err error) {
  78. sess := x.NewSession()
  79. defer sess.Close()
  80. if err = sess.Begin(); err != nil {
  81. return err
  82. }
  83. if _, err = sess.Insert(m); err != nil {
  84. return err
  85. }
  86. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
  87. return err
  88. }
  89. return sess.Commit()
  90. }
  91. func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
  92. m := &Milestone{
  93. ID: id,
  94. RepoID: repoID,
  95. }
  96. has, err := e.Get(m)
  97. if err != nil {
  98. return nil, err
  99. } else if !has {
  100. return nil, ErrMilestoneNotExist{id, repoID}
  101. }
  102. return m, nil
  103. }
  104. // GetMilestoneByRepoID returns the milestone in a repository.
  105. func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
  106. return getMilestoneByRepoID(x, repoID, id)
  107. }
  108. // GetMilestoneByID returns the milestone via id .
  109. func GetMilestoneByID(id int64) (*Milestone, error) {
  110. var m Milestone
  111. has, err := x.ID(id).Get(&m)
  112. if err != nil {
  113. return nil, err
  114. } else if !has {
  115. return nil, ErrMilestoneNotExist{id, 0}
  116. }
  117. return &m, nil
  118. }
  119. // MilestoneList is a list of milestones offering additional functionality
  120. type MilestoneList []*Milestone
  121. func (milestones MilestoneList) loadTotalTrackedTimes(e Engine) error {
  122. type totalTimesByMilestone struct {
  123. MilestoneID int64
  124. Time int64
  125. }
  126. if len(milestones) == 0 {
  127. return nil
  128. }
  129. var trackedTimes = make(map[int64]int64, len(milestones))
  130. // Get total tracked time by milestone_id
  131. rows, err := e.Table("issue").
  132. Join("INNER", "milestone", "issue.milestone_id = milestone.id").
  133. Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
  134. Select("milestone_id, sum(time) as time").
  135. In("milestone_id", milestones.getMilestoneIDs()).
  136. GroupBy("milestone_id").
  137. Rows(new(totalTimesByMilestone))
  138. if err != nil {
  139. return err
  140. }
  141. defer rows.Close()
  142. for rows.Next() {
  143. var totalTime totalTimesByMilestone
  144. err = rows.Scan(&totalTime)
  145. if err != nil {
  146. return err
  147. }
  148. trackedTimes[totalTime.MilestoneID] = totalTime.Time
  149. }
  150. for _, milestone := range milestones {
  151. milestone.TotalTrackedTime = trackedTimes[milestone.ID]
  152. }
  153. return nil
  154. }
  155. // LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
  156. func (milestones MilestoneList) LoadTotalTrackedTimes() error {
  157. return milestones.loadTotalTrackedTimes(x)
  158. }
  159. func (milestones MilestoneList) getMilestoneIDs() []int64 {
  160. var ids = make([]int64, 0, len(milestones))
  161. for _, ms := range milestones {
  162. ids = append(ids, ms.ID)
  163. }
  164. return ids
  165. }
  166. // GetMilestonesByRepoID returns all opened milestones of a repository.
  167. func GetMilestonesByRepoID(repoID int64, state api.StateType) (MilestoneList, error) {
  168. sess := x.Where("repo_id = ?", repoID)
  169. switch state {
  170. case api.StateClosed:
  171. sess = sess.And("is_closed = ?", true)
  172. case api.StateAll:
  173. break
  174. case api.StateOpen:
  175. fallthrough
  176. default:
  177. sess = sess.And("is_closed = ?", false)
  178. }
  179. miles := make([]*Milestone, 0, 10)
  180. return miles, sess.Asc("deadline_unix").Asc("id").Find(&miles)
  181. }
  182. // GetMilestones returns a list of milestones of given repository and status.
  183. func GetMilestones(repoID int64, page int, isClosed bool, sortType string) (MilestoneList, error) {
  184. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  185. sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
  186. if page > 0 {
  187. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  188. }
  189. switch sortType {
  190. case "furthestduedate":
  191. sess.Desc("deadline_unix")
  192. case "leastcomplete":
  193. sess.Asc("completeness")
  194. case "mostcomplete":
  195. sess.Desc("completeness")
  196. case "leastissues":
  197. sess.Asc("num_issues")
  198. case "mostissues":
  199. sess.Desc("num_issues")
  200. default:
  201. sess.Asc("deadline_unix")
  202. }
  203. return miles, sess.Find(&miles)
  204. }
  205. func updateMilestone(e Engine, m *Milestone) error {
  206. _, err := e.ID(m.ID).AllCols().
  207. SetExpr("num_issues", builder.Select("count(*)").From("issue").Where(
  208. builder.Eq{"milestone_id": m.ID},
  209. )).
  210. SetExpr("num_closed_issues", builder.Select("count(*)").From("issue").Where(
  211. builder.Eq{
  212. "milestone_id": m.ID,
  213. "is_closed": true,
  214. },
  215. )).
  216. Update(m)
  217. return err
  218. }
  219. // UpdateMilestone updates information of given milestone.
  220. func UpdateMilestone(m *Milestone) error {
  221. if err := updateMilestone(x, m); err != nil {
  222. return err
  223. }
  224. return updateMilestoneCompleteness(x, m.ID)
  225. }
  226. func updateMilestoneCompleteness(e Engine, milestoneID int64) error {
  227. _, err := e.Exec("UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?",
  228. milestoneID,
  229. )
  230. return err
  231. }
  232. func countRepoMilestones(e Engine, repoID int64) (int64, error) {
  233. return e.
  234. Where("repo_id=?", repoID).
  235. Count(new(Milestone))
  236. }
  237. func countRepoClosedMilestones(e Engine, repoID int64) (int64, error) {
  238. return e.
  239. Where("repo_id=? AND is_closed=?", repoID, true).
  240. Count(new(Milestone))
  241. }
  242. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  243. func CountRepoClosedMilestones(repoID int64) (int64, error) {
  244. return countRepoClosedMilestones(x, repoID)
  245. }
  246. // MilestoneStats returns number of open and closed milestones of given repository.
  247. func MilestoneStats(repoID int64) (open int64, closed int64, err error) {
  248. open, err = x.
  249. Where("repo_id=? AND is_closed=?", repoID, false).
  250. Count(new(Milestone))
  251. if err != nil {
  252. return 0, 0, nil
  253. }
  254. closed, err = CountRepoClosedMilestones(repoID)
  255. return open, closed, err
  256. }
  257. // ChangeMilestoneStatus changes the milestone open/closed status.
  258. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  259. sess := x.NewSession()
  260. defer sess.Close()
  261. if err = sess.Begin(); err != nil {
  262. return err
  263. }
  264. m.IsClosed = isClosed
  265. if isClosed {
  266. m.ClosedDateUnix = timeutil.TimeStampNow()
  267. }
  268. if _, err := sess.ID(m.ID).Cols("is_closed", "closed_date_unix").Update(m); err != nil {
  269. return err
  270. }
  271. if err := updateRepoMilestoneNum(sess, m.RepoID); err != nil {
  272. return err
  273. }
  274. return sess.Commit()
  275. }
  276. func updateRepoMilestoneNum(e Engine, repoID int64) error {
  277. _, 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=?",
  278. repoID,
  279. repoID,
  280. true,
  281. repoID,
  282. )
  283. return err
  284. }
  285. func updateMilestoneTotalNum(e Engine, milestoneID int64) (err error) {
  286. if _, err = e.Exec("UPDATE `milestone` SET num_issues=(SELECT count(*) FROM issue WHERE milestone_id=?) WHERE id=?",
  287. milestoneID,
  288. milestoneID,
  289. ); err != nil {
  290. return
  291. }
  292. return updateMilestoneCompleteness(e, milestoneID)
  293. }
  294. func updateMilestoneClosedNum(e Engine, milestoneID int64) (err error) {
  295. if _, err = e.Exec("UPDATE `milestone` SET num_closed_issues=(SELECT count(*) FROM issue WHERE milestone_id=? AND is_closed=?) WHERE id=?",
  296. milestoneID,
  297. true,
  298. milestoneID,
  299. ); err != nil {
  300. return
  301. }
  302. return updateMilestoneCompleteness(e, milestoneID)
  303. }
  304. func changeMilestoneAssign(e *xorm.Session, doer *User, issue *Issue, oldMilestoneID int64) error {
  305. if err := updateIssueCols(e, issue, "milestone_id"); err != nil {
  306. return err
  307. }
  308. if oldMilestoneID > 0 {
  309. if err := updateMilestoneTotalNum(e, oldMilestoneID); err != nil {
  310. return err
  311. }
  312. if issue.IsClosed {
  313. if err := updateMilestoneClosedNum(e, oldMilestoneID); err != nil {
  314. return err
  315. }
  316. }
  317. }
  318. if issue.MilestoneID > 0 {
  319. if err := updateMilestoneTotalNum(e, issue.MilestoneID); err != nil {
  320. return err
  321. }
  322. if issue.IsClosed {
  323. if err := updateMilestoneClosedNum(e, issue.MilestoneID); err != nil {
  324. return err
  325. }
  326. }
  327. }
  328. if oldMilestoneID > 0 || issue.MilestoneID > 0 {
  329. if err := issue.loadRepo(e); err != nil {
  330. return err
  331. }
  332. if _, err := createMilestoneComment(e, doer, issue.Repo, issue, oldMilestoneID, issue.MilestoneID); err != nil {
  333. return err
  334. }
  335. }
  336. return nil
  337. }
  338. // ChangeMilestoneAssign changes assignment of milestone for issue.
  339. func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err error) {
  340. sess := x.NewSession()
  341. defer sess.Close()
  342. if err = sess.Begin(); err != nil {
  343. return err
  344. }
  345. if err = changeMilestoneAssign(sess, doer, issue, oldMilestoneID); err != nil {
  346. return err
  347. }
  348. if err = sess.Commit(); err != nil {
  349. return fmt.Errorf("Commit: %v", err)
  350. }
  351. return nil
  352. }
  353. // DeleteMilestoneByRepoID deletes a milestone from a repository.
  354. func DeleteMilestoneByRepoID(repoID, id int64) error {
  355. m, err := GetMilestoneByRepoID(repoID, id)
  356. if err != nil {
  357. if IsErrMilestoneNotExist(err) {
  358. return nil
  359. }
  360. return err
  361. }
  362. repo, err := GetRepositoryByID(m.RepoID)
  363. if err != nil {
  364. return err
  365. }
  366. sess := x.NewSession()
  367. defer sess.Close()
  368. if err = sess.Begin(); err != nil {
  369. return err
  370. }
  371. if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
  372. return err
  373. }
  374. numMilestones, err := countRepoMilestones(sess, repo.ID)
  375. if err != nil {
  376. return err
  377. }
  378. numClosedMilestones, err := countRepoClosedMilestones(sess, repo.ID)
  379. if err != nil {
  380. return err
  381. }
  382. repo.NumMilestones = int(numMilestones)
  383. repo.NumClosedMilestones = int(numClosedMilestones)
  384. if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
  385. return err
  386. }
  387. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  388. return err
  389. }
  390. return sess.Commit()
  391. }
上海开阖软件有限公司 沪ICP备12045867号-1