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

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