-
Notifications
You must be signed in to change notification settings - Fork 787
/
provider.go
584 lines (523 loc) · 15.4 KB
/
provider.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package gits
import (
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/jenkins-x/jx/v2/pkg/auth"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"gopkg.in/AlecAivazis/survey.v1"
)
type GitOrganisation struct {
Login string
}
type GitRepository struct {
ID int64
Name string
AllowMergeCommit bool
HTMLURL string
CloneURL string
SSHURL string
Language string
Fork bool
Stars int
URL string
Scheme string
Host string
Organisation string
Project string
Private bool
HasIssues bool
OpenIssueCount int
HasWiki bool
HasProjects bool
Archived bool
}
type GitPullRequest struct {
URL string
Author *GitUser
Owner string
Repo string
Number *int
Mergeable *bool
Merged *bool
HeadRef *string
State *string
StatusesURL *string
IssueURL *string
DiffURL *string
MergeCommitSHA *string
ClosedAt *time.Time
MergedAt *time.Time
LastCommitSha string
Title string
Body string
Assignees []*GitUser
RequestedReviewers []*GitUser
Labels []*Label
UpdatedAt *time.Time
HeadOwner *string // HeadOwner is the string the PR is created from
}
// Label represents a label on an Issue
type Label struct {
ID *int64
URL *string
Name *string
Color *string
Description *string
Default *bool
}
type GitCommit struct {
SHA string
Message string
Author *GitUser
URL string
Branch string
Committer *GitUser
}
type ListCommitsArguments struct {
SHA string
Path string
Author string
Since time.Time
Until time.Time
Page int
PerPage int
}
type GitIssue struct {
URL string
Owner string
Repo string
Number *int
Key string
Title string
Body string
State *string
Labels []GitLabel
StatusesURL *string
IssueURL *string
CreatedAt *time.Time
UpdatedAt *time.Time
ClosedAt *time.Time
IsPullRequest bool
User *GitUser
ClosedBy *GitUser
Assignees []GitUser
}
type GitUser struct {
URL string
Login string
Name string
Email string
AvatarURL string
}
type GitRelease struct {
ID int64
Name string
TagName string
Body string
PreRelease bool
URL string
HTMLURL string
DownloadCount int
Assets *[]GitReleaseAsset
}
// GitReleaseAsset represents a release stored in Git
type GitReleaseAsset struct {
ID int64
BrowserDownloadURL string
Name string
ContentType string
}
type GitLabel struct {
URL string
Name string
Color string
}
type GitRepoStatus struct {
ID string
Context string
URL string
// State is the current state of the repository. Possible values are:
// pending, success, error, or failure.
State string `json:"state,omitempty"`
// TargetURL is the URL of the page representing this status
TargetURL string `json:"target_url,omitempty"`
// Description is a short high level summary of the status.
Description string
}
type GitPullRequestArguments struct {
Title string
Body string
Head string
Base string
GitRepository *GitRepository
Labels []string
}
func (a *GitPullRequestArguments) String() string {
return fmt.Sprintf("Title: %s; Body: %s; Head: %s; Base: %s; Labels: %s; Git Repo: %s", a.Title, a.Body, a.Head, a.Base, strings.Join(a.Labels, ", "), a.GitRepository.URL)
}
type GitWebHookArguments struct {
ID int64
Owner string
Repo *GitRepository
URL string
ExistingURL string
Secret string
InsecureSSL bool
}
type GitFileContent struct {
Type string
Encoding string
Size int
Name string
Path string
Content string
Sha string
Url string
GitUrl string
HtmlUrl string
DownloadUrl string
}
// GitBranch is info on a git branch including the commit at the tip
type GitBranch struct {
Name string
Commit *GitCommit
Protected bool
}
// PullRequestInfo describes a pull request that has been created
type PullRequestInfo struct {
GitProvider GitProvider
PullRequest *GitPullRequest
PullRequestArguments *GitPullRequestArguments
}
// GitProject is a project for managing issues
type GitProject struct {
Name string
Description string
Number int
State string
}
// IsClosed returns true if the PullRequest has been closed
func (pr *GitPullRequest) IsClosed() bool {
return pr.ClosedAt != nil
}
// NumberString returns the string representation of the Pull Request number or blank if its missing
func (pr *GitPullRequest) NumberString() string {
n := pr.Number
if n == nil {
return ""
}
return "#" + strconv.Itoa(*n)
}
// ShortSha returns short SHA of the commit.
func (c *GitCommit) ShortSha() string {
shortLen := 9
if len(c.SHA) < shortLen+1 {
return c.SHA
}
return c.SHA[:shortLen]
}
// Subject returns the subject line of the commit message.
func (c *GitCommit) Subject() string {
lines := strings.Split(c.Message, "\n")
return lines[0]
}
// OneLine returns the commit in the Oneline format
func (c *GitCommit) OneLine() string {
return fmt.Sprintf("%s %s", c.ShortSha(), c.Subject())
}
// CreateProvider creates a git provider for the given auth details
func CreateProvider(server *auth.AuthServer, user *auth.UserAuth, git Gitter) (GitProvider, error) {
if server.Kind == "" {
server.Kind = SaasGitKind(server.URL)
}
if server.Kind == KindBitBucketCloud {
return NewBitbucketCloudProvider(server, user, git)
} else if server.Kind == KindBitBucketServer {
return NewBitbucketServerProvider(server, user, git)
} else if server.Kind == KindGitea {
return NewGiteaProvider(server, user, git)
} else if server.Kind == KindGitlab {
return NewGitlabProvider(server, user, git)
} else if server.Kind == KindGitFake {
return NewFakeProvider(), nil
} else {
return NewGitHubProvider(server, user, git)
}
}
// GetHost returns the Git Provider hostname, e.g github.com
func GetHost(gitProvider GitProvider) (string, error) {
if gitProvider == nil {
return "", fmt.Errorf("no Git provider")
}
if gitProvider.ServerURL() == "" {
return "", fmt.Errorf("no Git provider server URL found")
}
url, err := url.Parse(gitProvider.ServerURL())
if err != nil {
return "", fmt.Errorf("error parsing ")
}
return url.Host, nil
}
func ProviderAccessTokenURL(kind string, url string, username string) string {
switch kind {
case KindBitBucketCloud:
// TODO pass in the username
return BitBucketCloudAccessTokenURL(url, username)
case KindBitBucketServer:
return BitBucketServerAccessTokenURL(url)
case KindGitea:
return GiteaAccessTokenURL(url)
case KindGitlab:
return GitlabAccessTokenURL(url)
default:
return GitHubAccessTokenURL(url)
}
}
// PickOwner allows to select a potential owner of a repository
func PickOwner(orgLister OrganisationLister, userName string, handles util.IOFileHandles) (string, error) {
msg := "Who should be the owner of the repository?"
return pickOwner(orgLister, userName, msg, handles)
}
// PickOrganisation picks an organisations login if there is one available
func PickOrganisation(orgLister OrganisationLister, userName string, handles util.IOFileHandles) (string, error) {
msg := "Which organisation do you want to use?"
return pickOwner(orgLister, userName, msg, handles)
}
func pickOwner(orgLister OrganisationLister, userName string, message string, handles util.IOFileHandles) (string, error) {
prompt := &survey.Select{
Message: message,
Options: GetOrganizations(orgLister, userName),
Default: userName,
}
orgName := ""
surveyOpts := survey.WithStdio(handles.In, handles.Out, handles.Err)
err := survey.AskOne(prompt, &orgName, nil, surveyOpts)
if err != nil {
return "", err
}
if orgName == userName {
return "", nil
}
return orgName, nil
}
// GetOrganizations gets the organisation
func GetOrganizations(orgLister OrganisationLister, userName string) []string {
var orgNames []string
// Always include the username as a pseudo organization
if userName != "" {
orgNames = append(orgNames, userName)
}
orgs, _ := orgLister.ListOrganisations()
for _, o := range orgs {
if name := o.Login; name != "" {
orgNames = append(orgNames, name)
}
}
sort.Strings(orgNames)
return orgNames
}
func PickRepositories(provider GitProvider, owner string, message string, selectAll bool, filter string, handles util.IOFileHandles) ([]*GitRepository, error) {
answer := []*GitRepository{}
repos, err := provider.ListRepositories(owner)
if err != nil {
return answer, err
}
repoMap := map[string]*GitRepository{}
allRepoNames := []string{}
for _, repo := range repos {
n := repo.Name
if n != "" && (filter == "" || strings.Contains(n, filter)) {
allRepoNames = append(allRepoNames, n)
repoMap[n] = repo
}
}
if len(allRepoNames) == 0 {
return answer, fmt.Errorf("No matching repositories could be found!")
}
sort.Strings(allRepoNames)
prompt := &survey.MultiSelect{
Message: message,
Options: allRepoNames,
}
if selectAll {
prompt.Default = allRepoNames
}
repoNames := []string{}
surveyOpts := survey.WithStdio(handles.In, handles.Out, handles.Err)
err = survey.AskOne(prompt, &repoNames, nil, surveyOpts)
for _, n := range repoNames {
repo := repoMap[n]
if repo != nil {
answer = append(answer, repo)
}
}
return answer, err
}
// IsGitRepoStatusSuccess returns true if all the statuses are successful
func IsGitRepoStatusSuccess(statuses ...*GitRepoStatus) bool {
for _, status := range statuses {
if !status.IsSuccess() {
return false
}
}
return true
}
// IsGitRepoStatusFailed returns true if any of the statuses have failed
func IsGitRepoStatusFailed(statuses ...*GitRepoStatus) bool {
for _, status := range statuses {
if status.IsFailed() {
return true
}
}
return false
}
func (s *GitRepoStatus) IsSuccess() bool {
return s.State == "success"
}
func (s *GitRepoStatus) IsFailed() bool {
return s.State == "error" || s.State == "failure"
}
// PickOrCreateProvider picks an existing server and auth or creates a new one if required
// then create a GitProvider for it
func (i *GitRepository) PickOrCreateProvider(authConfigSvc auth.ConfigService, message string, batchMode bool, gitKind string, githubAppMode bool, git Gitter, handles util.IOFileHandles) (GitProvider, error) {
config := authConfigSvc.Config()
hostUrl := i.HostURLWithoutUser()
server := config.GetOrCreateServer(hostUrl)
if server.Kind == "" {
server.Kind = gitKind
}
var userAuth *auth.UserAuth
var err error
if githubAppMode && i.Organisation != "" {
for _, u := range server.Users {
if i.Organisation == u.GithubAppOwner {
userAuth = u
break
}
}
}
if userAuth == nil {
userAuth, err = config.PickServerUserAuth(server, message, batchMode, i.Organisation, handles)
if err != nil {
return nil, err
}
}
if userAuth.IsInvalid() {
userAuth, err = createUserForServer(batchMode, userAuth, authConfigSvc, server, git, handles)
}
return i.CreateProviderForUser(server, userAuth, gitKind, git)
}
func (i *GitRepository) CreateProviderForUser(server *auth.AuthServer, user *auth.UserAuth, gitKind string, git Gitter) (GitProvider, error) {
if i.Host == GitHubHost {
return NewGitHubProvider(server, user, git)
}
if gitKind != "" && server.Kind != gitKind {
server.Kind = gitKind
}
return CreateProvider(server, user, git)
}
func (i *GitRepository) CreateProvider(inCluster bool, authConfigSvc auth.ConfigService, gitKind string, ghOwner string, git Gitter, batchMode bool, handles util.IOFileHandles) (GitProvider, error) {
hostUrl := i.HostURLWithoutUser()
return CreateProviderForURL(inCluster, authConfigSvc, gitKind, hostUrl, ghOwner, git, batchMode, handles)
}
// ProviderURL returns the git provider URL
func (i *GitRepository) ProviderURL() string {
scheme := i.Scheme
if !strings.HasPrefix(scheme, "http") {
scheme = "https"
}
return scheme + "://" + i.Host
}
// CreateProviderForURL creates the Git provider for the given git kind and host URL
func CreateProviderForURL(inCluster bool, authConfigSvc auth.ConfigService, gitKind string, hostURL string, ghOwner string, git Gitter, batchMode bool,
handles util.IOFileHandles) (GitProvider, error) {
config := authConfigSvc.Config()
server := config.GetOrCreateServer(hostURL)
if gitKind != "" {
server.Kind = gitKind
}
var userAuth *auth.UserAuth
if ghOwner != "" {
for _, u := range server.Users {
if ghOwner == u.GithubAppOwner {
userAuth = u
break
}
}
} else {
userAuth = config.CurrentUser(server, inCluster)
}
if userAuth != nil && !userAuth.IsInvalid() {
return CreateProvider(server, userAuth, git)
}
if ghOwner == "" {
kind := server.Kind
if kind == "" {
kind = "GIT"
}
userAuthVar := auth.CreateAuthUserFromEnvironment(strings.ToUpper(kind))
if !userAuthVar.IsInvalid() {
return CreateProvider(server, &userAuthVar, git)
}
var err error
userAuth, err = createUserForServer(batchMode, &auth.UserAuth{}, authConfigSvc, server, git, handles)
if err != nil {
return nil, errors.Wrapf(err, "creating user for server %q", server.URL)
}
}
if userAuth != nil && !userAuth.IsInvalid() {
return CreateProvider(server, userAuth, git)
}
return nil, fmt.Errorf("no valid git user found for kind %s host %s %s", gitKind, hostURL, ghOwner)
}
func createUserForServer(batchMode bool, userAuth *auth.UserAuth, authConfigSvc auth.ConfigService, server *auth.AuthServer,
git Gitter, handles util.IOFileHandles) (*auth.UserAuth, error) {
f := func(username string) error {
git.PrintCreateRepositoryGenerateAccessToken(server, username, handles.Out)
return nil
}
defaultUserName := ""
err := authConfigSvc.Config().EditUserAuth(server.Label(), userAuth, defaultUserName, false, batchMode, f, handles)
if err != nil {
return userAuth, err
}
err = authConfigSvc.SaveUserAuth(server.URL, userAuth)
if err != nil {
return userAuth, fmt.Errorf("failed to store git auth configuration %s", err)
}
if userAuth.IsInvalid() {
return userAuth, fmt.Errorf("you did not properly define the user authentication")
}
return userAuth, nil
}
// ToGitLabels converts the list of label names into an array of GitLabels
func ToGitLabels(names []string) []GitLabel {
answer := []GitLabel{}
for _, n := range names {
answer = append(answer, GitLabel{Name: n})
}
return answer
}
// IsRepoStatusUpToDate takes a provider, an owner, repo, sha, and GitRepoStatus, and checks if there's an existing commit
// status for the owner/repo/sha/context (from the GitRepoStatus) with the GitRepoStatus's status, target URL, and description
func IsRepoStatusUpToDate(provider GitProvider, owner string, repo string, sha string, commitStatus *GitRepoStatus) (bool, error) {
statuses, err := provider.ListCommitStatus(owner, repo, sha)
if err != nil {
return false, errors.Wrapf(err, "fetching commit statuses for %s/%s, sha %s", owner, repo, sha)
}
for _, existingStatus := range statuses {
if existingStatus != nil && existingStatus.Context == commitStatus.Context {
if existingStatus.State == commitStatus.State &&
existingStatus.TargetURL == commitStatus.TargetURL &&
existingStatus.Description == commitStatus.Description {
return true, nil
}
}
}
return false, nil
}