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

feat(commands/mr/checkout): accept OWNER:branch format for MRs #529

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions commands/mr/checkout/mr_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"

"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/commands/mr/mrutils"
"github.com/profclems/glab/internal/git"
"github.com/profclems/glab/internal/utils"
"github.com/profclems/glab/pkg/api"
"github.com/tcnksm/go-gitconfig"

"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)

type mrCheckoutConfig struct {
Expand All @@ -24,7 +23,7 @@ var (

func NewCmdCheckout(f *cmdutils.Factory) *cobra.Command {
var mrCheckoutCmd = &cobra.Command{
Use: "checkout <id>",
Use: "checkout [<id> | <branch>]",
Short: "Checkout to an open merge request",
Long: ``,
Args: cobra.ExactArgs(1),
Expand All @@ -37,21 +36,11 @@ func NewCmdCheckout(f *cmdutils.Factory) *cobra.Command {
return err
}

repo, err := f.BaseRepo()
mr, repo, err := mrutils.MRFromArgs(f, args)
if err != nil {
return err
}

mrID := utils.StringToInt(args[0])

mr, err := api.GetMR(apiClient, repo.FullName(), mrID, &gitlab.GetMergeRequestsOptions{})

if err != nil {
return err
}
if mr == nil {
return fmt.Errorf("merge Request !%d not found\n", mrID)
}
if mrCheckoutCfg.branch == "" {
mrCheckoutCfg.branch = mr.SourceBranch
}
Expand Down Expand Up @@ -79,7 +68,7 @@ func NewCmdCheckout(f *cmdutils.Factory) *cobra.Command {
fmt.Println(err)
}

mrRef := fmt.Sprintf("refs/merge-requests/%d/head", mrID)
mrRef := fmt.Sprintf("refs/merge-requests/%d/head", mr.IID)
fetchRefSpec := fmt.Sprintf("%s:%s", mrRef, fetchToRef)
if err := git.RunCmd([]string{"fetch", repoRemote.Name, fetchRefSpec}); err != nil {
return err
Expand Down