diff --git a/cmd/command/cd/cd_clusters.go b/cmd/command/cd/cd_clusters.go index b06a738b..cd6dc2bf 100644 --- a/cmd/command/cd/cd_clusters.go +++ b/cmd/command/cd/cd_clusters.go @@ -152,7 +152,7 @@ func (p *Plural) handleDescribeCluster(c *cli.Context) error { if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { return err } - existing, err := p.ConsoleClient.GetCluster(getIdAndName(c.Args().Get(0))) + existing, err := p.ConsoleClient.GetCluster(common.GetIdAndName(c.Args().Get(0))) if err != nil { return err } @@ -177,7 +177,7 @@ func (p *Plural) handleUpdateCluster(c *cli.Context) error { if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { return err } - existing, err := p.ConsoleClient.GetCluster(getIdAndName(c.Args().Get(0))) + existing, err := p.ConsoleClient.GetCluster(common.GetIdAndName(c.Args().Get(0))) if err != nil { return err } @@ -227,7 +227,7 @@ func (p *Plural) handleDeleteCluster(c *cli.Context) error { return err } - existing, err := p.ConsoleClient.GetCluster(getIdAndName(c.Args().Get(0))) + existing, err := p.ConsoleClient.GetCluster(common.GetIdAndName(c.Args().Get(0))) if err != nil { return err } @@ -248,7 +248,7 @@ func (p *Plural) handleGetClusterCredentials(c *cli.Context) error { return err } - cluster, err := p.ConsoleClient.GetCluster(getIdAndName(c.Args().Get(0))) + cluster, err := p.ConsoleClient.GetCluster(common.GetIdAndName(c.Args().Get(0))) if err != nil { return err } @@ -339,16 +339,6 @@ func (p *Plural) handleCreateCluster(c *cli.Context) error { return nil } -func getIdAndName(input string) (id, name *string) { - if strings.HasPrefix(input, "@") { - h := strings.Trim(input, "@") - name = &h - } else { - id = &input - } - return -} - func (p *Plural) handleCreateProvider(existingProviders []string) (*gqlclient.CreateClusterProvider, error) { provider := "" var resp struct { @@ -393,7 +383,7 @@ func (p *Plural) handleClusterReinstall(c *cli.Context) error { return err } - id, name := getIdAndName(c.Args().Get(0)) + id, name := common.GetIdAndName(c.Args().Get(0)) return p.reinstallOperator(c, id, name) } diff --git a/cmd/command/cd/cd_services.go b/cmd/command/cd/cd_services.go index d20f2392..ff941db4 100644 --- a/cmd/command/cd/cd_services.go +++ b/cmd/command/cd/cd_services.go @@ -135,7 +135,7 @@ func (p *Plural) handleListClusterServices(c *cli.Context) error { if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { return err } - sd, err := p.ConsoleClient.ListClusterServices(getIdAndName(c.Args().Get(0))) + sd, err := p.ConsoleClient.ListClusterServices(common.GetIdAndName(c.Args().Get(0))) if err != nil { return err } @@ -219,7 +219,7 @@ func (p *Plural) handleCreateClusterService(c *cli.Context) error { } } - clusterId, clusterName := getIdAndName(c.Args().Get(0)) + clusterId, clusterName := common.GetIdAndName(c.Args().Get(0)) sd, err := p.ConsoleClient.CreateClusterService(clusterId, clusterName, attributes) if err != nil { return err @@ -283,7 +283,7 @@ func (p *Plural) handleCloneClusterService(c *cli.Context) error { return err } - cluster, err := p.ConsoleClient.GetCluster(getIdAndName(c.Args().Get(0))) + cluster, err := p.ConsoleClient.GetCluster(common.GetIdAndName(c.Args().Get(0))) if err != nil { return err } diff --git a/cmd/command/pr/pr.go b/cmd/command/pr/pr.go index 23244348..e8589930 100644 --- a/cmd/command/pr/pr.go +++ b/cmd/command/pr/pr.go @@ -101,8 +101,19 @@ func (p *Plural) handleCreatePrAutomation(c *cli.Context) error { return err } var branch, context *string + var prID string + id, name := common.GetIdAndName(c.Args().Get(0)) + if id != nil { + prID = *id + } + if name != nil { + pr, err := p.ConsoleClient.GetPrAutomationByName(*name) + if err != nil { + return err + } + prID = pr.ID + } - id := c.Args().Get(0) if c := c.String("context"); c != "" { if c == "-" { bytes, err := io.ReadAll(os.Stdin) @@ -117,7 +128,7 @@ func (p *Plural) handleCreatePrAutomation(c *cli.Context) error { branch = &b } - pr, err := p.ConsoleClient.CreatePullRequest(id, branch, context) + pr, err := p.ConsoleClient.CreatePullRequest(prID, branch, context) if err != nil { return err } diff --git a/pkg/common/common.go b/pkg/common/common.go index bc5f8ad1..81be1224 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "time" "github.com/pkg/browser" @@ -230,3 +231,13 @@ func HandleImport(c *cli.Context) error { func HandleServe(c *cli.Context) error { return server.Run() } + +func GetIdAndName(input string) (id, name *string) { + if strings.HasPrefix(input, "@") { + h := strings.Trim(input, "@") + name = &h + } else { + id = &input + } + return +} diff --git a/pkg/console/console.go b/pkg/console/console.go index af397da3..bc70f3a6 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -55,6 +55,7 @@ type ConsoleClient interface { GetGlobalSettings() (*consoleclient.DeploymentSettingsFragment, error) ListStackRuns(stackID string) (*consoleclient.ListStackRuns, error) CreatePullRequest(id string, branch, context *string) (*consoleclient.PullRequestFragment, error) + GetPrAutomationByName(name string) (*consoleclient.PrAutomationFragment, error) } type authedTransport struct { diff --git a/pkg/console/pr.go b/pkg/console/pr.go index 4015114d..2e2ce218 100644 --- a/pkg/console/pr.go +++ b/pkg/console/pr.go @@ -1,6 +1,8 @@ package console import ( + "fmt" + consoleclient "github.com/pluralsh/console/go/client" "github.com/pluralsh/plural-cli/pkg/api" ) @@ -13,3 +15,16 @@ func (c *consoleClient) CreatePullRequest(id string, branch, context *string) (* return result.CreatePullRequest, nil } + +func (c *consoleClient) GetPrAutomationByName(name string) (*consoleclient.PrAutomationFragment, error) { + result, err := c.client.GetPrAutomationByName(c.ctx, name) + if err != nil { + return nil, api.GetErrorResponse(err, "GetPrAutomationByName") + } + + if result.PrAutomation == nil { + return nil, fmt.Errorf("pr automation %s not found", name) + } + + return result.PrAutomation, nil +} diff --git a/pkg/test/mocks/Client.go b/pkg/test/mocks/Client.go index 841d0bcd..bbdd84a5 100644 --- a/pkg/test/mocks/Client.go +++ b/pkg/test/mocks/Client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.2. DO NOT EDIT. +// Code generated by mockery v2.45.0. DO NOT EDIT. package mocks diff --git a/pkg/test/mocks/ConsoleClient.go b/pkg/test/mocks/ConsoleClient.go index d32d0a80..6ee2e403 100644 --- a/pkg/test/mocks/ConsoleClient.go +++ b/pkg/test/mocks/ConsoleClient.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.2. DO NOT EDIT. +// Code generated by mockery v2.45.0. DO NOT EDIT. package mocks @@ -513,6 +513,36 @@ func (_m *ConsoleClient) GetGlobalSettings() (*client.DeploymentSettingsFragment 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) + + if len(ret) == 0 { + panic("no return value specified for GetPrAutomationByName") + } + + var r0 *client.PrAutomationFragment + var r1 error + if rf, ok := ret.Get(0).(func(string) (*client.PrAutomationFragment, error)); ok { + return rf(name) + } + if rf, ok := ret.Get(0).(func(string) *client.PrAutomationFragment); ok { + r0 = rf(name) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*client.PrAutomationFragment) + } + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(name) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // GetProject provides a mock function with given fields: name func (_m *ConsoleClient) GetProject(name string) (*client.ProjectFragment, error) { ret := _m.Called(name) diff --git a/pkg/test/mocks/Kube.go b/pkg/test/mocks/Kube.go index 461e3dde..d1a9dc6c 100644 --- a/pkg/test/mocks/Kube.go +++ b/pkg/test/mocks/Kube.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.44.2. DO NOT EDIT. +// Code generated by mockery v2.45.0. DO NOT EDIT. package mocks