Skip to content

Commit

Permalink
Fix URL parsing not working when passing only a domain to GitlabHostname
Browse files Browse the repository at this point in the history
Fixes #377
  • Loading branch information
jocelynthode committed Dec 8, 2018
1 parent 284fa2e commit 2adaf4b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
24 changes: 14 additions & 10 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,29 @@ func NewGitlabClient(hostname string, token string, logger *logging.SimpleLogger

// If not using gitlab.com we need to set the URL to the API.
if hostname != "gitlab.com" {
u, err := url.Parse(hostname)
// We assume the url will be over HTTPS if the user doesn't specify a scheme.
absoluteURL := hostname
if !strings.HasPrefix(hostname, "http://") && !strings.HasPrefix(hostname, "https://") {
absoluteURL = "https://" + absoluteURL
}

url, err := url.Parse(absoluteURL)
if err != nil {
return nil, errors.Wrapf(err, "parsing hostname %q", hostname)
return nil, errors.Wrapf(err, "parsing URL %q", absoluteURL)
}

// Warn if this hostname isn't resolvable. The GitLab client
// doesn't give good error messages in this case.
ips, err := net.LookupIP(u.Hostname())
ips, err := net.LookupIP(url.Hostname())
if err != nil {
logger.Warn("unable to resolve %q: %s", u.Hostname(), err)
logger.Warn("unable to resolve %q: %s", url.Hostname(), err)
} else if len(ips) == 0 {
logger.Warn("found no IPs while resolving %q", u.Hostname())
logger.Warn("found no IPs while resolving %q", url.Hostname())
}

scheme := "https"
if u.Scheme != "" {
scheme = u.Scheme
}
apiURL := fmt.Sprintf("%s://%s/api/v4/", scheme, u.Host)
// Now we're ready to construct the client.
absoluteURL = strings.TrimSuffix(absoluteURL, "/")
apiURL := fmt.Sprintf("%s/api/v4/", absoluteURL)
if err := client.Client.SetBaseURL(apiURL); err != nil {
return nil, errors.Wrapf(err, "setting GitLab API URL: %s", apiURL)
}
Expand Down
16 changes: 16 additions & 0 deletions server/events/vcs/gitlab_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,30 @@ func TestNewGitlabClient_BaseURL(t *testing.T) {
"gitlab.com",
"https://gitlab.com/api/v4/",
},
{
"custom.domain",
"https://custom.domain/api/v4/",
},
{
"http://custom.domain",
"http://custom.domain/api/v4/",
},
{
"http://custom.domain:8080",
"http://custom.domain:8080/api/v4/",
},
{
"https://custom.domain",
"https://custom.domain/api/v4/",
},
{
"https://custom.domain/",
"https://custom.domain/api/v4/",
},
{
"https://custom.domain/basepath/",
"https://custom.domain/basepath/api/v4/",
},
}

for _, c := range cases {
Expand Down

0 comments on commit 2adaf4b

Please sign in to comment.