diff --git a/cmd/commands/app.go b/cmd/commands/app.go index d36bed4d..bda08811 100644 --- a/cmd/commands/app.go +++ b/cmd/commands/app.go @@ -181,6 +181,7 @@ func RunAppCreate(ctx context.Context, opts *AppCreateOptions) error { if opts.AppsCloneOpts.Auth.Password == "" { opts.AppsCloneOpts.Auth.Username = opts.CloneOpts.Auth.Username opts.AppsCloneOpts.Auth.Password = opts.CloneOpts.Auth.Password + opts.AppsCloneOpts.Provider = opts.CloneOpts.Provider } appsRepo, appsfs, err = getRepo(ctx, opts.AppsCloneOpts) @@ -282,6 +283,7 @@ var setAppOptsDefaults = func(ctx context.Context, repofs fs.FS, opts *AppCreate cloneOpts := &git.CloneOptions{ Repo: opts.AppOpts.AppSpecifier, Auth: opts.CloneOpts.Auth, + Provider: opts.CloneOpts.Provider, FS: fs.Create(memfs.New()), } cloneOpts.Parse() diff --git a/pkg/git/provider.go b/pkg/git/provider.go index 5ded6c6e..fff3865e 100644 --- a/pkg/git/provider.go +++ b/pkg/git/provider.go @@ -32,7 +32,7 @@ type ( ProviderOptions struct { Type string Auth *Auth - Host string + RepoURL string } CreateRepoOptions struct { diff --git a/pkg/git/provider_ado.go b/pkg/git/provider_ado.go index 5c97d0a9..3cbc308b 100644 --- a/pkg/git/provider_ado.go +++ b/pkg/git/provider_ado.go @@ -41,10 +41,11 @@ const AzureHostName = "dev.azure" const timeoutTime = 10 * time.Second func newAdo(opts *ProviderOptions) (Provider, error) { - adoUrl, err := parseAdoUrl(opts.Host) + adoUrl, err := parseAdoUrl(opts.RepoURL) if err != nil { return nil, err } + connection := azuredevops.NewPatConnection(adoUrl.loginUrl, opts.Auth.Password) ctx, cancel := context.WithTimeout(context.Background(), timeoutTime) defer cancel() @@ -85,7 +86,7 @@ func (g *adoGit) GetDefaultBranch(ctx context.Context, orgRepo string) (string, project := g.adoUrl.GetProjectName() r, err := g.adoClient.GetRepository(ctx, ado.GetRepositoryArgs{ RepositoryId: &orgRepo, - Project: &project, + Project: &project, }) if err != nil { return "", err @@ -109,15 +110,17 @@ func parseAdoUrl(host string) (*adoGitUrl, error) { if err != nil { return nil, err } + var sub, project string path := strings.Split(u.Path, "/") if len(path) < 5 { return nil, fmt.Errorf("unable to parse Azure DevOps url") - } else { - // 1 since the path starts with a slash - sub = path[1] - project = path[2] } + + // 1 since the path starts with a slash + sub = path[1] + project = path[2] + loginUrl := fmt.Sprintf("%s://%s/%s", u.Scheme, u.Host, sub) return &adoGitUrl{ loginUrl: loginUrl, diff --git a/pkg/git/provider_bitbucket-server.go b/pkg/git/provider_bitbucket-server.go index a139a901..d0685b21 100644 --- a/pkg/git/provider_bitbucket-server.go +++ b/pkg/git/provider_bitbucket-server.go @@ -23,7 +23,7 @@ type ( } bitbucketServer struct { - baseUrl *url.URL + baseURL *url.URL c HttpClient opts *ProviderOptions } @@ -78,15 +78,15 @@ var ( ) func newBitbucketServer(opts *ProviderOptions) (Provider, error) { - host, _, _, _, _, _, _ := util.ParseGitUrl(opts.Host) - baseUrl, err := url.Parse(host) + host, _, _, _, _, _, _ := util.ParseGitUrl(opts.RepoURL) + baseURL, err := url.Parse(host) if err != nil { return nil, err } httpClient := &http.Client{} g := &bitbucketServer{ - baseUrl: baseUrl, + baseURL: baseURL, c: httpClient, opts: opts, } @@ -111,13 +111,13 @@ func (bbs *bitbucketServer) CreateRepository(ctx context.Context, orgRepo string } for _, link := range repo.Links.Clone { - if link.Name == bbs.baseUrl.Scheme { + if link.Name == bbs.baseURL.Scheme { return link.Href, nil } } - return "", fmt.Errorf("created repo did not contain a valid %s clone url", bbs.baseUrl.Scheme) + return "", fmt.Errorf("created repo did not contain a valid %s clone url", bbs.baseURL.Scheme) } func (bbs *bitbucketServer) GetDefaultBranch(ctx context.Context, orgRepo string) (string, error) { @@ -198,7 +198,7 @@ func (bbs *bitbucketServer) requestRest(ctx context.Context, method, urlPath str func (bbs *bitbucketServer) request(ctx context.Context, method, urlPath string, body interface{}) ([]byte, error) { var err error - urlClone := *bbs.baseUrl + urlClone := *bbs.baseURL urlClone.Path = path.Join(urlClone.Path, urlPath) bodyStr := []byte{} if body != nil { diff --git a/pkg/git/provider_bitbucket-server_test.go b/pkg/git/provider_bitbucket-server_test.go index 7921a225..736b4170 100644 --- a/pkg/git/provider_bitbucket-server_test.go +++ b/pkg/git/provider_bitbucket-server_test.go @@ -24,7 +24,7 @@ var ( } ) -func baseUrl() *url.URL { +func baseURL() *url.URL { u, _ := url.Parse("https://some.server") return u } @@ -41,10 +41,10 @@ func Test_bitbucketServer_CreateRepository(t *testing.T) { wantErr string beforeFn func(t *testing.T, c *mocks.MockHttpClient) }{ - // "Should fail if orgRepo is invalid": { - // orgRepo: "no-scm/project/repo", - // wantErr: "invalid Bitbucket url \"no-scm/project/repo.git\" - must be in the form of \"scm/[~]project-or-username/repo-name\"", - // }, + "Should fail if orgRepo is invalid": { + orgRepo: "no-scm/project/repo", + wantErr: "invalid Bitbucket url \"no-scm/project/repo\" - must be in the form of \"scm/[~]project-or-username/repo-name\"", + }, "Should fail if repos POST fails": { orgRepo: "scm/project/repo", wantErr: "some error", @@ -52,80 +52,80 @@ func Test_bitbucketServer_CreateRepository(t *testing.T) { c.EXPECT().Do(gomock.AssignableToTypeOf(&http.Request{})).Times(1).Return(nil, errors.New("some error")) }, }, - // "Should fail if returned repo doesn't have clone url": { - // orgRepo: "scm/project/repo", - // wantErr: "created repo did not contain a valid https clone url", - // beforeFn: func(_ *testing.T, c *mocks.MockHttpClient) { - // repo := &repoResponse{ - // Links: Links{ - // Clone: []Link{ - // { - // Name: "ssh", - // Href: "ssh@some.server/scm/project/repo.git", - // }, - // }, - // }, - // } - // body := createBody(repo) - // res := &http.Response{ - // StatusCode: 200, - // Body: body, - // } - // c.EXPECT().Do(gomock.AssignableToTypeOf(&http.Request{})).Times(1).Return(res, nil) - // }, - // }, - // "Should create a valid project repo": { - // orgRepo: "scm/project/repo", - // want: "https://some.server/scm/project/repo.git", - // beforeFn: func(t *testing.T, c *mocks.MockHttpClient) { - // c.EXPECT().Do(gomock.AssignableToTypeOf(&http.Request{})).Times(1).DoAndReturn(func(req *http.Request) (*http.Response, error) { - // assert.Equal(t, "POST", req.Method) - // assert.Equal(t, "https://some.server/rest/api/1.0/projects/project/repos", req.URL.String()) - // repo := &repoResponse{ - // Links: Links{ - // Clone: []Link{ - // { - // Name: "https", - // Href: "https://some.server/scm/project/repo.git", - // }, - // }, - // }, - // } - // body := createBody(repo) - // res := &http.Response{ - // StatusCode: 200, - // Body: body, - // } - // return res, nil - // }) - // }, - // }, - // "Should create a valid user repo": { - // orgRepo: "scm/~user/repo", - // want: "https://some.server/scm/~user/repo.git", - // beforeFn: func(t *testing.T, c *mocks.MockHttpClient) { - // c.EXPECT().Do(gomock.AssignableToTypeOf(&http.Request{})).Times(1).DoAndReturn(func(req *http.Request) (*http.Response, error) { - // assert.Equal(t, "POST", req.Method) - // assert.Equal(t, "https://some.server/rest/api/1.0/users/user/repos", req.URL.String()) - // repo := &repoResponse{ - // Links: Links{ - // Clone: []Link{ - // { - // Name: "https", - // Href: "https://some.server/scm/~user/repo.git", - // }, - // }, - // }, - // } - // body := createBody(repo) - // res := &http.Response{ - // StatusCode: 200, - // Body: body, - // } - // return res, nil - // }) - // }, - // }, + "Should fail if returned repo doesn't have clone url": { + orgRepo: "scm/project/repo", + wantErr: "created repo did not contain a valid https clone url", + beforeFn: func(_ *testing.T, c *mocks.MockHttpClient) { + repo := &repoResponse{ + Links: Links{ + Clone: []Link{ + { + Name: "ssh", + Href: "ssh@some.server/scm/project/repo.git", + }, + }, + }, + } + body := createBody(repo) + res := &http.Response{ + StatusCode: 200, + Body: body, + } + c.EXPECT().Do(gomock.AssignableToTypeOf(&http.Request{})).Times(1).Return(res, nil) + }, + }, + "Should create a valid project repo": { + orgRepo: "scm/project/repo", + want: "https://some.server/scm/project/repo.git", + beforeFn: func(t *testing.T, c *mocks.MockHttpClient) { + c.EXPECT().Do(gomock.AssignableToTypeOf(&http.Request{})).Times(1).DoAndReturn(func(req *http.Request) (*http.Response, error) { + assert.Equal(t, "POST", req.Method) + assert.Equal(t, "https://some.server/rest/api/1.0/projects/project/repos", req.URL.String()) + repo := &repoResponse{ + Links: Links{ + Clone: []Link{ + { + Name: "https", + Href: "https://some.server/scm/project/repo.git", + }, + }, + }, + } + body := createBody(repo) + res := &http.Response{ + StatusCode: 200, + Body: body, + } + return res, nil + }) + }, + }, + "Should create a valid user repo": { + orgRepo: "scm/~user/repo", + want: "https://some.server/scm/~user/repo.git", + beforeFn: func(t *testing.T, c *mocks.MockHttpClient) { + c.EXPECT().Do(gomock.AssignableToTypeOf(&http.Request{})).Times(1).DoAndReturn(func(req *http.Request) (*http.Response, error) { + assert.Equal(t, "POST", req.Method) + assert.Equal(t, "https://some.server/rest/api/1.0/users/user/repos", req.URL.String()) + repo := &repoResponse{ + Links: Links{ + Clone: []Link{ + { + Name: "https", + Href: "https://some.server/scm/~user/repo.git", + }, + }, + }, + } + body := createBody(repo) + res := &http.Response{ + StatusCode: 200, + Body: body, + } + return res, nil + }) + }, + }, } for name, tt := range tests { t.Run(name, func(t *testing.T) { @@ -137,7 +137,7 @@ func Test_bitbucketServer_CreateRepository(t *testing.T) { } bbs := &bitbucketServer{ - baseUrl: baseUrl(), + baseURL: baseURL(), c: mockClient, opts: providerOptions, } @@ -213,11 +213,11 @@ func Test_bitbucketServer_GetDefaultBranch(t *testing.T) { want: "master", beforeFn: func(_ *testing.T, c *mocks.MockHttpClient) { repo := &repoResponse{} - body := createBody(repo) - res := &http.Response{ - StatusCode: 200, - Body: body, - } + body := createBody(repo) + res := &http.Response{ + StatusCode: 200, + Body: body, + } c.EXPECT().Do(gomock.AssignableToTypeOf(&http.Request{})).Times(1).Return(res, nil) }, }, @@ -232,7 +232,7 @@ func Test_bitbucketServer_GetDefaultBranch(t *testing.T) { } bbs := &bitbucketServer{ - baseUrl: baseUrl(), + baseURL: baseURL(), c: mockClient, opts: providerOptions, } @@ -343,7 +343,7 @@ func Test_bitbucketServer_GetAuthor(t *testing.T) { } bbs := &bitbucketServer{ - baseUrl: baseUrl(), + baseURL: baseURL(), c: mockClient, opts: providerOptions, } @@ -376,16 +376,16 @@ func Test_splitOrgRepo(t *testing.T) { wantErr: "invalid Bitbucket url \"scm/project/sub/repo\" - must be in the form of \"scm/[~]project-or-username/repo-name\"", }, "Should succeed for a simple orgRepo": { - orgRepo: "scm/project/repo", - wantNoun: "projects", + orgRepo: "scm/project/repo", + wantNoun: "projects", wantOwner: "project", - wantName: "repo", + wantName: "repo", }, "Should identify ~ as users": { - orgRepo: "scm/~user/repo", - wantNoun: "users", + orgRepo: "scm/~user/repo", + wantNoun: "users", wantOwner: "user", - wantName: "repo", + wantName: "repo", }, } for name, tt := range tests { diff --git a/pkg/git/provider_gitea.go b/pkg/git/provider_gitea.go index 6acce8eb..b0e6fade 100644 --- a/pkg/git/provider_gitea.go +++ b/pkg/git/provider_gitea.go @@ -23,7 +23,7 @@ type ( ) func newGitea(opts *ProviderOptions) (Provider, error) { - c, err := gt.NewClient(opts.Host, gt.SetToken(opts.Auth.Password)) + c, err := gt.NewClient(opts.RepoURL, gt.SetToken(opts.Auth.Password)) if err != nil { return nil, err } diff --git a/pkg/git/provider_github.go b/pkg/git/provider_github.go index 60813b66..e31708e6 100644 --- a/pkg/git/provider_github.go +++ b/pkg/git/provider_github.go @@ -7,6 +7,7 @@ import ( "strings" g "github.com/argoproj-labs/argocd-autopilot/pkg/git/github" + "github.com/argoproj-labs/argocd-autopilot/pkg/util" gh "github.com/google/go-github/v43/github" ) @@ -34,8 +35,9 @@ func newGithub(opts *ProviderOptions) (Provider, error) { } } - if opts.Host != "" && !strings.Contains(opts.Host, "github.com") { - c, err = gh.NewEnterpriseClient(opts.Host, opts.Host, hc) + host, _, _, _, _, _, _ := util.ParseGitUrl(opts.RepoURL) + if !strings.Contains(host, "github.com") { + c, err = gh.NewEnterpriseClient(host, host, hc) if err != nil { return nil, err } diff --git a/pkg/git/provider_gitlab.go b/pkg/git/provider_gitlab.go index 64ed091f..f3784d8d 100644 --- a/pkg/git/provider_gitlab.go +++ b/pkg/git/provider_gitlab.go @@ -31,7 +31,7 @@ type ( ) func newGitlab(opts *ProviderOptions) (Provider, error) { - host, _, _, _, _, _, _ := util.ParseGitUrl(opts.Host) + host, _, _, _, _, _, _ := util.ParseGitUrl(opts.RepoURL) c, err := gl.NewClient(opts.Auth.Password, gl.WithBaseURL(host)) if err != nil { return nil, err diff --git a/pkg/git/repository.go b/pkg/git/repository.go index a3faa3b9..b5a4c7ea 100644 --- a/pkg/git/repository.go +++ b/pkg/git/repository.go @@ -66,7 +66,6 @@ type ( url string revision string path string - provider Provider } PushOptions struct { @@ -78,9 +77,10 @@ type ( repo struct { gogit.Repository - auth Auth - progress io.Writer - provider Provider + auth Auth + progress io.Writer + providerType string + repoURL string } ) @@ -106,6 +106,29 @@ var ( return r.checkoutBranch(branch, upsertBranch) } + getProvider = func(providerType, repoURL string, auth *Auth) (Provider, error) { + if providerType == "" { + u, err := url.Parse(repoURL) + if err != nil { + return nil, err + } + + if strings.Contains(u.Hostname(), AzureHostName) { + providerType = Azure + } else { + providerType = strings.TrimSuffix(u.Hostname(), ".com") + } + + log.G().Warnf("--provider not specified, assuming provider from url: %s", providerType) + } + + return newProvider(&ProviderOptions{ + Type: providerType, + Auth: auth, + RepoURL: repoURL, + }) + } + ggClone = func(ctx context.Context, s storage.Storer, worktree billy.Filesystem, o *gg.CloneOptions) (gogit.Repository, error) { return gg.CloneContext(ctx, s, worktree, o) } @@ -146,7 +169,7 @@ func AddFlags(cmd *cobra.Command, opts *AddFlagsOptions) *CloneOptions { cmd.PersistentFlags().StringVar(&co.Provider, opts.Prefix+"provider", "", fmt.Sprintf("The git provider, one of: %v", strings.Join(Providers(), "|"))) if !opts.CreateIfNotExist { - util.Die(cmd.PersistentFlags().MarkHidden(opts.Prefix+"provider")) + util.Die(cmd.PersistentFlags().MarkHidden(opts.Prefix + "provider")) } if opts.CloneForWrite { @@ -187,7 +210,6 @@ func (o *CloneOptions) GetRepo(ctx context.Context) (Repository, fs.FS, error) { return nil, nil, ErrNoParse } - o.provider, err = getProvider(o.Provider, o.url, &o.Auth) if err != nil { log.G(ctx).Warn("failed initializing git provider: %s", err.Error()) } @@ -327,10 +349,13 @@ func (r *repo) getAuthor(ctx context.Context) (*object.Signature, error) { username := cfg.User.Name email := cfg.User.Email - if (username == "" || email == "") && r.provider != nil { - username, email, err = r.provider.GetAuthor(ctx) - if err != nil { - return nil, fmt.Errorf("failed to get author information: %w", err) + if username == "" || email == "" { + provider, _ := getProvider(r.providerType, r.repoURL, &r.auth) + if provider != nil { + username, email, err = provider.GetAuthor(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get author information: %w", err) + } } } @@ -411,10 +436,11 @@ var clone = func(ctx context.Context, opts *CloneOptions) (*repo, error) { } repo := &repo{ - Repository: r, - auth: opts.Auth, - progress: progress, - provider: opts.provider, + Repository: r, + auth: opts.Auth, + progress: progress, + providerType: opts.Provider, + repoURL: opts.Repo, } if opts.revision != "" { @@ -440,35 +466,13 @@ var clone = func(ctx context.Context, opts *CloneOptions) (*repo, error) { } var createRepo = func(ctx context.Context, opts *CloneOptions) (string, error) { - if opts.provider == nil { + provider, _ := getProvider(opts.Provider, opts.Repo, &opts.Auth) + if provider == nil { return "", errors.New("failed creating repository - no git provider supplied") } _, orgRepo, _, _, _, _, _ := util.ParseGitUrl(opts.Repo) - return opts.provider.CreateRepository(ctx, orgRepo) -} - -func getProvider(providerType, repoUrl string, auth *Auth) (Provider, error) { - if providerType == "" { - u, err := url.Parse(repoUrl) - if err != nil { - return nil, err - } - - if strings.Contains(u.Hostname(), AzureHostName) { - providerType = Azure - } else { - providerType = strings.TrimSuffix(u.Hostname(), ".com") - } - - log.G().Warnf("--provider not specified, assuming provider from url: %s", providerType) - } - - return newProvider(&ProviderOptions{ - Type: providerType, - Auth: auth, - Host: repoUrl, - }) + return provider.CreateRepository(ctx, orgRepo) } func getDefaultRepoOptions(orgRepo string) (*CreateRepoOptions, error) { @@ -498,10 +502,11 @@ var initRepo = func(ctx context.Context, opts *CloneOptions) (*repo, error) { } r := &repo{ - Repository: ggr, - auth: opts.Auth, - progress: progress, - provider: opts.provider, + Repository: ggr, + progress: progress, + providerType: opts.Provider, + repoURL: opts.Repo, + auth: opts.Auth, } if err = r.addRemote("origin", opts.url); err != nil { return nil, err @@ -528,7 +533,12 @@ var initRepo = func(ctx context.Context, opts *CloneOptions) (*repo, error) { func (r *repo) getDefaultBranch(ctx context.Context, repo string) (string, error) { _, orgRepo, _, _, _, _, _ := util.ParseGitUrl(repo) - defaultBranch, err := r.provider.GetDefaultBranch(ctx, orgRepo) + provider, err := getProvider(r.providerType, r.repoURL, &r.auth) + if err != nil { + return "", err + } + + defaultBranch, err := provider.GetDefaultBranch(ctx, orgRepo) if err != nil { return "", fmt.Errorf("failed to get default branch from provider. Error: %w", err) } diff --git a/pkg/git/repository_test.go b/pkg/git/repository_test.go index 2378e107..58fa5b77 100644 --- a/pkg/git/repository_test.go +++ b/pkg/git/repository_test.go @@ -310,7 +310,7 @@ func Test_initRepo(t *testing.T) { }, }, "Should fail when addRemote fails": { - repo: "https://github.com/owner/name", + repo: "https://github.com/owner/name", wantErr: "some error", beforeFn: func(r *mocks.MockRepository, _ *mocks.MockWorktree, _ *mockProvider) { r.EXPECT().CreateRemote(&config.RemoteConfig{ @@ -322,7 +322,7 @@ func Test_initRepo(t *testing.T) { }, }, "Should fail when initBranch fails": { - repo: "https://github.com/owner/name", + repo: "https://github.com/owner/name", wantErr: "failed to commit while trying to initialize the branch. Error: some error", beforeFn: func(r *mocks.MockRepository, wt *mocks.MockWorktree, p *mockProvider) { r.EXPECT().CreateRemote(&config.RemoteConfig{ @@ -343,9 +343,14 @@ func Test_initRepo(t *testing.T) { } orgInitRepo := ggInitRepo - defer func() { ggInitRepo = orgInitRepo }() + orgGetProvider := getProvider orgWorktree := worktree - defer func() { worktree = orgWorktree }() + defer func() { + ggInitRepo = orgInitRepo + worktree = orgWorktree + getProvider = orgGetProvider + }() + for tname, tt := range tests { t.Run(tname, func(t *testing.T) { ctrl := gomock.NewController(t) @@ -358,10 +363,10 @@ func Test_initRepo(t *testing.T) { ggInitRepo = func(s storage.Storer, worktree billy.Filesystem) (gogit.Repository, error) { return mockRepo, nil } worktree = func(r gogit.Repository) (gogit.Worktree, error) { return mockWt, nil } + getProvider = func(providerType, repoURL string, auth *Auth) (Provider, error) { return mockProvider, nil } opts := &CloneOptions{ - Repo: tt.repo, - provider: mockProvider, + Repo: tt.repo, } opts.Parse() got, err := initRepo(context.Background(), opts) @@ -538,7 +543,8 @@ func Test_clone(t *testing.T) { }, } - origCheckoutRef, origClone := checkoutRef, ggClone + origCheckoutRef := checkoutRef + origClone := ggClone defer func() { checkoutRef = origCheckoutRef ggClone = origClone @@ -704,6 +710,7 @@ func TestGetRepo(t *testing.T) { }, }, } + origClone, origCreateRepo, origInitRepo := clone, createRepo, initRepo defer func() { clone = origClone @@ -839,8 +846,12 @@ func Test_repo_Persist(t *testing.T) { }, } - worktreeOrg := worktree - defer func() { worktree = worktreeOrg }() + orgGetProvider := getProvider + orgWorktree := worktree + defer func() { + getProvider = orgGetProvider + worktree = orgWorktree + }() for tname, tt := range tests { t.Run(tname, func(t *testing.T) { @@ -850,10 +861,12 @@ func Test_repo_Persist(t *testing.T) { mockProvider := &mockProvider{} mockRepo.EXPECT().ConfigScoped(gomock.Any()).Return(gitConfig, nil).AnyTimes() + getProvider = func(providerType, repoURL string, auth *Auth) (Provider, error) { return mockProvider, nil } + worktree = func(r gogit.Repository) (gogit.Worktree, error) { return mockWt, nil } - r := &repo{Repository: mockRepo, progress: os.Stderr, provider: mockProvider} - worktree = func(r gogit.Repository) (gogit.Worktree, error) { - return mockWt, nil + r := &repo{ + Repository: mockRepo, + progress: os.Stderr, } tt.beforeFn(mockRepo, mockWt) @@ -1309,35 +1322,25 @@ func Test_createRepo(t *testing.T) { Username: "username", Password: "password", }, - provider: &mockProvider{func(orgRepo string) (string, error) { - assert.Equal(t, "owner/name", orgRepo) - return "https://github.com/owner/name.git", nil - }, nil, nil}, }, want: "https://github.com/owner/name.git", }, "Should infer correct provider type from repo url": { opts: &CloneOptions{ Repo: "https://github.com/owner/name.git", - provider: &mockProvider{func(orgRepo string) (string, error) { - return "https://github.com/owner/name.git", nil - }, nil, nil}, }, want: "https://github.com/owner/name.git", }, - "Should succesfully parse owner and name for long orgRepos": { - opts: &CloneOptions{ - Repo: "https://github.com/foo22/bar/fizz.git", - provider: &mockProvider{func(orgRepo string) (string, error) { - assert.Equal(t, "foo22/bar/fizz", orgRepo) - return "https://github.com/foo22/bar/fizz.git", nil - }, nil, nil}, - }, - want: "https://github.com/foo22/bar/fizz.git", - }, } + + orgGetProvider := getProvider + defer func() { getProvider = orgGetProvider }() for name, tt := range tests { t.Run(name, func(t *testing.T) { + mockProvider := &mockProvider{func(orgRepo string) (string, error) { + return "https://github.com/owner/name.git", nil + }, nil, nil} + getProvider = func(providerType, repoURL string, auth *Auth) (Provider, error) { return mockProvider, nil } got, err := createRepo(context.Background(), tt.opts) if err != nil || tt.wantErr != "" { assert.EqualError(t, err, tt.wantErr) @@ -1518,19 +1521,22 @@ func Test_repo_commit(t *testing.T) { }, } + orgGetProvider := getProvider orgWorktree := worktree - defer func() { worktree = orgWorktree }() + defer func() { + getProvider = orgGetProvider + worktree = orgWorktree + }() for tname, tt := range tests { t.Run(tname, func(t *testing.T) { ctrl := gomock.NewController(t) mockRepo := mocks.NewMockRepository(ctrl) mockWt := mocks.NewMockWorktree(ctrl) mockProvider := &mockProvider{} + getProvider = func(providerType, repoURL string, auth *Auth) (Provider, error) { return mockProvider, nil } + worktree = func(r gogit.Repository) (gogit.Worktree, error) { return mockWt, nil } - r := &repo{Repository: mockRepo, provider: mockProvider} - worktree = func(r gogit.Repository) (gogit.Worktree, error) { - return mockWt, nil - } + r := &repo{Repository: mockRepo} tt.beforeFn(mockRepo, mockWt, mockProvider) diff --git a/pkg/util/repospec_test.go b/pkg/util/repospec_test.go index d76dee04..60778fbd 100644 --- a/pkg/util/repospec_test.go +++ b/pkg/util/repospec_test.go @@ -138,6 +138,12 @@ func TestNewRepoSpecFromUrl_CloneSpecs(t *testing.T) { absPath: "", ref: "", }, + { + input: "https://gitlab-onprem.devops.cf-cd.com/root/gitlab-demo_git-source/resources_gitlab-demo", + cloneSpec: "https://gitlab-onprem.devops.cf-cd.com/root/gitlab-demo_git-source.git", + absPath: "resources_gitlab-demo", + ref: "", + }, } for _, testcase := range testcases { host, orgRepo, path, ref, _, suffix, _ := ParseGitUrl(testcase.input)