Skip to content

chore: extract git operations to separate package #279

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

Merged
merged 1 commit into from
Jul 23, 2024
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: 4 additions & 3 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"syscall"
"time"

"github.com/coder/envbuilder/git"
"github.com/coder/envbuilder/options"

"github.com/GoogleContainerTools/kaniko/pkg/config"
Expand Down Expand Up @@ -204,7 +205,7 @@ func Run(ctx context.Context, opts options.Options) error {
}
}()

cloneOpts := CloneRepoOptions{
cloneOpts := git.CloneRepoOptions{
Path: opts.WorkspaceFolder,
Storage: opts.Filesystem,
Insecure: opts.Insecure,
Expand All @@ -214,15 +215,15 @@ func Run(ctx context.Context, opts options.Options) error {
CABundle: caBundle,
}

cloneOpts.RepoAuth = SetupRepoAuth(&opts)
cloneOpts.RepoAuth = git.SetupRepoAuth(&opts)
if opts.GitHTTPProxyURL != "" {
cloneOpts.ProxyOptions = transport.ProxyOptions{
URL: opts.GitHTTPProxyURL,
}
}
cloneOpts.RepoURL = opts.GitURL

cloned, fallbackErr = CloneRepo(ctx, cloneOpts)
cloned, fallbackErr = git.CloneRepo(ctx, cloneOpts)
if fallbackErr == nil {
if cloned {
endStage("📦 Cloned repository!")
Expand Down
2 changes: 1 addition & 1 deletion git.go → git/git.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package envbuilder
package git

import (
"context"
Expand Down
37 changes: 19 additions & 18 deletions git_test.go → git/git_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package envbuilder_test
package git_test

import (
"context"
Expand All @@ -12,9 +12,10 @@ import (
"regexp"
"testing"

"github.com/coder/envbuilder/git"

"github.com/coder/envbuilder/options"

"github.com/coder/envbuilder"
"github.com/coder/envbuilder/internal/log"
"github.com/coder/envbuilder/testutil/gittest"
"github.com/coder/envbuilder/testutil/mwtest"
Expand Down Expand Up @@ -90,7 +91,7 @@ func TestCloneRepo(t *testing.T) {
clientFS := memfs.New()
// A repo already exists!
_ = gittest.NewRepo(t, clientFS)
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
Path: "/",
RepoURL: srv.URL,
Storage: clientFS,
Expand All @@ -108,7 +109,7 @@ func TestCloneRepo(t *testing.T) {
srv := httptest.NewServer(authMW(gittest.NewServer(srvFS)))
clientFS := memfs.New()

cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
Path: "/workspace",
RepoURL: srv.URL,
Storage: clientFS,
Expand Down Expand Up @@ -145,7 +146,7 @@ func TestCloneRepo(t *testing.T) {
authURL.User = url.UserPassword(tc.username, tc.password)
clientFS := memfs.New()

cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
Path: "/workspace",
RepoURL: authURL.String(),
Storage: clientFS,
Expand Down Expand Up @@ -184,7 +185,7 @@ func TestCloneRepoSSH(t *testing.T) {
gitURL := tr.String()
clientFS := memfs.New()

cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
Path: "/workspace",
RepoURL: gitURL,
Storage: clientFS,
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestCloneRepoSSH(t *testing.T) {
clientFS := memfs.New()

anotherKey := randKeygen(t)
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
Path: "/workspace",
RepoURL: gitURL,
Storage: clientFS,
Expand Down Expand Up @@ -246,7 +247,7 @@ func TestCloneRepoSSH(t *testing.T) {
gitURL := tr.String()
clientFS := memfs.New()

cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
Path: "/workspace",
RepoURL: gitURL,
Storage: clientFS,
Expand All @@ -270,7 +271,7 @@ func TestSetupRepoAuth(t *testing.T) {
opts := &options.Options{
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
require.Nil(t, auth)
})

Expand All @@ -279,7 +280,7 @@ func TestSetupRepoAuth(t *testing.T) {
GitURL: "http://host.tld/repo",
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
require.Nil(t, auth)
})

Expand All @@ -290,7 +291,7 @@ func TestSetupRepoAuth(t *testing.T) {
GitPassword: "pass",
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
ba, ok := auth.(*githttp.BasicAuth)
require.True(t, ok)
require.Equal(t, opts.GitUsername, ba.Username)
Expand All @@ -304,7 +305,7 @@ func TestSetupRepoAuth(t *testing.T) {
GitPassword: "pass",
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
ba, ok := auth.(*githttp.BasicAuth)
require.True(t, ok)
require.Equal(t, opts.GitUsername, ba.Username)
Expand All @@ -318,7 +319,7 @@ func TestSetupRepoAuth(t *testing.T) {
GitSSHPrivateKeyPath: kPath,
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
_, ok := auth.(*gitssh.PublicKeys)
require.True(t, ok)
})
Expand All @@ -330,7 +331,7 @@ func TestSetupRepoAuth(t *testing.T) {
GitSSHPrivateKeyPath: kPath,
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
_, ok := auth.(*gitssh.PublicKeys)
require.True(t, ok)
})
Expand All @@ -343,7 +344,7 @@ func TestSetupRepoAuth(t *testing.T) {
GitSSHPrivateKeyPath: kPath,
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
_, ok := auth.(*gitssh.PublicKeys)
require.True(t, ok)
})
Expand All @@ -356,7 +357,7 @@ func TestSetupRepoAuth(t *testing.T) {
GitUsername: "user",
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
_, ok := auth.(*gitssh.PublicKeys)
require.True(t, ok)
})
Expand All @@ -368,7 +369,7 @@ func TestSetupRepoAuth(t *testing.T) {
GitSSHPrivateKeyPath: kPath,
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
pk, ok := auth.(*gitssh.PublicKeys)
require.True(t, ok)
require.NotNil(t, pk.Signer)
Expand All @@ -382,7 +383,7 @@ func TestSetupRepoAuth(t *testing.T) {
GitURL: "ssh://[email protected]:repo/path",
Logger: testLog(t),
}
auth := envbuilder.SetupRepoAuth(opts)
auth := git.SetupRepoAuth(opts)
require.Nil(t, auth) // TODO: actually test SSH_AUTH_SOCK
})
}
Expand Down