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.

352 lines
8.4 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. "time"
  7. "code.gitea.io/gitea/modules/setting"
  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. Deadline time.Time `xorm:"-"`
  26. DeadlineUnix int64
  27. ClosedDate time.Time `xorm:"-"`
  28. ClosedDateUnix int64
  29. }
  30. // BeforeInsert is invoked from XORM before inserting an object of this type.
  31. func (m *Milestone) BeforeInsert() {
  32. m.DeadlineUnix = m.Deadline.Unix()
  33. }
  34. // BeforeUpdate is invoked from XORM before updating this object.
  35. func (m *Milestone) BeforeUpdate() {
  36. if m.NumIssues > 0 {
  37. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  38. } else {
  39. m.Completeness = 0
  40. }
  41. m.DeadlineUnix = m.Deadline.Unix()
  42. m.ClosedDateUnix = m.ClosedDate.Unix()
  43. }
  44. // AfterSet is invoked from XORM after setting the value of a field of
  45. // this object.
  46. func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
  47. switch colName {
  48. case "num_closed_issues":
  49. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  50. case "deadline_unix":
  51. m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
  52. if m.Deadline.Year() == 9999 {
  53. return
  54. }
  55. m.DeadlineString = m.Deadline.Format("2006-01-02")
  56. if time.Now().Local().After(m.Deadline) {
  57. m.IsOverDue = true
  58. }
  59. case "closed_date_unix":
  60. m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
  61. }
  62. }
  63. // State returns string representation of milestone status.
  64. func (m *Milestone) State() api.StateType {
  65. if m.IsClosed {
  66. return api.StateClosed
  67. }
  68. return api.StateOpen
  69. }
  70. // APIFormat returns this Milestone in API format.
  71. func (m *Milestone) APIFormat() *api.Milestone {
  72. apiMilestone := &api.Milestone{
  73. ID: m.ID,
  74. State: m.State(),
  75. Title: m.Name,
  76. Description: m.Content,
  77. OpenIssues: m.NumOpenIssues,
  78. ClosedIssues: m.NumClosedIssues,
  79. }
  80. if m.IsClosed {
  81. apiMilestone.Closed = &m.ClosedDate
  82. }
  83. if m.Deadline.Year() < 9999 {
  84. apiMilestone.Deadline = &m.Deadline
  85. }
  86. return apiMilestone
  87. }
  88. // NewMilestone creates new milestone of repository.
  89. func NewMilestone(m *Milestone) (err error) {
  90. sess := x.NewSession()
  91. defer sessionRelease(sess)
  92. if err = sess.Begin(); err != nil {
  93. return err
  94. }
  95. if _, err = sess.Insert(m); err != nil {
  96. return err
  97. }
  98. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
  99. return err
  100. }
  101. return sess.Commit()
  102. }
  103. func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
  104. m := &Milestone{
  105. ID: id,
  106. RepoID: repoID,
  107. }
  108. has, err := e.Get(m)
  109. if err != nil {
  110. return nil, err
  111. } else if !has {
  112. return nil, ErrMilestoneNotExist{id, repoID}
  113. }
  114. return m, nil
  115. }
  116. // GetMilestoneByRepoID returns the milestone in a repository.
  117. func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
  118. return getMilestoneByRepoID(x, repoID, id)
  119. }
  120. // GetMilestonesByRepoID returns all milestones of a repository.
  121. func GetMilestonesByRepoID(repoID int64) ([]*Milestone, error) {
  122. miles := make([]*Milestone, 0, 10)
  123. return miles, x.Where("repo_id = ?", repoID).Find(&miles)
  124. }
  125. // GetMilestones returns a list of milestones of given repository and status.
  126. func GetMilestones(repoID int64, page int, isClosed bool, sortType string) ([]*Milestone, error) {
  127. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  128. sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
  129. if page > 0 {
  130. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  131. }
  132. switch sortType {
  133. case "furthestduedate":
  134. sess.Desc("deadline_unix")
  135. case "leastcomplete":
  136. sess.Asc("completeness")
  137. case "mostcomplete":
  138. sess.Desc("completeness")
  139. case "leastissues":
  140. sess.Asc("num_issues")
  141. case "mostissues":
  142. sess.Desc("num_issues")
  143. default:
  144. sess.Asc("deadline_unix")
  145. }
  146. return miles, sess.Find(&miles)
  147. }
  148. func updateMilestone(e Engine, m *Milestone) error {
  149. _, err := e.Id(m.ID).AllCols().Update(m)
  150. return err
  151. }
  152. // UpdateMilestone updates information of given milestone.
  153. func UpdateMilestone(m *Milestone) error {
  154. return updateMilestone(x, m)
  155. }
  156. func countRepoMilestones(e Engine, repoID int64) int64 {
  157. count, _ := e.
  158. Where("repo_id=?", repoID).
  159. Count(new(Milestone))
  160. return count
  161. }
  162. func countRepoClosedMilestones(e Engine, repoID int64) int64 {
  163. closed, _ := e.
  164. Where("repo_id=? AND is_closed=?", repoID, true).
  165. Count(new(Milestone))
  166. return closed
  167. }
  168. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  169. func CountRepoClosedMilestones(repoID int64) int64 {
  170. return countRepoClosedMilestones(x, repoID)
  171. }
  172. // MilestoneStats returns number of open and closed milestones of given repository.
  173. func MilestoneStats(repoID int64) (open int64, closed int64) {
  174. open, _ = x.
  175. Where("repo_id=? AND is_closed=?", repoID, false).
  176. Count(new(Milestone))
  177. return open, CountRepoClosedMilestones(repoID)
  178. }
  179. // ChangeMilestoneStatus changes the milestone open/closed status.
  180. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  181. repo, err := GetRepositoryByID(m.RepoID)
  182. if err != nil {
  183. return err
  184. }
  185. sess := x.NewSession()
  186. defer sessionRelease(sess)
  187. if err = sess.Begin(); err != nil {
  188. return err
  189. }
  190. m.IsClosed = isClosed
  191. if err = updateMilestone(sess, m); err != nil {
  192. return err
  193. }
  194. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  195. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  196. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  197. return err
  198. }
  199. return sess.Commit()
  200. }
  201. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  202. if issue.MilestoneID == 0 {
  203. return nil
  204. }
  205. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  206. if err != nil {
  207. return err
  208. }
  209. if issue.IsClosed {
  210. m.NumOpenIssues--
  211. m.NumClosedIssues++
  212. } else {
  213. m.NumOpenIssues++
  214. m.NumClosedIssues--
  215. }
  216. return updateMilestone(e, m)
  217. }
  218. func changeMilestoneAssign(e *xorm.Session, doer *User, issue *Issue, oldMilestoneID int64) error {
  219. if oldMilestoneID > 0 {
  220. m, err := getMilestoneByRepoID(e, issue.RepoID, oldMilestoneID)
  221. if err != nil {
  222. return err
  223. }
  224. m.NumIssues--
  225. if issue.IsClosed {
  226. m.NumClosedIssues--
  227. }
  228. if err = updateMilestone(e, m); err != nil {
  229. return err
  230. }
  231. }
  232. if issue.MilestoneID > 0 {
  233. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  234. if err != nil {
  235. return err
  236. }
  237. m.NumIssues++
  238. if issue.IsClosed {
  239. m.NumClosedIssues++
  240. }
  241. if err = updateMilestone(e, m); err != nil {
  242. return err
  243. }
  244. }
  245. if err := issue.loadRepo(e); err != nil {
  246. return err
  247. }
  248. if oldMilestoneID > 0 || issue.MilestoneID > 0 {
  249. if _, err := createMilestoneComment(e, doer, issue.Repo, issue, oldMilestoneID, issue.MilestoneID); err != nil {
  250. return err
  251. }
  252. }
  253. return updateIssue(e, issue)
  254. }
  255. // ChangeMilestoneAssign changes assignment of milestone for issue.
  256. func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err error) {
  257. sess := x.NewSession()
  258. defer sess.Close()
  259. if err = sess.Begin(); err != nil {
  260. return err
  261. }
  262. if err = changeMilestoneAssign(sess, doer, issue, oldMilestoneID); err != nil {
  263. return err
  264. }
  265. return sess.Commit()
  266. }
  267. // DeleteMilestoneByRepoID deletes a milestone from a repository.
  268. func DeleteMilestoneByRepoID(repoID, id int64) error {
  269. m, err := GetMilestoneByRepoID(repoID, id)
  270. if err != nil {
  271. if IsErrMilestoneNotExist(err) {
  272. return nil
  273. }
  274. return err
  275. }
  276. repo, err := GetRepositoryByID(m.RepoID)
  277. if err != nil {
  278. return err
  279. }
  280. sess := x.NewSession()
  281. defer sessionRelease(sess)
  282. if err = sess.Begin(); err != nil {
  283. return err
  284. }
  285. if _, err = sess.Id(m.ID).Delete(new(Milestone)); err != nil {
  286. return err
  287. }
  288. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  289. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  290. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  291. return err
  292. }
  293. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  294. return err
  295. }
  296. return sess.Commit()
  297. }