Skip to content

Commit

Permalink
refactor newClient
Browse files Browse the repository at this point in the history
  • Loading branch information
amitsaha committed Oct 14, 2023
1 parent 9368e7d commit e788ba5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func getToken(service string) (string, error) {
return string(i.Data), nil
}

func newClient(service string, c *appConfig) interface{} {
func newClient(c *appConfig) interface{} {
var err error
var gitHostURLParsed *url.URL

Expand All @@ -82,7 +82,7 @@ func newClient(service string, c *appConfig) interface{} {
}
}

switch service {
switch c.service {
case "github":
githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken == "" {
Expand Down
59 changes: 36 additions & 23 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,65 @@ func TestNewClient(t *testing.T) {
setupRepositoryTests()
defer teardownRepositoryTests()

var defaultGithubConfig appConfig
defaultGithubConfig.gitHostURL = "https://github.com"

var defaultGitlabConfig appConfig
defaultGitlabConfig.gitHostURL = "https://gitlab.com"

var defaultBitbucketConfig appConfig
defaultBitbucketConfig.gitHostURL = "https://bitbucket.org"
defaultGithubConfig := appConfig{
service: "github",
gitHostURL: "https://github.com",
}
customGithubConfig := appConfig{
service: "github",
gitHostURL: "https://git.mycompany.com",
}

var customGithostConfig appConfig
customGitHost := "https://git.mycompany.com"
customGithostConfig.gitHostURL = customGitHost
defaultGitlabConfig := appConfig{
service: "gitlab",
gitHostURL: "https://gitlab.com",
}
customGitlabConfig := appConfig{
service: "gitlab",
gitHostURL: "https://git.mycompany.com",
}

// http://stackoverflow.com/questions/23051339/how-to-avoid-end-of-url-slash-being-removed-when-resolvereference-in-go
api, _ := url.Parse("api/v4/")
customGitHostParsed, _ := url.Parse(customGitHost)
expectedGitHostBaseURL := customGitHostParsed.ResolveReference(api)
defaultBitbucketConfig := appConfig{
service: "bitbucket",
gitHostURL: "https://bitbucket.org",
}

// Client for github.com
client := newClient("github", &defaultGithubConfig)
client := newClient(&defaultGithubConfig)
client = client.(*github.Client)

// Client for Enterprise Github
client = newClient("github", &customGithostConfig)
client = newClient(&customGithubConfig)
gotBaseURL := client.(*github.Client).BaseURL
if gotBaseURL.String() != customGitHostParsed.String() {
t.Errorf("Expected BaseURL to be: %v, Got: %v\n", expectedGitHostBaseURL, gotBaseURL)
if gotBaseURL.String() != customGithubConfig.gitHostURL {
t.Errorf("Expected BaseURL to be: %v, Got: %v\n", customGithubConfig.gitHostURL, gotBaseURL)
}

// Client for gitlab.com
client = newClient("gitlab", &defaultGitlabConfig)
client = newClient(&defaultGitlabConfig)
client = client.(*gitlab.Client)

// http://stackoverflow.com/questions/23051339/how-to-avoid-end-of-url-slash-being-removed-when-resolvereference-in-go
api, _ := url.Parse("api/v4/")
customGitHostParsed, _ := url.Parse(customGitlabConfig.gitHostURL)
expectedGitHostBaseURL := customGitHostParsed.ResolveReference(api)

// Client for custom gitlab installation
client = newClient("gitlab", &customGithostConfig)
client = newClient(&customGitlabConfig)
gotBaseURL = client.(*gitlab.Client).BaseURL()
if gotBaseURL.String() != expectedGitHostBaseURL.String() {
t.Errorf("Expected BaseURL to be: %v, Got: %v\n", expectedGitHostBaseURL, gotBaseURL)
}

// Client for bitbucket.com
client = newClient("bitbucket", &defaultBitbucketConfig)
client = newClient(&defaultBitbucketConfig)
client = client.(*bitbucket.Client)

// Not yet supported
client = newClient("notyetsupported", nil)
unsupportedServiceConfig := appConfig{
service: "notyetsupported",
}
client = newClient(&unsupportedServiceConfig)
if client != nil {
t.Errorf("Expected nil")
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
log.Fatal(err)
}

client := newClient(c.service, c)
client := newClient(c)
var executionErr error

// TODO implement validation of options so that we don't
Expand Down

0 comments on commit e788ba5

Please sign in to comment.