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

CR-5155 - gracefully handle repo-not-exist during clone #116

Merged
merged 7 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion pkg/git/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ var (
)

var supportedProviders = map[string]func(*ProviderOptions) (Provider, error){
"github": newGithub,
"github": newGithub,
"github.com": newGithub,
}

// New creates a new git provider
Expand Down
18 changes: 12 additions & 6 deletions pkg/git/provider_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"

g "github.com/argoproj-labs/argocd-autopilot/pkg/git/github"

Expand All @@ -18,13 +19,9 @@ type github struct {
}

func newGithub(opts *ProviderOptions) (Provider, error) {
var (
c *gh.Client
err error
)
var c *gh.Client

hc := &http.Client{}

if opts.Auth != nil {
hc.Transport = &gh.BasicAuthTransport{
Username: opts.Auth.Username,
Expand All @@ -33,10 +30,19 @@ func newGithub(opts *ProviderOptions) (Provider, error) {
}

if opts.Host != "" {
noam-codefresh marked this conversation as resolved.
Show resolved Hide resolved
c, err = gh.NewEnterpriseClient(opts.Host, opts.Host, hc)
u, err := url.Parse(opts.Host)
if err != nil {
return nil, err
}

if u.Hostname() == "github.com" {
c = gh.NewClient(hc)
} else {
c, err = gh.NewEnterpriseClient(opts.Host, opts.Host, hc)
if err != nil {
return nil, err
}
}
} else {
c = gh.NewClient(hc)
}
Expand Down
51 changes: 48 additions & 3 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"net/url"
"os"
"strings"

Expand Down Expand Up @@ -36,6 +37,7 @@ type (
}

CloneOptions struct {
Provider string
// URL clone url
Repo string
Auth Auth
Expand Down Expand Up @@ -136,10 +138,20 @@ func (o *CloneOptions) Clone(ctx context.Context) (Repository, fs.FS, error) {

r, err := clone(ctx, o)
if err != nil {
if err == transport.ErrEmptyRemoteRepository {
log.G(ctx).Debug("empty repository, initializing new one with specified remote")
r, err = initRepo(ctx, o)
if err != transport.ErrRepositoryNotFound && err != transport.ErrEmptyRemoteRepository {
noam-codefresh marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil, err
}

if err == transport.ErrRepositoryNotFound {
_, err = createRepo(ctx, o)
noam-codefresh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
noam-codefresh marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil, err
}
}

// just created repo OR err == transport.ErrEmptyRemoteRepository
log.G(ctx).Debug("empty repository, initializing new one with specified remote")
r, err = initRepo(ctx, o)
}

if err != nil {
Expand Down Expand Up @@ -229,6 +241,39 @@ var clone = func(ctx context.Context, opts *CloneOptions) (*repo, error) {
return repo, nil
}

var createRepo = func(ctx context.Context, opts *CloneOptions) (string, error) {
host, orgRepo, _, _, _, _, _ := util.ParseGitUrl(opts.Repo)
providerType := opts.Provider
if providerType == "" {
u, err := url.Parse(host)
if err != nil {
return "", err
}

providerType = u.Hostname()
}

p, err := NewProvider(&ProviderOptions{
Type: providerType,
Auth: &opts.Auth,
Host: host,
})
if err != nil {
return "", err
}

s := strings.Split(orgRepo, "/")
noam-codefresh marked this conversation as resolved.
Show resolved Hide resolved
if len(s) != 2 {
return "", fmt.Errorf("Failed parsing organization and repo from '%s'", orgRepo)
}

return p.CreateRepository(ctx, &CreateRepoOptions{
Owner: s[0],
Name: s[1],
Private: true,
})
}

var initRepo = func(ctx context.Context, opts *CloneOptions) (*repo, error) {
ggr, err := ggInitRepo(memory.NewStorage(), opts.FS)
if err != nil {
Expand Down