-
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.
Merge pull request #977 from CircleCI-Public/task/DEVEX-1028/cli-abst…
…ract-api-calls-to-allow-better-testing-and-backward-compatibility-CONFIG-ABSTRACTION refactor: Abstracted the different config validation routes
- Loading branch information
Showing
14 changed files
with
355 additions
and
138 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
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,62 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/api/graphql" | ||
"github.com/CircleCI-Public/circleci-cli/api/rest" | ||
"github.com/CircleCI-Public/circleci-cli/settings" | ||
) | ||
|
||
const compilePath = "compile-config-with-defaults" | ||
|
||
type apiClientVersion string | ||
|
||
type APIClient interface { | ||
CompileConfig(configContent string, orgID string, params Parameters, values Values) (*ConfigResponse, error) | ||
} | ||
|
||
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") | ||
} | ||
} | ||
|
||
// detectAPIClientVersion returns the highest available version of the config API. | ||
// | ||
// To do that it tries to request the `compilePath` API route. | ||
// If the route returns a 404, this means the route does not exist on the requested host and the function returns | ||
// `v1_string` indicating that the deprecated GraphQL endpoint should be used instead. | ||
// Else if the route returns any other status, this means it is available for request and the function returns | ||
// `v2_string` indicating that the route can be used | ||
func detectAPIClientVersion(restClient *rest.Client) (apiClientVersion, error) { | ||
req, err := restClient.NewRequest("POST", &url.URL{Path: compilePath}, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
_, err = restClient.DoRequest(req, nil) | ||
httpErr, ok := err.(*rest.HTTPError) | ||
if !ok { | ||
return "", err | ||
} | ||
if httpErr.Code == http.StatusNotFound { | ||
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,44 @@ | ||
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 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) | ||
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("on other cases return v2", func(t *testing.T) { | ||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(500) | ||
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) | ||
}) | ||
}) | ||
} |
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.