Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_milestone.go 8.3KB

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