-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_pr.go
379 lines (327 loc) · 10.3 KB
/
type_pr.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package ghid
import (
"bytes"
"fmt"
"strconv"
"github.com/vmihailenco/msgpack/v5"
)
func init() {
RegisterDecodeV1(TypePullRequest, func(key []byte) (KeyV1, error) {
id, err := strconv.ParseUint(string(key), 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to decode pr id: %w", err)
}
return PRKeyV1{ID: id}, nil
})
RegisterDecodeV1(TypePullRequestReview, func(key []byte) (KeyV1, error) {
id, err := strconv.ParseUint(string(key), 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to decode pr review id: %w", err)
}
return PRReviewKeyV1{ID: id}, nil
})
RegisterDecodeV1(TypePullRequestReviewThread, func(key []byte) (KeyV1, error) {
i := bytes.IndexByte(key, ':')
if i < 0 {
return nil, fmt.Errorf("unsupported pr review thread id: %q", string(key))
}
id, err := strconv.ParseUint(string(key[:i]), 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to decode pr review thread id: %w", err)
}
return PRReviewThreadKeyV1{ID: id, Vers: string(key[i+1:])}, nil
})
RegisterDecodeV1(TypePullRequestReviewComment, func(key []byte) (KeyV1, error) {
id, err := strconv.ParseUint(string(key), 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to decode pr review comment id: %w", err)
}
return PRReviewCommentKeyV1{ID: id}, nil
})
RegisterDecodeV2(TypePullRequest, func(key msgpack.RawMessage) (KeyV2, error) {
arr, err := decodeV2UintArr(TypePullRequest, 0, 2, key)
if err != nil {
return nil, err
}
return PRKeyV2{RepoID: RepoID(arr[0]), ID: arr[1]}, nil
})
RegisterDecodeV2(TypePullRequestReview, func(key msgpack.RawMessage) (KeyV2, error) {
arr, err := decodeV2UintArr(TypePullRequestReview, 0, 2, key)
if err != nil {
return nil, err
}
return PRReviewKeyV2{RepoID: RepoID(arr[0]), ID: arr[1]}, nil
})
RegisterDecodeV2(TypePullRequestReviewThread, func(key msgpack.RawMessage) (KeyV2, error) {
arr, err := decodeV2UintArr(TypePullRequestReviewThread, 0, 2, key)
if err != nil {
return nil, err
}
return PRReviewThreadKeyV2{RepoID: RepoID(arr[0]), ID: arr[1]}, nil
})
RegisterDecodeV2(TypePullRequestReviewComment, func(key msgpack.RawMessage) (KeyV2, error) {
arr, err := decodeV2UintArr(TypePullRequestReviewComment, 0, 2, key)
if err != nil {
return nil, err
}
return PRReviewCommentKeyV2{RepoID: RepoID(arr[0]), ID: arr[1]}, nil
})
RegisterDecodeV1(TypePullRequestCommit, func(key []byte) (KeyV1, error) {
pr, rest, err := decodeV1IDAndRest(TypePullRequestCommit, key)
if err != nil {
return nil, err
}
return PRCommitKeyV1{PR: pr, SHA: string(rest)}, nil
})
RegisterDecodeV2(TypePullRequestCommit, func(key msgpack.RawMessage) (KeyV2, error) {
var arr []any
err := msgpack.Unmarshal(key, &arr)
if err != nil {
return nil, err
}
if len(arr) != 4 {
return nil, fmt.Errorf("unsupported IDv2 PR commit key: %#v", arr)
}
if v, ok := asUint64(arr[0]); !ok || v != 0 {
return nil, fmt.Errorf("unsupported IDv2 PR commit key: %#v", arr)
}
repo, ok := asUint64(arr[1])
if !ok {
return nil, fmt.Errorf("unsupported IDv2 PR commit key: %#v", arr)
}
pr, ok := asUint64(arr[2])
if !ok {
return nil, fmt.Errorf("unsupported IDv2 PR commit key: %#v", arr)
}
sha, ok := arr[3].(string)
if !ok {
return nil, fmt.Errorf("unsupported IDv2 PR commit key: %#v", arr)
}
return PRCommitKeyV2{RepoID: RepoID(repo), PR: pr, SHA: sha}, nil
})
}
var (
_ KeyV1 = PRKeyV1{}
_ KeyV2 = PRKeyV2{}
)
// PRKeyV1 is a unique IDv1 key for PullRequest nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequest.
type PRKeyV1 struct {
ID uint64 // corresponds to databaseId
}
// Type implements Key.
func (r PRKeyV1) Type() string {
return TypePullRequest
}
// KeyV1 implements KeyV1.
func (r PRKeyV1) KeyV1() string {
return strconv.FormatUint(r.ID, 10)
}
// WithRepoV2 implements KeyV1NoRepo.
func (r PRKeyV1) WithRepoV2(repo RepoID) KeyV2 {
return PRKeyV2{RepoID: repo, ID: r.ID}
}
// PRKeyV2 is a unique IDv2 key for PullRequest nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequest.
type PRKeyV2 struct {
RepoID RepoID // corresponds to repository.databaseId
ID uint64 // corresponds to databaseId
}
// Type implements Key.
func (r PRKeyV2) Type() string {
return TypePullRequest
}
// GetRepoID implements KeyWithRepo.
func (r PRKeyV2) GetRepoID() RepoID {
return r.RepoID
}
// KeyV1 implements KeyV1.
func (r PRKeyV2) KeyV1() string {
return strconv.FormatUint(r.ID, 10)
}
// KeyV2 implements KeyV2.
func (r PRKeyV2) KeyV2() msgpack.RawMessage {
return mustEncodeV2([]any{uint(0), uint(r.RepoID), uint(r.ID)})
}
var (
_ KeyV1NoRepo = PRReviewKeyV1{}
_ KeyV2 = PRReviewKeyV2{}
)
// PRReviewKeyV1 is a unique IDv1 key for PullRequestReview nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequestreview.
type PRReviewKeyV1 struct {
ID uint64 // corresponds to databaseId
}
// Type implements Key.
func (r PRReviewKeyV1) Type() string {
return TypePullRequestReview
}
// KeyV1 implements KeyV1.
func (r PRReviewKeyV1) KeyV1() string {
return strconv.FormatUint(r.ID, 10)
}
// WithRepoV2 implements KeyV1NoRepo.
func (r PRReviewKeyV1) WithRepoV2(repo RepoID) KeyV2 {
return PRReviewKeyV2{RepoID: repo, ID: r.ID}
}
// PRReviewKeyV2 is a unique IDv2 key for PullRequestReview nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequestreview.
type PRReviewKeyV2 struct {
RepoID RepoID // corresponds to repository.databaseId
ID uint64 // corresponds to databaseId
}
// Type implements Key.
func (r PRReviewKeyV2) Type() string {
return TypePullRequestReview
}
// GetRepoID implements KeyWithRepo.
func (r PRReviewKeyV2) GetRepoID() RepoID {
return r.RepoID
}
// KeyV1 implements KeyV1.
func (r PRReviewKeyV2) KeyV1() string {
return strconv.FormatUint(r.ID, 10)
}
// KeyV2 implements KeyV2.
func (r PRReviewKeyV2) KeyV2() msgpack.RawMessage {
return mustEncodeV2([]any{uint(0), uint(r.RepoID), uint(r.ID)})
}
var (
_ KeyV1NoRepo = PRReviewThreadKeyV1{}
_ KeyV2 = PRReviewThreadKeyV2{}
)
// PRReviewThreadKeyV1 is a unique IDv1 key for PullRequestReviewThread nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequestreviewthread.
type PRReviewThreadKeyV1 struct {
ID uint64
Vers string
}
// Type implements Key.
func (r PRReviewThreadKeyV1) Type() string {
return TypePullRequestReviewThread
}
// KeyV1 implements KeyV1.
func (r PRReviewThreadKeyV1) KeyV1() string {
return strconv.FormatUint(r.ID, 10) + ":" + r.Vers
}
// WithRepoV2 implements KeyV1NoRepo.
func (r PRReviewThreadKeyV1) WithRepoV2(repo RepoID) KeyV2 {
return PRReviewThreadKeyV2{RepoID: repo, ID: r.ID}
}
// PRReviewThreadKeyV2 is a unique IDv2 key for PullRequestReviewThread nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequestreviewthread.
type PRReviewThreadKeyV2 struct {
RepoID RepoID // corresponds to repository.databaseId
ID uint64
}
// Type implements Key.
func (r PRReviewThreadKeyV2) Type() string {
return TypePullRequestReviewThread
}
// GetRepoID implements KeyWithRepo.
func (r PRReviewThreadKeyV2) GetRepoID() RepoID {
return r.RepoID
}
// KeyV2 implements KeyV2.
func (r PRReviewThreadKeyV2) KeyV2() msgpack.RawMessage {
return mustEncodeV2([]any{uint(0), uint(r.RepoID), uint(r.ID)})
}
var (
_ KeyV1NoRepo = PRReviewCommentKeyV1{}
_ KeyV2 = PRReviewCommentKeyV2{}
)
// PRReviewCommentKeyV1 is a unique IDv1 key for PullRequestReviewComment nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequestreviewcomment.
type PRReviewCommentKeyV1 struct {
ID uint64 // corresponds to databaseId
}
// Type implements Key.
func (r PRReviewCommentKeyV1) Type() string {
return TypePullRequestReviewComment
}
// KeyV1 implements KeyV1.
func (r PRReviewCommentKeyV1) KeyV1() string {
return strconv.FormatUint(r.ID, 10)
}
// WithRepoV2 implements KeyV1NoRepo.
func (r PRReviewCommentKeyV1) WithRepoV2(repo RepoID) KeyV2 {
return PRReviewCommentKeyV2{RepoID: repo, ID: r.ID}
}
// PRReviewCommentKeyV2 is a unique IDv2 key for PullRequestReviewComment nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequestreviewcomment.
type PRReviewCommentKeyV2 struct {
RepoID RepoID // corresponds to repository.databaseId
ID uint64 // corresponds to databaseId
}
// Type implements Key.
func (r PRReviewCommentKeyV2) Type() string {
return TypePullRequestReviewComment
}
// GetRepoID implements KeyWithRepo.
func (r PRReviewCommentKeyV2) GetRepoID() RepoID {
return r.RepoID
}
// KeyV1 implements KeyV1.
func (r PRReviewCommentKeyV2) KeyV1() string {
return strconv.FormatUint(r.ID, 10)
}
// KeyV2 implements KeyV2.
func (r PRReviewCommentKeyV2) KeyV2() msgpack.RawMessage {
return mustEncodeV2([]any{uint(0), uint(r.RepoID), uint(r.ID)})
}
var (
_ KeyV1NoRepo = PRCommitKeyV1{}
_ KeyV2 = PRCommitKeyV2{}
)
// PRCommitKeyV1 is a unique IDv1 key for PullRequestCommit nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequestcommit.
type PRCommitKeyV1 struct {
PR uint64 // corresponds to pullRequest.databaseId
SHA string
}
// Type implements Key.
func (r PRCommitKeyV1) Type() string {
return TypePullRequestCommit
}
// KeyV1 implements KeyV1.
func (r PRCommitKeyV1) KeyV1() string {
return strconv.FormatUint(r.PR, 10) + ":" + r.SHA
}
// WithRepoV2 implements KeyV1NoRepo.
func (r PRCommitKeyV1) WithRepoV2(repo RepoID) KeyV2 {
return PRCommitKeyV2{RepoID: repo, PR: r.PR, SHA: r.SHA}
}
// PRCommitKeyV2 is a unique IDv2 key for PullRequestCommit nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#pullrequestcommit.
type PRCommitKeyV2 struct {
RepoID RepoID // corresponds to repository.databaseId
PR uint64 // corresponds to pullRequest.databaseId
SHA string
}
// Type implements Key.
func (r PRCommitKeyV2) Type() string {
return TypePullRequestCommit
}
// GetRepoID implements KeyWithRepo.
func (r PRCommitKeyV2) GetRepoID() RepoID {
return r.RepoID
}
// KeyV1 implements KeyV1.
func (r PRCommitKeyV2) KeyV1() string {
return strconv.FormatUint(r.PR, 10) + ":" + r.SHA
}
// KeyV2 implements KeyV2.
func (r PRCommitKeyV2) KeyV2() msgpack.RawMessage {
// GitHub encodes commit SHA as string16, not string8, even though SHA length is only 40 (<256). Optimization?
return mustEncodeV2([]any{uint(0), uint(r.RepoID), uint(r.PR), msgpackEncodeStr16(r.SHA)})
}