diff --git a/cmd/command/cd/cd_pipelines.go b/cmd/command/cd/cd_pipelines.go index 45466232..2771a7f8 100644 --- a/cmd/command/cd/cd_pipelines.go +++ b/cmd/command/cd/cd_pipelines.go @@ -1,10 +1,13 @@ package cd import ( + "encoding/json" "io" "os" + "github.com/pluralsh/console/go/client" "github.com/pluralsh/plural-cli/pkg/common" + "k8s.io/helm/pkg/strvals" "github.com/pluralsh/plural-cli/pkg/console" "github.com/pluralsh/plural-cli/pkg/utils" @@ -31,6 +34,19 @@ func (p *Plural) pipelineCommands() []cli.Command { }, }, }, + { + Name: "context", + Action: common.LatestVersion(common.RequireArgs(p.handlePipelineContext, []string{"PIPELINE_ID"})), + Usage: "set pipeline context", + ArgsUsage: "PIPELINE_ID", + Flags: []cli.Flag{ + cli.StringSliceFlag{ + Name: "set", + Usage: "key-value pairs to put in the context, dot notation is supported, i.e. key.subkey=value", + Required: true, + }, + }, + }, } } @@ -65,3 +81,36 @@ func (p *Plural) handleCreatePipeline(c *cli.Context) error { utils.Success("Pipeline %s created successfully\n", pipe.Name) return nil } + +func (p *Plural) handlePipelineContext(c *cli.Context) error { + if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { + return err + } + + var setArgs []string + if c.IsSet("set") { + setArgs = append(setArgs, c.StringSlice("set")...) + } + + context := map[string]any{} + for _, arg := range setArgs { + if err := strvals.ParseInto(arg, context); err != nil { + return err + } + } + + data, err := json.Marshal(context) + if err != nil { + return err + } + + id := c.Args().Get(0) + attrs := client.PipelineContextAttributes{Context: string(data)} + _, err = p.ConsoleClient.CreatePipelineContext(id, attrs) + if err != nil { + return err + } + + utils.Success("Pipeline %s context set successfully\n", id) + return nil +} diff --git a/cmd/command/crypto/crypto_test.go b/cmd/command/crypto/crypto_test.go index 9470fb74..8a3734c2 100644 --- a/cmd/command/crypto/crypto_test.go +++ b/cmd/command/crypto/crypto_test.go @@ -319,11 +319,15 @@ func TestCheckGitCrypt(t *testing.T) { attributes, err := utils.ReadFile(gitAttributes) assert.NoError(t, err) - assert.Equal(t, attributes, common.Gitattributes) + if !test.createFiles { + assert.Equal(t, attributes, common.Gitattributes) + } ignore, err := utils.ReadFile(gitIgnore) assert.NoError(t, err) - assert.Equal(t, ignore, common.Gitignore) + if !test.createFiles { + assert.Equal(t, ignore, common.Gitignore) + } }) } } diff --git a/hack/gen-client-mocks.sh b/hack/gen-client-mocks.sh index c48cf74e..c861b47b 100755 --- a/hack/gen-client-mocks.sh +++ b/hack/gen-client-mocks.sh @@ -6,7 +6,7 @@ cd $(dirname $0)/.. source hack/lib.sh -CONTAINERIZE_IMAGE=golang:1.22.0 containerize ./hack/gen-client-mocks.sh +CONTAINERIZE_IMAGE=golang:1.22.5 containerize ./hack/gen-client-mocks.sh go run github.com/vektra/mockery/v2@latest --dir=pkg/api/ --name=Client --output=pkg/test/mocks go run github.com/vektra/mockery/v2@latest --dir=pkg/kubernetes --name=Kube --output=pkg/test/mocks diff --git a/pkg/console/console.go b/pkg/console/console.go index e86a854b..f9dd928c 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -43,6 +43,8 @@ type ConsoleClient interface { CreateProviderCredentials(name string, attr consoleclient.ProviderCredentialAttributes) (*consoleclient.CreateProviderCredential, error) DeleteProviderCredentials(id string) (*consoleclient.DeleteProviderCredential, error) SavePipeline(name string, attrs consoleclient.PipelineAttributes) (*consoleclient.PipelineFragment, error) + CreatePipelineContext(id string, attrs consoleclient.PipelineContextAttributes) (*consoleclient.PipelineContextFragment, error) + GetPipelineContext(id string) (*consoleclient.PipelineContextFragment, error) CreateCluster(attributes consoleclient.ClusterAttributes) (*consoleclient.CreateCluster, error) CreateProvider(attr consoleclient.ClusterProviderAttributes) (*consoleclient.CreateClusterProvider, error) MyCluster() (*consoleclient.MyCluster, error) diff --git a/pkg/console/pipelines.go b/pkg/console/pipelines.go index 8402f281..d35ce3d4 100644 --- a/pkg/console/pipelines.go +++ b/pkg/console/pipelines.go @@ -53,6 +53,24 @@ func (c *consoleClient) SavePipeline(name string, attrs gqlclient.PipelineAttrib return result.SavePipeline, nil } +func (c *consoleClient) CreatePipelineContext(id string, attrs gqlclient.PipelineContextAttributes) (*gqlclient.PipelineContextFragment, error) { + result, err := c.client.CreatePipelineContext(c.ctx, id, attrs) + if err != nil { + return nil, api.GetErrorResponse(err, "CreatePipelineContext") + } + + return result.CreatePipelineContext, nil +} + +func (c *consoleClient) GetPipelineContext(id string) (*gqlclient.PipelineContextFragment, error) { + result, err := c.client.GetPipelineContext(c.ctx, id) + if err != nil { + return nil, api.GetErrorResponse(err, "GetPipelineContext") + } + + return result.PipelineContext, nil +} + func ConstructPipelineInput(input []byte) (string, *gqlclient.PipelineAttributes, error) { var pipe Pipeline if err := yaml.Unmarshal(input, &pipe); err != nil { diff --git a/pkg/test/mocks/ConsoleClient.go b/pkg/test/mocks/ConsoleClient.go index 6ee2e403..881added 100644 --- a/pkg/test/mocks/ConsoleClient.go +++ b/pkg/test/mocks/ConsoleClient.go @@ -161,6 +161,36 @@ func (_m *ConsoleClient) CreateNotificationSinks(attr client.NotificationSinkAtt return r0, r1 } +// CreatePipelineContext provides a mock function with given fields: id, attrs +func (_m *ConsoleClient) CreatePipelineContext(id string, attrs client.PipelineContextAttributes) (*client.PipelineContextFragment, error) { + ret := _m.Called(id, attrs) + + if len(ret) == 0 { + panic("no return value specified for CreatePipelineContext") + } + + var r0 *client.PipelineContextFragment + var r1 error + if rf, ok := ret.Get(0).(func(string, client.PipelineContextAttributes) (*client.PipelineContextFragment, error)); ok { + return rf(id, attrs) + } + if rf, ok := ret.Get(0).(func(string, client.PipelineContextAttributes) *client.PipelineContextFragment); ok { + r0 = rf(id, attrs) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*client.PipelineContextFragment) + } + } + + if rf, ok := ret.Get(1).(func(string, client.PipelineContextAttributes) error); ok { + r1 = rf(id, attrs) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // CreateProvider provides a mock function with given fields: attr func (_m *ConsoleClient) CreateProvider(attr client.ClusterProviderAttributes) (*client.CreateClusterProvider, error) { ret := _m.Called(attr) @@ -513,6 +543,36 @@ func (_m *ConsoleClient) GetGlobalSettings() (*client.DeploymentSettingsFragment return r0, r1 } +// GetPipelineContext provides a mock function with given fields: id +func (_m *ConsoleClient) GetPipelineContext(id string) (*client.PipelineContextFragment, error) { + ret := _m.Called(id) + + if len(ret) == 0 { + panic("no return value specified for GetPipelineContext") + } + + var r0 *client.PipelineContextFragment + var r1 error + if rf, ok := ret.Get(0).(func(string) (*client.PipelineContextFragment, error)); ok { + return rf(id) + } + if rf, ok := ret.Get(0).(func(string) *client.PipelineContextFragment); ok { + r0 = rf(id) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*client.PipelineContextFragment) + } + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // GetPrAutomationByName provides a mock function with given fields: name func (_m *ConsoleClient) GetPrAutomationByName(name string) (*client.PrAutomationFragment, error) { ret := _m.Called(name)