Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

pull_review.go 3.0KB

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