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

Conversation

jrasell
Copy link

@jrasell jrasell commented Jan 12, 2018

This change updates server/server.go to set the GitLab URL using the Client.SetBaseURL to set the base URL off of the configured param. Previously even if a user set the --gitlab-hostname, the default URL for GitLab would not overridden to match this.

Tests run locally:

make test
?   	github.com/hootsuite/atlantis	[no test files]
?   	github.com/hootsuite/atlantis/bootstrap	[no test files]
ok  	github.com/hootsuite/atlantis/cmd	0.064s
ok  	github.com/hootsuite/atlantis/server	0.846s
ok  	github.com/hootsuite/atlantis/server/events	0.059s
ok  	github.com/hootsuite/atlantis/server/events/locking	0.010s
ok  	github.com/hootsuite/atlantis/server/events/locking/boltdb	0.121s
?   	github.com/hootsuite/atlantis/server/events/locking/mocks	[no test files]
?   	github.com/hootsuite/atlantis/server/events/locking/mocks/matchers	[no test files]
?   	github.com/hootsuite/atlantis/server/events/mocks	[no test files]
?   	github.com/hootsuite/atlantis/server/events/mocks/matchers	[no test files]
?   	github.com/hootsuite/atlantis/server/events/models	[no test files]
?   	github.com/hootsuite/atlantis/server/events/models/fixtures	[no test files]
ok  	github.com/hootsuite/atlantis/server/events/run	0.160s
?   	github.com/hootsuite/atlantis/server/events/run/mocks	[no test files]
?   	github.com/hootsuite/atlantis/server/events/run/mocks/matchers	[no test files]
ok  	github.com/hootsuite/atlantis/server/events/terraform	0.010s
?   	github.com/hootsuite/atlantis/server/events/terraform/mocks	[no test files]
?   	github.com/hootsuite/atlantis/server/events/terraform/mocks/matchers	[no test files]
ok  	github.com/hootsuite/atlantis/server/events/vcs	0.013s [no tests to run]
?   	github.com/hootsuite/atlantis/server/events/vcs/fixtures	[no test files]
?   	github.com/hootsuite/atlantis/server/events/vcs/mocks	[no test files]
?   	github.com/hootsuite/atlantis/server/events/vcs/mocks/matchers	[no test files]
ok  	github.com/hootsuite/atlantis/server/events/webhooks	0.014s
?   	github.com/hootsuite/atlantis/server/events/webhooks/mocks	[no test files]
?   	github.com/hootsuite/atlantis/server/events/webhooks/mocks/matchers	[no test files]
ok  	github.com/hootsuite/atlantis/server/logging	0.011s [no tests to run]
?   	github.com/hootsuite/atlantis/server/logging/mocks	[no test files]
?   	github.com/hootsuite/atlantis/server/logging/mocks/matchers	[no test files]
?   	github.com/hootsuite/atlantis/server/mocks	[no test files]
?   	github.com/hootsuite/atlantis/server/mocks/matchers	[no test files]
ok  	github.com/hootsuite/atlantis/server/recovery	0.011s [no tests to run]
?   	github.com/hootsuite/atlantis/testing	[no test files]

Closes #227

This change updates server/server.go to set the GitLab URL using
the `Client.SetBaseURL` to set the base URL off of the configured
param. Previosuly even if a user set the `--gitlab-hostname` the
default URL for GitLab would not overriden to match this.

Closes hootsuite#227
@CLAassistant
Copy link

CLAassistant commented Jan 12, 2018

CLA assistant check
All committers have signed the CLA.

server/server.go Outdated
@@ -105,6 +105,10 @@ func NewServer(config Config) (*Server, error) {
gitlabClient = &vcs.GitlabClient{
Client: gitlab.NewClient(nil, config.GitlabToken),
}
err := gitlabClient.Client.SetBaseURL(config.GitlabHostname)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The default base URL is

	defaultBaseURL = "https://gitlab.com/api/v4/"

based on the go-gitlab library (https://github.com/xanzy/go-gitlab/blob/c85e4e9d786c10e15e28446456f0e8918561b2d9/gitlab.go#L39)

We default to gitlab.com so if we're always going to call SetBaseURL then we'll need to convert it to the same format with https:// and /api/v4/.

Based on https://docs.gitlab.com/ce/api/#basic-usage I think we can assume that if you're running GitLab Enterprise that your API will be available at https://{hostname}/api/v4/. @jrasell is it possible for you to confirm that?

If so, then this PR needs to

  1. extract the protocol and hostname from the config.GitlabHostname string (in case the user adds the path we'll want to strip it)
  2. add the /api/v4/ path

Something like

		supportedVCSHosts = append(supportedVCSHosts, vcs.Gitlab)
		gitlabClient = &vcs.GitlabClient{
			Client: gitlab.NewClient(nil, config.GitlabToken),
		}
		// We need to ensure the BaseURL has /api/v4/ appended. We first
		// parse the URL because we only care about the Scheme and Host and
		// want to discard anything else.
		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
		}

@lkysow lkysow mentioned this pull request Jan 15, 2018
}
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.

@lkysow
Copy link
Collaborator

lkysow commented Jan 16, 2018

Commits pulled in in #231.

@lkysow lkysow closed this Jan 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GitLab - Issue when passing --gitlab-hostname param
3 participants