issue.go 4.4 KB

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