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.

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