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

issue.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. // UpdateIssue updates information of issue.
  120. func UpdateIssue(issue *Issue) error {
  121. _, err := orm.Id(issue.Id).AllCols().Update(issue)
  122. return err
  123. }
  124. // Label represents a list of labels of repository for issues.
  125. type Label struct {
  126. Id int64
  127. RepoId int64 `xorm:"index"`
  128. Names string
  129. Colors string
  130. }
  131. // Milestone represents a milestone of repository.
  132. type Milestone struct {
  133. Id int64
  134. Name string
  135. RepoId int64 `xorm:"index"`
  136. IsClosed bool
  137. Content string
  138. NumIssues int
  139. DueDate time.Time
  140. Created time.Time `xorm:"created"`
  141. }
  142. // Comment represents a comment in commit and issue page.
  143. type Comment struct {
  144. Id int64
  145. PosterId int64
  146. Poster *User `xorm:"-"`
  147. IssueId int64
  148. CommitId int64
  149. Line int64
  150. Content string
  151. Created time.Time `xorm:"created"`
  152. }
  153. // CreateComment creates comment of issue or commit.
  154. func CreateComment(userId, issueId, commitId, line int64, content string) error {
  155. sess := orm.NewSession()
  156. defer sess.Close()
  157. sess.Begin()
  158. if _, err := orm.Insert(&Comment{PosterId: userId, IssueId: issueId,
  159. CommitId: commitId, Line: line, Content: content}); err != nil {
  160. sess.Rollback()
  161. return err
  162. }
  163. rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
  164. if _, err := sess.Exec(rawSql, issueId); err != nil {
  165. sess.Rollback()
  166. return err
  167. }
  168. return sess.Commit()
  169. }
  170. // GetIssueComments returns list of comment by given issue id.
  171. func GetIssueComments(issueId int64) ([]Comment, error) {
  172. comments := make([]Comment, 0, 10)
  173. err := orm.Asc("created").Find(&comments, &Comment{IssueId: issueId})
  174. return comments, err
  175. }