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.

181 lines
4.5 KiB

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