Skip to content

Commit

Permalink
Create PR by automation name (#535)
Browse files Browse the repository at this point in the history
* Create PR by automation name

* add mocks

* fix linter
  • Loading branch information
zreigz authored Aug 26, 2024
1 parent b0ef777 commit e196810
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 23 deletions.
20 changes: 5 additions & 15 deletions cmd/command/cd/cd_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/command/cd/cd_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 13 additions & 2 deletions cmd/command/pr/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/pkg/browser"
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions pkg/console/pr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package console

import (
"fmt"

consoleclient "github.com/pluralsh/console/go/client"
"github.com/pluralsh/plural-cli/pkg/api"
)
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/test/mocks/Client.go

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

32 changes: 31 additions & 1 deletion pkg/test/mocks/ConsoleClient.go

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

2 changes: 1 addition & 1 deletion pkg/test/mocks/Kube.go

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

0 comments on commit e196810

Please sign in to comment.