Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed cloneURL from createrRepo returned values #375

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pkg/git/mocks/provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/git/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type (
Provider interface {
// CreateRepository creates the repository in the remote provider and returns a
// clone url
CreateRepository(ctx context.Context, orgRepo string) (cloneURL, defaultBranch string, err error)
CreateRepository(ctx context.Context, orgRepo string) (defaultBranch string, err error)

// GetDefaultBranch returns the default branch of the repository
GetDefaultBranch(ctx context.Context, orgRepo string) (string, error)
Expand Down
8 changes: 4 additions & 4 deletions pkg/git/provider_ado.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func newAdo(opts *ProviderOptions) (Provider, error) {
}, nil
}

func (g *adoGit) CreateRepository(ctx context.Context, orgRepo string) (cloneURL, defaultBranch string, err error) {
func (g *adoGit) CreateRepository(ctx context.Context, orgRepo string) (defaultBranch string, err error) {
if orgRepo == "" {
return "", "", fmt.Errorf("name needs to be provided to create an azure devops repository. name: '%s'", orgRepo)
return "", fmt.Errorf("name needs to be provided to create an azure devops repository. name: '%s'", orgRepo)
}

project := g.adoUrl.GetProjectName()
Expand All @@ -76,10 +76,10 @@ func (g *adoGit) CreateRepository(ctx context.Context, orgRepo string) (cloneURL
}
repository, err := g.adoClient.CreateRepository(ctx, createRepositoryArgs)
if err != nil {
return "", "", err
return "", err
}

return *repository.RemoteUrl, *repository.DefaultBranch, nil
return *repository.DefaultBranch, nil
}

func (g *adoGit) GetDefaultBranch(ctx context.Context, orgRepo string) (string, error) {
Expand Down
34 changes: 14 additions & 20 deletions pkg/git/provider_ado_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ import (
func Test_adoGit_CreateRepository(t *testing.T) {
emptyFunc := func(client *adoMock.MockAdoClient, url *adoMock.MockAdoUrl) {}
tests := []struct {
name string
mockClient func(client *adoMock.MockAdoClient, url *adoMock.MockAdoUrl)
repoName string
wantCloneURL string
wantDefaultBranch string
wantErr assert.ErrorAssertionFunc
name string
mockClient func(client *adoMock.MockAdoClient, url *adoMock.MockAdoUrl)
repoName string
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "Empty Name",
mockClient: emptyFunc,
repoName: "",
wantCloneURL: "",
name: "Empty Name",
mockClient: emptyFunc,
repoName: "",
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return true
},
Expand All @@ -41,16 +39,14 @@ func Test_adoGit_CreateRepository(t *testing.T) {
Times(1).
Return("blah")
},
repoName: "name",
wantCloneURL: "",
repoName: "name",
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return true
},
},
{
name: "Success creating repo",
mockClient: func(client *adoMock.MockAdoClient, url *adoMock.MockAdoUrl) {
remoteURL := "https://dev.azure.com/SUB/PROJECT/_git/REPO"
defaultBranch := "main"
url.EXPECT().GetProjectName().
Times(1).
Expand All @@ -65,17 +61,16 @@ func Test_adoGit_CreateRepository(t *testing.T) {
Name: nil,
ParentRepository: nil,
Project: nil,
RemoteUrl: &remoteURL,
RemoteUrl: nil,
Size: nil,
SshUrl: nil,
Url: nil,
ValidRemoteUrls: nil,
WebUrl: nil,
}, nil)
},
repoName: "name",
wantCloneURL: "https://dev.azure.com/SUB/PROJECT/_git/REPO",
wantDefaultBranch: "main",
repoName: "name",
want: "main",
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return false
},
Expand All @@ -91,13 +86,12 @@ func Test_adoGit_CreateRepository(t *testing.T) {
adoClient: mockClient,
adoUrl: mockUrl,
}
gotCloneURL, gotDefaultBranch, err := g.CreateRepository(context.Background(), tt.repoName)
got, err := g.CreateRepository(context.Background(), tt.repoName)
if !tt.wantErr(t, err, fmt.Sprintf("CreateRepository - %s", tt.repoName)) {
return
}

assert.Equalf(t, tt.wantCloneURL, gotCloneURL, "CreateRepository - %s", tt.name)
assert.Equalf(t, tt.wantDefaultBranch, gotDefaultBranch, "CreateRepository - %s", tt.name)
assert.Equalf(t, tt.want, got, "CreateRepository - %s", tt.name)
})
}
}
Expand Down
15 changes: 4 additions & 11 deletions pkg/git/provider_bitbucket-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func newBitbucketServer(opts *ProviderOptions) (Provider, error) {
return g, nil
}

