issue.go 13 KB

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