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