Skip to content

Commit 988d36d

Browse files
authored
Merge pull request cli#709 from cli/www-hostname-support
Support `www.github.com` git remote URLs
2 parents d4692c3 + 894899c commit 988d36d

File tree

2 files changed

+61
-28
lines changed

2 files changed

+61
-28
lines changed

internal/ghrepo/repo.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ import (
88

99
const defaultHostname = "github.com"
1010

11+
// Interface describes an object that represents a GitHub repository
1112
type Interface interface {
1213
RepoName() string
1314
RepoOwner() string
1415
}
1516

17+
// New instantiates a GitHub repository from owner and name arguments
1618
func New(owner, repo string) Interface {
1719
return &ghRepo{
1820
owner: owner,
1921
name: repo,
2022
}
2123
}
24+
25+
// FullName serializes a GitHub repository into an "OWNER/REPO" string
2226
func FullName(r Interface) string {
2327
return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName())
2428
}
2529

30+
// FromFullName extracts the GitHub repository inforation from an "OWNER/REPO" string
2631
func FromFullName(nwo string) Interface {
2732
var r ghRepo
2833
parts := strings.SplitN(nwo, "/", 2)
@@ -32,8 +37,9 @@ func FromFullName(nwo string) Interface {
3237
return &r
3338
}
3439

40+
// FromURL extracts the GitHub repository information from a URL
3541
func FromURL(u *url.URL) (Interface, error) {
36-
if !strings.EqualFold(u.Hostname(), defaultHostname) {
42+
if !strings.EqualFold(u.Hostname(), defaultHostname) && !strings.EqualFold(u.Hostname(), "www."+defaultHostname) {
3743
return nil, fmt.Errorf("unsupported hostname: %s", u.Hostname())
3844
}
3945
parts := strings.SplitN(strings.TrimPrefix(u.Path, "/"), "/", 3)
@@ -43,6 +49,7 @@ func FromURL(u *url.URL) (Interface, error) {
4349
return New(parts[0], strings.TrimSuffix(parts[1], ".git")), nil
4450
}
4551

52+
// IsSame compares two GitHub repositories
4653
func IsSame(a, b Interface) bool {
4754
return strings.EqualFold(a.RepoOwner(), b.RepoOwner()) &&
4855
strings.EqualFold(a.RepoName(), b.RepoName())

internal/ghrepo/repo_test.go

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,66 @@
11
package ghrepo
22

33
import (
4+
"errors"
5+
"fmt"
46
"net/url"
57
"testing"
68
)
79

810
func Test_repoFromURL(t *testing.T) {
9-
u, _ := url.Parse("http://github.com/monalisa/octo-cat.git")
10-
repo, err := FromURL(u)
11-
if err != nil {
12-
t.Fatalf("got error %q", err)
13-
}
14-
if repo.RepoOwner() != "monalisa" {
15-
t.Errorf("got owner %q", repo.RepoOwner())
16-
}
17-
if repo.RepoName() != "octo-cat" {
18-
t.Errorf("got name %q", repo.RepoName())
19-
}
20-
}
21-
22-
func Test_repoFromURL_invalid(t *testing.T) {
23-
cases := [][]string{
24-
[]string{
25-
"https://example.com/one/two",
26-
"unsupported hostname: example.com",
11+
tests := []struct {
12+
name string
13+
input string
14+
result string
15+
err error
16+
}{
17+
{
18+
name: "github.com URL",
19+
input: "https://github.com/monalisa/octo-cat.git",
20+
result: "monalisa/octo-cat",
21+
err: nil,
2722
},
28-
[]string{
29-
"/path/to/disk",
30-
"unsupported hostname: ",
23+
{
24+
name: "www.github.com URL",
25+
input: "http://www.GITHUB.com/monalisa/octo-cat.git",
26+
result: "monalisa/octo-cat",
27+
err: nil,
28+
},
29+
{
30+
name: "unsupported hostname",
31+
input: "https://example.com/one/two",
32+
result: "",
33+
err: errors.New("unsupported hostname: example.com"),
34+
},
35+
{
36+
name: "filesystem path",
37+
input: "/path/to/file",
38+
result: "",
39+
err: errors.New("unsupported hostname: "),
3140
},
3241
}
33-
for _, c := range cases {
34-
u, _ := url.Parse(c[0])
35-
_, err := FromURL(u)
36-
if err == nil || err.Error() != c[1] {
37-
t.Errorf("got %q", err)
38-
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
u, err := url.Parse(tt.input)
46+
if err != nil {
47+
t.Fatalf("got error %q", err)
48+
}
49+
50+
repo, err := FromURL(u)
51+
if err != nil {
52+
if tt.err == nil {
53+
t.Fatalf("got error %q", err)
54+
} else if tt.err.Error() == err.Error() {
55+
return
56+
}
57+
t.Fatalf("got error %q", err)
58+
}
59+
60+
got := fmt.Sprintf("%s/%s", repo.RepoOwner(), repo.RepoName())
61+
if tt.result != got {
62+
t.Errorf("expected %q, got %q", tt.result, got)
63+
}
64+
})
3965
}
4066
}

0 commit comments

Comments
 (0)