diff --git a/cmd/jb/main_test.go b/cmd/jb/main_test.go index c299103..3e1e1de 100644 --- a/cmd/jb/main_test.go +++ b/cmd/jb/main_test.go @@ -43,17 +43,17 @@ func TestParseDependency(t *testing.T) { }, { name: "Invalid", - path: "github.com/foo", + path: "example.com/foo", want: nil, }, { - name: "GitHub", - path: "github.com/jsonnet-bundler/jsonnet-bundler", + name: "GitHTTPS", + path: "example.com/jsonnet-bundler/jsonnet-bundler", want: &deps.Dependency{ Source: deps.Source{ GitSource: &deps.Git{ Scheme: deps.GitSchemeHTTPS, - Host: "github.com", + Host: "example.com", User: "jsonnet-bundler", Repo: "jsonnet-bundler", Subdir: "", diff --git a/spec/deps/dependencies_test.go b/spec/deps/dependencies_test.go index 3292c9c..fb2820b 100644 --- a/spec/deps/dependencies_test.go +++ b/spec/deps/dependencies_test.go @@ -40,7 +40,17 @@ func TestParseDependency(t *testing.T) { }, { name: "Invalid", - path: "github.com/foo", + path: "example.com/foo", + want: nil, + }, + { + name: "InvalidDomain", + path: "example.c/foo/bar", + want: nil, + }, + { + name: "InvalidDomain2", + path: "examplec/foo/bar", want: nil, }, { diff --git a/spec/deps/git.go b/spec/deps/git.go index c201084..0061b4a 100644 --- a/spec/deps/git.go +++ b/spec/deps/git.go @@ -33,11 +33,11 @@ type Git struct { // Hostname the repo is located at Host string - // User (github.com/) + // User (example.com/) User string - // Repo (github.com//) + // Repo (example.com//) Repo string - // Subdir (github.com///) + // Subdir (example.com///) Subdir string } @@ -75,7 +75,7 @@ func (gs *Git) UnmarshalJSON(data []byte) error { return nil } -// Name returns the repository in a go-like format (github.com/user/repo/subdir) +// Name returns the repository in a go-like format (example.com/user/repo/subdir) func (gs *Git) Name() string { return fmt.Sprintf("%s/%s/%s%s", gs.Host, gs.User, gs.Repo, gs.Subdir) } @@ -100,15 +100,13 @@ func (gs *Git) Remote() string { // regular expressions for matching package uris const ( - gitSSHExp = `ssh://git@(?P.+)/(?P.+)/(?P.+).git` - gitSCPExp = `^git@(?P.+):(?P.+)/(?P.+).git` - githubSlugExp = `github.com/(?P[-_a-zA-Z0-9]+)/(?P[-_a-zA-Z0-9]+)` + gitSSHExp = `ssh://git@(?P.+)/(?P.+)/(?P.+).git` + gitSCPExp = `^git@(?P.+):(?P.+)/(?P.+).git` + // The long ugly pattern for ${host} here is a generic pattern for "valid URL with zero or more subdomains and a valid TLD" + gitHTTPSExp = `(?P[a-zA-Z0-9][a-zA-Z0-9-\.]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,})/(?P[-_a-zA-Z0-9]+)/(?P[-_a-zA-Z0-9]+)` ) var ( - gitSSHRegex = regexp.MustCompile(gitSSHExp) - githubSlugRegex = regexp.MustCompile(githubSlugExp) - VersionRegex = `@(?P.*)` PathRegex = `/(?P.*)` PathAndVersionRegex = `/(?P.*)@(?P.*)` @@ -129,10 +127,9 @@ func parseGit(uri string) *Dependency { case reMatch(gitSCPExp, uri): gs, version = match(uri, gitSCPExp) gs.Scheme = GitSchemeSSH - case reMatch(githubSlugExp, uri): - gs, version = match(uri, githubSlugExp) + case reMatch(gitHTTPSExp, uri): + gs, version = match(uri, gitHTTPSExp) gs.Scheme = GitSchemeHTTPS - gs.Host = "github.com" default: return nil } @@ -150,7 +147,6 @@ func parseGit(uri string) *Dependency { func match(p string, exp string) (gs *Git, version string) { gs = &Git{} - exps := []*regexp.Regexp{ regexp.MustCompile(exp + PathAndVersionRegex), regexp.MustCompile(exp + PathRegex), diff --git a/spec/deps/git_test.go b/spec/deps/git_test.go index ba264cb..6722395 100644 --- a/spec/deps/git_test.go +++ b/spec/deps/git_test.go @@ -67,6 +67,108 @@ func TestParseGit(t *testing.T) { want: sshWant("my.host"), wantRemote: "ssh://git@my.host/user/repo.git", // want ssh format here }, + { + name: "ValidGitHTTPS", + uri: "https://example.com/foo/bar", + want: &Dependency{ + Version: "master", + Source: Source{ + GitSource: &Git{ + Scheme: GitSchemeHTTPS, + Host: "example.com", + User: "foo", + Repo: "bar", + Subdir: "", + }, + }, + }, + wantRemote: "https://example.com/foo/bar", + }, + { + name: "ValidGitNoScheme", + uri: "example.com/foo/bar", + want: &Dependency{ + Version: "master", + Source: Source{ + GitSource: &Git{ + Scheme: GitSchemeHTTPS, + Host: "example.com", + User: "foo", + Repo: "bar", + Subdir: "", + }, + }, + }, + wantRemote: "https://example.com/foo/bar", + }, + { + name: "ValidGitPath", + uri: "example.com/foo/bar/baz/bat", + want: &Dependency{ + Version: "master", + Source: Source{ + GitSource: &Git{ + Scheme: GitSchemeHTTPS, + Host: "example.com", + User: "foo", + Repo: "bar", + Subdir: "/baz/bat", + }, + }, + }, + wantRemote: "https://example.com/foo/bar", + }, + { + name: "ValidGitVersion", + uri: "example.com/foo/bar@baz", + want: &Dependency{ + Version: "baz", + Source: Source{ + GitSource: &Git{ + Scheme: GitSchemeHTTPS, + Host: "example.com", + User: "foo", + Repo: "bar", + Subdir: "", + }, + }, + }, + wantRemote: "https://example.com/foo/bar", + }, + { + name: "ValidGitPathVersion", + uri: "example.com/foo/bar/baz@bat", + want: &Dependency{ + Version: "bat", + Source: Source{ + GitSource: &Git{ + Scheme: GitSchemeHTTPS, + Host: "example.com", + User: "foo", + Repo: "bar", + Subdir: "/baz", + }, + }, + }, + wantRemote: "https://example.com/foo/bar", + }, + { + name: "ValidGitSubdomain", + uri: "git.example.com/foo/bar", + want: &Dependency{ + Version: "master", + Source: Source{ + GitSource: &Git{ + Scheme: GitSchemeHTTPS, + Host: "git.example.com", + User: "foo", + Repo: "bar", + Subdir: "", + }, + }, + }, + wantRemote: "https://git.example.com/foo/bar", + }, } for _, c := range tests {