Skip to content

Add support for cloning repositories from github - #534

Closed
shreyas-goenka wants to merge 14 commits into
mainfrom
git-clone
Closed

Add support for cloning repositories from github#534
shreyas-goenka wants to merge 14 commits into
mainfrom
git-clone

Conversation

@shreyas-goenka

@shreyas-goenkashreyas-goenka commented Jun 29, 2023

Copy link
Copy Markdown
Contributor

Changes

Adds support for cloning public and private github repositories for databricks templates

Tests

Integration tests

@shreyas-goenkashreyas-goenka changed the title Add support for cloneing public repositories from githubAdd support for cloning public repositories from githubJun 29, 2023
Comment threadlibs/zip/extract.go Outdated
Comment threadlibs/zip/extract.go
Comment threadlibs/git/clone.go Outdated
Comment threadlibs/git/clone.go Outdated
return err
}

func Clone(org string, repoName string, targetDir string) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is specific to GitHub. Please turn this into an interface (e.g. with only the Clone function) such that we can determine which one to use based on the URL or whatever parameter we get.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I did something different, ie added CloneOptions with the provider as an option. Please let me know if that looks good

Comment threadlibs/git/clone.go Outdated
Comment threadlibs/git/clone_test.go Outdated
@shreyas-goenkashreyas-goenka changed the title Add support for cloning public repositories from github[WIP] Add support for cloning public repositories from githubJun 29, 2023
@shreyas-goenkashreyas-goenka changed the title [WIP] Add support for cloning public repositories from githubAdd support for cloning public repositories from githubJun 30, 2023
@shreyas-goenka

Copy link
Copy Markdown
ContributorAuthor

I could not make integration tests for the private repository work. git clone does not work out of the box in the CI runner environment.

With some cursory investigation, I found out that it's probably the GITHUB_TOKEN that allows the CI runner to download private repos, which is not something the git CLI will support.

Manually tested however git clone functionality works properly for private repositories (for both branches and tags)

@shreyas-goenkashreyas-goenka changed the title Add support for cloning public repositories from githubAdd support for cloning repositories from githubJun 30, 2023
@shreyas-goenka

Copy link
Copy Markdown
ContributorAuthor

To create an integration test for cloning private repositories, we can create a new deco account on github, and add that to our test infra. WDYT?

Comment threadlibs/git/clone.go
}

func (opts CloneOptions) repoUrl() string {
return fmt.Sprintf(`https://github.com/%s/%s`, opts.Organization, opts.RepositoryName)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It assumes it's always github but it can be GitLab for example or any other provider, right?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, we can add support for gitlab as a followup. gitlab also support zip downloads, and for other providers like bitbucket we can defer to the CLI

Comment threadlibs/git/clone.go
return cmd.Wait()
}

func clonePublic(ctx context.Context, opts CloneOptions) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would call it "downloadRepo" or something like this, because clone assumes we do the git operation while we don't

Comment threadlibs/git/clone.go
zipDst := filepath.Join(tmpDir, opts.RepositoryName+".zip")

// Download public repository from github as a ZIP file
err := download(ctx, opts.zipUrl(), zipDst)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shall we have a timeout for download operation?

Comment threadlibs/git/clone.go
}

func (opts CloneOptions) zipUrl() string {
return fmt.Sprintf(`%s/archive/%s.zip`, opts.repoUrl(), opts.Reference)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This would work only for Github, right?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, gitlab has a different format for zip URLs

Comment threadlibs/git/clone.go

func Clone(ctx context.Context, opts CloneOptions) error {
if opts.Provider != "github" {
return fmt.Errorf("git provider not supported: %s", opts.Provider)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we support only Github?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Just as a starting point, followups coming soon :)

@andrewnesterandrewnesterJun 30, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But design of this code is very tightly coupled with Github specific implementation. If we plan to support other providers, shall we abstract this with some type Provider interface? You could have some implementation specific logic like zipUrl and etc in the implementation of this interface, so later when you decide to add GitLab support you will just need to extend and modify the code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I left some inline suggestion on how to make it more flexible and rely less on implementation details

Comment threadlibs/git/clone.go
return other == errNotFound
}

type CloneOptions struct {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add

type provider interface {
repoUrl() string,
zipUrl() string,
destination() string
}

Comment threadlibs/git/clone.go
TargetDir string
}

func (opts CloneOptions) repoUrl() string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

replace with

func (provider GitHubProvider) repoUrl() string

Comment threadlibs/git/clone.go
return err
}

func clonePrivate(ctx context.Context, opts CloneOptions) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

replace with

func clonePrivate(ctx context.Context, provider Provider) error 

@shreyas-goenka

Copy link
Copy Markdown
ContributorAuthor

I had a discussion with @pietern and based on feedback from @lennartkats-db we decided to not support ZIP downloads right out of the gate. Lets start with git CLI by default, and switch over to supporting ZIP downloads if there's a need based on customer feedback.

This PR is being closed in favour of #544

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@shreyas-goenka@pietern@andrewnester