Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #80 from lighttiger2505/add-merge-request-options
Browse files Browse the repository at this point in the history
Add merge request options(remove-source-branch, squash)
  • Loading branch information
lighttiger2505 authored Aug 15, 2019
2 parents 97e7e24 + 9cc304a commit 781b5e3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME := lab
VERSION := v0.6.2
VERSION := v0.6.3
REVISION := $(shell git rev-parse --short HEAD)
GOVERSION := $(go version)

Expand Down
6 changes: 6 additions & 0 deletions commands/mr/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
63 changes: 53 additions & 10 deletions commands/mr/flag.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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:"<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. "`
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 {
Expand All @@ -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
Expand All @@ -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\""`
Expand Down
4 changes: 4 additions & 0 deletions commands/mr/mr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
9 changes: 9 additions & 0 deletions commands/mr/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 781b5e3

Please sign in to comment.