Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIPE-2315 - Changes behaviour such that process and validate commands respect server hosts #880

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
})
}