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

Commit

Permalink
feat(mr): add support to rebase merge request
Browse files Browse the repository at this point in the history
  • Loading branch information
profclems committed Aug 15, 2020
1 parent f78518c commit 2c1e19a
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions commands/mr_rebase.go
@@ -0,0 +1,64 @@
package commands

import (
"fmt"
"log"
"strings"

"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
"glab/internal/git"
"glab/internal/manip"
)

var mrRebaseCmd = &cobra.Command{
Use: "rebase <id> [flags]",
Short: `Automatically rebase the source_branch of the merge request against its target_branch.`,
Long: `If you don’t have permissions to push to the merge request’s source branch - you’ll get a 403 Forbidden response.`,
Aliases: []string{"accept"},
Args: cobra.ExactArgs(1),
Run: acceptRebaseRequest,
}

func acceptRebaseRequest(cmd *cobra.Command, args []string) {
mergeID := strings.Trim(args[0], " ")
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
fmt.Println("Sending request...")
_, err := gitlabClient.MergeRequests.RebaseMergeRequest(repo, manip.StringToInt(mergeID))
if err != nil {
er(err)
return
}

opts := &gitlab.GetMergeRequestsOptions{}
opts.IncludeRebaseInProgress = gitlab.Bool(true)
fmt.Println("Checking rebase status...")
i := 0
for {
mr, _, err := gitlabClient.MergeRequests.GetMergeRequest(repo, manip.StringToInt(mergeID), opts)
if err != nil {
log.Fatal(err)
}
if mr.RebaseInProgress {
if i == 0 {
fmt.Println("Rebase in progress...")
}
} else {
if mr.MergeError != "" && mr.MergeError != "null" {
fmt.Println(mr.MergeError)
break
}
fmt.Println("Rebase successful")
break
}
i++
}

}

func init() {
mrCmd.AddCommand(mrRebaseCmd)
}

0 comments on commit 2c1e19a

Please sign in to comment.