diff --git a/cmd/policy/policy.go b/cmd/policy/policy.go index 19147fbc3..9060ac750 100644 --- a/cmd/policy/policy.go +++ b/cmd/policy/policy.go @@ -521,7 +521,7 @@ This group of commands allows the management of polices to be verified against b ConfigYaml: string(data), Options: config.Options{ OwnerID: ownerID, - PipelineValues: pipelineValues, + PipelineValues: config.LocalPipelineValues(parameters), PipelineParameters: parameters, }, }, diff --git a/config/commands.go b/config/commands.go index 8da291eaf..bb046e1f0 100644 --- a/config/commands.go +++ b/config/commands.go @@ -82,7 +82,7 @@ func (c *ConfigCompiler) ProcessConfig(opts ProcessConfigOpts) (response *Config } //if no orgId provided use org slug - values := LocalPipelineValues() + values := LocalPipelineValues(params) if opts.VerboseOutput { fmt.Fprintln(os.Stderr, "Processing config with following values:") printValues(values) @@ -126,7 +126,7 @@ func (c *ConfigCompiler) ValidateConfig(opts ValidateConfigOpts) error { var response *ConfigResponse //if no orgId provided use org slug - values := LocalPipelineValues() + values := LocalPipelineValues(nil) if opts.VerboseOutput { fmt.Fprintln(os.Stderr, "Validating config with following values:") printValues(values) @@ -141,7 +141,7 @@ func (c *ConfigCompiler) ValidateConfig(opts ValidateConfigOpts) error { opts.ConfigPath, orgID, nil, - LocalPipelineValues(), + values, ) if err != nil { return err diff --git a/config/config.go b/config/config.go index 9d52be9a1..79048f878 100644 --- a/config/config.go +++ b/config/config.go @@ -5,9 +5,10 @@ import ( "io" "os" + "github.com/pkg/errors" + "github.com/CircleCI-Public/circleci-cli/api/collaborators" "github.com/CircleCI-Public/circleci-cli/settings" - "github.com/pkg/errors" ) var ( @@ -71,7 +72,9 @@ type CompileConfigRequest struct { } type Options struct { - OwnerID string `json:"owner_id,omitempty"` + OwnerID string `json:"owner_id,omitempty"` + // PipelineParameters are deprecated and will be removed in the future. + // Use PipelineValues instead. PipelineParameters map[string]interface{} `json:"pipeline_parameters,omitempty"` PipelineValues map[string]interface{} `json:"pipeline_values,omitempty"` } diff --git a/config/pipeline.go b/config/pipeline.go index 923630c4e..e6b09b659 100644 --- a/config/pipeline.go +++ b/config/pipeline.go @@ -12,9 +12,9 @@ type Values map[string]interface{} // Static typing is bypassed using an empty interface here due to pipeline parameters supporting multiple types. type Parameters map[string]interface{} -// vars should contain any pipeline parameters that should be accessible via -// << pipeline.parameters.foo >> -func LocalPipelineValues() Values { +// LocalPipelineValues returns a map of pipeline values that can be used for local validation. +// The given parameters will be prefixed with "pipeline.parameters." and accessible via << pipeline.parameters.foo >>. +func LocalPipelineValues(parameters Parameters) Values { revision := git.Revision() gitUrl := "https://github.com/CircleCI-Public/circleci-cli" projectType := "github" @@ -32,14 +32,18 @@ func LocalPipelineValues() Values { } vals := map[string]interface{}{ - "id": "00000000-0000-0000-0000-000000000001", - "number": 1, - "project.git_url": gitUrl, - "project.type": projectType, - "git.tag": git.Tag(), - "git.branch": git.Branch(), - "git.revision": revision, - "git.base_revision": revision, + "pipeline.id": "00000000-0000-0000-0000-000000000001", + "pipeline.number": 1, + "pipeline.project.git_url": gitUrl, + "pipeline.project.type": projectType, + "pipeline.git.tag": git.Tag(), + "pipeline.git.branch": git.Branch(), + "pipeline.git.revision": revision, + "pipeline.git.base_revision": revision, + } + + for k, v := range parameters { + vals[fmt.Sprintf("pipeline.parameters.%s", k)] = v } return vals diff --git a/config/pipeline_test.go b/config/pipeline_test.go new file mode 100644 index 000000000..6e2cd815e --- /dev/null +++ b/config/pipeline_test.go @@ -0,0 +1,52 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "golang.org/x/exp/maps" +) + +func TestLocalPipelineValues(t *testing.T) { + tests := []struct { + name string + parameters Parameters + wantKeys []string + }{ + { + name: "standard values given nil parameters", + parameters: nil, + wantKeys: []string{ + "pipeline.id", + "pipeline.number", + "pipeline.project.git_url", + "pipeline.project.type", + "pipeline.git.tag", + "pipeline.git.branch", + "pipeline.git.revision", + "pipeline.git.base_revision", + }, + }, + { + name: "standard and prefixed parameters given map of parameters", + parameters: Parameters{"foo": "bar", "baz": "buzz"}, + wantKeys: []string{ + "pipeline.id", + "pipeline.number", + "pipeline.project.git_url", + "pipeline.project.type", + "pipeline.git.tag", + "pipeline.git.branch", + "pipeline.git.revision", + "pipeline.git.base_revision", + "pipeline.parameters.foo", + "pipeline.parameters.baz", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.ElementsMatchf(t, tt.wantKeys, maps.Keys(LocalPipelineValues(tt.parameters)), "LocalPipelineValues(%v)", tt.parameters) + }) + } +} diff --git a/go.mod b/go.mod index 247b6d05b..77c771aa3 100644 --- a/go.mod +++ b/go.mod @@ -120,7 +120,7 @@ require ( golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.16.0 // indirect - google.golang.org/protobuf v1.31.0 // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index dfe67a8be..34ad36ec8 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,6 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ= github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo= -github.com/CircleCI-Public/circle-policy-agent v0.0.702 h1:dEPPZs9j2pgVV1bZEnvthHprJTn8HB2KyMxCdeD6Z3g= -github.com/CircleCI-Public/circle-policy-agent v0.0.702/go.mod h1:72U4Q4OtvAGRGGo/GqlCCO0tARg1cSG9xwxWyz3ktQI= github.com/CircleCI-Public/circle-policy-agent v0.0.727 h1:DS5QkuffkAO/EwNIN2mgQC4an73wfo/VR7ZurAA2qkc= github.com/CircleCI-Public/circle-policy-agent v0.0.727/go.mod h1:72U4Q4OtvAGRGGo/GqlCCO0tARg1cSG9xwxWyz3ktQI= github.com/CircleCI-Public/circleci-config v0.0.0-20231003143420-842d4b0025ef h1:Bhr3xH8/XV0CF8wHFxWgBkmDRTQIW5O1MiuL3n1AEug= @@ -105,8 +103,6 @@ github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+ github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= -github.com/go-git/go-git/v5 v5.10.1 h1:tu8/D8i+TWxgKpzQ3Vc43e+kkhXqtsZCKI/egajKnxk= -github.com/go-git/go-git/v5 v5.10.1/go.mod h1:uEuHjxkHap8kAl//V5F/nNWwqIYtP/402ddd05mp0wg= github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= @@ -454,8 +450,8 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/local/local.go b/local/local.go index 041357d8e..87d103d41 100644 --- a/local/local.go +++ b/local/local.go @@ -10,10 +10,11 @@ import ( "strings" "syscall" - "github.com/CircleCI-Public/circleci-cli/config" - "github.com/CircleCI-Public/circleci-cli/settings" "github.com/pkg/errors" "github.com/spf13/pflag" + + "github.com/CircleCI-Public/circleci-cli/config" + "github.com/CircleCI-Public/circleci-cli/settings" ) var picardRepo = "circleci/picard" @@ -46,13 +47,13 @@ func Execute(flags *pflag.FlagSet, cfg *settings.Config, args []string) error { //if no orgId provided use org slug orgID, _ := flags.GetString("org-id") if strings.TrimSpace(orgID) != "" { - configResponse, err = compiler.ConfigQuery(configPath, orgID, nil, config.LocalPipelineValues()) + configResponse, err = compiler.ConfigQuery(configPath, orgID, nil, config.LocalPipelineValues(nil)) if err != nil { return err } } else { orgSlug, _ := flags.GetString("org-slug") - configResponse, err = compiler.ConfigQuery(configPath, orgSlug, nil, config.LocalPipelineValues()) + configResponse, err = compiler.ConfigQuery(configPath, orgSlug, nil, config.LocalPipelineValues(nil)) if err != nil { return err }