issue.go 5.7 KB

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