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

Commit 781b5e3

Browse files
Merge pull request #80 from lighttiger2505/add-merge-request-options
Add merge request options(remove-source-branch, squash)
2 parents 97e7e24 + 9cc304a commit 781b5e3

File tree

5 files changed

+73
-11
lines changed

5 files changed

+73
-11
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
NAME := lab
2-
VERSION := v0.6.2
2+
VERSION := v0.6.3
33
REVISION := $(shell git rev-parse --short HEAD)
44
GOVERSION := $(go version)
55

commands/mr/create.go

+6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ func makeCreateMergeRequestOption(opt *CreateUpdateOption, title, description, b
122122
if opt.MilestoneID != 0 {
123123
createMergeRequestOption.MilestoneID = gitlab.Int(opt.MilestoneID)
124124
}
125+
126+
_, removeSourceBranchFlag := opt.RemoveSourceBranchFlag()
127+
createMergeRequestOption.RemoveSourceBranch = gitlab.Bool(removeSourceBranchFlag)
128+
_, squashFlag := opt.SquashFlag()
129+
createMergeRequestOption.Squash = gitlab.Bool(squashFlag)
130+
125131
return createMergeRequestOption
126132
}
127133

commands/mr/flag.go

+53-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package mr
22

33
import (
4+
"fmt"
5+
46
flags "github.com/jessevdk/go-flags"
57
"github.com/lighttiger2505/lab/commands/internal"
68
"github.com/lighttiger2505/lab/internal/config"
@@ -15,15 +17,17 @@ type Option struct {
1517
}
1618

1719
type CreateUpdateOption struct {
18-
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."`
19-
Title string `short:"i" long:"title" value-name:"<title>" description:"The title of an merge request"`
20-
Message string `short:"m" long:"message" value-name:"<message>" description:"The message of an merge request"`
21-
Template string `short:"p" long:"template" value-name:"<merge request template>" description:"Start the editor with file using merge request template"`
22-
SourceBranch string `long:"source" value-name:"<source branch>" description:"The source branch"`
23-
TargetBranch string `long:"target" value-name:"<target branch>" default:"master" default-mask:"master" description:"The target branch"`
24-
StateEvent string `long:"state-event" value-name:"<state>" description:"Change the status. \"opened\", \"closed\""`
25-
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"`
26-
MilestoneID int `long:"cu-milestone-id" value-name:"<milestone id>" description:"The global ID of a milestone to assign the merge request to. "`
20+
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."`
21+
Title string `short:"i" long:"title" value-name:"<title>" description:"The title of an merge request"`
22+
Message string `short:"m" long:"message" value-name:"<message>" description:"The message of an merge request"`
23+
Template string `short:"p" long:"template" value-name:"<merge request template>" description:"Start the editor with file using merge request template"`
24+
SourceBranch string `long:"source" value-name:"<source branch>" description:"The source branch"`
25+
TargetBranch string `long:"target" value-name:"<target branch>" default:"master" default-mask:"master" description:"The target branch"`
26+
StateEvent string `long:"state-event" value-name:"<state>" description:"Change the status. \"opened\", \"closed\""`
27+
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"`
28+
MilestoneID int `long:"cu-milestone-id" value-name:"<milestone id>" description:"The global ID of a milestone to assign the merge request to. "`
29+
RemoveSourceBranch string `long:"remove-source-branch" value-name:"<true/false>" description:"Merge request should remove the source branch when merging"`
30+
Squash string `long:"squash" value-name:"<true/false>" description:"Squash commits into a single commit when merging"`
2731
}
2832

2933
func (o *CreateUpdateOption) hasEdit() bool {
@@ -47,7 +51,9 @@ func (o *CreateUpdateOption) hasUpdate() bool {
4751
o.Message != "" ||
4852
o.StateEvent != "" ||
4953
o.AssigneeID != 0 ||
50-
o.MilestoneID != 0 {
54+
o.MilestoneID != 0 ||
55+
o.RemoveSourceBranch != "" ||
56+
o.Squash != "" {
5157
return true
5258
}
5359
return false
@@ -63,6 +69,43 @@ func (o *CreateUpdateOption) getAssigneeID(profile *config.Profile) int {
6369
return 0
6470
}
6571

72+
func (o *CreateUpdateOption) isValid() error {
73+
if o.Squash != "" {
74+
if o.Squash != "true" && o.Squash != "false" {
75+
return fmt.Errorf("Invalid option value, %v", o.Squash)
76+
}
77+
}
78+
79+
if o.RemoveSourceBranch != "" {
80+
if o.RemoveSourceBranch != "true" && o.RemoveSourceBranch != "false" {
81+
return fmt.Errorf("Invalid option value, %v", o.RemoveSourceBranch)
82+
}
83+
}
84+
return nil
85+
}
86+
87+
func (o *CreateUpdateOption) RemoveSourceBranchFlag() (bool, bool) {
88+
switch o.RemoveSourceBranch {
89+
case "true":
90+
return true, true
91+
case "false":
92+
return true, false
93+
default:
94+
return false, false
95+
}
96+
}
97+
98+
func (o *CreateUpdateOption) SquashFlag() (bool, bool) {
99+
switch o.Squash {
100+
case "true":
101+
return true, true
102+
case "false":
103+
return true, false
104+
default:
105+
return false, false
106+
}
107+
}
108+
66109
type ListOption struct {
67110
Num int `short:"n" long:"num" value-name:"<num>" default:"20" default-mask:"20" description:"Limit the number of merge request to output."`
68111
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\""`

commands/mr/mr.go

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func (c *MergeRequestCommand) getMethod(opt Option, args []string, pInfo *gituti
9797
return nil, err
9898
}
9999

100+
if err := createUpdateOption.isValid(); err != nil {
101+
return nil, err
102+
}
103+
100104
if browseOption.HasBrowse() {
101105
return &internal.BrowseMethod{
102106
Opener: &browse.Browser{},

commands/mr/update.go

+9
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,14 @@ func makeUpdateMergeRequestOption(opt *CreateUpdateOption, title, description st
100100
if opt.MilestoneID != 0 {
101101
updateMergeRequestOptions.MilestoneID = gitlab.Int(opt.MilestoneID)
102102
}
103+
ok, removeSourceBranchFlag := opt.RemoveSourceBranchFlag()
104+
if ok {
105+
updateMergeRequestOptions.RemoveSourceBranch = gitlab.Bool(removeSourceBranchFlag)
106+
}
107+
ok, squashFlag := opt.SquashFlag()
108+
if ok {
109+
updateMergeRequestOptions.Squash = gitlab.Bool(squashFlag)
110+
}
111+
103112
return updateMergeRequestOptions
104113
}

0 commit comments

Comments
 (0)