Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 'SetBaseURL' to correctly set GitLab URL based on user params. #229

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ func NewServer(config Config) (*Server, error) {
gitlabClient = &vcs.GitlabClient{
Client: gitlab.NewClient(nil, config.GitlabToken),
}
// Ensure the BaseURL has /api/v4/ appended. We only care about the Scheme
// and Host. GH-229
u, err := url.Parse(config.GitlabHostname)
if err != nil {
return nil, errors.Wrap(err, "parsing GitLab hostname")
}
if err := gitlabClient.Client.SetBaseURL(fmt.Sprintf("%s://%s/api/v4/", u.Scheme, u.Host)); err != nil {
return nil, err
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled in your commits and got them working in #231.

I wanted to explain the changes I made and why. Here's what I ended up with:

		// If not using gitlab.com we need to set the URL to the API.
		if config.GitlabHostname != "gitlab.com" {
			// Check if they've also provided a scheme so we don't prepend it
			// again.
			scheme := "https"
			schemeSplit := strings.Split(config.GitlabHostname, "://")
			if len(schemeSplit) > 1 {
				scheme = schemeSplit[0]
				config.GitlabHostname = schemeSplit[1]
			}
			apiURL := fmt.Sprintf("%s://%s/api/v4/", scheme, config.GitlabHostname)
			if err := gitlabClient.Client.SetBaseURL(apiURL); err != nil {
				return nil, errors.Wrapf(err, "setting GitLab API URL: %s", apiURL)
			}
		}
  • I decided that if they hadn't specified a custom gitlab url that we shouldn't bother going through calling SetBaseURL because it's more complicated as a reader of the code if I don't care about that branch
  • I didn't include the ticket number in the comment because as a reader of the code, I don't want to have to go to github to find out why something was done. Hopefully the comment explains everything.
  • I ended up having to handle the case when the user prepends https:// to the option because url.Parse doesn't complain when we then create a url with https://https://gitlab.com so I needed to catch that sooner.

}
var webhooksConfig []webhooks.Config
for _, c := range config.Webhooks {
Expand Down