func (bbs *bitbucketServer) CreateRepository(ctx context.Context, orgRepo string) (cloneURL, defaultBranch string, err error) {
func (bbs *bitbucketServer) CreateRepository(ctx context.Context, orgRepo string) (defaultBranch string, err error) {
noun, owner, name, err := splitOrgRepo(orgRepo)
if err != nil {
return "", "", err
return "", err
}

path := fmt.Sprintf("%s/%s/repos", noun, owner)
Expand All @@ -107,17 +107,10 @@ func (bbs *bitbucketServer) CreateRepository(ctx context.Context, orgRepo string
Scm: "git",
}, repo)
if err != nil {
return "", "", err
}

for _, link := range repo.Links.Clone {
if link.Name == bbs.baseURL.Scheme {
return link.Href, repo.DefaultBranch, nil

}
return "", err
}

return "", "", fmt.Errorf("created repo did not contain a valid %s clone url", bbs.baseURL.Scheme)
return repo.DefaultBranch, nil
}

func (bbs *bitbucketServer) GetDefaultBranch(ctx context.Context, orgRepo string) (string, error) {
Expand Down
52 changes: 10 additions & 42 deletions pkg/git/provider_bitbucket-server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ func createBody(obj interface{}) io.ReadCloser {

func Test_bitbucketServer_CreateRepository(t *testing.T) {
tests := map[string]struct {
orgRepo string
wantCloneURL string
wantDefaultBranch string
wantErr string
beforeFn func(t *testing.T, c *mocks.MockHttpClient)
orgRepo string
want string
wantErr string
beforeFn func(t *testing.T, c *mocks.MockHttpClient)
}{
"Should fail if orgRepo is invalid": {
orgRepo: "no-scm/project/repo",
Expand All @@ -53,44 +52,15 @@ 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: "[email protected]/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",
wantCloneURL: "https://some.server/scm/project/repo.git",
orgRepo: "scm/project/repo",
want: "main",
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",
},
},
},
DefaultBranch: "main",
}
body := createBody(repo)
res := &http.Response{
Expand All @@ -102,8 +72,7 @@ func Test_bitbucketServer_CreateRepository(t *testing.T) {
},
},
"Should create a valid user repo": {
orgRepo: "scm/~user/repo",
wantCloneURL: "https://some.server/scm/~user/repo.git",
orgRepo: "scm/~user/repo",
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)
Expand Down Expand Up @@ -142,14 +111,13 @@ func Test_bitbucketServer_CreateRepository(t *testing.T) {
c: mockClient,
opts: providerOptions,
}
gotCloneURL, gotDefaultBranch, err := bbs.CreateRepository(context.Background(), tt.orgRepo)
got, err := bbs.CreateRepository(context.Background(), tt.orgRepo)
if err != nil || tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
return
}

assert.Equalf(t, tt.wantCloneURL, gotCloneURL, "CreateRepository - %s", name)
assert.Equalf(t, tt.wantDefaultBranch, gotDefaultBranch, "CreateRepository - %s", name)
assert.Equalf(t, tt.want, got, "CreateRepository - %s", name)
})
}
}
Expand Down
21 changes: 4 additions & 17 deletions pkg/git/provider_bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func newBitbucket(opts *ProviderOptions) (Provider, error) {

}

