Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

fix(gps): support source urls that point directly to go.googlesource.com #829

Closed
wants to merge 2 commits into from
Closed
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
35 changes: 35 additions & 0 deletions internal/gps/deduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var (
//gcRegex = regexp.MustCompile(`^(?P<root>code\.google\.com/[pr]/(?P<project>[a-z0-9\-]+)(\.(?P<subrepo>[a-z0-9\-]+))?)(/[A-Za-z0-9_.\-]+)*$`)
jazzRegex = regexp.MustCompile(`^(?P<root>hub\.jazz\.net(/git/[a-z0-9]+/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
apacheRegex = regexp.MustCompile(`^(?P<root>git\.apache\.org(/[a-z0-9_.\-]+\.git))((?:/[A-Za-z0-9_.\-]+)*)$`)
googlesourceRegex = regexp.MustCompile(`^(?P<root>go\.googlesource\.com(/[A-Za-z0-9-._]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
vcsExtensionRegex = regexp.MustCompile(`^(?P<root>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?/[A-Za-z0-9_.\-/~]*?\.(?P<vcs>bzr|git|hg|svn))((?:/[A-Za-z0-9_.\-]+)*)$`)
)

Expand All @@ -91,6 +92,7 @@ func pathDeducerTrie() *deducerTrie {
dxt.Insert("git.launchpad.net/", launchpadGitDeducer{regexp: glpRegex})
dxt.Insert("hub.jazz.net/", jazzDeducer{regexp: jazzRegex})
dxt.Insert("git.apache.org/", apacheDeducer{regexp: apacheRegex})
dxt.Insert("go.googlesource.com/", googlesourceDeducer{regexp: googlesourceRegex})

return dxt
}
Expand Down Expand Up @@ -463,6 +465,39 @@ func (m apacheDeducer) deduceSource(path string, u *url.URL) (maybeSource, error
return mb, nil
}

type googlesourceDeducer struct {
regexp *regexp.Regexp
}

func (m googlesourceDeducer) deduceRoot(path string) (string, error) {
v := m.regexp.FindStringSubmatch(path)
if v == nil {
return "", fmt.Errorf("%s is not a valid path for a source on go.googlesource.com", path)
}

return "go.googlesource.com" + v[2], nil
}

func (m googlesourceDeducer) deduceSource(path string, u *url.URL) (maybeSource, error) {
v := m.regexp.FindStringSubmatch(path)
if v == nil {
return nil, fmt.Errorf("%s is not a valid path for a source on go.googlesource.com", path)
}

u.Host = "go.googlesource.com"
u.Path = v[2]

switch u.Scheme {
case "":
u.Scheme = "https"
fallthrough
case "https":
return maybeGitSource{url: u}, nil
default:
return nil, fmt.Errorf("go.googlesource.com only supports https, %s is not allowed", u.String())
}
}

type vcsExtensionDeducer struct {
regexp *regexp.Regexp
}
Expand Down
26 changes: 26 additions & 0 deletions internal/gps/deduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,30 @@ var pathDeductionFixtures = map[string][]pathDeductionFixture{
},
},
},
"googlesource": {
// Official go repositories hosted on go.googlesource.com
{
in: "go.googlesource.com/crypto",
root: "go.googlesource.com/crypto",
mb: maybeGitSource{url: mkurl("https://go.googlesource.com/crypto")},
},
{
in: "go.googlesource.com/crypto/ssh",
root: "go.googlesource.com/crypto",
mb: maybeGitSource{url: mkurl("https://go.googlesource.com/crypto")},
},
// Spaces are not valid in package names
{
in: "go.googlesource.com/cry pto",
rerr: errors.New("go.googlesource.com/cry pto is not a valid path for a source on go.googlesource.com"),
},
// Non https schemes are not valid
{
in: "gopher://go.googlesource.com/crypto",
root: "go.googlesource.com/crypto",
srcerr: errors.New("go.googlesource.com only supports https, gopher://go.googlesource.com/crypto is not allowed"),
},
},
"vcsext": {
// VCS extension-based syntax
{
Expand Down Expand Up @@ -505,6 +529,8 @@ func TestDeduceFromPath(t *testing.T) {
deducer = launchpadGitDeducer{regexp: glpRegex}
case "apache":
deducer = apacheDeducer{regexp: apacheRegex}
case "googlesource":
deducer = googlesourceDeducer{regexp: googlesourceRegex}
case "vcsext":
deducer = vcsExtensionDeducer{regexp: vcsExtensionRegex}
default:
Expand Down