forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 84
/
review.go
201 lines (175 loc) · 5.68 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// 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 github
import (
"context"
"fmt"
"time"
"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) {
path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d", repo, number, id)
out := new(review)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertReview(out), res, err
}
func (s *reviewService) List(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Review, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/reviews?%s", repo, number, encodeListOptions(opts))
out := []*review{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertReviewList(out), res, err
}
func (s *reviewService) Create(ctx context.Context, repo string, number int, input *scm.ReviewInput) (*scm.Review, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/reviews", repo, number)
in := &reviewInput{
Body: input.Body,
CommitID: input.Sha,
Event: input.Event,
}
for _, c := range input.Comments {
in.Comments = append(in.Comments, &reviewCommentInput{
Body: c.Body,
Path: c.Path,
Position: c.Line,
})
}
out := new(review)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertReview(out), res, err
}
func (s *reviewService) Delete(ctx context.Context, repo string, number, id int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d", repo, number, id)
return s.client.do(ctx, "DELETE", path, nil, nil)
}
func (s *reviewService) ListComments(ctx context.Context, repo string, prID int, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d/comments?%s", repo, prID, reviewID, encodeListOptions(options))
out := []*reviewComment{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertReviewCommentList(out), res, err
}
func (s *reviewService) Update(ctx context.Context, repo string, prID int, reviewID int, body string) (*scm.Review, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d", repo, prID, reviewID)
in := &reviewUpdateInput{Body: body}
out := new(review)
res, err := s.client.do(ctx, "PUT", path, in, out)
return convertReview(out), res, err
}
func (s *reviewService) Submit(ctx context.Context, repo string, prID int, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d/events", repo, prID, reviewID)
in := &reviewSubmitInput{
Body: input.Body,
Event: input.Event,
}
out := new(review)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertReview(out), res, err
}
func (s *reviewService) Dismiss(ctx context.Context, repo string, prID int, reviewID int, msg string) (*scm.Review, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d/dismissals", repo, prID, reviewID)
in := &reviewDismissInput{
Message: msg,
}
if in.Message == "" {
in.Message = "Dismissing"
}
out := new(review)
res, err := s.client.do(ctx, "PUT", path, in, out)
return convertReview(out), res, err
}
type reviewComment struct {
ID int `json:"id"`
CommitID string `json:"commit_id"`
Position int `json:"position"`
Path string `json:"path"`
User struct {
ID int `json:"id"`
Login string `json:"login"`
AvatarURL string `json:"avatar_url"`
} `json:"user"`
Body string `json:"body"`
HTMLURL string `json:"html_url"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type reviewInput struct {
Body string `json:"body"`
CommitID string `json:"commit_id"`
Event string `json:"event"`
Comments []*reviewCommentInput `json:"comments"`
}
type reviewCommentInput struct {
Body string `json:"body"`
Path string `json:"path"`
Position int `json:"position"`
}
type reviewUpdateInput struct {
Body string `json:"body"`
}
type reviewSubmitInput struct {
Body string `json:"body"`
Event string `json:"event"`
}
type reviewDismissInput struct {
Message string `json:"message"`
}
type review struct {
ID int `json:"id"`
Body string `json:"body"`
User struct {
ID int `json:"id"`
Login string `json:"login"`
AvatarURL string `json:"avatar_url"`
} `json:"user"`
SubmittedAt time.Time `json:"submitted_at"`
CommitID string `json:"commit_id"`
State string `json:"state"`
HTMLURL string `json:"html_url"`
}
func convertReviewList(from []*review) []*scm.Review {
to := []*scm.Review{}
for _, v := range from {
to = append(to, convertReview(v))
}
return to
}
func convertReview(from *review) *scm.Review {
return &scm.Review{
ID: from.ID,
Body: from.Body,
Link: from.HTMLURL,
State: from.State,
Sha: from.CommitID,
Author: scm.User{
Login: from.User.Login,
Avatar: from.User.AvatarURL,
},
Created: from.SubmittedAt,
}
}
func convertReviewCommentList(from []*reviewComment) []*scm.ReviewComment {
to := []*scm.ReviewComment{}
for _, v := range from {
to = append(to, convertReviewComment(v))
}
return to
}
func convertReviewComment(from *reviewComment) *scm.ReviewComment {
return &scm.ReviewComment{
ID: 0,
Body: from.Body,
Path: from.Path,
Sha: from.CommitID,
Line: from.Position,
Link: from.HTMLURL,
Author: scm.User{
Login: from.User.Login,
Avatar: from.User.AvatarURL,
},
Created: from.CreatedAt,
Updated: from.UpdatedAt,
}
}