From b8be1fd6e34028b402ef6992388ef5fc1b4309bd Mon Sep 17 00:00:00 2001 From: Noam Gal Date: Tue, 20 Dec 2022 10:12:50 +0200 Subject: [PATCH] CR-15942-git-token (#660) * removed legacy git-source create/edit/delete * bumped version to 0.1.22 --- Makefile | 2 +- cmd/commands/git-source.go | 282 +++++----------------- docs/commands/cli-v2_git-source_create.md | 5 - docs/commands/cli-v2_git-source_delete.md | 7 +- docs/commands/cli-v2_git-source_edit.md | 6 - docs/releases/release_notes.md | 4 +- 6 files changed, 61 insertions(+), 245 deletions(-) diff --git a/Makefile b/Makefile index 1af42255..9dcdf74d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=v0.1.21 +VERSION=v0.1.22 OUT_DIR=dist YEAR?=$(shell date +"%Y") diff --git a/cmd/commands/git-source.go b/cmd/commands/git-source.go index a1236705..e254e3e0 100644 --- a/cmd/commands/git-source.go +++ b/cmd/commands/git-source.go @@ -30,12 +30,9 @@ import ( routingutil "github.com/codefresh-io/cli-v2/pkg/util/routing" wfutil "github.com/codefresh-io/cli-v2/pkg/util/workflow" - "github.com/Masterminds/semver/v3" - apcmd "github.com/argoproj-labs/argocd-autopilot/cmd/commands" "github.com/argoproj-labs/argocd-autopilot/pkg/application" "github.com/argoproj-labs/argocd-autopilot/pkg/fs" "github.com/argoproj-labs/argocd-autopilot/pkg/git" - apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store" aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util" apicommon "github.com/argoproj/argo-events/pkg/apis/common" eventsourcereg "github.com/argoproj/argo-events/pkg/apis/eventsource" @@ -76,19 +73,17 @@ type ( } GitSourceDeleteOptions struct { - RuntimeName string - GsName string - InsCloneOpts *git.CloneOptions - Timeout time.Duration + RuntimeName string + GsName string + Timeout time.Duration } GitSourceEditOptions struct { - RuntimeName string - GsName string - InsCloneOpts *git.CloneOptions - GsCloneOpts *git.CloneOptions - Include *string - Exclude *string + RuntimeName string + GsName string + GsCloneOpts *git.CloneOptions + Include *string + Exclude *string } gitSourceCalendarDemoPipelineOptions struct { @@ -112,16 +107,8 @@ type ( gatewayNamespace string useGatewayAPI bool } - - dirConfig struct { - application.Config - Exclude string `json:"exclude"` - Include string `json:"include"` - } ) -var appProxyGitSourceSupport = semver.MustParse("0.0.328") - func NewGitSourceCommand() *cobra.Command { cmd := &cobra.Command{ Use: "git-source", @@ -144,12 +131,11 @@ func NewGitSourceCommand() *cobra.Command { func NewGitSourceCreateCommand() *cobra.Command { var ( - insCloneOpts *git.CloneOptions - gsCloneOpts *git.CloneOptions - gitProvider cfgit.Provider - createRepo bool - include string - exclude string + gsCloneOpts *git.CloneOptions + gitProvider cfgit.Provider + createRepo bool + include string + exclude string ) cmd := &cobra.Command{ @@ -175,11 +161,6 @@ func NewGitSourceCreateCommand() *cobra.Command { log.G(ctx).Fatal("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name/path/to/workflow") } - err := ensureRepo(cmd, args[0], insCloneOpts, true) - if err != nil { - return err - } - isValid, err := IsValidName(args[1]) if err != nil { log.G(ctx).Fatal("failed to check the validity of the git-source name") @@ -189,15 +170,10 @@ func NewGitSourceCreateCommand() *cobra.Command { log.G(ctx).Fatal("git-source name cannot have any uppercase letters, must start with a character, end with character or number, and be shorter than 63 chars") } - if gsCloneOpts.Auth.Password == "" { - gsCloneOpts.Auth.Password = insCloneOpts.Auth.Password - } - if createRepo { gsCloneOpts.CreateIfNotExist = createRepo } - insCloneOpts.Parse() gsCloneOpts.Parse() gitProvider, err = cfgit.GetProvider(cfgit.ProviderType(gsCloneOpts.Provider), gsCloneOpts.Repo, gsCloneOpts.Auth.CertFile) @@ -211,7 +187,6 @@ func NewGitSourceCreateCommand() *cobra.Command { ctx := cmd.Context() return RunGitSourceCreate(ctx, &GitSourceCreateOptions{ - InsCloneOpts: insCloneOpts, GsCloneOpts: gsCloneOpts, GitProvider: gitProvider, GsName: args[1], @@ -227,7 +202,6 @@ func NewGitSourceCreateCommand() *cobra.Command { cmd.Flags().StringVar(&include, "include", "", "files to include. can be either filenames or a glob") cmd.Flags().StringVar(&exclude, "exclude", "", "files to exclude. can be either filenames or a glob") - insCloneOpts = apu.AddCloneFlags(cmd, &apu.CloneFlagsOptions{CloneForWrite: true}) gsCloneOpts = apu.AddCloneFlags(cmd, &apu.CloneFlagsOptions{ Prefix: "git-src", Optional: true, @@ -237,16 +211,6 @@ func NewGitSourceCreateCommand() *cobra.Command { } func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error { - version, err := getRuntimeVersion(ctx, opts.RuntimeName) - if err != nil { - return err - } - - if version.LessThan(appProxyGitSourceSupport) { - log.G(ctx).Warnf("runtime \"%s\" is using a deprecated git-source api. Versions %s and up use the app-proxy for this command. You are using version: %s", opts.RuntimeName, appProxyGitSourceSupport, version.String()) - return legacyGitSourceCreate(ctx, opts) - } - appProxy, err := cfConfig.NewClient().AppProxy(ctx, opts.RuntimeName, store.Get().InsecureIngressHost) if err != nil { return err @@ -264,11 +228,8 @@ func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error Include: &opts.Include, Exclude: &opts.Exclude, }) - if err != nil { - log.G(ctx).Errorf("failed to create git-source: %s", err.Error()) - log.G(ctx).Info("attempting creation of git-source without using app-proxy") - return legacyGitSourceCreate(ctx, opts) + return fmt.Errorf("failed to create git-source: %w", err) } log.G(ctx).Infof("Successfully created git-source: \"%s\"", opts.GsName) @@ -390,10 +351,6 @@ func RunGitSourceList(ctx context.Context, runtimeName string, includeInternal b } func NewGitSourceDeleteCommand() *cobra.Command { - var ( - insCloneOpts *git.CloneOptions - ) - cmd := &cobra.Command{ Use: "delete RUNTIME_NAME GITSOURCE_NAME", Short: "delete a git-source from a runtime", @@ -401,7 +358,7 @@ func NewGitSourceDeleteCommand() *cobra.Command { Example: util.Doc(` git-source delete runtime_name git-source_name `), - PreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(_ *cobra.Command, args []string) error { store.Get().Silent = true if len(args) < 1 { @@ -412,42 +369,23 @@ func NewGitSourceDeleteCommand() *cobra.Command { return fmt.Errorf("must enter git-source name") } - err := ensureRepo(cmd, args[0], insCloneOpts, true) - if err != nil { - return err - } - - insCloneOpts.Parse() return nil }, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() return RunGitSourceDelete(ctx, &GitSourceDeleteOptions{ - RuntimeName: args[0], - GsName: args[1], - Timeout: aputil.MustParseDuration(cmd.Flag("request-timeout").Value.String()), - InsCloneOpts: insCloneOpts, + RuntimeName: args[0], + GsName: args[1], + Timeout: aputil.MustParseDuration(cmd.Flag("request-timeout").Value.String()), }) }, } - insCloneOpts = apu.AddCloneFlags(cmd, &apu.CloneFlagsOptions{CloneForWrite: true}) - return cmd } func RunGitSourceDelete(ctx context.Context, opts *GitSourceDeleteOptions) error { - version, err := getRuntimeVersion(ctx, opts.RuntimeName) - if err != nil { - return err - } - - if version.LessThan(appProxyGitSourceSupport) { - log.G(ctx).Warnf("runtime \"%s\" is using a depracated git-source api. Versions %s and up use the app-proxy for this command. You are using version: %s", opts.RuntimeName, appProxyGitSourceSupport, version.String()) - return legacyGitSourceDelete(ctx, opts) - } - appProxy, err := cfConfig.NewClient().AppProxy(ctx, opts.RuntimeName, store.Get().InsecureIngressHost) if err != nil { return err @@ -455,18 +393,7 @@ func RunGitSourceDelete(ctx context.Context, opts *GitSourceDeleteOptions) error err = appProxy.AppProxyGitSources().Delete(ctx, opts.GsName) if err != nil { - log.G(ctx).Errorf("failed to delete git-source: %s", err.Error()) - log.G(ctx).Info("attempting deletion of git-source without using app-proxy") - err = apcmd.RunAppDelete(ctx, &apcmd.AppDeleteOptions{ - CloneOpts: opts.InsCloneOpts, - ProjectName: opts.RuntimeName, - AppName: opts.GsName, - Global: false, - }) - - if err != nil { - return fmt.Errorf("failed to delete the git-source %s. Err: %w", opts.GsName, err) - } + return fmt.Errorf("failed to delete git-source: %w", err) } log.G(ctx).Infof("Successfully deleted the git-source: %s", opts.GsName) @@ -475,10 +402,9 @@ func RunGitSourceDelete(ctx context.Context, opts *GitSourceDeleteOptions) error func NewGitSourceEditCommand() *cobra.Command { var ( - insCloneOpts *git.CloneOptions - gsCloneOpts *git.CloneOptions - include string - exclude string + gsCloneOpts *git.CloneOptions + include string + exclude string ) cmd := &cobra.Command{ @@ -488,7 +414,7 @@ func NewGitSourceEditCommand() *cobra.Command { Example: util.Doc(` git-source edit runtime_name git-source_name --git-src-repo https://github.com/owner/repo-name.git/path/to/dir `), - PreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(_ *cobra.Command, args []string) error { store.Get().Silent = true if len(args) < 1 { @@ -503,12 +429,6 @@ func NewGitSourceEditCommand() *cobra.Command { return fmt.Errorf("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name.git/path/to/dir") } - err := ensureRepo(cmd, args[0], insCloneOpts, true) - if err != nil { - return err - } - - insCloneOpts.Parse() gsCloneOpts.Parse() return nil }, @@ -516,10 +436,9 @@ func NewGitSourceEditCommand() *cobra.Command { ctx := cmd.Context() opts := &GitSourceEditOptions{ - RuntimeName: args[0], - GsName: args[1], - InsCloneOpts: insCloneOpts, - GsCloneOpts: gsCloneOpts, + RuntimeName: args[0], + GsName: args[1], + GsCloneOpts: gsCloneOpts, } if cmd.Flags().Changed("include") { opts.Include = &include @@ -536,31 +455,15 @@ func NewGitSourceEditCommand() *cobra.Command { cmd.Flags().StringVar(&include, "include", "", "files to include. can be either filenames or a glob") cmd.Flags().StringVar(&exclude, "exclude", "", "files to exclude. can be either filenames or a glob") - insCloneOpts = apu.AddCloneFlags(cmd, &apu.CloneFlagsOptions{ - CreateIfNotExist: true, - CloneForWrite: true, - }) - gsCloneOpts = apu.AddCloneFlags(cmd, &apu.CloneFlagsOptions{ Prefix: "git-src", Optional: true, CreateIfNotExist: true, }) - return cmd } func RunGitSourceEdit(ctx context.Context, opts *GitSourceEditOptions) error { - version, err := getRuntimeVersion(ctx, opts.RuntimeName) - if err != nil { - return err - } - - if version.LessThan(appProxyGitSourceSupport) { - log.G(ctx).Warnf("runtime \"%s\" is using a depracated git-source api. Versions %s and up use the app-proxy for this command. You are using version: %s", opts.RuntimeName, appProxyGitSourceSupport, version.String()) - return legacyGitSourceEdit(ctx, opts) - } - appProxy, err := cfConfig.NewClient().AppProxy(ctx, opts.RuntimeName, store.Get().InsecureIngressHost) if err != nil { return err @@ -572,11 +475,8 @@ func RunGitSourceEdit(ctx context.Context, opts *GitSourceEditOptions) error { Include: opts.Include, Exclude: opts.Exclude, }) - if err != nil { - log.G(ctx).Errorf("failed to edit git-source: %s", err.Error()) - log.G(ctx).Info("attempting edit of git-source without using app-proxy") - return legacyGitSourceEdit(ctx, opts) + return fmt.Errorf("failed to edit git-source: %w", err) } log.G(ctx).Infof("Successfully edited git-source: \"%s\"", opts.GsName) @@ -605,24 +505,24 @@ func createDemoResources(ctx context.Context, opts *GitSourceCreateOptions, gsRe return fmt.Errorf("failed to create calendar example pipeline. Error: %w", err) } - err = createDemoGitPipeline(&gitSourceGitDemoPipelineOptions{ - runtimeName: opts.RuntimeName, - gsCloneOpts: opts.GsCloneOpts, - gitProvider: opts.GitProvider, - gsFs: gsFs, - hostName: opts.HostName, - skipIngress: opts.SkipIngress, - ingressHost: opts.IngressHost, - ingressClass: opts.IngressClass, - ingressController: opts.IngressController, - accessMode: opts.AccessMode, - gatewayName: opts.GatewayName, - gatewayNamespace: opts.GatewayNamespace, - useGatewayAPI: opts.useGatewayAPI, - }) - if err != nil { - return fmt.Errorf("failed to create github example pipeline. Error: %w", err) - } + err = createDemoGitPipeline(&gitSourceGitDemoPipelineOptions{ + runtimeName: opts.RuntimeName, + gsCloneOpts: opts.GsCloneOpts, + gitProvider: opts.GitProvider, + gsFs: gsFs, + hostName: opts.HostName, + skipIngress: opts.SkipIngress, + ingressHost: opts.IngressHost, + ingressClass: opts.IngressClass, + ingressController: opts.IngressController, + accessMode: opts.AccessMode, + gatewayName: opts.GatewayName, + gatewayNamespace: opts.GatewayNamespace, + useGatewayAPI: opts.useGatewayAPI, + }) + if err != nil { + return fmt.Errorf("failed to create github example pipeline. Error: %w", err) + } commitMsg := fmt.Sprintf("Created demo pipelines in %s Directory", opts.GsCloneOpts.Path()) @@ -715,7 +615,6 @@ func createDemoCalendarSensor() *sensorsv1alpha1.Sensor { } return createDemoSensor(name, triggers, dependencies) - } func createDemoCalendarTrigger() sensorsv1alpha1.Trigger { @@ -1482,19 +1381,22 @@ func deleteCommonRedundantFields(crd map[string]interface{}) { delete(metadata, "creationTimestamp") } -func getRuntimeVersion(ctx context.Context, runtimeName string) (*semver.Version, error) { - rt, err := getRuntime(ctx, runtimeName) - if err != nil { - return nil, err - } - - if rt.RuntimeVersion == nil { - return nil, fmt.Errorf("runtime \"%s\" has no version", runtimeName) +func writeObjectToYaml[Object any]( + gsFs fs.FS, + filePath string, + object Object, + cleanUpFunc func(Object) (map[string]interface{}, error), +) error { + var finalObject interface{} = object + cleanObject, err := cleanUpFunc(object) + if err == nil { + finalObject = cleanObject } - return semver.MustParse(*rt.RuntimeVersion), nil + return gsFs.WriteYamls(filePath, finalObject) } +// used only from the runtime-install flow func legacyGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error { // upsert git-source repo gsRepo, gsFs, err := opts.GsCloneOpts.GetRepo(ctx) @@ -1529,73 +1431,3 @@ func legacyGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) er log.G(ctx).Infof("Successfully created git-source: \"%s\"", opts.GsName) return nil } - -func legacyGitSourceEdit(ctx context.Context, opts *GitSourceEditOptions) error { - repo, fs, err := opts.InsCloneOpts.GetRepo(ctx) - if err != nil { - return fmt.Errorf("failed to clone the installation repo, attempting to edit git-source %s. Err: %w", opts.GsName, err) - } - - c := &dirConfig{} - fileName := fs.Join(apstore.Default.AppsDir, opts.GsName, opts.RuntimeName, "config_dir.json") - err = fs.ReadJson(fileName, c) - if err != nil { - return fmt.Errorf("failed to read the %s of git-source: %s. Err: %w", fileName, opts.GsName, err) - } - - c.Config.SrcPath = opts.GsCloneOpts.Path() - c.Config.SrcRepoURL = opts.GsCloneOpts.URL() - c.Config.SrcTargetRevision = opts.GsCloneOpts.Revision() - - if opts.Include != nil { - c.Include = *opts.Include - } - - if opts.Exclude != nil { - c.Exclude = *opts.Exclude - } - - err = fs.WriteJson(fileName, c) - if err != nil { - return fmt.Errorf("failed to write the updated %s of git-source: %s. Err: %w", fileName, opts.GsName, err) - } - - log.G(ctx).Info("Pushing updated GitSource to the installation repo") - if err := apu.PushWithMessage(ctx, repo, fmt.Sprintf("Persisted an updated git-source \"%s\"", opts.GsName)); err != nil { - return fmt.Errorf("failed to persist the updated git-source: %s. Err: %w", opts.GsName, err) - } - - log.G(ctx).Infof("Successfully edited git-source: \"%s\"", opts.GsName) - return nil -} - -func legacyGitSourceDelete(ctx context.Context, opts *GitSourceDeleteOptions) error { - err := apcmd.RunAppDelete(ctx, &apcmd.AppDeleteOptions{ - CloneOpts: opts.InsCloneOpts, - ProjectName: opts.RuntimeName, - AppName: opts.GsName, - Global: false, - }) - - if err != nil { - return fmt.Errorf("failed to delete the git-source %s. Err: %w", opts.GsName, err) - } - - log.G(ctx).Infof("Successfully deleted the git-source: %s", opts.GsName) - return nil -} - -func writeObjectToYaml[Object any]( - gsFs fs.FS, - filePath string, - object Object, - cleanUpFunc func(Object) (map[string]interface{}, error), -) error { - var finalObject interface{} = object - cleanObject, err := cleanUpFunc(object) - if err == nil { - finalObject = cleanObject - } - - return gsFs.WriteYamls(filePath, finalObject) -} diff --git a/docs/commands/cli-v2_git-source_create.md b/docs/commands/cli-v2_git-source_create.md index 69ad679f..ddb0062b 100644 --- a/docs/commands/cli-v2_git-source_create.md +++ b/docs/commands/cli-v2_git-source_create.md @@ -19,17 +19,12 @@ cli-v2 git-source create RUNTIME_NAME GITSOURCE_NAME [flags] ``` --create-repo If true, will create the specified git-source repo in case it doesn't already exist --exclude string files to exclude. can be either filenames or a glob - --git-server-crt string Git Server certificate file --git-src-git-server-crt string Git Server certificate fileGIT_SRC_ --git-src-git-token string Your git provider api token [GIT_SRC_GIT_TOKEN] --git-src-git-user string Your git provider user name [GIT_SRC_GIT_USER] (not required in GitHub) --git-src-repo string Repository URL [GIT_SRC_GIT_REPO] - -t, --git-token string Your git provider api token [GIT_TOKEN] - -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) -h, --help help for create --include string files to include. can be either filenames or a glob - --repo string Repository URL [GIT_REPO] - -b, --upsert-branch If true will try to checkout the specified branch and create it if it doesn't exist ``` ### Options inherited from parent commands diff --git a/docs/commands/cli-v2_git-source_delete.md b/docs/commands/cli-v2_git-source_delete.md index 06e64d40..e8431730 100644 --- a/docs/commands/cli-v2_git-source_delete.md +++ b/docs/commands/cli-v2_git-source_delete.md @@ -17,12 +17,7 @@ cli-v2 git-source delete RUNTIME_NAME GITSOURCE_NAME [flags] ### Options ``` - --git-server-crt string Git Server certificate file - -t, --git-token string Your git provider api token [GIT_TOKEN] - -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) - -h, --help help for delete - --repo string Repository URL [GIT_REPO] - -b, --upsert-branch If true will try to checkout the specified branch and create it if it doesn't exist + -h, --help help for delete ``` ### Options inherited from parent commands diff --git a/docs/commands/cli-v2_git-source_edit.md b/docs/commands/cli-v2_git-source_edit.md index 9e2a9166..13c31a12 100644 --- a/docs/commands/cli-v2_git-source_edit.md +++ b/docs/commands/cli-v2_git-source_edit.md @@ -18,19 +18,13 @@ cli-v2 git-source edit RUNTIME_NAME GITSOURCE_NAME [flags] ``` --exclude string files to exclude. can be either filenames or a glob - --git-server-crt string Git Server certificate file --git-src-git-server-crt string Git Server certificate fileGIT_SRC_ --git-src-git-token string Your git provider api token [GIT_SRC_GIT_TOKEN] --git-src-git-user string Your git provider user name [GIT_SRC_GIT_USER] (not required in GitHub) --git-src-provider string The git provider, one of: azure|bitbucket|bitbucket-server|gitea|github|gitlab --git-src-repo string Repository URL [GIT_SRC_GIT_REPO] - -t, --git-token string Your git provider api token [GIT_TOKEN] - -u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub) -h, --help help for edit --include string files to include. can be either filenames or a glob - --provider string The git provider, one of: azure|bitbucket|bitbucket-server|gitea|github|gitlab - --repo string Repository URL [GIT_REPO] - -b, --upsert-branch If true will try to checkout the specified branch and create it if it doesn't exist ``` ### Options inherited from parent commands diff --git a/docs/releases/release_notes.md b/docs/releases/release_notes.md index 8b1718ef..6e4969da 100644 --- a/docs/releases/release_notes.md +++ b/docs/releases/release_notes.md @@ -23,7 +23,7 @@ cf version ```bash # download and extract the binary -curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.21/cf-linux-amd64.tar.gz | tar zx +curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.22/cf-linux-amd64.tar.gz | tar zx # move the binary to your $PATH mv ./cf-linux-amd64 /usr/local/bin/cf @@ -36,7 +36,7 @@ cf version ```bash # download and extract the binary -curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.21/cf-darwin-amd64.tar.gz | tar zx +curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.1.22/cf-darwin-amd64.tar.gz | tar zx # move the binary to your $PATH mv ./cf-darwin-amd64 /usr/local/bin/cf