-
Notifications
You must be signed in to change notification settings - Fork 786
/
common_environments.go
227 lines (204 loc) · 6.28 KB
/
common_environments.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
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/uuid"
)
// ModifyRequirementsFn callback for modifying requirements
type ModifyRequirementsFn func(requirements *helm.Requirements) error
// ConfigureGitFolderFn callback to optionally configure git before its used for creating commits and PRs
type ConfigureGitFolderFn func(dir string, gitInfo *gits.GitRepositoryInfo, gitAdapter gits.Gitter) error
type CreateEnvPullRequestFn func(env *v1.Environment, modifyRequirementsFn ModifyRequirementsFn, branchNameText string, title string, message string, pullRequestInfo *ReleasePullRequestInfo) (*ReleasePullRequestInfo, error)
func (o *CommonOptions) createEnvironmentPullRequest(env *v1.Environment, modifyRequirementsFn ModifyRequirementsFn, branchNameText string, title string, message string, pullRequestInfo *ReleasePullRequestInfo, configGitFn ConfigureGitFolderFn) (*ReleasePullRequestInfo, error) {
var answer *ReleasePullRequestInfo
source := &env.Spec.Source
gitURL := source.URL
if gitURL == "" {
return answer, fmt.Errorf("No source git URL")
}
gitInfo, err := gits.ParseGitURL(gitURL)
if err != nil {
return answer, err
}
environmentsDir, err := util.EnvironmentsDir()
if err != nil {
return answer, err
}
dir := filepath.Join(environmentsDir, gitInfo.Organisation, gitInfo.Name)
// now lets clone the fork and push it...
exists, err := util.FileExists(dir)
if err != nil {
return answer, err
}
branchName := o.Git().ConvertToValidBranchName(branchNameText)
base := source.Ref
if base == "" {
base = "master"
}
if exists {
if configGitFn != nil {
err = configGitFn(dir, gitInfo, o.Git())
if err != nil {
return answer, err
}
}
// lets check the git remote URL is setup correctly
err = o.Git().SetRemoteURL(dir, "origin", gitURL)
if err != nil {
return answer, err
}
err = o.Git().Stash(dir)
if err != nil {
return answer, err
}
err = o.Git().Checkout(dir, base)
if err != nil {
return answer, err
}
err = o.Git().Pull(dir)
if err != nil {
return answer, err
}
} else {
err := os.MkdirAll(dir, DefaultWritePermissions)
if err != nil {
return answer, fmt.Errorf("Failed to create directory %s due to %s", dir, err)
}
err = o.Git().Clone(gitURL, dir)
if err != nil {
return answer, err
}
if configGitFn != nil {
err = configGitFn(dir, gitInfo, o.Git())
if err != nil {
return answer, err
}
}
if base != "master" {
err = o.Git().Checkout(dir, base)
if err != nil {
return answer, err
}
}
// TODO lets fork if required???
}
branchNames, err := o.Git().RemoteBranchNames(dir, "remotes/origin/")
if err != nil {
return answer, fmt.Errorf("Failed to load remote branch names: %s", err)
}
//log.Infof("Found remote branch names %s\n", strings.Join(branchNames, ", "))
if util.StringArrayIndex(branchNames, branchName) >= 0 {
// lets append a UUID as the branch name already exists
branchName += "-" + string(uuid.NewUUID())
}
err = o.Git().CreateBranch(dir, branchName)
if err != nil {
return answer, err
}
err = o.Git().Checkout(dir, branchName)
if err != nil {
return answer, err
}
requirementsFile, err := helm.FindRequirementsFileName(dir)
if err != nil {
return answer, err
}
requirements, err := helm.LoadRequirementsFile(requirementsFile)
if err != nil {
return answer, err
}
err = modifyRequirementsFn(requirements)
err = helm.SaveRequirementsFile(requirementsFile, requirements)
err = o.Git().Add(dir, "*", "*/*")
if err != nil {
return answer, err
}
changed, err := o.Git().HasChanges(dir)
if err != nil {
return answer, err
}
if !changed {
log.Warnf("%s\n", "No changes made to the GitOps Environment source code. Code must be up to date!")
return answer, nil
}
err = o.Git().CommitDir(dir, message)
if err != nil {
return answer, err
}
// lets rebase an existing PR
if pullRequestInfo != nil {
remoteBranch := pullRequestInfo.PullRequestArguments.Head
err = o.Git().ForcePushBranch(dir, branchName, remoteBranch)
return pullRequestInfo, err
}
err = o.Git().Push(dir)
if err != nil {
return answer, err
}
authConfigSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return answer, err
}
gitKind, err := o.GitServerKind(gitInfo)
if err != nil {
return answer, err
}
provider, err := gitInfo.PickOrCreateProvider(authConfigSvc, "user name to submit the Pull Request", o.BatchMode, gitKind, o.Git(), o.In, o.Out, o.Err)
if err != nil {
return answer, err
}
gha := &gits.GitPullRequestArguments{
GitRepositoryInfo: gitInfo,
Title: title,
Body: message,
Base: base,
Head: branchName,
}
pr, err := provider.CreatePullRequest(gha)
if err != nil {
return answer, err
}
log.Infof("Created Pull Request: %s\n\n", util.ColorInfo(pr.URL))
return &ReleasePullRequestInfo{
GitProvider: provider,
PullRequest: pr,
PullRequestArguments: gha,
}, nil
}
func (o *CommonOptions) registerEnvironmentCRD() error {
apisClient, err := o.Factory.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterEnvironmentCRD(apisClient)
return err
}
// modifyDevEnvironment performs some mutation on the Development environemnt to modify team settings
func (o *CommonOptions) modifyDevEnvironment(jxClient versioned.Interface, ns string, fn func(env *v1.Environment) error) error {
env, err := kube.EnsureDevEnvironmentSetup(jxClient, ns)
if err != nil {
return errors.Wrapf(err, "failed to ensure that dev environment is setup for namespace '%s'", ns)
}
if env == nil {
return fmt.Errorf("No Development environment found in namespace %s", ns)
}
err = fn(env)
if err != nil {
return errors.Wrap(err, "failed to call the callback function for dev environment")
}
_, err = jxClient.JenkinsV1().Environments(ns).Update(env)
if err != nil {
return fmt.Errorf("Failed to update Development environment in namespace %s: %s", ns, err)
}
log.Infof("Updated the team settings in namespace %s\n", ns)
return nil
}