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-4359 Update config compilation pipeline values to add parameters #1052

Merged
merged 1 commit into from
Mar 13, 2024
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
2 changes: 1 addition & 1 deletion cmd/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down
6 changes: 3 additions & 3 deletions config/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -141,7 +141,7 @@ func (c *ConfigCompiler) ValidateConfig(opts ValidateConfigOpts) error {
opts.ConfigPath,
orgID,
nil,
LocalPipelineValues(),
values,
)
if err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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"`
}
Expand Down
26 changes: 15 additions & 11 deletions config/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
52 changes: 52 additions & 0 deletions config/pipeline_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
9 changes: 5 additions & 4 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down