-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Abstracted the different config validation routes
- Loading branch information
1 parent
bed1100
commit fde1bce
Showing
12 changed files
with
359 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"sync" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/api/graphql" | ||
"github.com/CircleCI-Public/circleci-cli/api/rest" | ||
"github.com/CircleCI-Public/circleci-cli/settings" | ||
) | ||
|
||
var ( | ||
compiler APIClient | ||
compilerError error | ||
once sync.Once | ||
|
||
compilePath = "compile-config-with-defaults" | ||
) | ||
|
||
type apiClientVersion string | ||
|
||
type APIClient interface { | ||
CompileConfig(configContent string, orgID string, params Parameters, values Values) (*ConfigResponse, error) | ||
} | ||
|
||
func GetAPIClient(config *settings.Config) (APIClient, error) { | ||
if compiler == nil { | ||
once.Do(func() { | ||
compiler, compilerError = newAPIClient(config) | ||
}) | ||
} | ||
return compiler, compilerError | ||
} | ||
|
||
func newAPIClient(config *settings.Config) (APIClient, error) { | ||
hostValue := getCompileHost(config.Host) | ||
restClient := rest.NewFromConfig(hostValue, config) | ||
|
||
version, err := detectAPIClientVersion(restClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch version { | ||
case v1_string: | ||
return &v1APIClient{graphql.NewClient(config.HTTPClient, config.Host, config.Endpoint, config.Token, config.Debug)}, nil | ||
case v2_string: | ||
return &v2APIClient{restClient}, nil | ||
default: | ||
return nil, fmt.Errorf("Unable to recognise your Server's config file API") | ||
} | ||
} | ||
|
||
func detectAPIClientVersion(restClient *rest.Client) (apiClientVersion, error) { | ||
req, err := restClient.NewRequest("POST", &url.URL{Path: compilePath}, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
statusCode, err := restClient.DoRequest(req, nil) | ||
if err != nil { | ||
if _, ok := err.(*rest.HTTPError); ok { | ||
return "", err | ||
} | ||
} | ||
if statusCode == 404 { | ||
return v1_string, nil | ||
} | ||
return v2_string, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/api/rest" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestAPIClient(t *testing.T) { | ||
t.Run("detectCompilerVersion", func(t *testing.T) { | ||
t.Run("when the route returns a 400 tells that the version is v2", func(t *testing.T) { | ||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(400) | ||
w.Header().Set("Content-Type", "application/octet-stream") | ||
fmt.Fprintf(w, "Invalid input") | ||
})) | ||
url, err := url.Parse(svr.URL) | ||
assert.NilError(t, err) | ||
|
||
restClient := rest.New(url, "token", http.DefaultClient) | ||
version, err := detectAPIClientVersion(restClient) | ||
assert.NilError(t, err) | ||
assert.Equal(t, version, v2_string) | ||
}) | ||
|
||
t.Run("when the route returns a 404 tells that the version is v1", func(t *testing.T) { | ||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(404) | ||
w.Header().Set("Content-Type", "application/octet-stream") | ||
fmt.Fprintf(w, "Invalid input") | ||
})) | ||
url, err := url.Parse(svr.URL) | ||
assert.NilError(t, err) | ||
|
||
restClient := rest.New(url, "token", http.DefaultClient) | ||
version, err := detectAPIClientVersion(restClient) | ||
assert.NilError(t, err) | ||
assert.Equal(t, version, v1_string) | ||
}) | ||
|
||
t.Run("in any other case, return an error", func(t *testing.T) { | ||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(500) | ||
w.Header().Set("Content-Type", "application/octet-stream") | ||
fmt.Fprintf(w, "Invalid input") | ||
})) | ||
url, err := url.Parse(svr.URL) | ||
assert.NilError(t, err) | ||
|
||
restClient := rest.New(url, "token", http.DefaultClient) | ||
version, err := detectAPIClientVersion(restClient) | ||
assert.Equal(t, version, apiClientVersion("")) | ||
assert.Error(t, err, "Invalid input") | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.