issue.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. "errors"
  7. "strings"
  8. "time"
  9. "github.com/gogits/gogs/modules/base"
  10. )
  11. var (
  12. ErrIssueNotExist = errors.New("Issue does not exist")
  13. )
  14. // Issue represents an issue or pull request of repository.
  15. type Issue struct {
  16. Id int64
  17. Index int64 // Index in one repository.
  18. Name string
  19. RepoId int64 `xorm:"index"`
  20. PosterId int64
  21. Poster *User `xorm:"-"`
  22. MilestoneId int64
  23. AssigneeId int64
  24. IsPull bool // Indicates whether is a pull request or not.
  25. IsClosed bool
  26. Labels string `xorm:"TEXT"`
  27. Mentions string `xorm:"TEXT"`
  28. Content string `xorm:"TEXT"`
  29. NumComments int
  30. Created time.Time `xorm:"created"`
  31. Updated time.Time `xorm:"updated"`
  32. }
  33. // CreateIssue creates new issue for repository.
  34. func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, name, labels, content string, isPull bool) (issue *Issue, err error) {
  35. // TODO: find out mentions
  36. mentions := ""
  37. sess := orm.NewSession()
  38. defer sess.Close()
  39. sess.Begin()
  40. issue = &Issue{
  41. Index: int64(issueCount) + 1,
  42. Name: name,
  43. RepoId: repoId,
  44. PosterId: userId,
  45. MilestoneId: milestoneId,
  46. AssigneeId: assigneeId,
  47. IsPull: isPull,
  48. Labels: labels,
  49. Mentions: mentions,
  50. Content: content,
  51. }
  52. if _, err = sess.Insert(issue); err != nil {
  53. sess.Rollback()
  54. return nil, err
  55. }
  56. rawSql := "UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?"
  57. if _, err = sess.Exec(rawSql, repoId); err != nil {
  58. sess.Rollback()
  59. return nil, err
  60. }
  61. if err = sess.Commit(); err != nil {
  62. sess.Rollback()
  63. return nil, err
  64. }
  65. return issue, nil
  66. }
  67. // GetIssueById returns issue object by given id.
  68. func GetIssueByIndex(repoId, index int64) (*Issue, error) {
  69. issue := &Issue{RepoId: repoId, Index: index}
  70. has, err := orm.Get(issue)
  71. if err != nil {
  72. return nil, err
  73. } else if !has {
  74. return nil, ErrIssueNotExist
  75. }
  76. return issue, nil
  77. }
  78. // GetIssues returns a list of issues by given conditions.
  79. func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, isMention bool, labels, sortType string) ([]Issue, error) {
  80. sess := orm.Limit(20, (page-1)*20)
  81. if repoId > 0 {
  82. sess.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
  83. } else {
  84. sess.Where("is_closed=?", isClosed)
  85. }
  86. if userId > 0 {
  87. sess.And("assignee_id=?", userId)
  88. } else if posterId > 0 {
  89. sess.And("poster_id=?", posterId)
  90. } else if isMention {
  91. sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
  92. }
  93. if milestoneId > 0 {
  94. sess.And("milestone_id=?", milestoneId)
  95. }
  96. if len(labels) > 0 {
  97. for _, label := range strings.Split(labels, ",") {
  98. sess.And("mentions like '%$" + label + "|%'")
  99. }
  100. }
  101. switch sortType {
  102. case "oldest":
  103. sess.Asc("created")
  104. case "recentupdate":
  105. sess.Desc("updated")
  106. case "leastupdate":
  107. sess.Asc("updated")
  108. case "mostcomment":
  109. sess.Desc("num_comments")
  110. case "leastcomment":
  111. sess.Asc("num_comments")
  112. default:
  113. sess.Desc("created")
  114. }
  115. var issues []Issue
  116. err := sess.Find(&issues)
  117. return issues, err
  118. }
  119. // GetUserIssueCount returns the number of issues that were created by given user in repository.
  120. func GetUserIssueCount(userId, repoId int64) int64 {
  121. count, _ := orm.Where("poster_id=?", userId).And("repo_id=?", repoId).Count(new(Issue))
  122. return count
  123. }
  124. // UpdateIssue updates information of issue.
  125. func UpdateIssue(issue *Issue) error {
  126. _, err := orm.Id(issue.Id).AllCols().Update(issue)
  127. return err
  128. }
  129. // Label represents a list of labels of repository for issues.
  130. type Label struct {
  131. Id int64
  132. RepoId int64 `xorm:"index"`
  133. Names string
  134. Colors string
  135. }
  136. // Milestone represents a milestone of repository.
  137. type Milestone struct {
  138. Id int64
  139. Name string
  140. RepoId int64 `xorm:"index"`
  141. IsClosed bool
  142. Content string
  143. NumIssues int
  144. DueDate time.Time
  145. Created time.Time `xorm:"created"`
  146. }
  147. // Comment represents a comment in commit and issue page.
  148. type Comment struct {
  149. Id int64
  150. PosterId int64
  151. Poster *User `xorm:"-"`
  152. IssueId int64
  153. CommitId int64
  154. Line int64
  155. Content string
  156. Created time.Time `xorm:"created"`
  157. }
  158. // CreateComment creates comment of issue or commit.
  159. func CreateComment(userId, issueId, commitId, line int64, content string) error {
  160. sess := orm.NewSession()
  161. defer sess.Close()
  162. sess.Begin()
  163. if _, err := orm.Insert(&Comment{PosterId: userId, IssueId: issueId,
  164. CommitId: commitId, Line: line, Content: content}); err != nil {
  165. sess.Rollback()
  166. return err
  167. }
  168. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  169. if _, err := sess.Exec(rawSql, issueId); err != nil {
  170. sess.Rollback()
  171. return err
  172. }
  173. return sess.Commit()
  174. }
  175. // GetIssueComments returns list of comment by given issue id.
  176. func GetIssueComments(issueId int64) ([]Comment, error) {
  177. comments := make([]Comment, 0, 10)
  178. err := orm.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  179. return comments, err
  180. }