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

create PR automation #528

Merged
merged 1 commit into from
Aug 20, 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/command/plural/plural.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func CreateNewApp(plural *Plural) *cli.App {
output.Command(),
ops.Command(plural.Plural),
profile.Command(),
pr.Command(),
pr.Command(plural.Plural),
proxy.Command(plural.Plural),
push.Command(plural.Plural),
repo.Command(plural.Plural),
Expand Down
82 changes: 79 additions & 3 deletions cmd/command/pr/pr.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
package pr

import (
"io"
"os"

"github.com/pluralsh/plural-cli/pkg/client"
"github.com/pluralsh/plural-cli/pkg/common"
"github.com/pluralsh/plural-cli/pkg/pr"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/samber/lo"
"github.com/urfave/cli"
)

func Command() cli.Command {
func init() {
consoleToken = ""
consoleURL = ""
}

var consoleToken string
var consoleURL string

type Plural struct {
client.Plural
}

func Command(clients client.Plural) cli.Command {
p := Plural{
Plural: clients,
}
return cli.Command{
Name: "pull-requests",
Aliases: []string{"pr"},
Usage: "Generate and manage pull requests",
Subcommands: prCommands(),
Subcommands: p.prCommands(),
Category: "CD",
Flags: []cli.Flag{
cli.StringFlag{
Name: "token",
Usage: "console token",
EnvVar: "PLURAL_CONSOLE_TOKEN",
Destination: &consoleToken,
},
cli.StringFlag{
Name: "url",
Usage: "console url address",
EnvVar: "PLURAL_CONSOLE_URL",
Destination: &consoleURL,
},
},
}
}

func prCommands() []cli.Command {
func (p *Plural) prCommands() []cli.Command {
return []cli.Command{
{
Name: "template",
Expand All @@ -34,6 +70,16 @@ func prCommands() []cli.Command {
},
},
},
{
Name: "create",
Action: common.LatestVersion(common.RequireArgs(p.handleCreatePrAutomation, []string{"ID"})),
Usage: "create PR automation",
ArgsUsage: "ID",
Flags: []cli.Flag{
cli.StringFlag{Name: "context", Usage: "JSON blob string"},
cli.StringFlag{Name: "branch", Usage: "branch name"},
},
},
}
}

Expand All @@ -49,3 +95,33 @@ func handlePrTemplate(c *cli.Context) error {

return pr.Apply(template)
}

func (p *Plural) handleCreatePrAutomation(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
var branch, context *string

id := c.Args().Get(0)
if c := c.String("context"); c != "" {
if c == "-" {
bytes, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
context = lo.ToPtr(string(bytes))
}
}

if b := c.String("branch"); b != "" {
branch = &b
}

pr, err := p.ConsoleClient.CreatePullRequest(id, branch, context)
if err != nil {
return err
}

utils.Success("PR %s created successfully\n", pr.ID)
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ require (
github.com/packethost/packngo v0.29.0
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pluralsh/cluster-api-migration v0.2.16
github.com/pluralsh/console/go/client v1.4.0
github.com/pluralsh/console/go/client v1.5.0
github.com/pluralsh/gqlclient v1.12.1
github.com/pluralsh/plural-operator v0.5.5
github.com/pluralsh/polly v0.1.8
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1884,8 +1884,8 @@ github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo=
github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Qk=
github.com/pluralsh/cluster-api-migration v0.2.16 h1:MQGrLQAhGSSpyjEDamhnJZaQ8MkxlHzR8PZxIVavLIM=
github.com/pluralsh/cluster-api-migration v0.2.16/go.mod h1:24PjMsYv3vSlUiYw7BeUQ0GAtK0Jk2B1iwh35WGQLx8=
github.com/pluralsh/console/go/client v1.4.0 h1:Mr/gyU20jrLZZ9diEznv3aU/CJ5wk13qLes/4O9unwM=
github.com/pluralsh/console/go/client v1.4.0/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
github.com/pluralsh/console/go/client v1.5.0 h1:O5pHJUXqBJWUt66MiCRlyatarNFQGy1Fy6QaC5P0avY=
github.com/pluralsh/console/go/client v1.5.0/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E=
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
github.com/pluralsh/gqlclient v1.12.1 h1:JDOkP9jjqkPdTYdpH5hooG4F8T6FDH90XfipeXJmJFY=
Expand Down
1 change: 1 addition & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type ConsoleClient interface {
UpdateDeploymentSettings(attr consoleclient.DeploymentSettingsAttributes) (*consoleclient.UpdateDeploymentSettings, error)
GetGlobalSettings() (*consoleclient.DeploymentSettingsFragment, error)
ListStackRuns(stackID string) (*consoleclient.ListStackRuns, error)
CreatePullRequest(id string, branch, context *string) (*consoleclient.PullRequestFragment, 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
@@ -0,0 +1,15 @@
package console

import (
consoleclient "github.com/pluralsh/console/go/client"
"github.com/pluralsh/plural-cli/pkg/api"
)

func (c *consoleClient) CreatePullRequest(id string, branch, context *string) (*consoleclient.PullRequestFragment, error) {
result, err := c.client.CreatePullRequest(c.ctx, id, nil, branch, context)
if err != nil {
return nil, api.GetErrorResponse(err, "CreatePullRequest")
}

return result.CreatePullRequest, 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.

Loading
Loading