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.

158 lines
3.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
  75. } else {
  76. sess.Where("is_closed=?", isClosed)
  77. }
  78. if userId > 0 {
  79. sess.And("assignee_id=?", userId)
  80. } else if posterId > 0 {
  81. sess.And("poster_id=?", posterId)
  82. } else if isMention {
  83. sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
  84. }
  85. if milestoneId > 0 {
  86. sess.And("milestone_id=?", milestoneId)
  87. }
  88. if len(labels) > 0 {
  89. for _, label := range strings.Split(labels, ",") {
  90. sess.And("mentions like '%$" + label + "|%'")
  91. }
  92. }
  93. switch sortType {
  94. case "oldest":
  95. sess.Asc("created")
  96. case "recentupdate":
  97. sess.Desc("updated")
  98. case "leastupdate":
  99. sess.Asc("updated")
  100. case "mostcomment":
  101. sess.Desc("num_comments")
  102. case "leastcomment":
  103. sess.Asc("num_comments")
  104. default:
  105. 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. }