-
Notifications
You must be signed in to change notification settings - Fork 784
/
step_create_pr_repositories.go
143 lines (125 loc) · 4.25 KB
/
step_create_pr_repositories.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
package pr
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
createPullRequestRepositoriesLong = templates.LongDesc(`
Creates a Pull Request on a 'jx boot' git repository to mirror all the SourceRepository CRDs into the repositories Chart
`)
createPullRequestRepositoriesExample = templates.Examples(`
`)
)
// StepCreatePullRequestRepositoriesOptions contains the command line flags
type StepCreatePullRequestRepositoriesOptions struct {
StepCreatePrOptions
}
// NewCmdStepCreatePullRequestRepositories Creates a new Command object
func NewCmdStepCreatePullRequestRepositories(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepCreatePullRequestRepositoriesOptions{
StepCreatePrOptions: StepCreatePrOptions{
StepCreateOptions: step.StepCreateOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
},
},
}
cmd := &cobra.Command{
Use: "repositories",
Short: "Creates a Pull Request on a 'jx boot' git repository to mirror all the SourceRepository CRDs into the repositories Chart",
Long: createPullRequestRepositoriesLong,
Example: createPullRequestRepositoriesExample,
Aliases: []string{"repos"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
AddStepCreatePrFlags(cmd, &options.StepCreatePrOptions)
return cmd
}
// ValidateRepositoriesOptions validates the common options for repositories pr steps
func (o *StepCreatePullRequestRepositoriesOptions) ValidateRepositoriesOptions() error {
if len(o.GitURLs) == 0 {
// lets default to the dev environment git repository
devEnv, _, err := o.DevEnvAndTeamSettings()
if err != nil {
return errors.Wrapf(err, "no --repo specified so trying to find the 'dev' Environment to default the repository but cannot find it")
}
o.GitURLs = []string{devEnv.Spec.Source.URL}
}
if err := o.ValidateOptions(true); err != nil {
return errors.WithStack(err)
}
return nil
}
// Run implements this command
func (o *StepCreatePullRequestRepositoriesOptions) Run() error {
if err := o.ValidateRepositoriesOptions(); err != nil {
return errors.WithStack(err)
}
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
srList, err := jxClient.JenkinsV1().SourceRepositories(ns).List(metav1.ListOptions{})
if err != nil {
return errors.Wrapf(err, "failed to query the SourceRepository resources in namespace %s", ns)
}
err = o.CreatePullRequest("repositories",
func(dir string, gitInfo *gits.GitRepository) ([]string, error) {
outDir := filepath.Join(dir, "repositories", "templates")
exists, err := util.DirExists(outDir)
if err != nil {
return nil, errors.Wrapf(err, "failed to check if output dir exists %s", outDir)
}
if !exists {
return nil, fmt.Errorf("output dir %s does not exist", outDir)
}
for _, sr := range srList.Items {
labels := sr.Labels
if labels != nil {
if strings.ToLower(labels[kube.LabelGitSync]) == "false" {
continue
}
}
sr.ObjectMeta = o.emptyObjectMeta(&sr.ObjectMeta)
data, err := yaml.Marshal(&sr)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal SourceRepository %s to YAML", sr.Name)
}
fileName := filepath.Join(outDir, sr.Name+".yaml")
err = ioutil.WriteFile(fileName, data, util.DefaultWritePermissions)
if err != nil {
return nil, errors.Wrapf(err, "failed to write file %s for SourceRepository %s to YAML", fileName, sr.Name)
}
}
return nil, nil
})
if err != nil {
return errors.WithStack(err)
}
return nil
}
// emptyObjectMeta lets return a clean ObjectMeta without any cluster or transient specific values
func (o *StepCreatePullRequestRepositoriesOptions) emptyObjectMeta(md *metav1.ObjectMeta) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: md.Name,
}
}