func (g *bitbucket) CreateRepository(ctx context.Context, orgRepo string) (cloneURL, defaultBranch string, err error) {
func (g *bitbucket) CreateRepository(ctx context.Context, orgRepo string) (defaultBranch string, err error) {
opts, err := getDefaultRepoOptions(orgRepo)
if err != nil {
return "", "", err
return "", err
}

createOpts := &bb.RepositoryOptions{
Expand All @@ -62,23 +62,10 @@ func (g *bitbucket) CreateRepository(ctx context.Context, orgRepo string) (clone
p, err := g.Repository.Create(createOpts)

if err != nil {
return "", "", fmt.Errorf("failed creating the repository \"%s\" under \"%s\": %w", opts.Name, opts.Owner, err)
}

var cloneUrl string
cloneLinksObj := p.Links["clone"]
for _, cloneLink := range cloneLinksObj.([]interface{}) {
link := cloneLink.(map[string]interface{})
if link["name"].(string) == "https" {
cloneUrl = link["href"].(string)
}
}

if cloneUrl == "" {
return "", "", fmt.Errorf("clone url is empty")
return "", fmt.Errorf("failed creating the repository \"%s\" under \"%s\": %w", opts.Name, opts.Owner, err)
}

return cloneUrl, p.Mainbranch.Name, err
return p.Mainbranch.Name, err
}

func (g *bitbucket) GetDefaultBranch(ctx context.Context, orgRepo string) (string, error) {
Expand Down
34 changes: 9 additions & 25 deletions pkg/git/provider_bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ import (

func Test_bitbucket_CreateRepository(t *testing.T) {
tests := map[string]struct {
orgRepo string
wantCloneURL string
wantDefaultBranch string
wantErr string
beforeRepoFn func(*bbmocks.MockbbRepo)
orgRepo string
want string
wantErr string
beforeRepoFn func(*bbmocks.MockbbRepo)
}{
"Should fail if orgRepo is invalid": {
orgRepo: "invalid",
wantErr: "failed parsing organization and repo from 'invalid'",
},
"Creates repository under user": {
orgRepo: "username/repoName",
wantCloneURL: "https://[email protected]/username/repoName.git",
wantDefaultBranch: "main",
orgRepo: "username/repoName",
want: "main",
beforeRepoFn: func(c *bbmocks.MockbbRepo) {
createOpts := bb.RepositoryOptions{
Owner: "username",
Expand All @@ -35,24 +33,11 @@ func Test_bitbucket_CreateRepository(t *testing.T) {
IsPrivate: "true",
}

links := map[string]interface{}{
"self": map[string]string{
"href": "https://api.bitbucket.org/2.0/repositories/userName/repoName",
},
"clone": []interface{}{
map[string]interface{}{
"name": "https",
"href": "https://[email protected]/username/repoName.git",
},
},
}

repo := &bb.Repository{
Name: "userName",
Name: "userName",
Mainbranch: bb.RepositoryBranch{
Name: "main",
},
Links: links,
}

c.EXPECT().Create(&createOpts).
Expand Down Expand Up @@ -109,14 +94,13 @@ func Test_bitbucket_CreateRepository(t *testing.T) {
Repository: mockRepoClient,
User: mockUserClient,
}
gotCloneURL, gotDefaultBranch, err := g.CreateRepository(context.Background(), tt.orgRepo)
got, err := g.CreateRepository(context.Background(), tt.orgRepo)
if err != nil || tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
return
}

assert.Equalf(t, tt.wantCloneURL, gotCloneURL, "CreateRepository - %s", name)
assert.Equalf(t, tt.wantDefaultBranch, gotDefaultBranch, "CreateRepository - %s",name)
assert.Equalf(t, tt.want, got, "CreateRepository - %s", name)
})
}
}
Expand Down
Loading