-
Notifications
You must be signed in to change notification settings - Fork 30
/
evaluate_buildpack.go
181 lines (162 loc) · 4.42 KB
/
evaluate_buildpack.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
package importcmd
import (
"context"
"net/url"
"path/filepath"
"strings"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient/giturl"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
)
// HasJenkinsfile returns the ile name if there is a Jenkinsfile or empty string if there is not
func (o *ImportOptions) HasJenkinsfile() (string, error) {
dir := o.Dir
var err error
jenkinsfile := jenkinsfileName
if o.Jenkinsfile != "" {
jenkinsfile = o.Jenkinsfile
}
if !filepath.IsAbs(jenkinsfile) {
jenkinsfile = filepath.Join(dir, jenkinsfile)
}
exists, err := files.FileExists(jenkinsfile)
if err != nil {
return "", err
}
if !exists {
return "", nil
}
return jenkinsfile, nil
}
// EvaluateBuildPack performs an evaluation of the build pack on the current source
func (o *ImportOptions) EvaluateBuildPack(devEnvCloneDir, jenkinsfile string) error {
// TODO this is a workaround of this draft issue:
// https://github.com/Azure/draft/issues/476
var err error
args := &InvokeDraftPack{
Dir: o.Dir,
DevEnvCloneDir: devEnvCloneDir,
CustomDraftPack: o.Pack,
Jenkinsfile: jenkinsfile,
InitialisedGit: o.InitialisedGit,
}
o.Pack, err = o.InvokeDraftPack(args)
if err != nil {
return err
}
// lets rename the chart to be the same as our app name
err = o.renameChartToMatchAppName()
if err != nil {
return err
}
/* TODO
err = o.modifyDeployKind()
if err != nil {
return err
}
*/
if o.PostDraftPackCallback != nil {
err = o.PostDraftPackCallback()
if err != nil {
return err
}
}
gitServerName := ""
if o.gitInfo != nil {
gitServerName = o.gitInfo.Host
}
gitServerURL := o.ScmFactory.GitServerURL
if gitServerName == "" {
if gitServerURL == "" {
return errors.Errorf("no git server URL")
}
u, err := url.Parse(gitServerURL)
if err != nil {
return errors.Wrapf(err, "failed to parse git server URL %s", gitServerURL)
}
gitServerName = u.Host
}
if gitServerName == "" {
return errors.Errorf("no git server name")
}
if o.Organisation == "" {
o.Organisation, err = o.PickOwner("")
if err != nil {
return errors.Wrapf(err, "failed to pick a git owner")
}
}
if o.AppName == "" {
_, defaultRepoName := filepath.Split(o.Dir)
o.AppName, err = o.PickRepoName(o.Organisation, defaultRepoName, false)
if err != nil {
return err
}
}
dockerRegistryOrg := o.getDockerRegistryOrg()
err = o.ReplacePlaceholders(gitServerName, dockerRegistryOrg)
if err != nil {
return err
}
// Create Prow owners file
err = o.CreateProwOwnersFile()
if err != nil {
return err
}
err = o.CreateProwOwnersAliasesFile()
if err != nil {
return err
}
return nil
}
func (o *ImportOptions) getDockerRegistryOrg() string {
dockerRegistryOrg := o.DockerRegistryOrg
if dockerRegistryOrg == "" {
dockerRegistryOrg = o.getOrganisationOrCurrentUser()
}
return strings.ToLower(dockerRegistryOrg)
}
func (o *ImportOptions) getOrganisationOrCurrentUser() string {
org := o.GetOrganisation()
if org == "" {
org = o.getCurrentUser()
}
return org
}
func (o *ImportOptions) getCurrentUser() string {
//walk through every file in the given dir and update the placeholders
if o.ScmFactory.GitUsername == "" {
if o.ScmFactory.ScmClient != nil {
ctx := context.Background()
user, _, err := o.ScmFactory.ScmClient.Users.Find(ctx)
if err != nil {
log.Logger().Warnf("failed to find current user in git %s", err.Error())
} else {
o.ScmFactory.GitUsername = user.Login
}
}
}
if o.ScmFactory.GitUsername == "" {
log.Logger().Warn("No username defined for the current Git server!")
}
return o.ScmFactory.GitUsername
}
// GetOrganisation gets the organisation from the RepoURL (if in the github format of github.com/org/repo). It will
// do this in preference to the Organisation field (if set). If the repo URL does not implicitly specify an organisation
// then the Organisation specified in the options is used.
func (o *ImportOptions) GetOrganisation() string {
org := ""
if o.DiscoveredGitURL == "" {
o.DiscoveredGitURL = o.RepoURL
}
gitInfo, err := giturl.ParseGitURL(o.DiscoveredGitURL)
if err == nil && gitInfo.Organisation != "" {
org = gitInfo.Organisation
if o.Organisation != "" && org != o.Organisation {
log.Logger().Warnf("organisation %s detected from URL %s. '--org %s' will be ignored", org, o.DiscoveredGitURL, o.Organisation)
}
} else {
org = o.Organisation
}
return org
}