issue.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "strings"
  7. "time"
  8. "github.com/gogits/gogs/modules/base"
  9. )
  10. // Issue represents an issue or pull request of repository.
  11. type Issue struct {
  12. Id int64
  13. Index int64 // Index in one repository.
  14. Name string
  15. RepoId int64 `xorm:"index"`
  16. PosterId int64
  17. MilestoneId int64
  18. AssigneeId int64
  19. IsPull bool // Indicates whether is a pull request or not.
  20. IsClosed bool
  21. Labels string
  22. Mentions string
  23. Content string
  24. NumComments int
  25. Created time.Time `xorm:"created"`
  26. Updated time.Time `xorm:"updated"`
  27. }
  28. // CreateIssue creates new issue for repository.
  29. func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, mentions, content string, isPull bool) error {
  30. count, err := GetIssueCount(repoId)
  31. if err != nil {
  32. return err
  33. }
  34. _, err = orm.Insert(&Issue{
  35. Index: count + 1,
  36. Name: name,
  37. RepoId: repoId,
  38. PosterId: userId,
  39. MilestoneId: milestoneId,
  40. AssigneeId: assigneeId,
  41. IsPull: isPull,
  42. Labels: labels,
  43. Mentions: mentions,
  44. Content: content,
  45. })
  46. return err
  47. }
  48. // GetIssueCount returns count of issues in the repository.
  49. func GetIssueCount(repoId int64) (int64, error) {
  50. return orm.Count(&Issue{RepoId: repoId})
  51. }
  52. // GetIssues returns a list of issues by given conditions.
  53. func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, isMention bool, labels, sortType string) ([]Issue, error) {
  54. sess := orm.Limit(20, (page-1)*20).Where("repo_id=?", repoId).And("is_closed=?", isClosed)
  55. if userId > 0 {
  56. sess = sess.And("assignee_id=?", userId)
  57. } else if posterId > 0 {
  58. sess = sess.And("poster_id=?", posterId)
  59. } else if isMention {
  60. sess = sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
  61. }
  62. if milestoneId > 0 {
  63. sess = sess.And("milestone_id=?", milestoneId)
  64. }
  65. if len(labels) > 0 {
  66. for _, label := range strings.Split(labels, ",") {
  67. sess = sess.And("mentions like '%$" + label + "|%'")
  68. }
  69. }
  70. switch sortType {
  71. case "oldest":
  72. sess = sess.Asc("created")
  73. case "recentupdate":
  74. sess = sess.Desc("updated")
  75. case "leastupdate":
  76. sess = sess.Asc("updated")
  77. case "mostcomment":
  78. sess = sess.Desc("num_comments")
  79. case "leastcomment":
  80. sess = sess.Asc("num_comments")
  81. default:
  82. sess = sess.Desc("created")
  83. }
  84. var issues []Issue
  85. err := sess.Find(&issues)
  86. return issues, err
  87. }
  88. // Label represents a list of labels of repository for issues.
  89. type Label struct {
  90. Id int64
  91. RepoId int64 `xorm:"index"`
  92. Names string
  93. Colors string
  94. }
  95. // Milestone represents a milestone of repository.
  96. type Milestone struct {
  97. Id int64
  98. Name string
  99. RepoId int64 `xorm:"index"`
  100. IsClosed bool
  101. Content string
  102. NumIssues int
  103. DueDate time.Time
  104. Created time.Time `xorm:"created"`
  105. }
  106. // Comment represents a comment in commit and issue page.
  107. type Comment struct {
  108. Id int64
  109. PosterId int64
  110. IssueId int64
  111. CommitId int64
  112. Line int
  113. Content string
  114. Created time.Time `xorm:"created"`
  115. }