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 3 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
10 changes: 7 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 @@ -50,6 +49,7 @@ import (
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/sirupsen/logrus"
"github.com/tailscale/hujson"
giturls "github.com/whilp/git-urls"
"golang.org/x/xerrors"
)

Expand Down Expand Up @@ -859,12 +859,16 @@ func DefaultWorkspaceFolder(repoURL string) (string, error) {
if repoURL == "" {
return "/workspaces/empty", nil
}
parsed, err := url.Parse(repoURL)
parsed, err := giturls.Parse(repoURL)
if err != nil {
return "", err
}
name := strings.Split(parsed.Path, "/")
return fmt.Sprintf("/workspaces/%s", name[len(name)-1]), nil
if len(name) == 0 {
return "", errors.New("no name found in the repository URL")
}
repo := strings.TrimSuffix(name[len(name)-1], ".git")
return fmt.Sprintf("/workspaces/%s", repo), nil
}

type userInfo struct {
Expand Down
45 changes: 39 additions & 6 deletions envbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,44 @@ 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: "username and password",
gitURL: "https://username:[email protected]/coder/envbuilder.git",
expected: "/workspaces/envbuilder",
},
{
name: "fragment",
gitURL: "https://github.com/coder/envbuilder.git#feature-branch",
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)
})
}
}
4 changes: 2 additions & 2 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net"
"net/url"
"os"
"strings"

Expand All @@ -22,6 +21,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 +46,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 := giturls.Parse(opts.RepoURL)
if err != nil {
return false, fmt.Errorf("parse url %q: %w", opts.RepoURL, err)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/skeema/knownhosts v1.2.2
github.com/stretchr/testify v1.9.0
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a
github.com/whilp/git-urls v1.0.0
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.22.0
golang.org/x/sync v0.7.0
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