Skip to content

Commit d8fff5c

Browse files
committed
fix issue with ghe deep link
1 parent ed2b926 commit d8fff5c

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Improve status display text in new bitbucket pull request screen, from [@bradrydzewski](https://github.com/bradrydzewski). See [#27](https://github.com/drone/go-scm/issues/27).
1010

11+
### Fixed
12+
- Fix issue with GitHub enterprise deep link including API prefix, from [@bradrydzewski](https://github.com/bradrydzewski).
13+
1114
## [1.6.0]
1215
### Added
1316
- Support Head and Base sha for GitHub pull request, from [@bradrydzewski](https://github.com/bradrydzewski).

scm/driver/github/github.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ func New(uri string) (*scm.Client, error) {
2525
if !strings.HasSuffix(base.Path, "/") {
2626
base.Path = base.Path + "/"
2727
}
28-
home := base.String()
29-
if home == "https://api.github.com/" {
30-
home = "https://github.com/"
31-
}
28+
// home := base.String()
29+
// if home == "https://api.github.com/" {
30+
// home = "https://github.com/"
31+
// }
32+
3233
client := &wrapper{new(scm.Client)}
3334
client.BaseURL = base
3435
// initialize services
3536
client.Driver = scm.DriverGithub
36-
client.Linker = &linker{home}
37+
client.Linker = &linker{websiteAddress(base)}
3738
client.Contents = &contentService{client}
3839
client.Git = &gitService{client}
3940
client.Issues = &issueService{client}
@@ -126,3 +127,14 @@ type Error struct {
126127
func (e *Error) Error() string {
127128
return e.Message
128129
}
130+
131+
// helper function converts the github API url to
132+
// the website url.
133+
func websiteAddress(u *url.URL) string {
134+
host, proto := u.Host, u.Scheme
135+
switch host {
136+
case "api.github.com":
137+
return "https://github.com/"
138+
}
139+
return proto + "://" + host + "/"
140+
}

scm/driver/github/github_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package github
66

77
import (
8+
"net/url"
89
"testing"
910

1011
"github.com/drone/go-scm/scm"
@@ -96,3 +97,23 @@ func testRequest(res *scm.Response) func(t *testing.T) {
9697
}
9798
}
9899
}
100+
101+
func TestWebsiteAddress(t *testing.T) {
102+
tests := []struct {
103+
api string
104+
web string
105+
}{
106+
{"https://api.github.com/", "https://github.com/"},
107+
{"https://api.github.com", "https://github.com/"},
108+
{"https://github.acme.com/api/v3", "https://github.acme.com/"},
109+
{"https://github.acme.com/api/v3/", "https://github.acme.com/"},
110+
}
111+
112+
for _, test := range tests {
113+
parsed, _ := url.Parse(test.api)
114+
got, want := websiteAddress(parsed), test.web
115+
if got != want {
116+
t.Errorf("Want website address %q, got %q", want, got)
117+
}
118+
}
119+
}

scm/driver/github/linker_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestDiff(t *testing.T) {
9797
}
9898

9999
func TestLink_GitHub_Enterprise(t *testing.T) {
100-
client, _ := New("https://github.acme.com")
100+
client, _ := New("https://github.acme.com/api/v3")
101101
ref := scm.Reference{
102102
Path: "refs/heads/master",
103103
Sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde",
@@ -112,3 +112,14 @@ func TestLink_GitHub_Enterprise(t *testing.T) {
112112
t.Errorf("Want link %q, got %q", want, got)
113113
}
114114
}
115+
116+
func TestLinkBase(t *testing.T) {
117+
if got, want := NewDefault().Linker.(*linker).base, "https://github.com/"; got != want {
118+
t.Errorf("Want url %s, got %s", want, got)
119+
}
120+
121+
client, _ := New("https://github.acme.com/api/v3")
122+
if got, want := client.Linker.(*linker).base, "https://github.acme.com/"; got != want {
123+
t.Errorf("Want url %s, got %s", want, got)
124+
}
125+
}

0 commit comments

Comments
 (0)