2
0

milestone.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 db
  5. import (
  6. "fmt"
  7. "time"
  8. log "unknwon.dev/clog/v2"
  9. "xorm.io/xorm"
  10. api "github.com/gogs/go-gogs-client"
  11. "gogs.io/gogs/internal/conf"
  12. "gogs.io/gogs/internal/errutil"
  13. )
  14. // Milestone represents a milestone of repository.
  15. type Milestone struct {
  16. ID int64
  17. RepoID int64 `xorm:"INDEX"`
  18. Name string
  19. Content string `xorm:"TEXT"`
  20. RenderedContent string `xorm:"-" json:"-"`
  21. IsClosed bool
  22. NumIssues int
  23. NumClosedIssues int
  24. NumOpenIssues int `xorm:"-" json:"-"`
  25. Completeness int // Percentage(1-100).
  26. IsOverDue bool `xorm:"-" json:"-"`
  27. DeadlineString string `xorm:"-" json:"-"`
  28. Deadline time.Time `xorm:"-" json:"-"`
  29. DeadlineUnix int64
  30. ClosedDate time.Time `xorm:"-" json:"-"`
  31. ClosedDateUnix int64
  32. }
  33. func (m *Milestone) BeforeInsert() {
  34. m.DeadlineUnix = m.Deadline.Unix()
  35. }
  36. func (m *Milestone) BeforeUpdate() {
  37. if m.NumIssues > 0 {
  38. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  39. } else {
  40. m.Completeness = 0
  41. }
  42. m.DeadlineUnix = m.Deadline.Unix()
  43. m.ClosedDateUnix = m.ClosedDate.Unix()
  44. }
  45. func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
  46. switch colName {
  47. case "num_closed_issues":
  48. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  49. case "deadline_unix":
  50. m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
  51. if m.Deadline.Year() == 9999 {
  52. return
  53. }
  54. m.DeadlineString = m.Deadline.Format("2006-01-02")
  55. if time.Now().Local().After(m.Deadline) {
  56. m.IsOverDue = true
  57. }
  58. case "closed_date_unix":
  59. m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
  60. }
  61. }
  62. // State returns string representation of milestone status.
  63. func (m *Milestone) State() api.StateType {
  64. if m.IsClosed {
  65. return api.STATE_CLOSED
  66. }
  67. return api.STATE_OPEN
  68. }
  69. func (m *Milestone) ChangeStatus(isClosed bool) error {
  70. return ChangeMilestoneStatus(m, isClosed)
  71. }
  72. func (m *Milestone) APIFormat() *api.Milestone {
  73. apiMilestone := &api.Milestone{
  74. ID: m.ID,
  75. State: m.State(),
  76. Title: m.Name,
  77. Description: m.Content,
  78. OpenIssues: m.NumOpenIssues,
  79. ClosedIssues: m.NumClosedIssues,
  80. }
  81. if m.IsClosed {
  82. apiMilestone.Closed = &m.ClosedDate
  83. }
  84. if m.Deadline.Year() < 9999 {
  85. apiMilestone.Deadline = &m.Deadline
  86. }
  87. return apiMilestone
  88. }
  89. func (m *Milestone) CountIssues(isClosed, includePulls bool) int64 {
  90. sess := x.Where("milestone_id = ?", m.ID).And("is_closed = ?", isClosed)
  91. if !includePulls {
  92. sess.And("is_pull = ?", false)
  93. }
  94. count, _ := sess.Count(new(Issue))
  95. return count
  96. }
  97. // NewMilestone creates new milestone of repository.
  98. func NewMilestone(m *Milestone) (err error) {
  99. sess := x.NewSession()
  100. defer sess.Close()
  101. if err = sess.Begin(); err != nil {
  102. return err
  103. }
  104. if _, err = sess.Insert(m); err != nil {
  105. return err
  106. }
  107. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
  108. return err
  109. }
  110. return sess.Commit()
  111. }
  112. var _ errutil.NotFound = (*ErrMilestoneNotExist)(nil)
  113. type ErrMilestoneNotExist struct {
  114. args map[string]interface{}
  115. }
  116. func IsErrMilestoneNotExist(err error) bool {
  117. _, ok := err.(ErrMilestoneNotExist)
  118. return ok
  119. }
  120. func (err ErrMilestoneNotExist) Error() string {
  121. return fmt.Sprintf("milestone does not exist: %v", err.args)
  122. }
  123. func (ErrMilestoneNotExist) NotFound() bool {
  124. return true
  125. }
  126. func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
  127. m := &Milestone{
  128. ID: id,
  129. RepoID: repoID,
  130. }
  131. has, err := e.Get(m)
  132. if err != nil {
  133. return nil, err
  134. } else if !has {
  135. return nil, ErrMilestoneNotExist{args: map[string]interface{}{"repoID": repoID, "milestoneID": id}}
  136. }
  137. return m, nil
  138. }
  139. // GetWebhookByRepoID returns the milestone in a repository.
  140. func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
  141. return getMilestoneByRepoID(x, repoID, id)
  142. }
  143. // GetMilestonesByRepoID returns all milestones of a repository.
  144. func GetMilestonesByRepoID(repoID int64) ([]*Milestone, error) {
  145. miles := make([]*Milestone, 0, 10)
  146. return miles, x.Where("repo_id = ?", repoID).Find(&miles)
  147. }
  148. // GetMilestones returns a list of milestones of given repository and status.
  149. func GetMilestones(repoID int64, page int, isClosed bool) ([]*Milestone, error) {
  150. miles := make([]*Milestone, 0, conf.UI.IssuePagingNum)
  151. sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
  152. if page > 0 {
  153. sess = sess.Limit(conf.UI.IssuePagingNum, (page-1)*conf.UI.IssuePagingNum)
  154. }
  155. return miles, sess.Find(&miles)
  156. }
  157. func updateMilestone(e Engine, m *Milestone) error {
  158. _, err := e.ID(m.ID).AllCols().Update(m)
  159. return err
  160. }
  161. // UpdateMilestone updates information of given milestone.
  162. func UpdateMilestone(m *Milestone) error {
  163. return updateMilestone(x, m)
  164. }
  165. func countRepoMilestones(e Engine, repoID int64) int64 {
  166. count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
  167. return count
  168. }
  169. // CountRepoMilestones returns number of milestones in given repository.
  170. func CountRepoMilestones(repoID int64) int64 {
  171. return countRepoMilestones(x, repoID)
  172. }
  173. func countRepoClosedMilestones(e Engine, repoID int64) int64 {
  174. closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
  175. return closed
  176. }
  177. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  178. func CountRepoClosedMilestones(repoID int64) int64 {
  179. return countRepoClosedMilestones(x, repoID)
  180. }
  181. // MilestoneStats returns number of open and closed milestones of given repository.
  182. func MilestoneStats(repoID int64) (open int64, closed int64) {
  183. open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
  184. return open, CountRepoClosedMilestones(repoID)
  185. }
  186. // ChangeMilestoneStatus changes the milestone open/closed status.
  187. // If milestone passes with changed values, those values will be
  188. // updated to database as well.
  189. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  190. repo, err := GetRepositoryByID(m.RepoID)
  191. if err != nil {
  192. return err
  193. }
  194. sess := x.NewSession()
  195. defer sess.Close()
  196. if err = sess.Begin(); err != nil {
  197. return err
  198. }
  199. m.IsClosed = isClosed
  200. if err = updateMilestone(sess, m); err != nil {
  201. return err
  202. }
  203. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  204. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  205. if _, err = sess.ID(repo.ID).AllCols().Update(repo); err != nil {
  206. return err
  207. }
  208. return sess.Commit()
  209. }
  210. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  211. if issue.MilestoneID == 0 {
  212. return nil
  213. }
  214. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  215. if err != nil {
  216. return err
  217. }
  218. if issue.IsClosed {
  219. m.NumOpenIssues--
  220. m.NumClosedIssues++
  221. } else {
  222. m.NumOpenIssues++
  223. m.NumClosedIssues--
  224. }
  225. return updateMilestone(e, m)
  226. }
  227. // ChangeMilestoneIssueStats updates the open/closed issues counter and progress
  228. // for the milestone associated with the given issue.
  229. func ChangeMilestoneIssueStats(issue *Issue) (err error) {
  230. sess := x.NewSession()
  231. defer sess.Close()
  232. if err = sess.Begin(); err != nil {
  233. return err
  234. }
  235. if err = changeMilestoneIssueStats(sess, issue); err != nil {
  236. return err
  237. }
  238. return sess.Commit()
  239. }
  240. func changeMilestoneAssign(e *xorm.Session, issue *Issue, oldMilestoneID int64) error {
  241. if oldMilestoneID > 0 {
  242. m, err := getMilestoneByRepoID(e, issue.RepoID, oldMilestoneID)
  243. if err != nil {
  244. return err
  245. }
  246. m.NumIssues--
  247. if issue.IsClosed {
  248. m.NumClosedIssues--
  249. }
  250. if err = updateMilestone(e, m); err != nil {
  251. return err
  252. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?", issue.ID); err != nil {
  253. return err
  254. }
  255. issue.Milestone = nil
  256. }
  257. if issue.MilestoneID > 0 {
  258. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  259. if err != nil {
  260. return err
  261. }
  262. m.NumIssues++
  263. if issue.IsClosed {
  264. m.NumClosedIssues++
  265. }
  266. if err = updateMilestone(e, m); err != nil {
  267. return err
  268. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?", m.ID, issue.ID); err != nil {
  269. return err
  270. }
  271. issue.Milestone = m
  272. }
  273. return updateIssue(e, issue)
  274. }
  275. // ChangeMilestoneAssign changes assignment of milestone for issue.
  276. func ChangeMilestoneAssign(doer *User, issue *Issue, oldMilestoneID int64) (err error) {
  277. sess := x.NewSession()
  278. defer sess.Close()
  279. if err = sess.Begin(); err != nil {
  280. return err
  281. }
  282. if err = changeMilestoneAssign(sess, issue, oldMilestoneID); err != nil {
  283. return err
  284. }
  285. if err = sess.Commit(); err != nil {
  286. return fmt.Errorf("Commit: %v", err)
  287. }
  288. var hookAction api.HookIssueAction
  289. if issue.MilestoneID > 0 {
  290. hookAction = api.HOOK_ISSUE_MILESTONED
  291. } else {
  292. hookAction = api.HOOK_ISSUE_DEMILESTONED
  293. }
  294. if issue.IsPull {
  295. err = issue.PullRequest.LoadIssue()
  296. if err != nil {
  297. log.Error("LoadIssue: %v", err)
  298. return
  299. }
  300. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_PULL_REQUEST, &api.PullRequestPayload{
  301. Action: hookAction,
  302. Index: issue.Index,
  303. PullRequest: issue.PullRequest.APIFormat(),
  304. Repository: issue.Repo.APIFormat(nil),
  305. Sender: doer.APIFormat(),
  306. })
  307. } else {
  308. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_ISSUES, &api.IssuesPayload{
  309. Action: hookAction,
  310. Index: issue.Index,
  311. Issue: issue.APIFormat(),
  312. Repository: issue.Repo.APIFormat(nil),
  313. Sender: doer.APIFormat(),
  314. })
  315. }
  316. if err != nil {
  317. log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
  318. }
  319. return nil
  320. }
  321. // DeleteMilestoneOfRepoByID deletes a milestone from a repository.
  322. func DeleteMilestoneOfRepoByID(repoID, id int64) error {
  323. m, err := GetMilestoneByRepoID(repoID, id)
  324. if err != nil {
  325. if IsErrMilestoneNotExist(err) {
  326. return nil
  327. }
  328. return err
  329. }
  330. repo, err := GetRepositoryByID(m.RepoID)
  331. if err != nil {
  332. return err
  333. }
  334. sess := x.NewSession()
  335. defer sess.Close()
  336. if err = sess.Begin(); err != nil {
  337. return err
  338. }
  339. if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
  340. return err
  341. }
  342. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  343. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  344. if _, err = sess.ID(repo.ID).AllCols().Update(repo); err != nil {
  345. return err
  346. }
  347. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  348. return err
  349. } else if _, err = sess.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  350. return err
  351. }
  352. return sess.Commit()
  353. }