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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. ctx.Repo.GitRepo,
  34. issue,
  35. signedLine,
  36. form.Content,
  37. form.TreePath,
  38. form.IsReview,
  39. form.Reply,
  40. form.LatestCommitID,
  41. )
  42. if err != nil {
  43. ctx.ServerError("CreateCodeComment", err)
  44. return
  45. }
  46. log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
  47. if comment != nil {
  48. ctx.Redirect(comment.HTMLURL())
  49. } else {
  50. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  51. }
  52. }
  53. // SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
  54. func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
  55. issue := GetActionIssue(ctx)
  56. if !issue.IsPull {
  57. return
  58. }
  59. if ctx.Written() {
  60. return
  61. }
  62. if ctx.HasError() {
  63. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  64. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  65. return
  66. }
  67. reviewType := form.ReviewType()
  68. switch reviewType {
  69. case models.ReviewTypeUnknown:
  70. ctx.ServerError("ReviewType", fmt.Errorf("unknown ReviewType: %s", form.Type))
  71. return
  72. // can not approve/reject your own PR
  73. case models.ReviewTypeApprove, models.ReviewTypeReject:
  74. if issue.IsPoster(ctx.User.ID) {
  75. var translated string
  76. if reviewType == models.ReviewTypeApprove {
  77. translated = ctx.Tr("repo.issues.review.self.approval")
  78. } else {
  79. translated = ctx.Tr("repo.issues.review.self.rejection")
  80. }
  81. ctx.Flash.Error(translated)
  82. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  83. return
  84. }
  85. }
  86. _, comm, err := pull_service.SubmitReview(ctx.User, ctx.Repo.GitRepo, issue, reviewType, form.Content, form.CommitID)
  87. if err != nil {
  88. if models.IsContentEmptyErr(err) {
  89. ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
  90. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  91. } else {
  92. ctx.ServerError("SubmitReview", err)
  93. }
  94. return
  95. }
  96. ctx.Redirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, issue.Index, comm.HashTag()))
  97. }