forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
review.go
151 lines (132 loc) · 4.97 KB
/
review.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gitea
import (
"context"
"code.gitea.io/sdk/gitea"
"github.com/jenkins-x/go-scm/scm"
)
type reviewService struct {
client *wrapper
}
func (s *reviewService) Find(ctx context.Context, repo string, number, id int) (*scm.Review, *scm.Response, error) {
namespace, name := scm.Split(repo)
review, resp, err := s.client.GiteaClient.GetPullReview(namespace, name, int64(number), int64(id))
return convertReview(review), toSCMResponse(resp), err
}
func (s *reviewService) List(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Review, *scm.Response, error) {
namespace, name := scm.Split(repo)
reviews, resp, err := s.client.GiteaClient.ListPullReviews(namespace, name, int64(number), gitea.ListPullReviewsOptions{ListOptions: toGiteaListOptions(opts)})
return convertReviewList(reviews), toSCMResponse(resp), err
}
func (s *reviewService) Create(ctx context.Context, repo string, number int, input *scm.ReviewInput) (*scm.Review, *scm.Response, error) {
namespace, name := scm.Split(repo)
in := gitea.CreatePullReviewOptions{
State: toGiteaState(input.Event),
Body: input.Body,
CommitID: input.Sha,
Comments: toCreatePullRequestComments(input.Comments),
}
review, resp, err := s.client.GiteaClient.CreatePullReview(namespace, name, int64(number), in)
return convertReview(review), toSCMResponse(resp), err
}
func (s *reviewService) Delete(ctx context.Context, repo string, number, id int) (*scm.Response, error) {
namespace, name := scm.Split(repo)
resp, err := s.client.GiteaClient.DeletePullReview(namespace, name, int64(number), int64(id))
return toSCMResponse(resp), err
}
func (s *reviewService) ListComments(ctx context.Context, repo string, prID int, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) {
namespace, name := scm.Split(repo)
comments, resp, err := s.client.GiteaClient.ListPullReviewComments(namespace, name, int64(prID), int64(reviewID))
return convertReviewCommentList(comments), toSCMResponse(resp), err
}
func (s *reviewService) Update(ctx context.Context, repo string, prID int, reviewID int, body string) (*scm.Review, *scm.Response, error) {
namespace, name := scm.Split(repo)
in := gitea.SubmitPullReviewOptions{
Body: body,
}
review, resp, err := s.client.GiteaClient.SubmitPullReview(namespace, name, int64(prID), int64(reviewID), in)
return convertReview(review), toSCMResponse(resp), err
}
func (s *reviewService) Submit(ctx context.Context, repo string, prID int, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) {
namespace, name := scm.Split(repo)
in := gitea.SubmitPullReviewOptions{
State: toGiteaState(input.Event),
Body: input.Body,
}
review, resp, err := s.client.GiteaClient.SubmitPullReview(namespace, name, int64(prID), int64(reviewID), in)
return convertReview(review), toSCMResponse(resp), err
}
// TODO: Figure out whether this actually is a _thing_ exactly in Gitea. I don't think it is.
func (s *reviewService) Dismiss(ctx context.Context, repo string, prID int, reviewID int, msg string) (*scm.Review, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func convertReviewList(from []*gitea.PullReview) []*scm.Review {
to := []*scm.Review{}
for _, v := range from {
to = append(to, convertReview(v))
}
return to
}
func convertReview(src *gitea.PullReview) *scm.Review {
if src == nil || src.Reviewer == nil {
return nil
}
return &scm.Review{
ID: int(src.ID),
Body: src.Body,
Sha: src.CommitID,
Link: src.HTMLURL,
State: string(src.State),
Author: *convertUser(src.Reviewer),
Created: src.Submitted,
}
}
func convertReviewCommentList(src []*gitea.PullReviewComment) []*scm.ReviewComment {
var out []*scm.ReviewComment
for _, v := range src {
out = append(out, convertReviewComment(v))
}
return out
}
func convertReviewComment(src *gitea.PullReviewComment) *scm.ReviewComment {
return &scm.ReviewComment{
ID: int(src.ID),
Body: src.Body,
Path: src.Path,
Sha: src.CommitID,
Line: int(src.LineNum),
Link: src.HTMLURL,
Author: *convertUser(src.Reviewer),
Created: src.Created,
Updated: src.Updated,
}
}
func toCreatePullRequestComments(src []*scm.ReviewCommentInput) []gitea.CreatePullReviewComment {
var out []gitea.CreatePullReviewComment
for _, c := range src {
out = append(out, gitea.CreatePullReviewComment{
Path: c.Path,
Body: c.Body,
NewLineNum: int64(c.Line),
})
}
return out
}
func toGiteaState(src string) gitea.ReviewStateType {
switch src {
case "APPROVED":
return gitea.ReviewStateApproved
case "PENDING":
return gitea.ReviewStatePending
case "COMMENT":
return gitea.ReviewStateComment
case "REQUEST_CHANGES":
return gitea.ReviewStateRequestChanges
case "REQUEST_REVIEW":
return gitea.ReviewStateRequestReview
default:
return gitea.ReviewStateComment
}
}