/
quickstarts.go
125 lines (114 loc) · 3.87 KB
/
quickstarts.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
package opts
import (
"fmt"
"strings"
v1 "github.com/jenkins-x/jx/v2/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/v2/pkg/auth"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/quickstarts"
"github.com/jenkins-x/jx/v2/pkg/versionstream"
"github.com/pkg/errors"
)
// LoadQuickStartsModel Load all quickstarts
func (o *CommonOptions) LoadQuickStartsModel(gitHubOrganisations []string, ignoreTeam bool) (*quickstarts.QuickstartModel, error) {
authConfigSvc, err := o.GitLocalAuthConfigService()
if err != nil {
return nil, err
}
resolver, err := o.GetVersionResolver()
if err != nil {
return nil, err
}
config := authConfigSvc.Config()
locations, err := o.loadQuickStartLocations(gitHubOrganisations, ignoreTeam)
if err != nil {
return nil, err
}
model, err := o.LoadQuickStartsFromLocations(locations, config)
if err != nil {
return nil, fmt.Errorf("failed to load quickstarts: %s", err)
}
quickstarts, err := versionstream.GetQuickStarts(resolver.VersionsDir)
if err != nil {
return nil, errors.Wrapf(err, "loading quickstarts from version stream in dir %s", resolver.VersionsDir)
}
quickstarts.DefaultMissingValues()
err = model.LoadQuickStarts(quickstarts)
if err != nil {
return nil, errors.Wrapf(err, "loading quickstarts: %v", quickstarts)
}
return model, nil
}
// LoadQuickStartsFromLocations Load all quickstarts from the given locatiotns
func (o *CommonOptions) LoadQuickStartsFromLocations(locations []v1.QuickStartLocation, config *auth.AuthConfig) (*quickstarts.QuickstartModel, error) {
gitMap := map[string]map[string]v1.QuickStartLocation{}
for _, loc := range locations {
m := gitMap[loc.GitURL]
if m == nil {
m = map[string]v1.QuickStartLocation{}
gitMap[loc.GitURL] = m
}
m[loc.Owner] = loc
}
model := quickstarts.NewQuickstartModel()
for gitURL, m := range gitMap {
for _, location := range m {
kind := location.GitKind
if kind == "" {
kind = gits.KindGitHub
}
// If this is a default quickstart location but there's no github.com credentials, skip and rely on the version stream alone.
server := config.GetOrCreateServer(gitURL)
userAuth := config.CurrentUser(server, o.InCluster())
if kube.IsDefaultQuickstartLocation(location) && (userAuth == nil || userAuth.IsInvalid()) {
continue
}
gitProvider, err := o.GitProviderForGitServerURL(gitURL, kind, "")
if err != nil {
return model, err
}
log.Logger().Debugf("Searching for repositories in Git server %s owner %s includes %s excludes %s as user %s ", gitProvider.ServerURL(), location.Owner, strings.Join(location.Includes, ", "), strings.Join(location.Excludes, ", "), gitProvider.CurrentUsername())
err = model.LoadGithubQuickstarts(gitProvider, location.Owner, location.Includes, location.Excludes)
if err != nil {
log.Logger().Debugf("Quickstart load error: %s", err.Error())
}
}
}
return model, nil
}
// loadQuickStartLocations loads the quickstarts
func (o *CommonOptions) loadQuickStartLocations(gitHubOrganisations []string, ignoreTeam bool) ([]v1.QuickStartLocation, error) {
var locations []v1.QuickStartLocation
if !ignoreTeam {
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return nil, err
}
locations, err = kube.GetQuickstartLocations(jxClient, ns)
if err != nil {
return nil, err
}
}
// lets add any extra github organisations if they are not already configured
for _, org := range gitHubOrganisations {
found := false
for _, loc := range locations {
if loc.GitURL == gits.GitHubURL && loc.Owner == org {
found = true
break
}
}
if !found {
locations = append(locations, v1.QuickStartLocation{
GitURL: gits.GitHubURL,
GitKind: gits.KindGitHub,
Owner: org,
Includes: []string{"*"},
Excludes: []string{"WIP-*"},
})
}
}
return locations, nil
}