Skip to content

Commit

Permalink
Merge pull request #654 from bmeneguele/feature-mr_merge-pipeline-suc…
Browse files Browse the repository at this point in the history
…ceeds

mr_merge: add option for merging immediately
  • Loading branch information
bmeneg authored Apr 6, 2021
2 parents 7011100 + bc51d1b commit f4aed0c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 15 additions & 4 deletions cmd/mr_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var mergeImmediate bool

var mrMergeCmd = &cobra.Command{
Use: "merge [remote] <id>",
Short: "Merge an open merge request",
Long: `If the pipeline for the mr is still running, lab sets merge on success`,
Use: "merge [remote] <id>",
Short: "Merge an open merge request",
Long: `Merges an open merge request. If the pipeline in the project is
enabled and is still running for that specific MR, by default,
this command will sets the merge to only happen when the pipeline
succeeds`,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, id, err := parseArgsWithGitBranchMR(args)
Expand All @@ -25,14 +31,19 @@ var mrMergeCmd = &cobra.Command{
log.Fatal(err)
}

err = lab.MRMerge(p.ID, int(id))
opts := gitlab.AcceptMergeRequestOptions{
MergeWhenPipelineSucceeds: gitlab.Bool(!mergeImmediate),
}

err = lab.MRMerge(p.ID, int(id), &opts)
if err != nil {
log.Fatal(err)
}
},
}

func init() {
mrMergeCmd.Flags().BoolVarP(&mergeImmediate, "immediate", "i", false, "merge immediately, regardless pipeline results")
mrCmd.AddCommand(mrMergeCmd)
carapace.Gen(mrMergeCmd).PositionalCompletion(
action.Remotes(),
Expand Down
6 changes: 2 additions & 4 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,8 @@ func MRRebase(pid interface{}, id int) error {
}

// MRMerge merges an mr on a GitLab project
func MRMerge(pid interface{}, id int) error {
_, _, err := lab.MergeRequests.AcceptMergeRequest(pid, int(id), &gitlab.AcceptMergeRequestOptions{
MergeWhenPipelineSucceeds: gitlab.Bool(true),
})
func MRMerge(pid interface{}, id int, opts *gitlab.AcceptMergeRequestOptions) error {
_, _, err := lab.MergeRequests.AcceptMergeRequest(pid, int(id), opts)
if err != nil {
return err
}
Expand Down

0 comments on commit f4aed0c

Please sign in to comment.