issue.go 4.7 KB

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