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

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