Skip to content

fix: fix git url parsing #188

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 7 commits into from
May 14, 2024
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
6 changes: 3 additions & 3 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"maps"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -859,12 +858,13 @@ func DefaultWorkspaceFolder(repoURL string) (string, error) {
if repoURL == "" {
return "/workspaces/empty", nil
}
parsed, err := url.Parse(repoURL)
parsed, err := ParseGitURL(repoURL)
if err != nil {
return "", err
}
name := strings.Split(parsed.Path, "/")
return fmt.Sprintf("/workspaces/%s", name[len(name)-1]), nil
repo := strings.TrimSuffix(name[len(name)-1], ".git")
return fmt.Sprintf("/workspaces/%s", repo), nil
}

type userInfo struct {
Expand Down
35 changes: 29 additions & 6 deletions envbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,34 @@ import (

func TestDefaultWorkspaceFolder(t *testing.T) {
t.Parallel()
dir, err := envbuilder.DefaultWorkspaceFolder("https://github.com/coder/coder")
require.NoError(t, err)
require.Equal(t, "/workspaces/coder", dir)

dir, err = envbuilder.DefaultWorkspaceFolder("")
require.NoError(t, err)
require.Equal(t, envbuilder.EmptyWorkspaceDir, dir)
tests := []struct {
name string
gitURL string
expected string
}{
{
name: "HTTP",
gitURL: "https://github.com/coder/envbuilder.git",
expected: "/workspaces/envbuilder",
},
{
name: "SSH",
gitURL: "[email protected]:coder/envbuilder.git",
expected: "/workspaces/envbuilder",
},
{
name: "empty",
gitURL: "",
expected: envbuilder.EmptyWorkspaceDir,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir, err := envbuilder.DefaultWorkspaceFolder(tt.gitURL)
require.NoError(t, err)
require.Equal(t, tt.expected, dir)
})
}
}
8 changes: 7 additions & 1 deletion git.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/skeema/knownhosts"
giturls "github.com/whilp/git-urls"
"golang.org/x/crypto/ssh"
gossh "golang.org/x/crypto/ssh"
)
Expand All @@ -46,7 +47,7 @@ type CloneRepoOptions struct {
//
// The bool returned states whether the repository was cloned or not.
func CloneRepo(ctx context.Context, opts CloneRepoOptions) (bool, error) {
parsed, err := url.Parse(opts.RepoURL)
parsed, err := ParseGitURL(opts.RepoURL)
if err != nil {
return false, fmt.Errorf("parse url %q: %w", opts.RepoURL, err)
}
Expand Down Expand Up @@ -251,3 +252,8 @@ func SetupRepoAuth(options *Options) transport.AuthMethod {
}
return auth
}

// Parses a Git URL and returns a corresponding URL object
func ParseGitURL(URL string) (*url.URL, error) {
return giturls.Parse(URL)
}
29 changes: 29 additions & 0 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,35 @@ func TestSetupRepoAuth(t *testing.T) {
})
}

func TestParseGitURL(t *testing.T) {
t.Parallel()

tests := []struct {
name string
gitURL string
expected string
}{
{
name: "HTTP",
gitURL: "https://github.com/coder/envbuilder.git",
expected: "https://github.com/coder/envbuilder.git",
},
{
name: "SSH",
gitURL: "[email protected]:coder/envbuilder.git",
expected: "ssh://[email protected]/coder/envbuilder.git",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
url, err := envbuilder.ParseGitURL(tt.gitURL)
require.NoError(t, err)
require.Equal(t, tt.expected, url.String())
})
}
}

func mustRead(t *testing.T, fs billy.Filesystem, path string) string {
t.Helper()
f, err := fs.OpenFile(path, os.O_RDONLY, 0o644)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ require (
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/whilp/git-urls v1.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vb
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/wagslane/go-password-validator v0.3.0 h1:vfxOPzGHkz5S146HDpavl0cw1DSVP061Ry2PX0/ON6I=
github.com/wagslane/go-password-validator v0.3.0/go.mod h1:TI1XJ6T5fRdRnHqHt14pvy1tNVnrwe7m3/f1f2fDphQ=
github.com/whilp/git-urls v1.0.0 h1:95f6UMWN5FKW71ECsXRUd3FVYiXdrE7aX4NZKcPmIjU=
github.com/whilp/git-urls v1.0.0/go.mod h1:J16SAmobsqc3Qcy98brfl5f5+e0clUvg1krgwk/qCfE=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
Expand Down