Skip to content

Commit b1847ec

Browse files
committed
Stricter matching for github.com and ghe.com URLs
1 parent bf64678 commit b1847ec

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

pkg/utils/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ func parseAPIHost(s string) (APIHost, error) {
235235
return APIHost{}, fmt.Errorf("host must have a scheme (http or https): %s", s)
236236
}
237237

238-
if strings.HasSuffix(u.Hostname(), "github.com") {
238+
if u.Hostname() == "github.com" || strings.HasSuffix(u.Hostname(), ".github.com") {
239239
return newDotcomHost()
240240
}
241241

242-
if strings.HasSuffix(u.Hostname(), "ghe.com") {
242+
if u.Hostname() == "ghe.com" || strings.HasSuffix(u.Hostname(), ".ghe.com") {
243243
return newGHECHost(s)
244244
}
245245

pkg/utils/api_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestParseAPIHost(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
input string
14+
wantRestURL string
15+
wantErr bool
16+
}{
17+
{
18+
name: "empty string defaults to dotcom",
19+
input: "",
20+
wantRestURL: "https://api.github.com/",
21+
},
22+
{
23+
name: "github.com hostname",
24+
input: "https://github.com",
25+
wantRestURL: "https://api.github.com/",
26+
},
27+
{
28+
name: "subdomain of github.com",
29+
input: "https://foo.github.com",
30+
wantRestURL: "https://api.github.com/",
31+
},
32+
{
33+
name: "hostname ending in github.com but not a subdomain",
34+
input: "https://mycompanygithub.com",
35+
wantRestURL: "https://mycompanygithub.com/api/v3/",
36+
},
37+
{
38+
name: "hostname ending in notgithub.com",
39+
input: "https://notgithub.com",
40+
wantRestURL: "https://notgithub.com/api/v3/",
41+
},
42+
{
43+
name: "ghe.com hostname",
44+
input: "https://ghe.com",
45+
wantRestURL: "https://api.ghe.com/",
46+
},
47+
{
48+
name: "subdomain of ghe.com",
49+
input: "https://mycompany.ghe.com",
50+
wantRestURL: "https://api.mycompany.ghe.com/",
51+
},
52+
{
53+
name: "hostname ending in ghe.com but not a subdomain",
54+
input: "https://myghe.com",
55+
wantRestURL: "https://myghe.com/api/v3/",
56+
},
57+
{
58+
name: "missing scheme",
59+
input: "github.com",
60+
wantErr: true,
61+
},
62+
}
63+
64+
for _, tc := range tests {
65+
t.Run(tc.name, func(t *testing.T) {
66+
host, err := parseAPIHost(tc.input)
67+
if tc.wantErr {
68+
assert.Error(t, err)
69+
return
70+
}
71+
require.NoError(t, err)
72+
assert.Equal(t, tc.wantRestURL, host.restURL.String())
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)