milestone.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/pkg/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. func (m *Milestone) CountIssues(isClosed, includePulls bool) int64 {
  89. sess := x.Where("milestone_id = ?", m.ID).And("is_closed = ?", isClosed)
  90. if !includePulls {
  91. sess.And("is_pull = ?", false)
  92. }
  93. count, _ := sess.Count(new(Issue))
  94. return count
  95. }
  96. // NewMilestone creates new milestone of repository.
  97. func NewMilestone(m *Milestone) (err error) {
  98. sess := x.NewSession()
  99. defer sessionRelease(sess)
  100. if err = sess.Begin(); err != nil {
  101. return err
  102. }
  103. if _, err = sess.Insert(m); err != nil {
  104. return err
  105. }
  106. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
  107. return err
  108. }
  109. return sess.Commit()
  110. }
  111. func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
  112. m := &Milestone{
  113. ID: id,
  114. RepoID: repoID,
  115. }
  116. has, err := e.Get(m)
  117. if err != nil {
  118. return nil, err
  119. } else if !has {
  120. return nil, ErrMilestoneNotExist{id, repoID}
  121. }
  122. return m, nil
  123. }
  124. // GetWebhookByRepoID returns the milestone in a repository.
  125. func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
  126. return getMilestoneByRepoID(x, repoID, id)
  127. }
  128. // GetMilestonesByRepoID returns all milestones of a repository.
  129. func GetMilestonesByRepoID(repoID int64) ([]*Milestone, error) {
  130. miles := make([]*Milestone, 0, 10)
  131. return miles, x.Where("repo_id = ?", repoID).Find(&miles)
  132. }
  133. // GetMilestones returns a list of milestones of given repository and status.
  134. func GetMilestones(repoID int64, page int, isClosed bool) ([]*Milestone, error) {
  135. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  136. sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
  137. if page > 0 {
  138. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  139. }
  140. return miles, sess.Find(&miles)
  141. }
  142. func updateMilestone(e Engine, m *Milestone) error {
  143. _, err := e.Id(m.ID).AllCols().Update(m)
  144. return err
  145. }
  146. // UpdateMilestone updates information of given milestone.
  147. func UpdateMilestone(m *Milestone) error {
  148. return updateMilestone(x, m)
  149. }
  150. func countRepoMilestones(e Engine, repoID int64) int64 {
  151. count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
  152. return count
  153. }
  154. // CountRepoMilestones returns number of milestones in given repository.
  155. func CountRepoMilestones(repoID int64) int64 {
  156. return countRepoMilestones(x, repoID)
  157. }
  158. func countRepoClosedMilestones(e Engine, repoID int64) int64 {
  159. closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
  160. return closed
  161. }
  162. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  163. func CountRepoClosedMilestones(repoID int64) int64 {
  164. return countRepoClosedMilestones(x, repoID)
  165. }
  166. // MilestoneStats returns number of open and closed milestones of given repository.
  167. func MilestoneStats(repoID int64) (open int64, closed int64) {
  168. open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
  169. return open, CountRepoClosedMilestones(repoID)
  170. }
  171. // ChangeMilestoneStatus changes the milestone open/closed status.
  172. // If milestone passes with changed values, those values will be
  173. // updated to database as well.
  174. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  175. repo, err := GetRepositoryByID(m.RepoID)
  176. if err != nil {
  177. return err
  178. }
  179. sess := x.NewSession()
  180. defer sessionRelease(sess)
  181. if err = sess.Begin(); err != nil {
  182. return err
  183. }
  184. m.IsClosed = isClosed
  185. if err = updateMilestone(sess, m); err != nil {
  186. return err
  187. }
  188. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  189. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  190. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  191. return err
  192. }
  193. return sess.Commit()
  194. }
  195. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  196. if issue.MilestoneID == 0 {
  197. return nil
  198. }
  199. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  200. if err != nil {
  201. return err
  202. }
  203. if issue.IsClosed {
  204. m.NumOpenIssues--
  205. m.NumClosedIssues++
  206. } else {
  207. m.NumOpenIssues++
  208. m.NumClosedIssues--
  209. }
  210. return updateMilestone(e, m)
  211. }
  212. // ChangeMilestoneIssueStats updates the open/closed issues counter and progress
  213. // for the milestone associated with the given issue.
  214. func ChangeMilestoneIssueStats(issue *Issue) (err error) {
  215. sess := x.NewSession()
  216. defer sessionRelease(sess)
  217. if err = sess.Begin(); err != nil {
  218. return err
  219. }
  220. if err = changeMilestoneIssueStats(sess, issue); err != nil {
  221. return err
  222. }
  223. return sess.Commit()
  224. }
  225. func changeMilestoneAssign(e *xorm.Session, issue *Issue, oldMilestoneID int64) error {
  226. if oldMilestoneID > 0 {
  227. m, err := getMilestoneByRepoID(e, issue.RepoID, oldMilestoneID)
  228. if err != nil {
  229. return err
  230. }
  231. m.NumIssues--
  232. if issue.IsClosed {
  233. m.NumClosedIssues--
  234. }
  235. if err = updateMilestone(e, m); err != nil {
  236. return err
  237. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?", issue.ID); err != nil {
  238. return err
  239. }
  240. issue.Milestone = nil
  241. }
  242. if issue.MilestoneID > 0 {
  243. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  244. if err != nil {
  245. return err
  246. }
  247. m.NumIssues++
  248. if issue.IsClosed {
  249. m.NumClosedIssues++
  250. }
  251. if err = updateMilestone(e, m); err != nil {
  252. return err
  253. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?", m.ID, issue.ID); err != nil {
  254. return err
  255. }
  256. issue.Milestone = m
  257. }
  258. return updateIssue(e, issue)
  259. }
  260. // ChangeMilestoneAssign changes assignment of milestone for issue.
  261. func ChangeMilestoneAssign(doer *User, issue *Issue, oldMilestoneID int64) (err error) {
  262. sess := x.NewSession()
  263. defer sess.Close()
  264. if err = sess.Begin(); err != nil {
  265. return err
  266. }
  267. if err = changeMilestoneAssign(sess, issue, oldMilestoneID); err != nil {
  268. return err
  269. }
  270. if err = sess.Commit(); err != nil {
  271. return fmt.Errorf("Commit: %v", err)
  272. }
  273. var hookAction api.HookIssueAction
  274. if issue.MilestoneID > 0 {
  275. hookAction = api.HOOK_ISSUE_MILESTONED
  276. } else {
  277. hookAction = api.HOOK_ISSUE_DEMILESTONED
  278. }
  279. if issue.IsPull {
  280. err = issue.PullRequest.LoadIssue()
  281. if err != nil {
  282. log.Error(2, "LoadIssue: %v", err)
  283. return
  284. }
  285. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_PULL_REQUEST, &api.PullRequestPayload{
  286. Action: hookAction,
  287. Index: issue.Index,
  288. PullRequest: issue.PullRequest.APIFormat(),
  289. Repository: issue.Repo.APIFormat(nil),
  290. Sender: doer.APIFormat(),
  291. })
  292. } else {
  293. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_ISSUES, &api.IssuesPayload{
  294. Action: hookAction,
  295. Index: issue.Index,
  296. Issue: issue.APIFormat(),
  297. Repository: issue.Repo.APIFormat(nil),
  298. Sender: doer.APIFormat(),
  299. })
  300. }
  301. if err != nil {
  302. log.Error(2, "PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
  303. }
  304. return nil
  305. }
  306. // DeleteMilestoneOfRepoByID deletes a milestone from a repository.
  307. func DeleteMilestoneOfRepoByID(repoID, id int64) error {
  308. m, err := GetMilestoneByRepoID(repoID, id)
  309. if err != nil {
  310. if IsErrMilestoneNotExist(err) {
  311. return nil
  312. }
  313. return err
  314. }
  315. repo, err := GetRepositoryByID(m.RepoID)
  316. if err != nil {
  317. return err
  318. }
  319. sess := x.NewSession()
  320. defer sessionRelease(sess)
  321. if err = sess.Begin(); err != nil {
  322. return err
  323. }
  324. if _, err = sess.Id(m.ID).Delete(new(Milestone)); err != nil {
  325. return err
  326. }
  327. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  328. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  329. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  330. return err
  331. }
  332. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  333. return err
  334. } else if _, err = sess.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  335. return err
  336. }
  337. return sess.Commit()
  338. }