issue.go 4.7 KB

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