Skip to content

Commit 557ff78

Browse files
author
Rachel Dowavic
committed
Fix parseGitHostURL appending /api/v4/ to all services
Only GitLab requires /api/v4/ appended to custom host URLs. Previously this was appended for all services (with a special case to skip Forgejo), which broke GitHub Enterprise and Bitbucket custom host URLs. Fixes #195
1 parent 15e3a70 commit 557ff78

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

‎client.go‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ func newClient(service string, gitHostURL string) interface{} {
9090
}
9191

9292
// parseGitHostURL parses the git host URL if provided
93-
// TODO: there is a chance, this parsing breaks more than
94-
// one git service
95-
// https://github.com/amitsaha/gitbackup/issues/195
9693
func parseGitHostURL(gitHostURL string, service string) *url.URL {
9794
if len(gitHostURL) == 0 {
9895
return nil
@@ -103,12 +100,12 @@ func parseGitHostURL(gitHostURL string, service string) *url.URL {
103100
log.Fatalf("Invalid git host URL: %s", gitHostURL)
104101
}
105102

106-
// temp fix for https://github.com/amitsaha/gitbackup/issues/193
107-
if service == "forgejo" {
108-
return gitHostURLParsed
103+
// Only GitLab requires /api/v4/ to be appended
104+
if service == "gitlab" {
105+
api, _ := url.Parse("api/v4/")
106+
return gitHostURLParsed.ResolveReference(api)
109107
}
110-
api, _ := url.Parse("api/v4/")
111-
return gitHostURLParsed.ResolveReference(api)
108+
return gitHostURLParsed
112109
}
113110

114111
// newGitHubClient creates a new GitHub client

‎client_test.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,30 @@ func TestNewClient(t *testing.T) {
1515
defer teardownRepositoryTests()
1616

1717
customGitHost, _ := url.Parse("https://git.mycompany.com")
18-
// http://stackoverflow.com/questions/23051339/how-to-avoid-end-of-url-slash-being-removed-when-resolvereference-in-go
18+
// GitLab expects /api/v4/ appended
1919
api, _ := url.Parse("api/v4/")
20-
expectedGitHostBaseURL := customGitHost.ResolveReference(api)
20+
expectedGitLabBaseURL := customGitHost.ResolveReference(api)
2121

2222
// Client for github.com
2323
client := newClient("github", "")
2424
client = client.(*github.Client)
2525

26-
// Client for Enterprise Github
26+
// Client for Enterprise Github - should use the URL as-is, not append /api/v4/
2727
client = newClient("github", customGitHost.String())
2828
gotBaseURL := client.(*github.Client).BaseURL
29-
if gotBaseURL.String() != expectedGitHostBaseURL.String() {
30-
t.Errorf("Expected BaseURL to be: %v, Got: %v\n", expectedGitHostBaseURL, gotBaseURL)
29+
if gotBaseURL.String() != customGitHost.String() {
30+
t.Errorf("Expected BaseURL to be: %v, Got: %v\n", customGitHost, gotBaseURL)
3131
}
3232

3333
// Client for gitlab.com
3434
client = newClient("gitlab", "")
3535
client = client.(*gitlab.Client)
3636

37-
// Client for custom gitlab installation
37+
// Client for custom gitlab installation - should append /api/v4/
3838
client = newClient("gitlab", customGitHost.String())
3939
gotBaseURL = client.(*gitlab.Client).BaseURL()
40-
if gotBaseURL.String() != expectedGitHostBaseURL.String() {
41-
t.Errorf("Expected BaseURL to be: %v, Got: %v\n", expectedGitHostBaseURL, gotBaseURL)
40+
if gotBaseURL.String() != expectedGitLabBaseURL.String() {
41+
t.Errorf("Expected BaseURL to be: %v, Got: %v\n", expectedGitLabBaseURL, gotBaseURL)
4242
}
4343

4444
// Client for bitbucket.com

0 commit comments

Comments
 (0)