milestone.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. "fmt"
  7. "time"
  8. "github.com/go-xorm/xorm"
  9. log "gopkg.in/clog.v1"
  10. api "github.com/gogits/go-gogs-client"
  11. "github.com/gogits/gogs/modules/setting"
  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. Deadline time.Time `xorm:"-"`
  28. DeadlineUnix int64
  29. ClosedDate time.Time `xorm:"-"`
  30. ClosedDateUnix int64
  31. }
  32. func (m *Milestone) BeforeInsert() {
  33. m.DeadlineUnix = m.Deadline.Unix()
  34. }
  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. func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
  45. switch colName {
  46. case "num_closed_issues":
  47. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  48. case "deadline_unix":
  49. m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
  50. if m.Deadline.Year() == 9999 {
  51. return
  52. }
  53. m.DeadlineString = m.Deadline.Format("2006-01-02")
  54. if time.Now().Local().After(m.Deadline) {
  55. m.IsOverDue = true
  56. }
  57. case "closed_date_unix":
  58. m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
  59. }
  60. }
  61. // State returns string representation of milestone status.
  62. func (m *Milestone) State() api.StateType {
  63. if m.IsClosed {
  64. return api.STATE_CLOSED
  65. }
  66. return api.STATE_OPEN
  67. }
  68. func (m *Milestone) ChangeStatus(isClosed bool) error {
  69. return ChangeMilestoneStatus(m, isClosed)
  70. }
  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. // GetWebhookByRepoID 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) ([]*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. return miles, sess.Find(&miles)
  133. }
  134. func updateMilestone(e Engine, m *Milestone) error {
  135. _, err := e.Id(m.ID).AllCols().Update(m)
  136. return err
  137. }
  138. // UpdateMilestone updates information of given milestone.
  139. func UpdateMilestone(m *Milestone) error {
  140. return updateMilestone(x, m)
  141. }
  142. func countRepoMilestones(e Engine, repoID int64) int64 {
  143. count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
  144. return count
  145. }
  146. // CountRepoMilestones returns number of milestones in given repository.
  147. func CountRepoMilestones(repoID int64) int64 {
  148. return countRepoMilestones(x, repoID)
  149. }
  150. func countRepoClosedMilestones(e Engine, repoID int64) int64 {
  151. closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
  152. return closed
  153. }
  154. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  155. func CountRepoClosedMilestones(repoID int64) int64 {
  156. return countRepoClosedMilestones(x, repoID)
  157. }
  158. // MilestoneStats returns number of open and closed milestones of given repository.
  159. func MilestoneStats(repoID int64) (open int64, closed int64) {
  160. open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
  161. return open, CountRepoClosedMilestones(repoID)
  162. }
  163. // ChangeMilestoneStatus changes the milestone open/closed status.
  164. // If milestone passes with changed values, those values will be
  165. // updated to database as well.
  166. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  167. repo, err := GetRepositoryByID(m.RepoID)
  168. if err != nil {
  169. return err
  170. }
  171. sess := x.NewSession()
  172. defer sessionRelease(sess)
  173. if err = sess.Begin(); err != nil {
  174. return err
  175. }
  176. m.IsClosed = isClosed
  177. if err = updateMilestone(sess, m); err != nil {
  178. return err
  179. }
  180. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  181. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  182. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  183. return err
  184. }
  185. return sess.Commit()
  186. }
  187. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  188. if issue.MilestoneID == 0 {
  189. return nil
  190. }
  191. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  192. if err != nil {
  193. return err
  194. }
  195. if issue.IsClosed {
  196. m.NumOpenIssues--
  197. m.NumClosedIssues++
  198. } else {
  199. m.NumOpenIssues++
  200. m.NumClosedIssues--
  201. }
  202. return updateMilestone(e, m)
  203. }
  204. // ChangeMilestoneIssueStats updates the open/closed issues counter and progress
  205. // for the milestone associated with the given issue.
  206. func ChangeMilestoneIssueStats(issue *Issue) (err error) {
  207. sess := x.NewSession()
  208. defer sessionRelease(sess)
  209. if err = sess.Begin(); err != nil {
  210. return err
  211. }
  212. if err = changeMilestoneIssueStats(sess, issue); err != nil {
  213. return err
  214. }
  215. return sess.Commit()
  216. }
  217. func changeMilestoneAssign(e *xorm.Session, 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. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?", issue.ID); err != nil {
  230. return err
  231. }
  232. issue.Milestone = nil
  233. }
  234. if issue.MilestoneID > 0 {
  235. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  236. if err != nil {
  237. return err
  238. }
  239. m.NumIssues++
  240. if issue.IsClosed {
  241. m.NumClosedIssues++
  242. }
  243. if err = updateMilestone(e, m); err != nil {
  244. return err
  245. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?", m.ID, issue.ID); err != nil {
  246. return err
  247. }
  248. issue.Milestone = m
  249. }
  250. return updateIssue(e, issue)
  251. }
  252. // ChangeMilestoneAssign changes assignment of milestone for issue.
  253. func ChangeMilestoneAssign(doer *User, issue *Issue, oldMilestoneID int64) (err error) {
  254. sess := x.NewSession()
  255. defer sess.Close()
  256. if err = sess.Begin(); err != nil {
  257. return err
  258. }
  259. if err = changeMilestoneAssign(sess, issue, oldMilestoneID); err != nil {
  260. return err
  261. }
  262. if err = sess.Commit(); err != nil {
  263. return fmt.Errorf("Commit: %v", err)
  264. }
  265. var hookAction api.HookIssueAction
  266. if issue.MilestoneID > 0 {
  267. hookAction = api.HOOK_ISSUE_MILESTONED
  268. } else {
  269. hookAction = api.HOOK_ISSUE_DEMILESTONED
  270. }
  271. if issue.IsPull {
  272. err = issue.PullRequest.LoadIssue()
  273. if err != nil {
  274. log.Error(2, "LoadIssue: %v", err)
  275. return
  276. }
  277. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_PULL_REQUEST, &api.PullRequestPayload{
  278. Action: hookAction,
  279. Index: issue.Index,
  280. PullRequest: issue.PullRequest.APIFormat(),
  281. Repository: issue.Repo.APIFormat(nil),
  282. Sender: doer.APIFormat(),
  283. })
  284. } else {
  285. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_ISSUES, &api.IssuesPayload{
  286. Action: hookAction,
  287. Index: issue.Index,
  288. Issue: issue.APIFormat(),
  289. Repository: issue.Repo.APIFormat(nil),
  290. Sender: doer.APIFormat(),
  291. })
  292. }
  293. if err != nil {
  294. log.Error(2, "PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
  295. }
  296. return nil
  297. }
  298. // DeleteMilestoneOfRepoByID deletes a milestone from a repository.
  299. func DeleteMilestoneOfRepoByID(repoID, id int64) error {
  300. m, err := GetMilestoneByRepoID(repoID, id)
  301. if err != nil {
  302. if IsErrMilestoneNotExist(err) {
  303. return nil
  304. }
  305. return err
  306. }
  307. repo, err := GetRepositoryByID(m.RepoID)
  308. if err != nil {
  309. return err
  310. }
  311. sess := x.NewSession()
  312. defer sessionRelease(sess)
  313. if err = sess.Begin(); err != nil {
  314. return err
  315. }
  316. if _, err = sess.Id(m.ID).Delete(new(Milestone)); err != nil {
  317. return err
  318. }
  319. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  320. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  321. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  322. return err
  323. }
  324. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  325. return err
  326. } else if _, err = sess.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  327. return err
  328. }
  329. return sess.Commit()
  330. }