diff --git a/pkg/apps/gitops.go b/pkg/apps/gitops.go index c32d3be080..e3ded3ce6c 100644 --- a/pkg/apps/gitops.go +++ b/pkg/apps/gitops.go @@ -42,7 +42,7 @@ func (o *GitOpsOptions) AddApp(app string, dir string, version string, repositor GitProvider: o.GitProvider, } - info, err := options.Create(o.DevEnv, o.EnvironmentsDir, &details, nil, "", autoMerge) + info, err := options.Create(o.DevEnv, dir, &details, nil, "", autoMerge) if err != nil { return errors.Wrapf(err, "creating pr for %s", app) } @@ -104,7 +104,12 @@ func (o *GitOpsOptions) UpgradeApp(app string, version string, repository string o.Helmer, inspectChartFunc, o.Verbose, o.valuesFiles), GitProvider: o.GitProvider, } - _, err = options.Create(o.DevEnv, o.EnvironmentsDir, &details, nil, app, autoMerge) + dir, err := ioutil.TempDir("", "create-pr") + if err != nil { + return err + } + defer os.RemoveAll(dir) + _, err = options.Create(o.DevEnv, dir, &details, nil, app, autoMerge) if err != nil { return err } @@ -156,7 +161,12 @@ func (o *GitOpsOptions) DeleteApp(app string, alias string, autoMerge bool) erro GitProvider: o.GitProvider, } - info, err := options.Create(o.DevEnv, o.EnvironmentsDir, &details, nil, "", autoMerge) + dir, err := ioutil.TempDir("", "create-pr") + if err != nil { + return err + } + defer os.RemoveAll(dir) + info, err := options.Create(o.DevEnv, dir, &details, nil, "", autoMerge) if err != nil { return err } @@ -166,9 +176,18 @@ func (o *GitOpsOptions) DeleteApp(app string, alias string, autoMerge bool) erro // GetApps retrieves all the apps information for the given appNames from the repository and / or the CRD API func (o *GitOpsOptions) GetApps(appNames map[string]bool, expandFn func([]string) (*v1.AppList, error)) (*v1.AppList, error) { - dir, _, _, _, err := gits.ForkAndPullRepo(o.DevEnv.Spec.Source.URL, o.EnvironmentsDir, o.DevEnv.Spec.Source.Ref, "master", o.GitProvider, o.Gitter, "") + dir, err := ioutil.TempDir("", "create-pr") + if err != nil { + return nil, err + } + defer os.RemoveAll(dir) + err = o.Gitter.Clone(o.DevEnv.Spec.Source.URL, dir) + if err != nil { + return nil, errors.Wrapf(err, "failed to clone %s to dir %s", o.DevEnv.Spec.Source.URL, dir) + } + err = o.Gitter.Checkout(dir, o.DevEnv.Spec.Source.Ref) if err != nil { - return nil, errors.Wrapf(err, "couldn't pull the environment repository from %s", o.DevEnv.Name) + return nil, errors.Wrapf(err, "failed to checkout %s to dir %s", o.DevEnv.Spec.Source.Ref, dir) } envDir := filepath.Join(dir, helm.DefaultEnvironmentChartDir) diff --git a/pkg/apps/install.go b/pkg/apps/install.go index 9110cd0561..83db8f266c 100644 --- a/pkg/apps/install.go +++ b/pkg/apps/install.go @@ -104,7 +104,12 @@ func (o *InstallOptions) AddApp(app string, version string, repository string, u opts := GitOpsOptions{ InstallOptions: o, } - err := opts.AddApp(chartDetails.Name, dir, chartDetails.Version, repository, alias, o.AutoMerge) + dir, err := ioutil.TempDir("", "create-pr") + if err != nil { + return err + } + defer os.RemoveAll(dir) + err = opts.AddApp(chartDetails.Name, dir, chartDetails.Version, repository, alias, o.AutoMerge) if err != nil { return errors.Wrapf(err, "adding app %s version %s with alias %s using gitops", chartName, version, alias) } diff --git a/pkg/cmd/add/add_app.go b/pkg/cmd/add/add_app.go index 3708e8f861..58d41a5525 100644 --- a/pkg/cmd/add/add_app.go +++ b/pkg/cmd/add/add_app.go @@ -163,11 +163,6 @@ func (o *AddAppOptions) Run() error { return util.InvalidOptionf(optionValues, o.SetValues, "no more than one --%s can be specified when using GitOps for your dev environment", optionValues) } - environmentsDir, err := o.EnvironmentsDir() - if err != nil { - return errors.Wrapf(err, "getting environments dir") - } - installOpts.EnvironmentsDir = environmentsDir gitProvider, _, err := o.CreateGitProviderForURLWithoutKind(o.DevEnv.Spec.Source.URL) if err != nil { diff --git a/pkg/cmd/deletecmd/delete_app.go b/pkg/cmd/deletecmd/delete_app.go index e06e7b032f..97f64af5d1 100644 --- a/pkg/cmd/deletecmd/delete_app.go +++ b/pkg/cmd/deletecmd/delete_app.go @@ -45,6 +45,7 @@ type DeleteAppOptions struct { Namespace string Purge bool Alias string + AutoMerge bool } // NewCmdDeleteApp creates a command object for this command @@ -73,6 +74,7 @@ func NewCmdDeleteApp(commonOpts *opts.CommonOptions) *cobra.Command { cmd.Flags().StringVarP(&o.Namespace, opts.OptionNamespace, "n", defaultNamespace, "The Namespace to install into (available when NOT using GitOps for your dev environment)") cmd.Flags().StringVarP(&o.Alias, opts.OptionAlias, "", "", "An alias to use for the app (available when using GitOps for your dev environment)") + cmd.Flags().BoolVarP(&o.AutoMerge, "auto-merge", "", false, "Automatically merge GitOps pull requests that pass CI") return cmd } @@ -88,6 +90,7 @@ func (o *DeleteAppOptions) Run() error { GitOps: o.GitOps, BatchMode: o.BatchMode, Helmer: o.Helm(), + AutoMerge: o.AutoMerge, } if o.GitOps { @@ -102,13 +105,8 @@ func (o *DeleteAppOptions) Run() error { if err != nil { return errors.Wrapf(err, "creating git provider for %s", o.DevEnv.Spec.Source.URL) } - environmentsDir, err := o.EnvironmentsDir() - if err != nil { - return errors.Wrapf(err, "getting environments dir") - } installOptions.GitProvider = gitProvider installOptions.Gitter = o.Git() - installOptions.EnvironmentsDir = environmentsDir } if !o.GitOps { err := o.EnsureHelm() diff --git a/pkg/cmd/get/get_apps.go b/pkg/cmd/get/get_apps.go index 570017cd30..72058fada0 100644 --- a/pkg/cmd/get/get_apps.go +++ b/pkg/cmd/get/get_apps.go @@ -150,10 +150,8 @@ func (o *GetAppsOptions) Run() error { if err != nil { return errors.Wrapf(err, "creating git provider for %s", o.DevEnv.Spec.Source.URL) } - environmentsDir := envsDir installOptions.GitProvider = gitProvider installOptions.Gitter = o.Git() - installOptions.EnvironmentsDir = environmentsDir } apps, err := installOptions.GetApps(o.Args) diff --git a/pkg/cmd/upgrade/upgrade_apps.go b/pkg/cmd/upgrade/upgrade_apps.go index ae4a9316b0..2158857170 100644 --- a/pkg/cmd/upgrade/upgrade_apps.go +++ b/pkg/cmd/upgrade/upgrade_apps.go @@ -158,11 +158,6 @@ func (o *UpgradeAppsOptions) Run() error { if !o.HelmUpdate { return util.InvalidOptionf(optionHelmUpdate, o.HelmUpdate, msg, optionHelmUpdate) } - environmentsDir, err := o.EnvironmentsDir() - if err != nil { - return errors.Wrapf(err, "getting environments dir") - } - installOpts.EnvironmentsDir = environmentsDir gitProvider, _, err := o.CreateGitProviderForURLWithoutKind(o.DevEnv.Spec.Source.URL) if err != nil { diff --git a/pkg/environments/gitops.go b/pkg/environments/gitops.go index f6f8c42c64..097fc01429 100644 --- a/pkg/environments/gitops.go +++ b/pkg/environments/gitops.go @@ -53,14 +53,13 @@ type EnvironmentPullRequestOptions struct { // the message as the body for both the commit and the pull request, // and the pullRequestInfo for any existing PR that exists to modify the environment that we want to merge these // changes into. -func (o *EnvironmentPullRequestOptions) Create(env *jenkinsv1.Environment, environmentsDir string, +func (o *EnvironmentPullRequestOptions) Create(env *jenkinsv1.Environment, prDir string, pullRequestDetails *gits.PullRequestDetails, filter *gits.PullRequestFilter, chartName string, autoMerge bool) (*gits.PullRequestInfo, error) { - dir := filepath.Join(environmentsDir, env.Name) - dir, base, upstreamRepo, forkURL, err := gits.ForkAndPullRepo(env.Spec.Source.URL, dir, env.Spec.Source.Ref, pullRequestDetails.BranchName, o.GitProvider, o.Gitter, "") + dir, base, upstreamRepo, forkURL, err := gits.ForkAndPullRepo(env.Spec.Source.URL, prDir, env.Spec.Source.Ref, pullRequestDetails.BranchName, o.GitProvider, o.Gitter, "") if err != nil { return nil, errors.Wrapf(err, "pulling environment repo %s into %s", env.Spec.Source.URL, - environmentsDir) + prDir) } err = ModifyChartFiles(dir, pullRequestDetails, o.ModifyChartFn, chartName)