diff --git a/Makefile b/Makefile index b618aed..91eeccc 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME := lab -VERSION := v0.6.2 +VERSION := v0.6.3 REVISION := $(shell git rev-parse --short HEAD) GOVERSION := $(go version) diff --git a/commands/mr/create.go b/commands/mr/create.go index 96dcc78..2d8efe9 100644 --- a/commands/mr/create.go +++ b/commands/mr/create.go @@ -122,6 +122,12 @@ func makeCreateMergeRequestOption(opt *CreateUpdateOption, title, description, b if opt.MilestoneID != 0 { createMergeRequestOption.MilestoneID = gitlab.Int(opt.MilestoneID) } + + _, removeSourceBranchFlag := opt.RemoveSourceBranchFlag() + createMergeRequestOption.RemoveSourceBranch = gitlab.Bool(removeSourceBranchFlag) + _, squashFlag := opt.SquashFlag() + createMergeRequestOption.Squash = gitlab.Bool(squashFlag) + return createMergeRequestOption } diff --git a/commands/mr/flag.go b/commands/mr/flag.go index 3ce2900..3f33898 100644 --- a/commands/mr/flag.go +++ b/commands/mr/flag.go @@ -1,6 +1,8 @@ package mr import ( + "fmt" + flags "github.com/jessevdk/go-flags" "github.com/lighttiger2505/lab/commands/internal" "github.com/lighttiger2505/lab/internal/config" @@ -15,15 +17,17 @@ type Option struct { } type CreateUpdateOption struct { - Edit bool `short:"e" long:"edit" description:"Edit the merge request on editor. Start the editor with the contents in the given title and message options."` - Title string `short:"i" long:"title" value-name:"" description:"The title of an merge request"` - Message string `short:"m" long:"message" value-name:"<message>" description:"The message of an merge request"` - Template string `short:"p" long:"template" value-name:"<merge request template>" description:"Start the editor with file using merge request template"` - SourceBranch string `long:"source" value-name:"<source branch>" description:"The source branch"` - TargetBranch string `long:"target" value-name:"<target branch>" default:"master" default-mask:"master" description:"The target branch"` - StateEvent string `long:"state-event" value-name:"<state>" description:"Change the status. \"opened\", \"closed\""` - AssigneeID int `long:"cu-assignee-id" value-name:"<assignee id>" description:"The ID of the user to assign the merge request to. If default_assignee_id is set in config, it is automatically entered"` - MilestoneID int `long:"cu-milestone-id" value-name:"<milestone id>" description:"The global ID of a milestone to assign the merge request to. "` + Edit bool `short:"e" long:"edit" description:"Edit the merge request on editor. Start the editor with the contents in the given title and message options."` + Title string `short:"i" long:"title" value-name:"<title>" description:"The title of an merge request"` + Message string `short:"m" long:"message" value-name:"<message>" description:"The message of an merge request"` + Template string `short:"p" long:"template" value-name:"<merge request template>" description:"Start the editor with file using merge request template"` + SourceBranch string `long:"source" value-name:"<source branch>" description:"The source branch"` + TargetBranch string `long:"target" value-name:"<target branch>" default:"master" default-mask:"master" description:"The target branch"` + StateEvent string `long:"state-event" value-name:"<state>" description:"Change the status. \"opened\", \"closed\""` + AssigneeID int `long:"cu-assignee-id" value-name:"<assignee id>" description:"The ID of the user to assign the merge request to. If default_assignee_id is set in config, it is automatically entered"` + MilestoneID int `long:"cu-milestone-id" value-name:"<milestone id>" description:"The global ID of a milestone to assign the merge request to. "` + RemoveSourceBranch string `long:"remove-source-branch" value-name:"<true/false>" description:"Merge request should remove the source branch when merging"` + Squash string `long:"squash" value-name:"<true/false>" description:"Squash commits into a single commit when merging"` } func (o *CreateUpdateOption) hasEdit() bool { @@ -47,7 +51,9 @@ func (o *CreateUpdateOption) hasUpdate() bool { o.Message != "" || o.StateEvent != "" || o.AssigneeID != 0 || - o.MilestoneID != 0 { + o.MilestoneID != 0 || + o.RemoveSourceBranch != "" || + o.Squash != "" { return true } return false @@ -63,6 +69,43 @@ func (o *CreateUpdateOption) getAssigneeID(profile *config.Profile) int { return 0 } +func (o *CreateUpdateOption) isValid() error { + if o.Squash != "" { + if o.Squash != "true" && o.Squash != "false" { + return fmt.Errorf("Invalid option value, %v", o.Squash) + } + } + + if o.RemoveSourceBranch != "" { + if o.RemoveSourceBranch != "true" && o.RemoveSourceBranch != "false" { + return fmt.Errorf("Invalid option value, %v", o.RemoveSourceBranch) + } + } + return nil +} + +func (o *CreateUpdateOption) RemoveSourceBranchFlag() (bool, bool) { + switch o.RemoveSourceBranch { + case "true": + return true, true + case "false": + return true, false + default: + return false, false + } +} + +func (o *CreateUpdateOption) SquashFlag() (bool, bool) { + switch o.Squash { + case "true": + return true, true + case "false": + return true, false + default: + return false, false + } +} + type ListOption struct { Num int `short:"n" long:"num" value-name:"<num>" default:"20" default-mask:"20" description:"Limit the number of merge request to output."` State string `long:"state" value-name:"<state>" default:"all" default-mask:"all" description:"Print only merge request of the state just those that are \"opened\", \"closed\", \"merged\" or \"all\""` diff --git a/commands/mr/mr.go b/commands/mr/mr.go index d06454e..17285fe 100644 --- a/commands/mr/mr.go +++ b/commands/mr/mr.go @@ -97,6 +97,10 @@ func (c *MergeRequestCommand) getMethod(opt Option, args []string, pInfo *gituti return nil, err } + if err := createUpdateOption.isValid(); err != nil { + return nil, err + } + if browseOption.HasBrowse() { return &internal.BrowseMethod{ Opener: &browse.Browser{}, diff --git a/commands/mr/update.go b/commands/mr/update.go index 09784a2..301a9f0 100644 --- a/commands/mr/update.go +++ b/commands/mr/update.go @@ -100,5 +100,14 @@ func makeUpdateMergeRequestOption(opt *CreateUpdateOption, title, description st if opt.MilestoneID != 0 { updateMergeRequestOptions.MilestoneID = gitlab.Int(opt.MilestoneID) } + ok, removeSourceBranchFlag := opt.RemoveSourceBranchFlag() + if ok { + updateMergeRequestOptions.RemoveSourceBranch = gitlab.Bool(removeSourceBranchFlag) + } + ok, squashFlag := opt.SquashFlag() + if ok { + updateMergeRequestOptions.Squash = gitlab.Bool(squashFlag) + } + return updateMergeRequestOptions }