issue.go 4.7 KB

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