issue.go 12 KB

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