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.

pull_review.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2018 The Gitea 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. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. pull_service "code.gitea.io/gitea/services/pull"
  12. )
  13. // CreateCodeComment will create a code comment including an pending review if required
  14. func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
  15. issue := GetActionIssue(ctx)
  16. if !issue.IsPull {
  17. return
  18. }
  19. if ctx.Written() {
  20. return
  21. }
  22. if ctx.HasError() {
  23. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  24. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  25. return
  26. }
  27. signedLine := form.Line
  28. if form.Side == "previous" {
  29. signedLine *= -1
  30. }
  31. comment, err := pull_service.CreateCodeComment(
  32. ctx.User,
  33. issue,
  34. signedLine,
  35. form.Content,
  36. form.TreePath,
  37. form.IsReview,
  38. form.Reply,
  39. )
  40. if err != nil {
  41. ctx.ServerError("CreateCodeComment", err)
  42. return
  43. }
  44. log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
  45. if comment != nil {
  46. ctx.Redirect(comment.HTMLURL())
  47. } else {
  48. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  49. }
  50. }
  51. // SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
  52. func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
  53. issue := GetActionIssue(ctx)
  54. if !issue.IsPull {
  55. return
  56. }
  57. if ctx.Written() {
  58. return
  59. }
  60. if ctx.HasError() {
  61. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  62. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  63. return
  64. }
  65. reviewType := form.ReviewType()
  66. switch reviewType {
  67. case models.ReviewTypeUnknown:
  68. ctx.ServerError("ReviewType", fmt.Errorf("unknown ReviewType: %s", form.Type))
  69. return
  70. // can not approve/reject your own PR
  71. case models.ReviewTypeApprove, models.ReviewTypeReject:
  72. if issue.Poster.ID == ctx.User.ID {
  73. var translated string
  74. if reviewType == models.ReviewTypeApprove {
  75. translated = ctx.Tr("repo.issues.review.self.approval")
  76. } else {
  77. translated = ctx.Tr("repo.issues.review.self.rejection")
  78. }
  79. ctx.Flash.Error(translated)
  80. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  81. return
  82. }
  83. }
  84. _, comm, err := pull_service.SubmitReview(ctx.User, issue, reviewType, form.Content)
  85. if err != nil {
  86. if models.IsContentEmptyErr(err) {
  87. ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
  88. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  89. } else {
  90. ctx.ServerError("SubmitReview", err)
  91. }
  92. return
  93. }
  94. ctx.Redirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, issue.Index, comm.HashTag()))
  95. }