issue.go 5.0 KB

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