You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // Copyright 2014 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. "bytes"
  7. "errors"
  8. "strings"
  9. "time"
  10. "github.com/go-xorm/xorm"
  11. "github.com/gogits/gogs/modules/base"
  12. )
  13. var (
  14. ErrIssueNotExist = errors.New("Issue does not exist")
  15. ErrMilestoneNotExist = errors.New("Milestone does not exist")
  16. )
  17. // Issue represents an issue or pull request of repository.
  18. type Issue struct {
  19. Id int64
  20. RepoId int64 `xorm:"INDEX"`
  21. Index int64 // Index in one repository.
  22. Name string
  23. Repo *Repository `xorm:"-"`
  24. PosterId int64
  25. Poster *User `xorm:"-"`
  26. MilestoneId int64
  27. AssigneeId int64
  28. Assignee *User `xorm:"-"`
  29. IsRead bool `xorm:"-"`
  30. IsPull bool // Indicates whether is a pull request or not.
  31. IsClosed bool
  32. Labels string `xorm:"TEXT"`
  33. Content string `xorm:"TEXT"`
  34. RenderedContent string `xorm:"-"`
  35. Priority int
  36. NumComments int
  37. Deadline time.Time
  38. Created time.Time `xorm:"CREATED"`
  39. Updated time.Time `xorm:"UPDATED"`
  40. }
  41. func (i *Issue) GetPoster() (err error) {
  42. i.Poster, err = GetUserById(i.PosterId)
  43. if err == ErrUserNotExist {
  44. i.Poster = &User{Name: "FakeUser"}
  45. return nil
  46. }
  47. return err
  48. }
  49. func (i *Issue) GetAssignee() (err error) {
  50. if i.AssigneeId == 0 {
  51. return nil
  52. }
  53. i.Assignee, err = GetUserById(i.AssigneeId)
  54. return err
  55. }
  56. // CreateIssue creates new issue for repository.
  57. func NewIssue(issue *Issue) (err error) {
  58. sess := orm.NewSession()
  59. defer sess.Close()
  60. if err = sess.Begin(); err != nil {
  61. return err
  62. }
  63. if _, err = sess.Insert(issue); err != nil {
  64. sess.Rollback()
  65. return err
  66. }
  67. rawSql := "UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?"
  68. if _, err = sess.Exec(rawSql, issue.RepoId); err != nil {
  69. sess.Rollback()
  70. return err
  71. }
  72. return sess.Commit()
  73. }
  74. // GetIssueByIndex returns issue by given index in repository.
  75. func GetIssueByIndex(rid, index int64) (*Issue, error) {
  76. issue := &Issue{RepoId: rid, Index: index}
  77. has, err := orm.Get(issue)
  78. if err != nil {
  79. return nil, err
  80. } else if !has {
  81. return nil, ErrIssueNotExist
  82. }
  83. return issue, nil
  84. }
  85. // GetIssueById returns an issue by ID.
  86. func GetIssueById(id int64) (*Issue, error) {
  87. issue := &Issue{Id: id}
  88. has, err := orm.Get(issue)
  89. if err != nil {
  90. return nil, err
  91. } else if !has {
  92. return nil, ErrIssueNotExist
  93. }
  94. return issue, nil
  95. }
  96. // GetIssues returns a list of issues by given conditions.
  97. func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labels, sortType string) ([]Issue, error) {
  98. sess := orm.Limit(20, (page-1)*20)
  99. if rid > 0 {
  100. sess.Where("repo_id=?", rid).And("is_closed=?", isClosed)
  101. } else {
  102. sess.Where("is_closed=?", isClosed)
  103. }
  104. if uid > 0 {
  105. sess.And("assignee_id=?", uid)
  106. } else if pid > 0 {
  107. sess.And("poster_id=?", pid)
  108. }
  109. if mid > 0 {
  110. sess.And("milestone_id=?", mid)
  111. }
  112. if len(labels) > 0 {
  113. for _, label := range strings.Split(labels, ",") {
  114. sess.And("labels like '%$" + label + "|%'")
  115. }
  116. }
  117. switch sortType {
  118. case "oldest":
  119. sess.Asc("created")
  120. case "recentupdate":
  121. sess.Desc("updated")
  122. case "leastupdate":
  123. sess.Asc("updated")
  124. case "mostcomment":
  125. sess.Desc("num_comments")
  126. case "leastcomment":
  127. sess.Asc("num_comments")
  128. case "priority":
  129. sess.Desc("priority")
  130. default:
  131. sess.Desc("created")
  132. }
  133. var issues []Issue
  134. err := sess.Find(&issues)
  135. return issues, err
  136. }
  137. // GetIssueCountByPoster returns number of issues of repository by poster.
  138. func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
  139. count, _ := orm.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue))
  140. return count
  141. }
  142. // IssueUser represents an issue-user relation.
  143. type IssueUser struct {
  144. Id int64
  145. Uid int64 // User ID.
  146. IssueId int64
  147. RepoId int64
  148. MilestoneId int64
  149. Labels string `xorm:"TEXT"`
  150. IsRead bool
  151. IsAssigned bool
  152. IsMentioned bool
  153. IsPoster bool
  154. IsClosed bool
  155. }
  156. // NewIssueUserPairs adds new issue-user pairs for new issue of repository.
  157. func NewIssueUserPairs(rid, iid, oid, pid, aid int64, repoName string) (err error) {
  158. iu := &IssueUser{IssueId: iid, RepoId: rid}
  159. us, err := GetCollaborators(repoName)
  160. if err != nil {
  161. return err
  162. }
  163. isNeedAddPoster := true
  164. for _, u := range us {
  165. iu.Uid = u.Id
  166. iu.IsPoster = iu.Uid == pid
  167. if isNeedAddPoster && iu.IsPoster {
  168. isNeedAddPoster = false
  169. }
  170. iu.IsAssigned = iu.Uid == aid
  171. if _, err = orm.Insert(iu); err != nil {
  172. return err
  173. }
  174. }
  175. if isNeedAddPoster {
  176. iu.Uid = pid
  177. iu.IsPoster = true
  178. iu.IsAssigned = iu.Uid == aid
  179. if _, err = orm.Insert(iu); err != nil {
  180. return err
  181. }
  182. }
  183. return nil
  184. }
  185. // PairsContains returns true when pairs list contains given issue.
  186. func PairsContains(ius []*IssueUser, issueId int64) int {
  187. for i := range ius {
  188. if ius[i].IssueId == issueId {
  189. return i
  190. }
  191. }
  192. return -1
  193. }
  194. // GetIssueUserPairs returns issue-user pairs by given repository and user.
  195. func GetIssueUserPairs(rid, uid int64, isClosed bool) ([]*IssueUser, error) {
  196. ius := make([]*IssueUser, 0, 10)
  197. err := orm.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoId: rid, Uid: uid})
  198. return ius, err
  199. }
  200. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
  201. func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
  202. buf := bytes.NewBufferString("")
  203. for _, rid := range rids {
  204. buf.WriteString("repo_id=")
  205. buf.WriteString(base.ToStr(rid))
  206. buf.WriteString(" OR ")
  207. }
  208. cond := strings.TrimSuffix(buf.String(), " OR ")
  209. ius := make([]*IssueUser, 0, 10)
  210. sess := orm.Limit(20, (page-1)*20).Where("is_closed=?", isClosed)
  211. if len(cond) > 0 {
  212. sess.And(cond)
  213. }
  214. err := sess.Find(&ius)
  215. return ius, err
  216. }
  217. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  218. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  219. ius := make([]*IssueUser, 0, 10)
  220. sess := orm.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  221. if rid > 0 {
  222. sess.And("repo_id=?", rid)
  223. }
  224. switch filterMode {
  225. case FM_ASSIGN:
  226. sess.And("is_assigned=?", true)
  227. case FM_CREATE:
  228. sess.And("is_poster=?", true)
  229. default:
  230. return ius, nil
  231. }
  232. err := sess.Find(&ius)
  233. return ius, err
  234. }
  235. // IssueStats represents issue statistic information.
  236. type IssueStats struct {
  237. OpenCount, ClosedCount int64
  238. AllCount int64
  239. AssignCount int64
  240. CreateCount int64
  241. MentionCount int64
  242. }
  243. // Filter modes.
  244. const (
  245. FM_ASSIGN = iota + 1
  246. FM_CREATE
  247. FM_MENTION
  248. )
  249. // GetIssueStats returns issue statistic information by given conditions.
  250. func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStats {
  251. stats := &IssueStats{}
  252. issue := new(Issue)
  253. tmpSess := &xorm.Session{}
  254. sess := orm.Where("repo_id=?", rid)
  255. *tmpSess = *sess
  256. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  257. *tmpSess = *sess
  258. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  259. if isShowClosed {
  260. stats.AllCount = stats.ClosedCount
  261. } else {
  262. stats.AllCount = stats.OpenCount
  263. }
  264. if filterMode != FM_MENTION {
  265. sess = orm.Where("repo_id=?", rid)
  266. switch filterMode {
  267. case FM_ASSIGN:
  268. sess.And("assignee_id=?", uid)
  269. case FM_CREATE:
  270. sess.And("poster_id=?", uid)
  271. default:
  272. goto nofilter
  273. }
  274. *tmpSess = *sess
  275. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  276. *tmpSess = *sess
  277. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  278. } else {
  279. sess := orm.Where("repo_id=?", rid).And("uid=?", uid).And("is_mentioned=?", true)
  280. *tmpSess = *sess
  281. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(new(IssueUser))
  282. *tmpSess = *sess
  283. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(new(IssueUser))
  284. }
  285. nofilter:
  286. stats.AssignCount, _ = orm.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("assignee_id=?", uid).Count(issue)
  287. stats.CreateCount, _ = orm.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("poster_id=?", uid).Count(issue)
  288. stats.MentionCount, _ = orm.Where("repo_id=?", rid).And("uid=?", uid).And("is_closed=?", isShowClosed).And("is_mentioned=?", true).Count(new(IssueUser))
  289. return stats
  290. }
  291. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  292. func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
  293. stats := &IssueStats{}
  294. issue := new(Issue)
  295. stats.AssignCount, _ = orm.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue)
  296. stats.CreateCount, _ = orm.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue)
  297. return stats
  298. }
  299. // UpdateIssue updates information of issue.
  300. func UpdateIssue(issue *Issue) error {
  301. _, err := orm.Id(issue.Id).AllCols().Update(issue)
  302. return err
  303. }
  304. // UpdateIssueUserByStatus updates issue-user pairs by issue status.
  305. func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error {
  306. rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?"
  307. _, err := orm.Exec(rawSql, isClosed, iid)
  308. return err
  309. }
  310. // UpdateIssueUserPairByAssignee updates issue-user pair for assigning.
  311. func UpdateIssueUserPairByAssignee(aid, iid int64) error {
  312. rawSql := "UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?"
  313. if _, err := orm.Exec(rawSql, false, iid); err != nil {
  314. return err
  315. }
  316. // Assignee ID equals to 0 means clear assignee.
  317. if aid == 0 {
  318. return nil
  319. }
  320. rawSql = "UPDATE `issue_user` SET is_assigned = true WHERE uid = ? AND issue_id = ?"
  321. _, err := orm.Exec(rawSql, aid, iid)
  322. return err
  323. }
  324. // UpdateIssueUserPairByRead updates issue-user pair for reading.
  325. func UpdateIssueUserPairByRead(uid, iid int64) error {
  326. rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?"
  327. _, err := orm.Exec(rawSql, true, uid, iid)
  328. return err
  329. }
  330. // UpdateIssueUserPairsByMentions updates issue-user pairs by mentioning.
  331. func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error {
  332. for _, uid := range uids {
  333. iu := &IssueUser{Uid: uid, IssueId: iid}
  334. has, err := orm.Get(iu)
  335. if err != nil {
  336. return err
  337. }
  338. iu.IsMentioned = true
  339. if has {
  340. _, err = orm.Id(iu.Id).AllCols().Update(iu)
  341. } else {
  342. _, err = orm.Insert(iu)
  343. }
  344. if err != nil {
  345. return err
  346. }
  347. }
  348. return nil
  349. }
  350. // Label represents a label of repository for issues.
  351. type Label struct {
  352. Id int64
  353. RepoId int64 `xorm:"INDEX"`
  354. Name string
  355. Color string
  356. NumIssues int
  357. NumClosedIssues int
  358. NumOpenIssues int `xorm:"-"`
  359. }
  360. // Milestone represents a milestone of repository.
  361. type Milestone struct {
  362. Id int64
  363. RepoId int64 `xorm:"INDEX"`
  364. Index int64
  365. Name string
  366. Content string
  367. RenderedContent string `xorm:"-"`
  368. IsClosed bool
  369. NumIssues int
  370. NumClosedIssues int
  371. NumOpenIssues int `xorm:"-"`
  372. Completeness int // Percentage(1-100).
  373. Deadline time.Time
  374. DeadlineString string `xorm:"-"`
  375. ClosedDate time.Time
  376. }
  377. // CalOpenIssues calculates the open issues of milestone.
  378. func (m *Milestone) CalOpenIssues() {
  379. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  380. }
  381. // NewMilestone creates new milestone of repository.
  382. func NewMilestone(m *Milestone) (err error) {
  383. sess := orm.NewSession()
  384. defer sess.Close()
  385. if err = sess.Begin(); err != nil {
  386. return err
  387. }
  388. if _, err = sess.Insert(m); err != nil {
  389. sess.Rollback()
  390. return err
  391. }
  392. rawSql := "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?"
  393. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  394. sess.Rollback()
  395. return err
  396. }
  397. return sess.Commit()
  398. }
  399. // GetMilestoneById returns the milestone by given ID.
  400. func GetMilestoneById(id int64) (*Milestone, error) {
  401. m := &Milestone{Id: id}
  402. has, err := orm.Get(m)
  403. if err != nil {
  404. return nil, err
  405. } else if !has {
  406. return nil, ErrMilestoneNotExist
  407. }
  408. return m, nil
  409. }
  410. // GetMilestoneByIndex returns the milestone of given repository and index.
  411. func GetMilestoneByIndex(repoId, idx int64) (*Milestone, error) {
  412. m := &Milestone{RepoId: repoId, Index: idx}
  413. has, err := orm.Get(m)
  414. if err != nil {
  415. return nil, err
  416. } else if !has {
  417. return nil, ErrMilestoneNotExist
  418. }
  419. return m, nil
  420. }
  421. // GetMilestones returns a list of milestones of given repository and status.
  422. func GetMilestones(repoId int64, isClosed bool) ([]*Milestone, error) {
  423. miles := make([]*Milestone, 0, 10)
  424. err := orm.Where("repo_id=?", repoId).And("is_closed=?", isClosed).Find(&miles)
  425. return miles, err
  426. }
  427. // UpdateMilestone updates information of given milestone.
  428. func UpdateMilestone(m *Milestone) error {
  429. _, err := orm.Id(m.Id).Update(m)
  430. return err
  431. }
  432. // ChangeMilestoneStatus changes the milestone open/closed status.
  433. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  434. repo, err := GetRepositoryById(m.RepoId)
  435. if err != nil {
  436. return err
  437. }
  438. sess := orm.NewSession()
  439. defer sess.Close()
  440. if err = sess.Begin(); err != nil {
  441. return err
  442. }
  443. m.IsClosed = isClosed
  444. if _, err = sess.Id(m.Id).AllCols().Update(m); err != nil {
  445. sess.Rollback()
  446. return err
  447. }
  448. if isClosed {
  449. repo.NumClosedMilestones++
  450. } else {
  451. repo.NumClosedMilestones--
  452. }
  453. if _, err = sess.Id(repo.Id).Update(repo); err != nil {
  454. sess.Rollback()
  455. return err
  456. }
  457. return sess.Commit()
  458. }
  459. // ChangeMilestoneAssign changes assignment of milestone for issue.
  460. func ChangeMilestoneAssign(oldMid, mid int64, isIssueClosed bool) (err error) {
  461. sess := orm.NewSession()
  462. defer sess.Close()
  463. if err = sess.Begin(); err != nil {
  464. return err
  465. }
  466. if oldMid > 0 {
  467. m, err := GetMilestoneById(oldMid)
  468. if err != nil {
  469. return err
  470. }
  471. m.NumIssues--
  472. if isIssueClosed {
  473. m.NumClosedIssues--
  474. }
  475. if m.NumIssues > 0 {
  476. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  477. } else {
  478. m.Completeness = 0
  479. }
  480. if _, err = sess.Id(m.Id).Update(m); err != nil {
  481. sess.Rollback()
  482. return err
  483. }
  484. }
  485. if mid > 0 {
  486. m, err := GetMilestoneById(mid)
  487. if err != nil {
  488. return err
  489. }
  490. m.NumIssues++
  491. if isIssueClosed {
  492. m.NumClosedIssues++
  493. }
  494. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  495. if _, err = sess.Id(m.Id).Update(m); err != nil {
  496. sess.Rollback()
  497. return err
  498. }
  499. }
  500. return sess.Commit()
  501. }
  502. // DeleteMilestone deletes a milestone.
  503. func DeleteMilestone(m *Milestone) (err error) {
  504. sess := orm.NewSession()
  505. defer sess.Close()
  506. if err = sess.Begin(); err != nil {
  507. return err
  508. }
  509. if _, err = sess.Delete(m); err != nil {
  510. sess.Rollback()
  511. return err
  512. }
  513. rawSql := "UPDATE `repository` SET num_milestones = num_milestones - 1 WHERE id = ?"
  514. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  515. sess.Rollback()
  516. return err
  517. }
  518. rawSql = "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?"
  519. if _, err = sess.Exec(rawSql, m.Id); err != nil {
  520. sess.Rollback()
  521. return err
  522. }
  523. return sess.Commit()
  524. }
  525. // Issue types.
  526. const (
  527. IT_PLAIN = iota // Pure comment.
  528. IT_REOPEN // Issue reopen status change prompt.
  529. IT_CLOSE // Issue close status change prompt.
  530. )
  531. // Comment represents a comment in commit and issue page.
  532. type Comment struct {
  533. Id int64
  534. Type int
  535. PosterId int64
  536. Poster *User `xorm:"-"`
  537. IssueId int64
  538. CommitId int64
  539. Line int64
  540. Content string
  541. Created time.Time `xorm:"CREATED"`
  542. }
  543. // CreateComment creates comment of issue or commit.
  544. func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error {
  545. sess := orm.NewSession()
  546. defer sess.Close()
  547. if err := sess.Begin(); err != nil {
  548. return err
  549. }
  550. if _, err := sess.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
  551. CommitId: commitId, Line: line, Content: content}); err != nil {
  552. sess.Rollback()
  553. return err
  554. }
  555. // Check comment type.
  556. switch cmtType {
  557. case IT_PLAIN:
  558. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  559. if _, err := sess.Exec(rawSql, issueId); err != nil {
  560. sess.Rollback()
  561. return err
  562. }
  563. case IT_REOPEN:
  564. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
  565. if _, err := sess.Exec(rawSql, repoId); err != nil {
  566. sess.Rollback()
  567. return err
  568. }
  569. case IT_CLOSE:
  570. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
  571. if _, err := sess.Exec(rawSql, repoId); err != nil {
  572. sess.Rollback()
  573. return err
  574. }
  575. }
  576. return sess.Commit()
  577. }
  578. // GetIssueComments returns list of comment by given issue id.
  579. func GetIssueComments(issueId int64) ([]Comment, error) {
  580. comments := make([]Comment, 0, 10)
  581. err := orm.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  582. return comments, err
  583. }