Skip to content

Commit

Permalink
Merge pull request #880 from CircleCI-Public/PIPE-2315-bugfix-server-…
Browse files Browse the repository at this point in the history
…issue

PIPE-2315 - Changes behaviour such that process and validate commands respect server hosts
  • Loading branch information
elliotforbes authored Mar 22, 2023
2 parents c3e90a1 + 5298cf6 commit d3a9440
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ var configAnnotations = map[string]string{
"<path>": "The path to your config (use \"-\" for STDIN)",
}

func GetConfigAPIHost(cfg *settings.Config) string {
if cfg.Host != defaultHost {
return cfg.Host
} else {
return cfg.ConfigAPIHost
}
}

func newConfigCommand(config *settings.Config) *cobra.Command {
opts := configOptions{
cfg: config,
Expand Down Expand Up @@ -68,7 +76,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command {
Short: "Check that the config file is well formed.",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
opts.rest = rest.NewFromConfig(config.ConfigAPIHost, config)
opts.rest = rest.NewFromConfig(GetConfigAPIHost(opts.cfg), config)
},
RunE: func(cmd *cobra.Command, _ []string) error {
return validateConfig(opts, cmd.Flags())
Expand All @@ -90,7 +98,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command {
Short: "Validate config and display expanded configuration.",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
opts.rest = rest.NewFromConfig(config.ConfigAPIHost, config)
opts.rest = rest.NewFromConfig(GetConfigAPIHost(opts.cfg), config)
},
RunE: func(cmd *cobra.Command, _ []string) error {
return processConfig(opts, cmd.Flags())
Expand Down
18 changes: 18 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"fmt"
"os/exec"
"path/filepath"
"testing"

"github.com/CircleCI-Public/circleci-cli/clitest"
"github.com/CircleCI-Public/circleci-cli/cmd"
"github.com/CircleCI-Public/circleci-cli/settings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/stretchr/testify/assert"
"gotest.tools/v3/golden"
)

Expand Down Expand Up @@ -246,3 +250,17 @@ var _ = Describe("Config", func() {
})
})
})

func TestGetConfigAPIHost(t *testing.T) {
t.Run("tests that we correctly get the config api host when the host is not the default one", func(t *testing.T) {
// if the host isn't equal to `https://circleci.com` then this is likely a server instance and
// wont have the api.X.com subdomain so we should instead just respect the host for config commands
host := cmd.GetConfigAPIHost(&settings.Config{Host: "test"})
assert.Equal(t, host, "test")

// If the host passed in is the same as the defaultHost 'https://circleci.com' - then we know this is cloud
// and as such should use the `api.circleci.com` subdomain
host = cmd.GetConfigAPIHost(&settings.Config{Host: "https://circleci.com", ConfigAPIHost: "https://api.circleci.com"})
assert.Equal(t, host, "https://api.circleci.com")
})
}

0 comments on commit d3a9440

Please sign in to comment.