Skip to content

Commit

Permalink
Move gitlab client setup into constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed Nov 29, 2018
1 parent 09ecb27 commit 1263ccb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 30 deletions.
40 changes: 40 additions & 0 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ package vcs

import (
"fmt"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/logging"
"net"
"net/url"
"strings"

"github.com/lkysow/go-gitlab"
"github.com/runatlantis/atlantis/server/events/models"
Expand All @@ -25,6 +29,42 @@ type GitlabClient struct {
Client *gitlab.Client
}

// NewGitlabClient returns a valid GitLab client.
func NewGitlabClient(hostname string, token string, logger *logging.SimpleLogger) (*GitlabClient, error) {
client := &GitlabClient{
Client: gitlab.NewClient(nil, token),
}

// If not using gitlab.com we need to set the URL to the API.
if hostname != "gitlab.com" {
// Check if they've also provided a scheme so we don't prepend it
// again.
scheme := "https"
hostname := hostname
schemeSplit := strings.Split(hostname, "://")
if len(schemeSplit) > 1 {
scheme = schemeSplit[0]
hostname = schemeSplit[1]
}

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

apiURL := fmt.Sprintf("%s://%s/api/v4/", scheme, hostname)
if err := client.Client.SetBaseURL(apiURL); err != nil {
return nil, errors.Wrapf(err, "setting GitLab API URL: %s", apiURL)
}
}

return client, nil
}

// GetModifiedFiles returns the names of files that were modified in the merge request.
// The names include the path to the file from the repo root, ex. parent/child/file.txt.
func (g *GitlabClient) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error) {
Expand Down
35 changes: 35 additions & 0 deletions server/events/vcs/gitlab_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package vcs

import (
. "github.com/runatlantis/atlantis/testing"
"testing"
)

// Test that the base url gets set properly.
func TestNewGitlabClient_BaseURL(t *testing.T) {
cases := []struct {
Hostname string
ExpBaseURL string
}{
{
"gitlab.com",
"https://gitlab.com/api/v4/",
},
{
"http://custom.domain",
"http://custom.domain/api/v4/",
},
{
"https://custom.domain",
"https://custom.domain/api/v4/",
},
}

for _, c := range cases {
t.Run(c.Hostname, func(t *testing.T) {
client, err := NewGitlabClient(c.Hostname, "token", nil)
Ok(t, err)
Equals(t, c.ExpBaseURL, client.Client.BaseURL().String())
})
}
}
34 changes: 4 additions & 30 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"flag"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
Expand All @@ -32,7 +31,6 @@ import (

"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/mux"
"github.com/lkysow/go-gitlab"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/locking"
Expand Down Expand Up @@ -155,34 +153,10 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
}
if userConfig.GitlabUser != "" {
supportedVCSHosts = append(supportedVCSHosts, models.Gitlab)
gitlabClient = &vcs.GitlabClient{
Client: gitlab.NewClient(nil, userConfig.GitlabToken),
}
// If not using gitlab.com we need to set the URL to the API.
if userConfig.GitlabHostname != "gitlab.com" {
// Check if they've also provided a scheme so we don't prepend it
// again.
scheme := "https"
hostname := userConfig.GitlabHostname
schemeSplit := strings.Split(userConfig.GitlabHostname, "://")
if len(schemeSplit) > 1 {
scheme = schemeSplit[0]
hostname = schemeSplit[1]
}

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

apiURL := fmt.Sprintf("%s://%s/api/v4/", scheme, hostname)
if err := gitlabClient.Client.SetBaseURL(apiURL); err != nil {
return nil, errors.Wrapf(err, "setting GitLab API URL: %s", apiURL)
}
var err error
gitlabClient, err = vcs.NewGitlabClient(userConfig.GitlabHostname, userConfig.GitlabToken, logger)
if err != nil {
return nil, err
}
}
if userConfig.BitbucketUser != "" {
Expand Down

0 comments on commit 1263ccb

Please sign in to comment.