Skip to content

Commit

Permalink
feat: Command to set pipeline context (#534)
Browse files Browse the repository at this point in the history
* add plural cd pipelines context command

* fetch pipeline context

* merge input context

* update logic

* update unit test

* update mocks
  • Loading branch information
maciaszczykm authored Aug 28, 2024
1 parent 617bfd8 commit 5383ab7
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 3 deletions.
49 changes: 49 additions & 0 deletions cmd/command/cd/cd_pipelines.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
},
},
},
}
}

Expand Down Expand Up @@ -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
}
8 changes: 6 additions & 2 deletions cmd/command/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion hack/gen-client-mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions pkg/console/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions pkg/test/mocks/ConsoleClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5383ab7

Please sign in to comment.