milestone.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // Copyright 2017 The Gogs 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. "github.com/go-xorm/xorm"
  8. api "github.com/gogits/go-gogs-client"
  9. "github.com/gogits/gogs/modules/setting"
  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. func (m *Milestone) BeforeInsert() {
  31. m.DeadlineUnix = m.Deadline.Unix()
  32. }
  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. m.DeadlineUnix = m.Deadline.Unix()
  40. m.ClosedDateUnix = m.ClosedDate.Unix()
  41. }
  42. func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
  43. switch colName {
  44. case "num_closed_issues":
  45. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  46. case "deadline_unix":
  47. m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
  48. if m.Deadline.Year() == 9999 {
  49. return
  50. }
  51. m.DeadlineString = m.Deadline.Format("2006-01-02")
  52. if time.Now().Local().After(m.Deadline) {
  53. m.IsOverDue = true
  54. }
  55. case "closed_date_unix":
  56. m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
  57. }
  58. }
  59. // State returns string representation of milestone status.
  60. func (m *Milestone) State() api.StateType {
  61. if m.IsClosed {
  62. return api.STATE_CLOSED
  63. }
  64. return api.STATE_OPEN
  65. }
  66. func (m *Milestone) ChangeStatus(isClosed bool) error {
  67. return ChangeMilestoneStatus(m, isClosed)
  68. }
  69. func (m *Milestone) APIFormat() *api.Milestone {
  70. apiMilestone := &api.Milestone{
  71. ID: m.ID,
  72. State: m.State(),
  73. Title: m.Name,
  74. Description: m.Content,
  75. OpenIssues: m.NumOpenIssues,
  76. ClosedIssues: m.NumClosedIssues,
  77. }
  78. if m.IsClosed {
  79. apiMilestone.Closed = &m.ClosedDate
  80. }
  81. if m.Deadline.Year() < 9999 {
  82. apiMilestone.Deadline = &m.Deadline
  83. }
  84. return apiMilestone
  85. }
  86. // NewMilestone creates new milestone of repository.
  87. func NewMilestone(m *Milestone) (err error) {
  88. sess := x.NewSession()
  89. defer sessionRelease(sess)
  90. if err = sess.Begin(); err != nil {
  91. return err
  92. }
  93. if _, err = sess.Insert(m); err != nil {
  94. return err
  95. }
  96. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
  97. return err
  98. }
  99. return sess.Commit()
  100. }
  101. func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
  102. m := &Milestone{
  103. ID: id,
  104. RepoID: repoID,
  105. }
  106. has, err := e.Get(m)
  107. if err != nil {
  108. return nil, err
  109. } else if !has {
  110. return nil, ErrMilestoneNotExist{id, repoID}
  111. }
  112. return m, nil
  113. }
  114. // GetWebhookByRepoID returns the milestone in a repository.
  115. func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
  116. return getMilestoneByRepoID(x, repoID, id)
  117. }
  118. // GetMilestonesByRepoID returns all milestones of a repository.
  119. func GetMilestonesByRepoID(repoID int64) ([]*Milestone, error) {
  120. miles := make([]*Milestone, 0, 10)
  121. return miles, x.Where("repo_id = ?", repoID).Find(&miles)
  122. }
  123. // GetMilestones returns a list of milestones of given repository and status.
  124. func GetMilestones(repoID int64, page int, isClosed bool) ([]*Milestone, error) {
  125. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  126. sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
  127. if page > 0 {
  128. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  129. }
  130. return miles, sess.Find(&miles)
  131. }
  132. func updateMilestone(e Engine, m *Milestone) error {
  133. _, err := e.Id(m.ID).AllCols().Update(m)
  134. return err
  135. }
  136. // UpdateMilestone updates information of given milestone.
  137. func UpdateMilestone(m *Milestone) error {
  138. return updateMilestone(x, m)
  139. }
  140. func countRepoMilestones(e Engine, repoID int64) int64 {
  141. count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
  142. return count
  143. }
  144. // CountRepoMilestones returns number of milestones in given repository.
  145. func CountRepoMilestones(repoID int64) int64 {
  146. return countRepoMilestones(x, repoID)
  147. }
  148. func countRepoClosedMilestones(e Engine, repoID int64) int64 {
  149. closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
  150. return closed
  151. }
  152. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  153. func CountRepoClosedMilestones(repoID int64) int64 {
  154. return countRepoClosedMilestones(x, repoID)
  155. }
  156. // MilestoneStats returns number of open and closed milestones of given repository.
  157. func MilestoneStats(repoID int64) (open int64, closed int64) {
  158. open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
  159. return open, CountRepoClosedMilestones(repoID)
  160. }
  161. // ChangeMilestoneStatus changes the milestone open/closed status.
  162. // If milestone passes with changed values, those values will be
  163. // updated to database as well.
  164. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  165. repo, err := GetRepositoryByID(m.RepoID)
  166. if err != nil {
  167. return err
  168. }
  169. sess := x.NewSession()
  170. defer sessionRelease(sess)
  171. if err = sess.Begin(); err != nil {
  172. return err
  173. }
  174. m.IsClosed = isClosed
  175. if err = updateMilestone(sess, m); err != nil {
  176. return err
  177. }
  178. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  179. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  180. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  181. return err
  182. }
  183. return sess.Commit()
  184. }
  185. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  186. if issue.MilestoneID == 0 {
  187. return nil
  188. }
  189. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  190. if err != nil {
  191. return err
  192. }
  193. if issue.IsClosed {
  194. m.NumOpenIssues--
  195. m.NumClosedIssues++
  196. } else {
  197. m.NumOpenIssues++
  198. m.NumClosedIssues--
  199. }
  200. return updateMilestone(e, m)
  201. }
  202. // ChangeMilestoneIssueStats updates the open/closed issues counter and progress
  203. // for the milestone associated with the given issue.
  204. func ChangeMilestoneIssueStats(issue *Issue) (err error) {
  205. sess := x.NewSession()
  206. defer sessionRelease(sess)
  207. if err = sess.Begin(); err != nil {
  208. return err
  209. }
  210. if err = changeMilestoneIssueStats(sess, issue); err != nil {
  211. return err
  212. }
  213. return sess.Commit()
  214. }
  215. func changeMilestoneAssign(e *xorm.Session, issue *Issue, oldMilestoneID int64) error {
  216. if oldMilestoneID > 0 {
  217. m, err := getMilestoneByRepoID(e, issue.RepoID, oldMilestoneID)
  218. if err != nil {
  219. return err
  220. }
  221. m.NumIssues--
  222. if issue.IsClosed {
  223. m.NumClosedIssues--
  224. }
  225. if err = updateMilestone(e, m); err != nil {
  226. return err
  227. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?", issue.ID); 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. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?", m.ID, issue.ID); err != nil {
  243. return err
  244. }
  245. }
  246. return updateIssue(e, issue)
  247. }
  248. // ChangeMilestoneAssign changes assignment of milestone for issue.
  249. func ChangeMilestoneAssign(issue *Issue, oldMilestoneID int64) (err error) {
  250. sess := x.NewSession()
  251. defer sess.Close()
  252. if err = sess.Begin(); err != nil {
  253. return err
  254. }
  255. if err = changeMilestoneAssign(sess, issue, oldMilestoneID); err != nil {
  256. return err
  257. }
  258. return sess.Commit()
  259. }
  260. // DeleteMilestoneOfRepoByID deletes a milestone from a repository.
  261. func DeleteMilestoneOfRepoByID(repoID, id int64) error {
  262. m, err := GetMilestoneByRepoID(repoID, id)
  263. if err != nil {
  264. if IsErrMilestoneNotExist(err) {
  265. return nil
  266. }
  267. return err
  268. }
  269. repo, err := GetRepositoryByID(m.RepoID)
  270. if err != nil {
  271. return err
  272. }
  273. sess := x.NewSession()
  274. defer sessionRelease(sess)
  275. if err = sess.Begin(); err != nil {
  276. return err
  277. }
  278. if _, err = sess.Id(m.ID).Delete(new(Milestone)); err != nil {
  279. return err
  280. }
  281. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  282. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  283. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  284. return err
  285. }
  286. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  287. return err
  288. } else if _, err = sess.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  289. return err
  290. }
  291. return sess.Commit()
  292. }