Skip to content

Commit 741d421

Browse files
committed
Relax rules for valid github usernames
The current rules for github usernames only allow for alphanumeric characters or single hyphens and they cannot begin or end with a hyphen. In the past however github username rules were less strict and we need to support these (issue golang#194). Using the Google BigQuery public github dataset, I've checked the usernames of all public commits against the currently defined regex in deduce.go. From these results I've concluded that usernames with multiple consecutive hyphens and usernames that end with a hyphen were allowed in the past and do exist. Fortunately these are the only exceptions I've found, there were no usernames that started with a hyphen or contained any other special characters. In addition, this change now also allows one-letter usernames.
1 parent 65f245d commit 741d421

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

deduce.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ func validateVCSScheme(scheme, typ string) bool {
4848

4949
// Regexes for the different known import path flavors
5050
var (
51-
// This regex allowed some usernames that github currently disallows. They
52-
// may have allowed them in the past; keeping it in case we need to revert.
53-
//ghRegex = regexp.MustCompile(`^(?P<root>github\.com/([A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`)
54-
ghRegex = regexp.MustCompile(`^(?P<root>github\.com(/[A-Za-z0-9][-A-Za-z0-9]*[A-Za-z0-9]/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
51+
// This regex allows some usernames that github currently disallows. They
52+
// have allowed them in the past.
53+
ghRegex = regexp.MustCompile(`^(?P<root>github\.com(/[A-Za-z0-9][-A-Za-z0-9]*/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
5554
gpinNewRegex = regexp.MustCompile(`^(?P<root>gopkg\.in(?:(/[a-zA-Z0-9][-a-zA-Z0-9]+)?)(/[a-zA-Z][-.a-zA-Z0-9]*)\.((?:v0|v[1-9][0-9]*)(?:\.0|\.[1-9][0-9]*){0,2}(?:-unstable)?)(?:\.git)?)((?:/[a-zA-Z0-9][-.a-zA-Z0-9]*)*)$`)
5655
//gpinOldRegex = regexp.MustCompile(`^(?P<root>gopkg\.in/(?:([a-z0-9][-a-z0-9]+)/)?((?:v0|v[1-9][0-9]*)(?:\.0|\.[1-9][0-9]*){0,2}(-unstable)?)/([a-zA-Z][-a-zA-Z0-9]*)(?:\.git)?)((?:/[a-zA-Z][-a-zA-Z0-9]*)*)$`)
5756
bbRegex = regexp.MustCompile(`^(?P<root>bitbucket\.org(?P<bitname>/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)

deduce_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,31 @@ var pathDeductionFixtures = map[string][]pathDeductionFixture{
7777
root: "github.com/sdboyer/gps",
7878
mb: maybeGitSource{url: mkurl("https://github.com/sdboyer/gps")},
7979
},
80+
{
81+
in: "github.com/sdboyer-/gps/foo",
82+
root: "github.com/sdboyer-/gps",
83+
mb: maybeSources{
84+
maybeGitSource{url: mkurl("https://github.com/sdboyer-/gps")},
85+
maybeGitSource{url: mkurl("ssh://[email protected]/sdboyer-/gps")},
86+
maybeGitSource{url: mkurl("git://github.com/sdboyer-/gps")},
87+
maybeGitSource{url: mkurl("http://github.com/sdboyer-/gps")},
88+
},
89+
},
90+
{
91+
in: "github.com/a/gps/foo",
92+
root: "github.com/a/gps",
93+
mb: maybeSources{
94+
maybeGitSource{url: mkurl("https://github.com/a/gps")},
95+
maybeGitSource{url: mkurl("ssh://[email protected]/a/gps")},
96+
maybeGitSource{url: mkurl("git://github.com/a/gps")},
97+
maybeGitSource{url: mkurl("http://github.com/a/gps")},
98+
},
99+
},
80100
// some invalid github username patterns
81101
{
82102
in: "github.com/-sdboyer/gps/foo",
83103
rerr: errors.New("github.com/-sdboyer/gps/foo is not a valid path for a source on github.com"),
84104
},
85-
{
86-
in: "github.com/sdboyer-/gps/foo",
87-
rerr: errors.New("github.com/sdboyer-/gps/foo is not a valid path for a source on github.com"),
88-
},
89105
{
90106
in: "github.com/sdbo.yer/gps/foo",
91107
rerr: errors.New("github.com/sdbo.yer/gps/foo is not a valid path for a source on github.com"),

0 commit comments

Comments
 (0)