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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. MilestoneId int64
  22. AssigneeId int64
  23. IsPull bool // Indicates whether is a pull request or not.
  24. IsClosed bool
  25. Labels string `xorm:"TEXT"`
  26. Mentions string `xorm:"TEXT"`
  27. Content string `xorm:"TEXT"`
  28. NumComments int
  29. Created time.Time `xorm:"created"`
  30. Updated time.Time `xorm:"updated"`
  31. }
  32. // CreateIssue creates new issue for repository.
  33. func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, content string, isPull bool) (*Issue, error) {
  34. count, err := GetIssueCount(repoId)
  35. if err != nil {
  36. return nil, err
  37. }
  38. // TODO: find out mentions
  39. mentions := ""
  40. issue := &Issue{
  41. Index: count + 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. _, err = orm.Insert(issue)
  53. return issue, err
  54. }
  55. // GetIssueCount returns count of issues in the repository.
  56. func GetIssueCount(repoId int64) (int64, error) {
  57. return orm.Count(&Issue{RepoId: repoId})
  58. }
  59. // GetIssueById returns issue object by given id.
  60. func GetIssueById(id int64) (*Issue, error) {
  61. issue := new(Issue)
  62. has, err := orm.Id(id).Get(issue)
  63. if err != nil {
  64. return nil, err
  65. } else if !has {
  66. return nil, ErrIssueNotExist
  67. }
  68. return issue, nil
  69. }
  70. // GetIssues returns a list of issues by given conditions.
  71. func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, isMention bool, labels, sortType string) ([]Issue, error) {
  72. sess := orm.Limit(20, (page-1)*20)
  73. if repoId > 0 {
  74. sess = sess.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
  75. } else {
  76. sess = sess.Where("is_closed=?", isClosed)
  77. }
  78. if userId > 0 {
  79. sess = sess.And("assignee_id=?", userId)
  80. } else if posterId > 0 {
  81. sess = sess.And("poster_id=?", posterId)
  82. } else if isMention {
  83. sess = sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
  84. }
  85. if milestoneId > 0 {
  86. sess = sess.And("milestone_id=?", milestoneId)
  87. }
  88. if len(labels) > 0 {
  89. for _, label := range strings.Split(labels, ",") {
  90. sess = sess.And("mentions like '%$" + label + "|%'")
  91. }
  92. }
  93. switch sortType {
  94. case "oldest":
  95. sess = sess.Asc("created")
  96. case "recentupdate":
  97. sess = sess.Desc("updated")
  98. case "leastupdate":
  99. sess = sess.Asc("updated")
  100. case "mostcomment":
  101. sess = sess.Desc("num_comments")
  102. case "leastcomment":
  103. sess = sess.Asc("num_comments")
  104. default:
  105. sess = sess.Desc("created")
  106. }
  107. var issues []Issue
  108. err := sess.Find(&issues)
  109. return issues, err
  110. }
  111. // Label represents a list of labels of repository for issues.
  112. type Label struct {
  113. Id int64
  114. RepoId int64 `xorm:"index"`
  115. Names string
  116. Colors string
  117. }
  118. // Milestone represents a milestone of repository.
  119. type Milestone struct {
  120. Id int64
  121. Name string
  122. RepoId int64 `xorm:"index"`
  123. IsClosed bool
  124. Content string
  125. NumIssues int
  126. DueDate time.Time
  127. Created time.Time `xorm:"created"`
  128. }
  129. // Comment represents a comment in commit and issue page.
  130. type Comment struct {
  131. Id int64
  132. PosterId int64
  133. IssueId int64
  134. CommitId int64
  135. Line int
  136. Content string
  137. Created time.Time `xorm:"created"`
  138. }