github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/create/pr/step_create_pr_chart.go (about) 1 package pr 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "regexp" 9 10 "github.com/olli-ai/jx/v2/pkg/cmd/opts/step" 11 12 "github.com/jenkins-x/jx-logging/pkg/log" 13 "github.com/olli-ai/jx/v2/pkg/gits" 14 "github.com/olli-ai/jx/v2/pkg/helm" 15 "github.com/pkg/errors" 16 17 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 18 19 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 20 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 21 "github.com/olli-ai/jx/v2/pkg/util" 22 "github.com/spf13/cobra" 23 ) 24 25 var ( 26 createPullRequestChartLong = templates.LongDesc(` 27 Creates a Pull Request on a git repository updating the requirements.yaml and values.yaml with the new version 28 `) 29 30 createPullRequestChartExample = templates.Examples(` 31 `) 32 ) 33 34 // StepCreatePullRequestChartsOptions contains the command line flags 35 type StepCreatePullRequestChartsOptions struct { 36 StepCreatePrOptions 37 38 Names []string 39 } 40 41 // NewCmdStepCreatePullRequestChart Creates a new Command object 42 func NewCmdStepCreatePullRequestChart(commonOpts *opts.CommonOptions) *cobra.Command { 43 options := &StepCreatePullRequestChartsOptions{ 44 StepCreatePrOptions: StepCreatePrOptions{ 45 StepCreateOptions: step.StepCreateOptions{ 46 StepOptions: step.StepOptions{ 47 CommonOptions: commonOpts, 48 }, 49 }, 50 }, 51 } 52 53 cmd := &cobra.Command{ 54 Use: "chart", 55 Short: "Creates a Pull Request on a git repository updating the Chart", 56 Long: createPullRequestChartLong, 57 Example: createPullRequestChartExample, 58 Aliases: []string{"version pullrequest"}, 59 Run: func(cmd *cobra.Command, args []string) { 60 options.Cmd = cmd 61 options.Args = args 62 err := options.Run() 63 helper.CheckErr(err) 64 }, 65 } 66 AddStepCreatePrFlags(cmd, &options.StepCreatePrOptions) 67 cmd.Flags().StringArrayVarP(&options.Names, "name", "n", make([]string, 0), "The name of the property to update") 68 return cmd 69 } 70 71 // ValidateChartsOptions validates the common options for chart pr steps 72 func (o *StepCreatePullRequestChartsOptions) ValidateChartsOptions() error { 73 if err := o.ValidateOptions(false); err != nil { 74 return errors.WithStack(err) 75 } 76 if len(o.Names) == 0 { 77 return util.MissingOption("name") 78 } 79 if o.SrcGitURL == "" { 80 log.Logger().Warnf("srcRepo is not provided so generated PR will not be correctly linked in release notesPR") 81 } 82 83 return nil 84 } 85 86 // Run implements this command 87 func (o *StepCreatePullRequestChartsOptions) Run() error { 88 if err := o.ValidateChartsOptions(); err != nil { 89 return errors.WithStack(err) 90 } 91 err := o.CreatePullRequest("chart", 92 func(dir string, gitInfo *gits.GitRepository) ([]string, error) { 93 oldVersions := make([]string, 0) 94 // walk the filepath, looking for values.yaml and requirements.yaml 95 err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { 96 base := filepath.Base(path) 97 if base == helm.RequirementsFileName { 98 requirements, err := helm.LoadRequirementsFile(path) 99 if err != nil { 100 return errors.Wrapf(err, "loading %s", path) 101 } 102 for _, name := range o.Names { 103 oldVersions = append(oldVersions, helm.UpdateRequirementsToNewVersion(requirements, name, o.Version)...) 104 } 105 err = helm.SaveFile(path, *requirements) 106 if err != nil { 107 return errors.Wrapf(err, "saving %s", path) 108 } 109 } else if base == helm.ValuesFileName { 110 values, err := ioutil.ReadFile(path) 111 if err != nil { 112 return errors.Wrapf(err, "reading %s", path) 113 } 114 newValues := string(values) 115 for _, name := range o.Names { 116 re, err := regexp.Compile(fmt.Sprintf(`(?m)^\s*Image: %s:(.*)$`, name)) 117 if err != nil { 118 return errors.WithStack(err) 119 } 120 newValues = util.ReplaceAllStringSubmatchFunc(re, newValues, func(groups []util.Group) []string { 121 answer := make([]string, 0) 122 for i := range groups { 123 oldVersions = append(oldVersions, groups[i].Value) 124 answer = append(answer, o.Version) 125 } 126 return answer 127 }) 128 } 129 err = ioutil.WriteFile(path, []byte(newValues), info.Mode()) 130 if err != nil { 131 return errors.Wrapf(err, "writing %s", path) 132 } 133 } 134 return nil 135 }) 136 if err != nil { 137 return nil, errors.WithStack(err) 138 } 139 return oldVersions, nil 140 }) 141 if err != nil { 142 return errors.WithStack(err) 143 } 144 return nil 145 }