This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mr): add support to rebase merge request
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |