github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/create/pr/step_create_pr_repositories.go (about) 1 package pr 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path/filepath" 7 "strings" 8 9 "github.com/olli-ai/jx/v2/pkg/cmd/opts/step" 10 11 "github.com/ghodss/yaml" 12 "github.com/olli-ai/jx/v2/pkg/gits" 13 "github.com/olli-ai/jx/v2/pkg/kube" 14 "github.com/pkg/errors" 15 16 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 17 18 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 19 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 20 "github.com/olli-ai/jx/v2/pkg/util" 21 "github.com/spf13/cobra" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 ) 24 25 var ( 26 createPullRequestRepositoriesLong = templates.LongDesc(` 27 Creates a Pull Request on a 'jx boot' git repository to mirror all the SourceRepository CRDs into the repositories Chart 28 `) 29 30 createPullRequestRepositoriesExample = templates.Examples(` 31 `) 32 ) 33 34 // StepCreatePullRequestRepositoriesOptions contains the command line flags 35 type StepCreatePullRequestRepositoriesOptions struct { 36 StepCreatePrOptions 37 } 38 39 // NewCmdStepCreatePullRequestRepositories Creates a new Command object 40 func NewCmdStepCreatePullRequestRepositories(commonOpts *opts.CommonOptions) *cobra.Command { 41 options := &StepCreatePullRequestRepositoriesOptions{ 42 StepCreatePrOptions: StepCreatePrOptions{ 43 StepCreateOptions: step.StepCreateOptions{ 44 StepOptions: step.StepOptions{ 45 CommonOptions: commonOpts, 46 }, 47 }, 48 }, 49 } 50 51 cmd := &cobra.Command{ 52 Use: "repositories", 53 Short: "Creates a Pull Request on a 'jx boot' git repository to mirror all the SourceRepository CRDs into the repositories Chart", 54 Long: createPullRequestRepositoriesLong, 55 Example: createPullRequestRepositoriesExample, 56 Aliases: []string{"repos"}, 57 Run: func(cmd *cobra.Command, args []string) { 58 options.Cmd = cmd 59 options.Args = args 60 err := options.Run() 61 helper.CheckErr(err) 62 }, 63 } 64 AddStepCreatePrFlags(cmd, &options.StepCreatePrOptions) 65 return cmd 66 } 67 68 // ValidateRepositoriesOptions validates the common options for repositories pr steps 69 func (o *StepCreatePullRequestRepositoriesOptions) ValidateRepositoriesOptions() error { 70 if len(o.GitURLs) == 0 { 71 // lets default to the dev environment git repository 72 devEnv, _, err := o.DevEnvAndTeamSettings() 73 if err != nil { 74 return errors.Wrapf(err, "no --repo specified so trying to find the 'dev' Environment to default the repository but cannot find it") 75 } 76 o.GitURLs = []string{devEnv.Spec.Source.URL} 77 } 78 if err := o.ValidateOptions(true); err != nil { 79 return errors.WithStack(err) 80 } 81 82 return nil 83 } 84 85 // Run implements this command 86 func (o *StepCreatePullRequestRepositoriesOptions) Run() error { 87 if err := o.ValidateRepositoriesOptions(); err != nil { 88 return errors.WithStack(err) 89 } 90 jxClient, ns, err := o.JXClientAndDevNamespace() 91 if err != nil { 92 return err 93 } 94 srList, err := jxClient.JenkinsV1().SourceRepositories(ns).List(metav1.ListOptions{}) 95 if err != nil { 96 return errors.Wrapf(err, "failed to query the SourceRepository resources in namespace %s", ns) 97 } 98 99 err = o.CreatePullRequest("repositories", 100 func(dir string, gitInfo *gits.GitRepository) ([]string, error) { 101 outDir := filepath.Join(dir, "repositories", "templates") 102 exists, err := util.DirExists(outDir) 103 if err != nil { 104 return nil, errors.Wrapf(err, "failed to check if output dir exists %s", outDir) 105 } 106 if !exists { 107 return nil, fmt.Errorf("output dir %s does not exist", outDir) 108 } 109 110 for _, sr := range srList.Items { 111 sourceRepository := sr 112 labels := sourceRepository.Labels 113 if labels != nil { 114 if strings.ToLower(labels[kube.LabelGitSync]) == "false" { 115 continue 116 } 117 } 118 sourceRepository.ObjectMeta = o.emptyObjectMeta(&sourceRepository.ObjectMeta) 119 120 data, err := yaml.Marshal(&sourceRepository) 121 if err != nil { 122 return nil, errors.Wrapf(err, "failed to marshal SourceRepository %s to YAML", sourceRepository.Name) 123 } 124 125 fileName := filepath.Join(outDir, sourceRepository.Name+".yaml") 126 err = ioutil.WriteFile(fileName, data, util.DefaultWritePermissions) 127 if err != nil { 128 return nil, errors.Wrapf(err, "failed to write file %s for SourceRepository %s to YAML", fileName, sourceRepository.Name) 129 } 130 } 131 return nil, nil 132 }) 133 if err != nil { 134 return errors.WithStack(err) 135 } 136 return nil 137 } 138 139 // emptyObjectMeta lets return a clean ObjectMeta without any cluster or transient specific values 140 func (o *StepCreatePullRequestRepositoriesOptions) emptyObjectMeta(md *metav1.ObjectMeta) metav1.ObjectMeta { 141 return metav1.ObjectMeta{ 142 Name: md.Name, 143 } 144 }