issue.go 3.9 KB

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