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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package convert
  4. import (
  5. "context"
  6. "strings"
  7. issues_model "code.gitea.io/gitea/models/issues"
  8. user_model "code.gitea.io/gitea/models/user"
  9. api "code.gitea.io/gitea/modules/structs"
  10. )
  11. // ToPullReview convert a review to api format
  12. func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model.User) (*api.PullReview, error) {
  13. if err := r.LoadAttributes(ctx); err != nil {
  14. if !user_model.IsErrUserNotExist(err) {
  15. return nil, err
  16. }
  17. r.Reviewer = user_model.NewGhostUser()
  18. }
  19. result := &api.PullReview{
  20. ID: r.ID,
  21. Reviewer: ToUser(ctx, r.Reviewer, doer),
  22. State: api.ReviewStateUnknown,
  23. Body: r.Content,
  24. CommitID: r.CommitID,
  25. Stale: r.Stale,
  26. Official: r.Official,
  27. Dismissed: r.Dismissed,
  28. CodeCommentsCount: r.GetCodeCommentsCount(ctx),
  29. Submitted: r.CreatedUnix.AsTime(),
  30. Updated: r.UpdatedUnix.AsTime(),
  31. HTMLURL: r.HTMLURL(ctx),
  32. HTMLPullURL: r.Issue.HTMLURL(),
  33. }
  34. if r.ReviewerTeam != nil {
  35. var err error
  36. result.ReviewerTeam, err = ToTeam(ctx, r.ReviewerTeam)
  37. if err != nil {
  38. return nil, err
  39. }
  40. }
  41. switch r.Type {
  42. case issues_model.ReviewTypeApprove:
  43. result.State = api.ReviewStateApproved
  44. case issues_model.ReviewTypeReject:
  45. result.State = api.ReviewStateRequestChanges
  46. case issues_model.ReviewTypeComment:
  47. result.State = api.ReviewStateComment
  48. case issues_model.ReviewTypePending:
  49. result.State = api.ReviewStatePending
  50. case issues_model.ReviewTypeRequest:
  51. result.State = api.ReviewStateRequestReview
  52. }
  53. return result, nil
  54. }
  55. // ToPullReviewList convert a list of review to it's api format
  56. func ToPullReviewList(ctx context.Context, rl []*issues_model.Review, doer *user_model.User) ([]*api.PullReview, error) {
  57. result := make([]*api.PullReview, 0, len(rl))
  58. for i := range rl {
  59. // show pending reviews only for the user who created them
  60. if rl[i].Type == issues_model.ReviewTypePending && (doer == nil || (!doer.IsAdmin && doer.ID != rl[i].ReviewerID)) {
  61. continue
  62. }
  63. r, err := ToPullReview(ctx, rl[i], doer)
  64. if err != nil {
  65. return nil, err
  66. }
  67. result = append(result, r)
  68. }
  69. return result, nil
  70. }
  71. // ToPullReviewCommentList convert the CodeComments of an review to it's api format
  72. func ToPullReviewCommentList(ctx context.Context, review *issues_model.Review, doer *user_model.User) ([]*api.PullReviewComment, error) {
  73. if err := review.LoadAttributes(ctx); err != nil {
  74. if !user_model.IsErrUserNotExist(err) {
  75. return nil, err
  76. }
  77. review.Reviewer = user_model.NewGhostUser()
  78. }
  79. apiComments := make([]*api.PullReviewComment, 0, len(review.CodeComments))
  80. for _, lines := range review.CodeComments {
  81. for _, comments := range lines {
  82. for _, comment := range comments {
  83. apiComment := &api.PullReviewComment{
  84. ID: comment.ID,
  85. Body: comment.Content,
  86. Poster: ToUser(ctx, comment.Poster, doer),
  87. Resolver: ToUser(ctx, comment.ResolveDoer, doer),
  88. ReviewID: review.ID,
  89. Created: comment.CreatedUnix.AsTime(),
  90. Updated: comment.UpdatedUnix.AsTime(),
  91. Path: comment.TreePath,
  92. CommitID: comment.CommitSHA,
  93. OrigCommitID: comment.OldRef,
  94. DiffHunk: patch2diff(comment.Patch),
  95. HTMLURL: comment.HTMLURL(ctx),
  96. HTMLPullURL: review.Issue.HTMLURL(),
  97. }
  98. if comment.Line < 0 {
  99. apiComment.OldLineNum = comment.UnsignedLine()
  100. } else {
  101. apiComment.LineNum = comment.UnsignedLine()
  102. }
  103. apiComments = append(apiComments, apiComment)
  104. }
  105. }
  106. }
  107. return apiComments, nil
  108. }
  109. func patch2diff(patch string) string {
  110. split := strings.Split(patch, "\n@@")
  111. if len(split) == 2 {
  112. return "@@" + split[1]
  113. }
  114. return ""
  115. }