issue.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2016 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 repo
  5. import (
  6. "fmt"
  7. "strings"
  8. api "github.com/gogs/go-gogs-client"
  9. "github.com/gogs/gogs/models"
  10. "github.com/gogs/gogs/models/errors"
  11. "github.com/gogs/gogs/pkg/context"
  12. "github.com/gogs/gogs/pkg/setting"
  13. )
  14. func listIssues(c *context.APIContext, opts *models.IssuesOptions) {
  15. issues, err := models.Issues(opts)
  16. if err != nil {
  17. c.Error(500, "Issues", err)
  18. return
  19. }
  20. count, err := models.IssuesCount(opts)
  21. if err != nil {
  22. c.Error(500, "IssuesCount", err)
  23. return
  24. }
  25. // FIXME: use IssueList to improve performance.
  26. apiIssues := make([]*api.Issue, len(issues))
  27. for i := range issues {
  28. if err = issues[i].LoadAttributes(); err != nil {
  29. c.Error(500, "LoadAttributes", err)
  30. return
  31. }
  32. apiIssues[i] = issues[i].APIFormat()
  33. }
  34. c.SetLinkHeader(int(count), setting.UI.IssuePagingNum)
  35. c.JSON(200, &apiIssues)
  36. }
  37. func ListUserIssues(c *context.APIContext) {
  38. opts := models.IssuesOptions{
  39. AssigneeID: c.User.ID,
  40. Page: c.QueryInt("page"),
  41. IsClosed: api.StateType(c.Query("state")) == api.STATE_CLOSED,
  42. }
  43. listIssues(c, &opts)
  44. }
  45. func ListIssues(c *context.APIContext) {
  46. opts := models.IssuesOptions{
  47. RepoID: c.Repo.Repository.ID,
  48. Page: c.QueryInt("page"),
  49. IsClosed: api.StateType(c.Query("state")) == api.STATE_CLOSED,
  50. }
  51. listIssues(c, &opts)
  52. }
  53. func GetIssue(c *context.APIContext) {
  54. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  55. if err != nil {
  56. if errors.IsIssueNotExist(err) {
  57. c.Status(404)
  58. } else {
  59. c.Error(500, "GetIssueByIndex", err)
  60. }
  61. return
  62. }
  63. c.JSON(200, issue.APIFormat())
  64. }
  65. func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
  66. issue := &models.Issue{
  67. RepoID: c.Repo.Repository.ID,
  68. Title: form.Title,
  69. PosterID: c.User.ID,
  70. Poster: c.User,
  71. Content: form.Body,
  72. }
  73. if c.Repo.IsWriter() {
  74. if len(form.Assignee) > 0 {
  75. assignee, err := models.GetUserByName(form.Assignee)
  76. if err != nil {
  77. if errors.IsUserNotExist(err) {
  78. c.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", form.Assignee))
  79. } else {
  80. c.Error(500, "GetUserByName", err)
  81. }
  82. return
  83. }
  84. issue.AssigneeID = assignee.ID
  85. }
  86. issue.MilestoneID = form.Milestone
  87. } else {
  88. form.Labels = nil
  89. }
  90. if err := models.NewIssue(c.Repo.Repository, issue, form.Labels, nil); err != nil {
  91. c.Error(500, "NewIssue", err)
  92. return
  93. }
  94. if form.Closed {
  95. if err := issue.ChangeStatus(c.User, c.Repo.Repository, true); err != nil {
  96. c.Error(500, "ChangeStatus", err)
  97. return
  98. }
  99. }
  100. // Refetch from database to assign some automatic values
  101. var err error
  102. issue, err = models.GetIssueByID(issue.ID)
  103. if err != nil {
  104. c.Error(500, "GetIssueByID", err)
  105. return
  106. }
  107. c.JSON(201, issue.APIFormat())
  108. }
  109. func EditIssue(c *context.APIContext, form api.EditIssueOption) {
  110. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  111. if err != nil {
  112. if errors.IsIssueNotExist(err) {
  113. c.Status(404)
  114. } else {
  115. c.Error(500, "GetIssueByIndex", err)
  116. }
  117. return
  118. }
  119. if !issue.IsPoster(c.User.ID) && !c.Repo.IsWriter() {
  120. c.Status(403)
  121. return
  122. }
  123. if len(form.Title) > 0 {
  124. issue.Title = form.Title
  125. }
  126. if form.Body != nil {
  127. issue.Content = *form.Body
  128. }
  129. if c.Repo.IsWriter() && form.Assignee != nil &&
  130. (issue.Assignee == nil || issue.Assignee.LowerName != strings.ToLower(*form.Assignee)) {
  131. if len(*form.Assignee) == 0 {
  132. issue.AssigneeID = 0
  133. } else {
  134. assignee, err := models.GetUserByName(*form.Assignee)
  135. if err != nil {
  136. if errors.IsUserNotExist(err) {
  137. c.Error(422, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee))
  138. } else {
  139. c.Error(500, "GetUserByName", err)
  140. }
  141. return
  142. }
  143. issue.AssigneeID = assignee.ID
  144. }
  145. if err = models.UpdateIssueUserByAssignee(issue); err != nil {
  146. c.Error(500, "UpdateIssueUserByAssignee", err)
  147. return
  148. }
  149. }
  150. if c.Repo.IsWriter() && form.Milestone != nil &&
  151. issue.MilestoneID != *form.Milestone {
  152. oldMilestoneID := issue.MilestoneID
  153. issue.MilestoneID = *form.Milestone
  154. if err = models.ChangeMilestoneAssign(c.User, issue, oldMilestoneID); err != nil {
  155. c.Error(500, "ChangeMilestoneAssign", err)
  156. return
  157. }
  158. }
  159. if err = models.UpdateIssue(issue); err != nil {
  160. c.Error(500, "UpdateIssue", err)
  161. return
  162. }
  163. if form.State != nil {
  164. if err = issue.ChangeStatus(c.User, c.Repo.Repository, api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
  165. c.Error(500, "ChangeStatus", err)
  166. return
  167. }
  168. }
  169. // Refetch from database to assign some automatic values
  170. issue, err = models.GetIssueByID(issue.ID)
  171. if err != nil {
  172. c.Error(500, "GetIssueByID", err)
  173. return
  174. }
  175. c.JSON(201, issue.APIFormat())
  176. }