Skip to content

Commit

Permalink
Use new branch promotion APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
iheanyi committed Sep 14, 2021
1 parent a91c97f commit 8b99561
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
40 changes: 37 additions & 3 deletions internal/cmd/branch/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package branch

import (
"fmt"
"time"

"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"
Expand All @@ -11,7 +12,7 @@ import (
)

func PromoteCmd(ch *cmdutil.Helper) *cobra.Command {
promoteReq := &ps.PromoteBranchRequest{}
promoteReq := &ps.PromoteRequest{}

cmd := &cobra.Command{
Use: "promote <database> <branch> [options]",
Expand Down Expand Up @@ -67,7 +68,7 @@ func PromoteCmd(ch *cmdutil.Helper) *cobra.Command {

end := ch.Printer.PrintProgress(fmt.Sprintf("Promoting %s branch in %s to production...", printer.BoldBlue(branch), printer.BoldBlue(source)))
defer end()
dbBranch, err := client.DatabaseBranches.Promote(cmd.Context(), promoteReq)
promotionRequest, err := client.DatabaseBranches.Promote(cmd.Context(), promoteReq)
if err != nil {
switch cmdutil.ErrCode(err) {
case ps.ErrNotFound:
Expand All @@ -78,13 +79,46 @@ func PromoteCmd(ch *cmdutil.Helper) *cobra.Command {
}
}

getReq := &ps.GetPromotionRequestRequest{
Organization: ch.Config.Organization,
Database: source,
Branch: branch,
}

for promotionRequest.State == "pending" {
promotionRequest, err = client.DatabaseBranches.GetPromotionRequest(cmd.Context(), getReq)
if err != nil {
switch cmdutil.ErrCode(err) {
case ps.ErrNotFound:
return fmt.Errorf("promotion request for branch %s does not exist in database %s", printer.BoldBlue(branch), printer.BoldBlue(source))
default:
return cmdutil.HandleError(err)
}
}

time.Sleep(1 * time.Second)
}

end()

if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Branch %s in %s was successfully promoted.\n", printer.BoldBlue(dbBranch.Name), printer.BoldBlue(source))
if promotionRequest.State == "lint_error" {
ch.Printer.Printf("Branch promotion failed. Fix the following errors and then retry: \n\n%s\n", printer.BoldRed(promotionRequest.LintErrors))
} else {
ch.Printer.Printf("Branch %s in %s was successfully promoted.\n", printer.BoldBlue(promotionRequest.Branch), printer.BoldBlue(source))
}
return nil
}

dbBranch, err := client.DatabaseBranches.Get(cmd.Context(), &ps.GetDatabaseBranchRequest{
Organization: ch.Config.Organization,
Database: source,
Branch: branch,
})
if err != nil {
return cmdutil.HandleError(err)
}

return ch.Printer.PrintResource(ToDatabaseBranch(dbBranch))
},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/mock/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type DatabaseBranchesService struct {
RefreshSchemaFn func(context.Context, *ps.RefreshSchemaRequest) error
RefreshSchemaFnInvoked bool

PromoteFn func(context.Context, *ps.PromoteBranchRequest) (*ps.DatabaseBranch, error)
PromoteFn func(context.Context, *ps.PromoteRequest) (*ps.DatabaseBranch, error)
PromoteFnInvoked bool
}

Expand Down Expand Up @@ -75,7 +75,7 @@ func (d *DatabaseBranchesService) RefreshSchema(ctx context.Context, req *ps.Ref
return d.RefreshSchemaFn(ctx, req)
}

func (d *DatabaseBranchesService) Promote(ctx context.Context, req *ps.PromoteBranchRequest) (*ps.DatabaseBranch, error) {
func (d *DatabaseBranchesService) Promote(ctx context.Context, req *ps.PromoteRequest) (*ps.DatabaseBranch, error) {
d.PromoteFnInvoked = true
return d.PromoteFn(ctx, req)
}

0 comments on commit 8b99561

Please sign in to comment.