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 5.2 kB

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