issue.go 4.1 KB

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