issue.go 4.7 KB

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