forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
repo.go
523 lines (475 loc) · 16.1 KB
/
repo.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// 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"
"net/http"
"strconv"
"time"
"github.com/jenkins-x/go-scm/scm"
)
type repository struct {
ID int `json:"id"`
Owner struct {
ID int `json:"id"`
Login string `json:"login"`
AvatarURL string `json:"avatar_url"`
} `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Private bool `json:"private"`
Archived bool `json:"archived"`
Fork bool `json:"fork"`
HTMLURL string `json:"html_url"`
SSHURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
DefaultBranch string `json:"default_branch"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Permissions struct {
Admin bool `json:"admin"`
Push bool `json:"push"`
Pull bool `json:"pull"`
} `json:"permissions"`
}
type repositoryInput struct {
Name string `json:"name"`
Description string `json:"description"`
Homepage string `json:"homepage"`
Private bool `json:"private"`
}
type hook struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Events []string `json:"events"`
Active bool `json:"active"`
Config struct {
URL string `json:"url"`
Secret string `json:"secret"`
ContentType string `json:"content_type"`
InsecureSSL string `json:"insecure_ssl"`
} `json:"config"`
}
type collaboratorBody struct {
Permission string `json:"permission"`
}
type repositoryService struct {
client *wrapper
}
// AddCollaborator adds a collaborator to the repo.
// See https://developer.github.com/v3/repos/collaborators/#add-user-as-a-collaborator
func (s *repositoryService) AddCollaborator(ctx context.Context, repo, user, permission string) (bool, bool, *scm.Response, error) {
req := &scm.Request{
Method: http.MethodPut,
Path: fmt.Sprintf("repos/%s/collaborators/%s", repo, user),
Header: map[string][]string{
// This accept header enables the nested teams preview.
// https://developer.github.com/changes/2017-08-30-preview-nested-teams/
"Accept": {"application/vnd.github.hellcat-preview+json"},
},
}
body := collaboratorBody{
Permission: permission,
}
res, err := s.client.doRequest(ctx, req, &body, nil)
if err != nil && res == nil {
return false, false, res, err
}
code := res.Status
if code == 201 {
return true, false, res, nil
} else if code == 204 {
return false, true, res, nil
} else if code == 404 {
return false, false, res, nil
}
return false, false, res, fmt.Errorf("unexpected status: %d", code)
}
// IsCollaborator returns whether or not the user is a collaborator of the repo.
// From GitHub's API reference:
// For organization-owned repositories, the list of collaborators includes
// outside collaborators, organization members that are direct collaborators,
// organization members with access through team memberships, organization
// members with access through default organization permissions, and
// organization owners.
//
// See https://developer.github.com/v3/repos/collaborators/
func (s *repositoryService) IsCollaborator(ctx context.Context, repo, user string) (bool, *scm.Response, error) {
req := &scm.Request{
Method: http.MethodGet,
Path: fmt.Sprintf("repos/%s/collaborators/%s", repo, user),
Header: map[string][]string{
// This accept header enables the nested teams preview.
// https://developer.github.com/changes/2017-08-30-preview-nested-teams/
"Accept": {"application/vnd.github.hellcat-preview+json"},
},
}
res, err := s.client.doRequest(ctx, req, nil, nil)
if err != nil && res == nil {
return false, res, err
}
code := res.Status
if code == 204 {
return true, res, nil
} else if code == 404 {
return false, res, nil
}
return false, res, fmt.Errorf("unexpected status: %d", code)
}
// ListCollaborators gets a list of all users who have access to a repo (and
// can become assignees or requested reviewers).
//
// See 'IsCollaborator' for more details.
// See https://developer.github.com/v3/repos/collaborators/
func (s *repositoryService) ListCollaborators(ctx context.Context, repo string, opts *scm.ListOptions) ([]scm.User, *scm.Response, error) {
params := encodeListOptions(opts)
req := &scm.Request{
Method: http.MethodGet,
Path: fmt.Sprintf("repos/%s/collaborators?%s", repo, params),
Header: map[string][]string{
// This accept header enables the nested teams preview.
// https://developer.github.com/changes/2017-08-30-preview-nested-teams/
"Accept": {"application/vnd.github.hellcat-preview+json"},
},
}
out := []user{}
res, err := s.client.doRequest(ctx, req, nil, &out)
return convertUsers(out), res, err
}
// Find returns the repository by name.
func (s *repositoryService) Find(ctx context.Context, repo string) (*scm.Repository, *scm.Response, error) {
path := fmt.Sprintf("repos/%s", repo)
out := new(repository)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertRepository(out), res, err
}
// FindHook returns a repository hook.
func (s *repositoryService) FindHook(ctx context.Context, repo, id string) (*scm.Hook, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/hooks/%s", repo, id)
out := new(hook)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertHook(out), res, err
}
// FindPerms returns the repository permissions.
func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Perm, *scm.Response, error) {
path := fmt.Sprintf("repos/%s", repo)
out := new(repository)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertRepository(out).Perm, res, err
}
// FindUserPermission returns the repository permissions.
// https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level
func (s *repositoryService) FindUserPermission(ctx context.Context, repo, user string) (string, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/collaborators/%s/permission", repo, user)
var out struct {
Perm string `json:"permission"`
}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return out.Perm, res, err
}
// List returns the user repository list.
func (s *repositoryService) List(ctx context.Context, opts *scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
req := &scm.Request{
Method: http.MethodGet,
Path: fmt.Sprintf("user/repos?visibility=all&affiliation=owner&%s", encodeListOptions(opts)),
Header: map[string][]string{
// This accepts header enables the visibility parameter.
// https://developer.github.com/changes/2019-12-03-internal-visibility-changes/
"Accept": {"application/vnd.github.nebula-preview+json"},
},
}
out := []*repository{}
res, err := s.client.doRequest(ctx, req, nil, &out)
return convertRepositoryList(out), res, err
}
// ListOrganisation returns the repositories for an organisation
func (s *repositoryService) ListOrganisation(ctx context.Context, org string, opts *scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
path := fmt.Sprintf("orgs/%s/repos?%s", org, encodeListOptions(opts))
out := []*repository{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertRepositoryList(out), res, err
}
// ListUser returns the public repositories for the specified user
func (s *repositoryService) ListUser(ctx context.Context, user string, opts *scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
path := fmt.Sprintf("users/%s/repos?%s", user, encodeListOptions(opts))
out := []*repository{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertRepositoryList(out), res, err
}
// ListHooks returns a list or repository hooks.
func (s *repositoryService) ListHooks(ctx context.Context, repo string, opts *scm.ListOptions) ([]*scm.Hook, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/hooks?%s", repo, encodeListOptions(opts))
out := []*hook{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertHookList(out), res, err
}
// ListStatus returns a list of commit statuses.
func (s *repositoryService) ListStatus(ctx context.Context, repo, ref string, opts *scm.ListOptions) ([]*scm.Status, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/statuses/%s?%s", repo, ref, encodeListOptions(opts))
out := []*status{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertStatusList(out), res, err
}
// FindCombinedStatus returns the latest statuses for a given ref.
//
// See https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-ref
func (s *repositoryService) FindCombinedStatus(ctx context.Context, repo, ref string) (*scm.CombinedStatus, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/commits/%s/status", repo, ref)
out := &combinedStatus{}
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertCombinedStatus(out), res, err
}
func (s *repositoryService) ListLabels(ctx context.Context, repo string, opts *scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/labels?%s", repo, encodeListOptions(opts))
out := []*label{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertLabelObjects(out), res, err
}
// Create creates a new repository
func (s *repositoryService) Create(ctx context.Context, input *scm.RepositoryInput) (*scm.Repository, *scm.Response, error) {
path := "user/repos"
if input.Namespace != "" {
path = fmt.Sprintf("orgs/%s/repos", input.Namespace)
}
in := new(repositoryInput)
in.Name = input.Name
in.Description = input.Description
in.Homepage = input.Homepage
in.Private = input.Private
out := new(repository)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertRepository(out), res, err
}
type forkInput struct {
Organization string `json:"organization,omitempty"`
}
func (s *repositoryService) Fork(ctx context.Context, input *scm.RepositoryInput, origRepo string) (*scm.Repository, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/forks", origRepo)
in := new(forkInput)
if input.Namespace != "" {
in.Organization = input.Namespace
}
out := new(repository)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertRepository(out), res, err
}
// CreateHook creates a new repository webhook.
func (s *repositoryService) CreateHook(ctx context.Context, repo string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/hooks", repo)
in := new(hook)
in.Active = true
in.Name = "web"
in.Config.Secret = input.Secret
in.Config.ContentType = "json"
in.Config.URL = input.Target
if input.SkipVerify {
in.Config.InsecureSSL = "1"
} else {
in.Config.InsecureSSL = "0"
}
input.NativeEvents = append(
input.NativeEvents,
convertHookEvents(input.Events)...,
)
in.Events = input.NativeEvents
out := new(hook)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertHook(out), res, err
}
func (s *repositoryService) UpdateHook(ctx context.Context, repo string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/hooks/%s", repo, input.Name)
in := new(hook)
in.Active = true
in.Name = "web"
in.Config.Secret = input.Secret
in.Config.ContentType = "json"
in.Config.URL = input.Target
if input.SkipVerify {
in.Config.InsecureSSL = "1"
} else {
in.Config.InsecureSSL = "0"
}
input.NativeEvents = append(
input.NativeEvents,
convertHookEvents(input.Events)...,
)
in.Events = input.NativeEvents
out := new(hook)
res, err := s.client.do(ctx, "PATCH", path, in, out)
return convertHook(out), res, err
}
// CreateStatus creates a new commit status.
func (s *repositoryService) CreateStatus(ctx context.Context, repo, ref string, input *scm.StatusInput) (*scm.Status, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/statuses/%s", repo, ref)
in := &status{
State: convertFromState(input.State),
Context: input.Label,
Description: input.Desc,
TargetURL: input.Target,
}
out := new(status)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertStatus(out), res, err
}
// DeleteHook deletes a repository webhook.
func (s *repositoryService) DeleteHook(ctx context.Context, repo, id string) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/hooks/%s", repo, id)
return s.client.do(ctx, "DELETE", path, nil, nil)
}
func (s *repositoryService) Delete(ctx context.Context, repo string) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s", repo)
return s.client.do(ctx, "DELETE", path, nil, nil)
}
// helper function to convert from the gogs repository list to
// the common repository structure.
func convertRepositoryList(from []*repository) []*scm.Repository {
to := []*scm.Repository{}
for _, v := range from {
if v != nil {
to = append(to, convertRepository(v))
}
}
return to
}
// helper function to convert from the gogs repository structure
// to the common repository structure.
func convertRepository(from *repository) *scm.Repository {
return &scm.Repository{
ID: strconv.Itoa(from.ID),
Name: from.Name,
Namespace: from.Owner.Login,
FullName: from.FullName,
Perm: &scm.Perm{
Push: from.Permissions.Push,
Pull: from.Permissions.Pull,
Admin: from.Permissions.Admin,
},
Link: from.HTMLURL,
Branch: from.DefaultBranch,
Private: from.Private,
Archived: from.Archived,
Clone: from.CloneURL,
CloneSSH: from.SSHURL,
Created: from.CreatedAt,
Updated: from.UpdatedAt,
}
}
func convertHookList(from []*hook) []*scm.Hook {
to := []*scm.Hook{}
for _, v := range from {
to = append(to, convertHook(v))
}
return to
}
func convertHook(from *hook) *scm.Hook {
skipVerify := false
if from.Config.InsecureSSL == "1" {
skipVerify = true
}
return &scm.Hook{
ID: strconv.Itoa(from.ID),
Active: from.Active,
Target: from.Config.URL,
SkipVerify: skipVerify,
Events: from.Events,
}
}
func convertHookEvents(from scm.HookEvents) []string {
var events []string
if from.Push {
events = append(events, "push")
}
if from.PullRequest {
events = append(events, "pull_request")
}
if from.Review {
events = append(events, "pull_request_review")
}
if from.PullRequestComment {
events = append(events, "pull_request_review_comment")
}
if from.Issue {
events = append(events, "issues")
}
if from.IssueComment || from.PullRequestComment {
events = append(events, "issue_comment")
}
if from.Branch || from.Tag {
events = append(events, "create", "delete")
}
if from.Deployment {
events = append(events, "deployment")
}
if from.DeploymentStatus {
events = append(events, "deployment_status")
}
if from.Release {
events = append(events, "release")
}
return events
}
type combinedStatus struct {
Sha string `json:"sha"`
Statuses []*status `json:"statuses"`
State string `json:"state"`
}
type status struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
State string `json:"state"`
TargetURL string `json:"target_url"`
URL string `json:"url"`
Description string `json:"description"`
Context string `json:"context"`
}
func convertCombinedStatus(from *combinedStatus) *scm.CombinedStatus {
return &scm.CombinedStatus{
Sha: from.Sha,
State: convertState(from.State),
Statuses: convertStatusList(from.Statuses),
}
}
func convertStatusList(from []*status) []*scm.Status {
to := []*scm.Status{}
for _, v := range from {
to = append(to, convertStatus(v))
}
return to
}
func convertStatus(from *status) *scm.Status {
return &scm.Status{
State: convertState(from.State),
Label: from.Context,
Desc: from.Description,
Target: from.TargetURL,
Link: from.URL,
}
}
func convertState(from string) scm.State {
switch from {
case "error":
return scm.StateError
case "failure":
return scm.StateFailure
case "pending":
return scm.StatePending
case "success":
return scm.StateSuccess
default:
return scm.StateUnknown
}
}
func convertFromState(from scm.State) string {
switch from {
case scm.StatePending, scm.StateRunning:
return "pending"
case scm.StateSuccess:
return "success"
case scm.StateFailure:
return "failure"
default:
return "error"
}
}