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.

357 lines
8.6 KiB

  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. "code.gitea.io/gitea/modules/setting"
  7. "code.gitea.io/gitea/modules/util"
  8. api "code.gitea.io/sdk/gitea"
  9. "github.com/go-xorm/xorm"
  10. )
  11. // Milestone represents a milestone of repository.
  12. type Milestone struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. RepoID int64 `xorm:"INDEX"`
  15. Name string
  16. Content string `xorm:"TEXT"`
  17. RenderedContent string `xorm:"-"`
  18. IsClosed bool
  19. NumIssues int
  20. NumClosedIssues int
  21. NumOpenIssues int `xorm:"-"`
  22. Completeness int // Percentage(1-100).
  23. IsOverDue bool `xorm:"-"`
  24. DeadlineString string `xorm:"-"`
  25. DeadlineUnix util.TimeStamp
  26. ClosedDateUnix util.TimeStamp
  27. }
  28. // BeforeInsert is invoked from XORM before inserting an object of this type.
  29. func (m *Milestone) BeforeInsert() {
  30. m.DeadlineUnix = util.TimeStampNow()
  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 util.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. // GetMilestonesByRepoID returns all milestones of a repository.
  110. func GetMilestonesByRepoID(repoID int64) ([]*Milestone, error) {
  111. miles := make([]*Milestone, 0, 10)
  112. return miles, x.Where("repo_id = ?", repoID).Find(&miles)
  113. }
  114. // GetMilestones returns a list of milestones of given repository and status.
  115. func GetMilestones(repoID int64, page int, isClosed bool, sortType string) ([]*Milestone, error) {
  116. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  117. sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
  118. if page > 0 {
  119. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  120. }
  121. switch sortType {
  122. case "furthestduedate":
  123. sess.Desc("deadline_unix")
  124. case "leastcomplete":
  125. sess.Asc("completeness")
  126. case "mostcomplete":
  127. sess.Desc("completeness")
  128. case "leastissues":
  129. sess.Asc("num_issues")
  130. case "mostissues":
  131. sess.Desc("num_issues")
  132. default:
  133. sess.Asc("deadline_unix")
  134. }
  135. return miles, sess.Find(&miles)
  136. }
  137. func updateMilestone(e Engine, m *Milestone) error {
  138. _, err := e.ID(m.ID).AllCols().Update(m)
  139. return err
  140. }
  141. // UpdateMilestone updates information of given milestone.
  142. func UpdateMilestone(m *Milestone) error {
  143. return updateMilestone(x, m)
  144. }
  145. func countRepoMilestones(e Engine, repoID int64) (int64, error) {
  146. return e.
  147. Where("repo_id=?", repoID).
  148. Count(new(Milestone))
  149. }
  150. func countRepoClosedMilestones(e Engine, repoID int64) (int64, error) {
  151. return e.
  152. Where("repo_id=? AND is_closed=?", repoID, true).
  153. Count(new(Milestone))
  154. }
  155. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  156. func CountRepoClosedMilestones(repoID int64) (int64, error) {
  157. return countRepoClosedMilestones(x, repoID)
  158. }
  159. // MilestoneStats returns number of open and closed milestones of given repository.
  160. func MilestoneStats(repoID int64) (open int64, closed int64, err error) {
  161. open, err = x.
  162. Where("repo_id=? AND is_closed=?", repoID, false).
  163. Count(new(Milestone))
  164. if err != nil {
  165. return 0, 0, nil
  166. }
  167. closed, err = CountRepoClosedMilestones(repoID)
  168. return open, closed, err
  169. }
  170. // ChangeMilestoneStatus changes the milestone open/closed status.
  171. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  172. repo, err := GetRepositoryByID(m.RepoID)
  173. if err != nil {
  174. return err
  175. }
  176. sess := x.NewSession()
  177. defer sess.Close()
  178. if err = sess.Begin(); err != nil {
  179. return err
  180. }
  181. m.IsClosed = isClosed
  182. if err = updateMilestone(sess, m); err != nil {
  183. return err
  184. }
  185. numMilestones, err := countRepoMilestones(sess, repo.ID)
  186. if err != nil {
  187. return err
  188. }
  189. numClosedMilestones, err := countRepoClosedMilestones(sess, repo.ID)
  190. if err != nil {
  191. return err
  192. }
  193. repo.NumMilestones = int(numMilestones)
  194. repo.NumClosedMilestones = int(numClosedMilestones)
  195. if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
  196. return err
  197. }
  198. return sess.Commit()
  199. }
  200. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  201. if issue.MilestoneID == 0 {
  202. return nil
  203. }
  204. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  205. if err != nil {
  206. return err
  207. }
  208. if issue.IsClosed {
  209. m.NumOpenIssues--
  210. m.NumClosedIssues++
  211. } else {
  212. m.NumOpenIssues++
  213. m.NumClosedIssues--
  214. }
  215. return updateMilestone(e, m)
  216. }
  217. func changeMilestoneAssign(e *xorm.Session, doer *User, issue *Issue, oldMilestoneID int64) error {
  218. if oldMilestoneID > 0 {
  219. m, err := getMilestoneByRepoID(e, issue.RepoID, oldMilestoneID)
  220. if err != nil {
  221. return err
  222. }
  223. m.NumIssues--
  224. if issue.IsClosed {
  225. m.NumClosedIssues--
  226. }
  227. if err = updateMilestone(e, m); err != nil {
  228. return err
  229. }
  230. }
  231. if issue.MilestoneID > 0 {
  232. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  233. if err != nil {
  234. return err
  235. }
  236. m.NumIssues++
  237. if issue.IsClosed {
  238. m.NumClosedIssues++
  239. }
  240. if err = updateMilestone(e, m); err != nil {
  241. return err
  242. }
  243. }
  244. if err := issue.loadRepo(e); err != nil {
  245. return err
  246. }
  247. if oldMilestoneID > 0 || issue.MilestoneID > 0 {
  248. if _, err := createMilestoneComment(e, doer, issue.Repo, issue, oldMilestoneID, issue.MilestoneID); err != nil {
  249. return err
  250. }
  251. }
  252. return updateIssueCols(e, issue, "milestone_id")
  253. }
  254. // ChangeMilestoneAssign changes assignment of milestone for issue.
  255. func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err error) {
  256. sess := x.NewSession()
  257. defer sess.Close()
  258. if err = sess.Begin(); err != nil {
  259. return err
  260. }
  261. if err = changeMilestoneAssign(sess, doer, issue, oldMilestoneID); err != nil {
  262. return err
  263. }
  264. return sess.Commit()
  265. }
  266. // DeleteMilestoneByRepoID deletes a milestone from a repository.
  267. func DeleteMilestoneByRepoID(repoID, id int64) error {
  268. m, err := GetMilestoneByRepoID(repoID, id)
  269. if err != nil {
  270. if IsErrMilestoneNotExist(err) {
  271. return nil
  272. }
  273. return err
  274. }
  275. repo, err := GetRepositoryByID(m.RepoID)
  276. if err != nil {
  277. return err
  278. }
  279. sess := x.NewSession()
  280. defer sess.Close()
  281. if err = sess.Begin(); err != nil {
  282. return err
  283. }
  284. if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
  285. return err
  286. }
  287. numMilestones, err := countRepoMilestones(sess, repo.ID)
  288. if err != nil {
  289. return err
  290. }
  291. numClosedMilestones, err := countRepoClosedMilestones(sess, repo.ID)
  292. if err != nil {
  293. return err
  294. }
  295. repo.NumMilestones = int(numMilestones)
  296. repo.NumClosedMilestones = int(numClosedMilestones)
  297. if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
  298. return err
  299. }
  300. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  301. return err
  302. }
  303. return sess.Commit()
  304. }