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

fix github-enterprise client creation #353

Merged
merged 2 commits into from
Aug 2, 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
2 changes: 2 additions & 0 deletions cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have an issue that this one can close, can you link it?

}

appsRepo, appsfs, err = getRepo(ctx, opts.AppsCloneOpts)
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pkg/git/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type (
ProviderOptions struct {
Type string
Auth *Auth
Host string
RepoURL string
}

CreateRepoOptions struct {
Expand Down
15 changes: 9 additions & 6 deletions pkg/git/provider_ado.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions pkg/git/provider_bitbucket-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type (
}

bitbucketServer struct {
baseUrl *url.URL
baseURL *url.URL
c HttpClient
opts *ProviderOptions
}
Expand Down Expand Up @@ -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,
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
186 changes: 93 additions & 93 deletions pkg/git/provider_bitbucket-server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
}
)

func baseUrl() *url.URL {
func baseURL() *url.URL {
u, _ := url.Parse("https://some.server")
return u
}
Expand All @@ -41,91 +41,91 @@ 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",
beforeFn: func(_ *testing.T, c *mocks.MockHttpClient) {
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",
// 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: "[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",
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) {
Expand All @@ -137,7 +137,7 @@ func Test_bitbucketServer_CreateRepository(t *testing.T) {
}

bbs := &bitbucketServer{
baseUrl: baseUrl(),
baseURL: baseURL(),
c: mockClient,
opts: providerOptions,
}
Expand Down Expand Up @@ -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)
},
},
Expand All @@ -232,7 +232,7 @@ func Test_bitbucketServer_GetDefaultBranch(t *testing.T) {
}

bbs := &bitbucketServer{
baseUrl: baseUrl(),
baseURL: baseURL(),
c: mockClient,
opts: providerOptions,
}
Expand Down Expand Up @@ -343,7 +343,7 @@ func Test_bitbucketServer_GetAuthor(t *testing.T) {
}

bbs := &bitbucketServer{
baseUrl: baseUrl(),
baseURL: baseURL(),
c: mockClient,
opts: providerOptions,
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/git/provider_gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/git/provider_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
Loading