Skip to content

Commit

Permalink
Changed the way we create config API client
Browse files Browse the repository at this point in the history
Fixed the tests that needed to be adapted to this change
  • Loading branch information
JulesFaucherre committed Aug 7, 2023
1 parent 42ca438 commit 416053e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
20 changes: 16 additions & 4 deletions cmd/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,10 @@ test: config
CompilerServerHandler: func(w http.ResponseWriter, r *http.Request) {
var req config.CompileConfigRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

// dummy compilation here (remove the _compiled_ key in compiled config, as compiled config can't have that at top-level key).
var yamlResp map[string]any
Expand Down Expand Up @@ -678,7 +681,10 @@ test: config
CompilerServerHandler: func(w http.ResponseWriter, r *http.Request) {
var req config.CompileConfigRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

response := config.ConfigResponse{Valid: true, SourceYaml: req.ConfigYaml, OutputYaml: req.ConfigYaml}

Expand Down Expand Up @@ -837,7 +843,10 @@ test: config
CompilerServerHandler: func(w http.ResponseWriter, r *http.Request) {
var req config.CompileConfigRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

response := config.ConfigResponse{Valid: true, SourceYaml: req.ConfigYaml, OutputYaml: req.ConfigYaml}

Expand Down Expand Up @@ -1082,7 +1091,10 @@ test: config
CompilerServerHandler: func(w http.ResponseWriter, r *http.Request) {
var req config.CompileConfigRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

response := config.ConfigResponse{Valid: true, SourceYaml: req.ConfigYaml, OutputYaml: req.ConfigYaml}

Expand Down
14 changes: 0 additions & 14 deletions config/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ 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"
)

Expand All @@ -24,15 +19,6 @@ 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)
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ConfigCompiler struct {
}

func NewWithConfig(cfg *settings.Config) (*ConfigCompiler, error) {
apiClient, err := GetAPIClient(cfg)
apiClient, err := newAPIClient(cfg)
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions config/v2_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ func (client *v2APIClient) CompileConfig(configContent string, orgID string, par

req, err := client.restClient.NewRequest(
"POST",
&url.URL{
Path: "compile-config-with-defaults",
},
&url.URL{Path: compilePath},
compileRequest,
)
if err != nil {
Expand Down

0 comments on commit 416053e

Please sign in to comment.