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

issue.go 14 kB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. IsRead bool
  149. IsAssigned bool
  150. IsMentioned bool
  151. IsPoster bool
  152. IsClosed bool
  153. }
  154. // NewIssueUserPairs adds new issue-user pairs for new issue of repository.
  155. func NewIssueUserPairs(rid, iid, oid, pid, aid int64, repoName string) (err error) {
  156. iu := &IssueUser{IssueId: iid, RepoId: rid}
  157. us, err := GetCollaborators(repoName)
  158. if err != nil {
  159. return err
  160. }
  161. isNeedAddPoster := true
  162. for _, u := range us {
  163. iu.Uid = u.Id
  164. iu.IsPoster = iu.Uid == pid
  165. if isNeedAddPoster && iu.IsPoster {
  166. isNeedAddPoster = false
  167. }
  168. iu.IsAssigned = iu.Uid == aid
  169. if _, err = orm.Insert(iu); err != nil {
  170. return err
  171. }
  172. }
  173. if isNeedAddPoster {
  174. iu.Uid = pid
  175. iu.IsPoster = true
  176. iu.IsAssigned = iu.Uid == aid
  177. if _, err = orm.Insert(iu); err != nil {
  178. return err
  179. }
  180. }
  181. return nil
  182. }
  183. // PairsContains returns true when pairs list contains given issue.
  184. func PairsContains(ius []*IssueUser, issueId int64) int {
  185. for i := range ius {
  186. if ius[i].IssueId == issueId {
  187. return i
  188. }
  189. }
  190. return -1
  191. }
  192. // GetIssueUserPairs returns issue-user pairs by given repository and user.
  193. func GetIssueUserPairs(rid, uid int64, isClosed bool) ([]*IssueUser, error) {
  194. ius := make([]*IssueUser, 0, 10)
  195. err := orm.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoId: rid, Uid: uid})
  196. return ius, err
  197. }
  198. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
  199. func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
  200. buf := bytes.NewBufferString("")
  201. for _, rid := range rids {
  202. buf.WriteString("repo_id=")
  203. buf.WriteString(base.ToStr(rid))
  204. buf.WriteString(" OR ")
  205. }
  206. cond := strings.TrimSuffix(buf.String(), " OR ")
  207. ius := make([]*IssueUser, 0, 10)
  208. sess := orm.Limit(20, (page-1)*20).Where("is_closed=?", isClosed)
  209. if len(cond) > 0 {
  210. sess.And(cond)
  211. }
  212. err := sess.Find(&ius)
  213. return ius, err
  214. }
  215. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  216. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  217. ius := make([]*IssueUser, 0, 10)
  218. sess := orm.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  219. if rid > 0 {
  220. sess.And("repo_id=?", rid)
  221. }
  222. switch filterMode {
  223. case FM_ASSIGN:
  224. sess.And("is_assigned=?", true)
  225. case FM_CREATE:
  226. sess.And("is_poster=?", true)
  227. default:
  228. return ius, nil
  229. }
  230. err := sess.Find(&ius)
  231. return ius, err
  232. }
  233. // IssueStats represents issue statistic information.
  234. type IssueStats struct {
  235. OpenCount, ClosedCount int64
  236. AllCount int64
  237. AssignCount int64
  238. CreateCount int64
  239. MentionCount int64
  240. }
  241. // Filter modes.
  242. const (
  243. FM_ASSIGN = iota + 1
  244. FM_CREATE
  245. FM_MENTION
  246. )
  247. // GetIssueStats returns issue statistic information by given conditions.
  248. func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStats {
  249. stats := &IssueStats{}
  250. issue := new(Issue)
  251. tmpSess := &xorm.Session{}
  252. sess := orm.Where("repo_id=?", rid)
  253. *tmpSess = *sess
  254. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  255. *tmpSess = *sess
  256. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  257. if isShowClosed {
  258. stats.AllCount = stats.ClosedCount
  259. } else {
  260. stats.AllCount = stats.OpenCount
  261. }
  262. if filterMode != FM_MENTION {
  263. sess = orm.Where("repo_id=?", rid)
  264. switch filterMode {
  265. case FM_ASSIGN:
  266. sess.And("assignee_id=?", uid)
  267. case FM_CREATE:
  268. sess.And("poster_id=?", uid)
  269. default:
  270. goto nofilter
  271. }
  272. *tmpSess = *sess
  273. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue)
  274. *tmpSess = *sess
  275. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue)
  276. } else {
  277. sess := orm.Where("repo_id=?", rid).And("uid=?", uid).And("is_mentioned=?", true)
  278. *tmpSess = *sess
  279. stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(new(IssueUser))
  280. *tmpSess = *sess
  281. stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(new(IssueUser))
  282. }
  283. nofilter:
  284. stats.AssignCount, _ = orm.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("assignee_id=?", uid).Count(issue)
  285. stats.CreateCount, _ = orm.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("poster_id=?", uid).Count(issue)
  286. stats.MentionCount, _ = orm.Where("repo_id=?", rid).And("uid=?", uid).And("is_closed=?", isShowClosed).And("is_mentioned=?", true).Count(new(IssueUser))
  287. return stats
  288. }
  289. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  290. func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
  291. stats := &IssueStats{}
  292. issue := new(Issue)
  293. stats.AssignCount, _ = orm.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue)
  294. stats.CreateCount, _ = orm.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue)
  295. return stats
  296. }
  297. // UpdateIssue updates information of issue.
  298. func UpdateIssue(issue *Issue) error {
  299. _, err := orm.Id(issue.Id).AllCols().Update(issue)
  300. return err
  301. }
  302. // UpdateIssueUserByStatus updates issue-user pairs by issue status.
  303. func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error {
  304. rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?"
  305. _, err := orm.Exec(rawSql, isClosed, iid)
  306. return err
  307. }
  308. // UpdateIssueUserPairByAssignee updates issue-user pair for assigning.
  309. func UpdateIssueUserPairByAssignee(aid, iid int64) error {
  310. rawSql := "UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?"
  311. if _, err := orm.Exec(rawSql, false, iid); err != nil {
  312. return err
  313. }
  314. // Assignee ID equals to 0 means clear assignee.
  315. if aid == 0 {
  316. return nil
  317. }
  318. rawSql = "UPDATE `issue_user` SET is_assigned = true WHERE uid = ? AND issue_id = ?"
  319. _, err := orm.Exec(rawSql, aid, iid)
  320. return err
  321. }
  322. // UpdateIssueUserPairByRead updates issue-user pair for reading.
  323. func UpdateIssueUserPairByRead(uid, iid int64) error {
  324. rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?"
  325. _, err := orm.Exec(rawSql, true, uid, iid)
  326. return err
  327. }
  328. // UpdateIssueUserPairsByMentions updates issue-user pairs by mentioning.
  329. func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error {
  330. for _, uid := range uids {
  331. iu := &IssueUser{Uid: uid, IssueId: iid}
  332. has, err := orm.Get(iu)
  333. if err != nil {
  334. return err
  335. }
  336. iu.IsMentioned = true
  337. if has {
  338. _, err = orm.Id(iu.Id).AllCols().Update(iu)
  339. } else {
  340. _, err = orm.Insert(iu)
  341. }
  342. if err != nil {
  343. return err
  344. }
  345. }
  346. return nil
  347. }
  348. // Label represents a label of repository for issues.
  349. type Label struct {
  350. Id int64
  351. RepoId int64 `xorm:"INDEX"`
  352. Name string
  353. Color string
  354. NumIssues int
  355. NumClosedIssues int
  356. NumOpenIssues int `xorm:"-"`
  357. }
  358. // Milestone represents a milestone of repository.
  359. type Milestone struct {
  360. Id int64
  361. RepoId int64 `xorm:"INDEX"`
  362. Index int64
  363. Name string
  364. Content string
  365. RenderedContent string `xorm:"-"`
  366. IsClosed bool
  367. NumIssues int
  368. NumClosedIssues int
  369. NumOpenIssues int `xorm:"-"`
  370. Completeness int // Percentage(1-100).
  371. Deadline time.Time
  372. DeadlineString string `xorm:"-"`
  373. ClosedDate time.Time
  374. }
  375. // CalOpenIssues calculates the open issues of milestone.
  376. func (m *Milestone) CalOpenIssues() {
  377. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  378. }
  379. // NewMilestone creates new milestone of repository.
  380. func NewMilestone(m *Milestone) (err error) {
  381. sess := orm.NewSession()
  382. defer sess.Close()
  383. if err = sess.Begin(); err != nil {
  384. return err
  385. }
  386. if _, err = sess.Insert(m); err != nil {
  387. sess.Rollback()
  388. return err
  389. }
  390. rawSql := "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?"
  391. if _, err = sess.Exec(rawSql, m.RepoId); err != nil {
  392. sess.Rollback()
  393. return err
  394. }
  395. return sess.Commit()
  396. }
  397. // GetMilestoneByIndex returns the milestone of given repository and index.
  398. func GetMilestoneByIndex(repoId, idx int64) (*Milestone, error) {
  399. m := &Milestone{RepoId: repoId, Index: idx}
  400. has, err := orm.Get(m)
  401. if err != nil {
  402. return nil, err
  403. } else if !has {
  404. return nil, ErrMilestoneNotExist
  405. }
  406. return m, nil
  407. }
  408. // GetMilestones returns a list of milestones of given repository and status.
  409. func GetMilestones(repoId int64, isClosed bool) ([]*Milestone, error) {
  410. miles := make([]*Milestone, 0, 10)
  411. err := orm.Where("repo_id=?", repoId).And("is_closed=?", isClosed).Find(&miles)
  412. return miles, err
  413. }
  414. // UpdateMilestone updates information of given milestone.
  415. func UpdateMilestone(m *Milestone) error {
  416. _, err := orm.Id(m.Id).Update(m)
  417. return err
  418. }
  419. // Issue types.
  420. const (
  421. IT_PLAIN = iota // Pure comment.
  422. IT_REOPEN // Issue reopen status change prompt.
  423. IT_CLOSE // Issue close status change prompt.
  424. )
  425. // Comment represents a comment in commit and issue page.
  426. type Comment struct {
  427. Id int64
  428. Type int
  429. PosterId int64
  430. Poster *User `xorm:"-"`
  431. IssueId int64
  432. CommitId int64
  433. Line int64
  434. Content string
  435. Created time.Time `xorm:"CREATED"`
  436. }
  437. // CreateComment creates comment of issue or commit.
  438. func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error {
  439. sess := orm.NewSession()
  440. defer sess.Close()
  441. if err := sess.Begin(); err != nil {
  442. return err
  443. }
  444. if _, err := sess.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
  445. CommitId: commitId, Line: line, Content: content}); err != nil {
  446. sess.Rollback()
  447. return err
  448. }
  449. // Check comment type.
  450. switch cmtType {
  451. case IT_PLAIN:
  452. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  453. if _, err := sess.Exec(rawSql, issueId); err != nil {
  454. sess.Rollback()
  455. return err
  456. }
  457. case IT_REOPEN:
  458. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
  459. if _, err := sess.Exec(rawSql, repoId); err != nil {
  460. sess.Rollback()
  461. return err
  462. }
  463. case IT_CLOSE:
  464. rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
  465. if _, err := sess.Exec(rawSql, repoId); err != nil {
  466. sess.Rollback()
  467. return err
  468. }
  469. }
  470. return sess.Commit()
  471. }
  472. // GetIssueComments returns list of comment by given issue id.
  473. func GetIssueComments(issueId int64) ([]Comment, error) {
  474. comments := make([]Comment, 0, 10)
  475. err := orm.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  476. return comments, err
  477. }