simplify slightly, add tests

This commit is contained in:
vilmibm 2021-01-21 15:18:08 -08:00
parent 8624a9397e
commit 6decf4384f
2 changed files with 33 additions and 15 deletions

View file

@ -31,8 +31,7 @@ type MergeOptions struct {
DeleteBranch bool
MergeMethod api.PullRequestMergeMethod
Body string
BodyChanged bool
Body *string
IsDeleteBranchIndicated bool
CanDeleteLocalBranch bool
@ -98,7 +97,10 @@ func NewCmdMerge(f *cmdutil.Factory, runF func(*MergeOptions) error) *cobra.Comm
opts.IsDeleteBranchIndicated = cmd.Flags().Changed("delete-branch")
opts.CanDeleteLocalBranch = !cmd.Flags().Changed("repo")
opts.BodyChanged = cmd.Flags().Changed("body")
if cmd.Flags().Changed("body") {
bodyStr, _ := cmd.Flags().GetString("body")
opts.Body = &bodyStr
}
if runF != nil {
return runF(opts)
@ -108,7 +110,7 @@ func NewCmdMerge(f *cmdutil.Factory, runF func(*MergeOptions) error) *cobra.Comm
}
cmd.Flags().BoolVarP(&opts.DeleteBranch, "delete-branch", "d", false, "Delete the local and remote branch after merge")
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "Body for merge commit")
cmd.Flags().StringP("body", "b", "", "Body for merge commit")
cmd.Flags().BoolVarP(&flagMerge, "merge", "m", false, "Merge the commits with the base branch")
cmd.Flags().BoolVarP(&flagRebase, "rebase", "r", false, "Rebase the commits onto the base branch")
cmd.Flags().BoolVarP(&flagSquash, "squash", "s", false, "Squash the commits into one commit and merge it into the base branch")
@ -153,12 +155,7 @@ func mergeRun(opts *MergeOptions) error {
}
}
var body *string = nil
if opts.BodyChanged {
body = &opts.Body
}
err = api.PullRequestMerge(apiClient, baseRepo, pr, mergeMethod, body)
err = api.PullRequestMerge(apiClient, baseRepo, pr, mergeMethod, opts.Body)
if err != nil {
return err
}

View file

@ -27,11 +27,12 @@ import (
func Test_NewCmdMerge(t *testing.T) {
tests := []struct {
name string
args string
isTTY bool
want MergeOptions
wantErr string
name string
args string
isTTY bool
want MergeOptions
wantBody string
wantErr string
}{
{
name: "number argument",
@ -59,6 +60,20 @@ func Test_NewCmdMerge(t *testing.T) {
InteractiveMode: true,
},
},
{
name: "body",
args: "123 -bcool",
isTTY: true,
want: MergeOptions{
SelectorArg: "123",
DeleteBranch: false,
IsDeleteBranchIndicated: false,
CanDeleteLocalBranch: true,
MergeMethod: api.PullRequestMergeMethodMerge,
InteractiveMode: true,
},
wantBody: "cool",
},
{
name: "no argument with --repo override",
args: "-R owner/repo",
@ -123,6 +138,12 @@ func Test_NewCmdMerge(t *testing.T) {
assert.Equal(t, tt.want.CanDeleteLocalBranch, opts.CanDeleteLocalBranch)
assert.Equal(t, tt.want.MergeMethod, opts.MergeMethod)
assert.Equal(t, tt.want.InteractiveMode, opts.InteractiveMode)
if tt.wantBody == "" {
assert.Nil(t, opts.Body)
} else {
assert.Equal(t, tt.wantBody, *opts.Body)
}
})
}
}