Skip to content

Commit

Permalink
cmd/mr_checkout: simplify MR lookup
Browse files Browse the repository at this point in the history
Instead of retrieving a list of MRs for a single ID (size == 1), get the
specific MR.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Feb 7, 2022
1 parent 1ce393c commit c944c2e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
26 changes: 11 additions & 15 deletions cmd/mr_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
Expand Down Expand Up @@ -40,24 +39,18 @@ var checkoutCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
var targetRemote = defaultRemote

targetRemote := defaultRemote
if len(args) == 2 {
// parseArgs above already validated this is a remote
targetRemote = args[0]
}

mrs, err := lab.MRList(rn, gitlab.ListProjectMergeRequestsOptions{
IIDs: []int{int(mrID)},
}, 1)
mr, err := lab.MRGet(rn, int(mrID))
if err != nil {
log.Fatal(err)
}
if len(mrs) < 1 {
fmt.Printf("MR !%d not found\n", mrID)
return
}

mr := mrs[0]
// If the config does not specify a branch, use the mr source branch name
if mrCheckoutCfg.branch == "" {
mrCheckoutCfg.branch = mr.SourceBranch
Expand All @@ -66,7 +59,8 @@ var checkoutCmd = &cobra.Command{
err = git.New("show-ref", "--verify", "--quiet", "refs/heads/"+mrCheckoutCfg.branch).Run()
if err == nil {
if mrCheckoutCfg.force {
if err := git.New("branch", "-D", mrCheckoutCfg.branch).Run(); err != nil {
err = git.New("branch", "-D", mrCheckoutCfg.branch).Run()
if err != nil {
log.Fatal(err)
}
} else {
Expand Down Expand Up @@ -131,20 +125,22 @@ var checkoutCmd = &cobra.Command{
// https://docs.gitlab.com/ee/user/project/merge_requests/reviews/#checkout-merge-requests-locally-through-the-head-ref
mrRef := fmt.Sprintf("refs/merge-requests/%d/head", mrID)
fetchRefSpec := fmt.Sprintf("%s:%s", mrRef, fetchToRef)
if err := git.New("fetch", targetRemote, fetchRefSpec).Run(); err != nil {
err = git.New("fetch", targetRemote, fetchRefSpec).Run()
if err != nil {
log.Fatal(err)
}

if mrCheckoutCfg.track {
// Create configured branch with tracking from fetchToRef
// git branch --flags <branchname> [<start-point>]
if err := git.New("branch", "--track", mrCheckoutCfg.branch, fetchToRef).Run(); err != nil {
err = git.New("branch", "--track", mrCheckoutCfg.branch, fetchToRef).Run()
if err != nil {
log.Fatal(err)
}
}

// Check out branch
if err := git.New("checkout", mrCheckoutCfg.branch).Run(); err != nil {
err = git.New("checkout", mrCheckoutCfg.branch).Run()
if err != nil {
log.Fatal(err)
}
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/mr_checkout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Test_mrCheckoutCmd_track(t *testing.T) {
cmd.Dir = repo
cmd.CombinedOutput()

cmd = exec.Command(labBinaryPath, "mr", "checkout", "1", "-t", "-b", "mrtest_track")
cmd = exec.Command(labBinaryPath, "mr", "checkout", "1", "-f", "-t", "-b", "mrtest_track")
cmd.Dir = repo
b, err := cmd.CombinedOutput()
if err != nil {
Expand Down

0 comments on commit c944c2e

Please sign in to comment.