-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update planetscale-go dependency. * Fix up promote arg. * Add Demote command. * Update mocks. * Add demotion tests. * Handle when a demotion request is returned.
- Loading branch information
Showing
9 changed files
with
292 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package branch | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/planetscale/cli/internal/cmdutil" | ||
"github.com/planetscale/cli/internal/printer" | ||
"github.com/spf13/cobra" | ||
|
||
ps "github.com/planetscale/planetscale-go/planetscale" | ||
) | ||
|
||
func DemoteCmd(ch *cmdutil.Helper) *cobra.Command { | ||
demoteReq := &ps.DemoteRequest{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "demote <database> <branch> [options]", | ||
Short: "Demote a production branch to development", | ||
Args: cmdutil.RequiredArgs("database", "branch"), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
database := args[0] | ||
branch := args[1] | ||
|
||
demoteReq.Database = database | ||
demoteReq.Branch = branch | ||
demoteReq.Organization = ch.Config.Organization | ||
|
||
client, err := ch.Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
end := ch.Printer.PrintProgress(fmt.Sprintf("Demoting %s branch in %s to development...", printer.BoldBlue(branch), printer.BoldBlue(database))) | ||
defer end() | ||
|
||
demotionRequest, err := client.DatabaseBranches.Demote(ctx, demoteReq) | ||
if err != nil { | ||
switch cmdutil.ErrCode(err) { | ||
case ps.ErrNotFound: | ||
return fmt.Errorf("branch %s does not exist in database %s", | ||
printer.BoldBlue(branch), printer.BoldBlue(database)) | ||
default: | ||
return cmdutil.HandleError(err) | ||
} | ||
} | ||
|
||
end() | ||
|
||
if demotionRequest == nil { | ||
if ch.Printer.Format() == printer.Human { | ||
ch.Printer.Printf("%s branch was successfully demoted to development.\n", printer.BoldBlue(branch)) | ||
return nil | ||
} else { | ||
dbBranch, err := client.DatabaseBranches.Get(cmd.Context(), &ps.GetDatabaseBranchRequest{ | ||
Organization: ch.Config.Organization, | ||
Database: database, | ||
Branch: branch, | ||
}) | ||
if err != nil { | ||
return cmdutil.HandleError(err) | ||
} | ||
return ch.Printer.PrintResource(ToDatabaseBranch(dbBranch)) | ||
} | ||
} | ||
|
||
if ch.Printer.Format() == printer.Human { | ||
ch.Printer.Printf("Successfully requested to demote %s branch to development, requires admin approval.\n", printer.BoldBlue(branch)) | ||
return nil | ||
} else { | ||
return ch.Printer.PrintResource(ToBranchDemotionRequest(demotionRequest)) | ||
} | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package branch | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
"github.com/planetscale/cli/internal/cmdutil" | ||
"github.com/planetscale/cli/internal/config" | ||
"github.com/planetscale/cli/internal/mock" | ||
"github.com/planetscale/cli/internal/printer" | ||
ps "github.com/planetscale/planetscale-go/planetscale" | ||
) | ||
|
||
func TestBranch_DemoteCmd(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
var buf bytes.Buffer | ||
format := printer.JSON | ||
p := printer.NewPrinter(&format) | ||
p.SetResourceOutput(&buf) | ||
|
||
org := "planetscale" | ||
db := "planetscale" | ||
branch := "development" | ||
|
||
res := &ps.DatabaseBranch{ | ||
Name: branch, | ||
} | ||
|
||
svc := &mock.DatabaseBranchesService{ | ||
DemoteFn: func(ctx context.Context, req *ps.DemoteRequest) (*ps.BranchDemotionRequest, error) { | ||
c.Assert(req.Branch, qt.Equals, branch) | ||
c.Assert(req.Database, qt.Equals, db) | ||
c.Assert(req.Organization, qt.Equals, org) | ||
|
||
return nil, nil | ||
}, | ||
GetFn: func(ctx context.Context, req *ps.GetDatabaseBranchRequest) (*ps.DatabaseBranch, error) { | ||
c.Assert(req.Branch, qt.Equals, branch) | ||
c.Assert(req.Database, qt.Equals, db) | ||
c.Assert(req.Organization, qt.Equals, org) | ||
|
||
return res, nil | ||
}, | ||
} | ||
|
||
ch := &cmdutil.Helper{ | ||
Printer: p, | ||
Config: &config.Config{ | ||
Organization: org, | ||
}, | ||
Client: func() (*ps.Client, error) { | ||
return &ps.Client{ | ||
DatabaseBranches: svc, | ||
}, nil | ||
|
||
}, | ||
} | ||
|
||
cmd := DemoteCmd(ch) | ||
cmd.SetArgs([]string{db, branch}) | ||
err := cmd.Execute() | ||
|
||
c.Assert(err, qt.IsNil) | ||
c.Assert(svc.DemoteFnInvoked, qt.IsTrue) | ||
c.Assert(svc.GetFnInvoked, qt.IsTrue) | ||
c.Assert(buf.String(), qt.JSONEquals, res) | ||
} | ||
|
||
func TestBranch_DemoteCmdWithDemotionRequest(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
var buf bytes.Buffer | ||
format := printer.JSON | ||
p := printer.NewPrinter(&format) | ||
p.SetResourceOutput(&buf) | ||
|
||
org := "planetscale" | ||
db := "planetscale" | ||
branch := "development" | ||
|
||
res := &ps.BranchDemotionRequest{ | ||
ID: "test", | ||
State: "pending", | ||
Actor: &ps.Actor{ | ||
Name: "Test User", | ||
}, | ||
} | ||
|
||
svc := &mock.DatabaseBranchesService{ | ||
DemoteFn: func(ctx context.Context, req *ps.DemoteRequest) (*ps.BranchDemotionRequest, error) { | ||
c.Assert(req.Branch, qt.Equals, branch) | ||
c.Assert(req.Database, qt.Equals, db) | ||
c.Assert(req.Organization, qt.Equals, org) | ||
|
||
return res, nil | ||
}, | ||
} | ||
|
||
ch := &cmdutil.Helper{ | ||
Printer: p, | ||
Config: &config.Config{ | ||
Organization: org, | ||
}, | ||
Client: func() (*ps.Client, error) { | ||
return &ps.Client{ | ||
DatabaseBranches: svc, | ||
}, nil | ||
|
||
}, | ||
} | ||
|
||
cmd := DemoteCmd(ch) | ||
cmd.SetArgs([]string{db, branch}) | ||
err := cmd.Execute() | ||
|
||
c.Assert(err, qt.IsNil) | ||
c.Assert(svc.DemoteFnInvoked, qt.IsTrue) | ||
c.Assert(buf.String(), qt.JSONEquals, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.