X Tutup
Skip to content

Commit 487dd06

Browse files
committed
Isolate pr checkout command
1 parent af68a74 commit 487dd06

File tree

6 files changed

+195
-168
lines changed

6 files changed

+195
-168
lines changed

api/queries_repo.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ type Repository struct {
2727
IsPrivate bool
2828
HasIssuesEnabled bool
2929
ViewerPermission string
30-
DefaultBranchRef struct {
31-
Name string
32-
}
30+
DefaultBranchRef BranchRef
3331

3432
Parent *Repository
3533

@@ -42,6 +40,11 @@ type RepositoryOwner struct {
4240
Login string
4341
}
4442

43+
// BranchRef is the branch name in a GitHub repository
44+
type BranchRef struct {
45+
Name string
46+
}
47+
4548
// RepoOwner is the login name of the owner
4649
func (r Repository) RepoOwner() string {
4750
return r.Owner.Login

command/pr.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func init() {
2727
prCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format")
2828

2929
RootCmd.AddCommand(prCmd)
30-
prCmd.AddCommand(prCheckoutCmd)
3130
prCmd.AddCommand(prCreateCmd)
3231
prCmd.AddCommand(prStatusCmd)
3332
prCmd.AddCommand(prCloseCmd)

command/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/cli/cli/internal/run"
2323
apiCmd "github.com/cli/cli/pkg/cmd/api"
2424
gistCreateCmd "github.com/cli/cli/pkg/cmd/gist/create"
25+
prCheckoutCmd "github.com/cli/cli/pkg/cmd/pr/checkout"
2526
prDiffCmd "github.com/cli/cli/pkg/cmd/pr/diff"
2627
prReviewCmd "github.com/cli/cli/pkg/cmd/pr/review"
2728
repoCmd "github.com/cli/cli/pkg/cmd/repo"
@@ -172,6 +173,7 @@ func init() {
172173

173174
prCmd.AddCommand(prReviewCmd.NewCmdReview(&repoResolvingCmdFactory, nil))
174175
prCmd.AddCommand(prDiffCmd.NewCmdDiff(&repoResolvingCmdFactory, nil))
176+
prCmd.AddCommand(prCheckoutCmd.NewCmdCheckout(&repoResolvingCmdFactory, nil))
175177

176178
RootCmd.AddCommand(creditsCmd.NewCmdCredits(cmdFactory, nil))
177179
}

command/root_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package command
22

33
import (
44
"testing"
5-
6-
"github.com/cli/cli/internal/ghrepo"
75
)
86

97
func TestChangelogURL(t *testing.T) {
@@ -42,22 +40,3 @@ func TestChangelogURL(t *testing.T) {
4240
t.Errorf("expected %s to create url %s but got %s", tag, url, result)
4341
}
4442
}
45-
46-
func TestRemoteURLFormatting_no_config(t *testing.T) {
47-
initBlankContext("", "OWNER/REPO", "master")
48-
result := formatRemoteURL(prCheckoutCmd, ghrepo.New("OWNER", "REPO"))
49-
eq(t, result, "https://github.com/OWNER/REPO.git")
50-
}
51-
52-
func TestRemoteURLFormatting_ssh_config(t *testing.T) {
53-
cfg := `---
54-
hosts:
55-
github.com:
56-
user: OWNER
57-
oauth_token: MUSTBEHIGHCUZIMATOKEN
58-
git_protocol: ssh
59-
`
60-
initBlankContext(cfg, "OWNER/REPO", "master")
61-
result := formatRemoteURL(prCheckoutCmd, ghrepo.New("OWNER", "REPO"))
62-
eq(t, result, "git@github.com:OWNER/REPO.git")
63-
}
Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,101 @@
1-
package command
1+
package checkout
22

33
import (
44
"errors"
55
"fmt"
6+
"net/http"
67
"os"
78
"os/exec"
89
"strings"
910

10-
"github.com/spf13/cobra"
11-
1211
"github.com/cli/cli/api"
12+
"github.com/cli/cli/context"
1313
"github.com/cli/cli/git"
14+
"github.com/cli/cli/internal/config"
1415
"github.com/cli/cli/internal/ghrepo"
1516
"github.com/cli/cli/internal/run"
17+
"github.com/cli/cli/pkg/cmd/pr/shared"
18+
"github.com/cli/cli/pkg/cmdutil"
19+
"github.com/cli/cli/pkg/iostreams"
20+
"github.com/spf13/cobra"
1621
)
1722

18-
func prCheckout(cmd *cobra.Command, args []string) error {
19-
ctx := contextForCommand(cmd)
20-
currentBranch, _ := ctx.Branch()
21-
remotes, err := ctx.Remotes()
23+
type CheckoutOptions struct {
24+
HttpClient func() (*http.Client, error)
25+
Config func() (config.Config, error)
26+
IO *iostreams.IOStreams
27+
BaseRepo func() (ghrepo.Interface, error)
28+
Remotes func() (context.Remotes, error)
29+
Branch func() (string, error)
30+
31+
SelectorArg string
32+
}
33+
34+
func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobra.Command {
35+
opts := &CheckoutOptions{
36+
IO: f.IOStreams,
37+
HttpClient: f.HttpClient,
38+
Config: f.Config,
39+
BaseRepo: f.BaseRepo,
40+
Remotes: f.Remotes,
41+
Branch: f.Branch,
42+
}
43+
44+
cmd := &cobra.Command{
45+
Use: "checkout {<number> | <url> | <branch>}",
46+
Short: "Check out a pull request in git",
47+
Args: func(cmd *cobra.Command, args []string) error {
48+
if len(args) == 0 {
49+
return &cmdutil.FlagError{Err: errors.New("argument required")}
50+
}
51+
return nil
52+
},
53+
RunE: func(cmd *cobra.Command, args []string) error {
54+
if len(args) > 0 {
55+
opts.SelectorArg = args[0]
56+
}
57+
58+
if runF != nil {
59+
return runF(opts)
60+
}
61+
return checkoutRun(opts)
62+
},
63+
}
64+
65+
return cmd
66+
}
67+
68+
func checkoutRun(opts *CheckoutOptions) error {
69+
currentBranch, err := opts.Branch()
2270
if err != nil {
2371
return err
2472
}
2573

26-
apiClient, err := apiClientForContext(ctx)
74+
remotes, err := opts.Remotes()
2775
if err != nil {
2876
return err
2977
}
3078

31-
pr, baseRepo, err := prFromArgs(ctx, apiClient, cmd, args)
79+
httpClient, err := opts.HttpClient()
3280
if err != nil {
3381
return err
3482
}
83+
apiClient := api.NewClientFromHTTP(httpClient)
84+
85+
pr, baseRepo, err := shared.PRFromArgs(apiClient, opts.BaseRepo, opts.Branch, opts.Remotes, opts.SelectorArg)
86+
if err != nil {
87+
return err
88+
}
89+
90+
cfg, err := opts.Config()
91+
if err != nil {
92+
return err
93+
}
94+
protocol, _ := cfg.Get(baseRepo.RepoHost(), "git_protocol")
3595

3696
baseRemote, _ := remotes.FindByRepo(baseRepo.RepoOwner(), baseRepo.RepoName())
3797
// baseRemoteSpec is a repository URL or a remote name to be used in git fetch
38-
baseURLOrName := formatRemoteURL(cmd, baseRepo)
98+
baseURLOrName := ghrepo.FormatRemoteURL(baseRepo, protocol)
3999
if baseRemote != nil {
40100
baseURLOrName = baseRemote.Name
41101
}
@@ -95,7 +155,7 @@ func prCheckout(cmd *cobra.Command, args []string) error {
95155
mergeRef := ref
96156
if pr.MaintainerCanModify {
97157
headRepo := ghrepo.NewWithHost(pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name, baseRepo.RepoHost())
98-
remote = formatRemoteURL(cmd, headRepo)
158+
remote = ghrepo.FormatRemoteURL(headRepo, protocol)
99159
mergeRef = fmt.Sprintf("refs/heads/%s", pr.HeadRefName)
100160
}
101161
if mc, err := git.Config(fmt.Sprintf("branch.%s.merge", newBranchName)); err != nil || mc == "" {
@@ -115,15 +175,3 @@ func prCheckout(cmd *cobra.Command, args []string) error {
115175

116176
return nil
117177
}
118-
119-
var prCheckoutCmd = &cobra.Command{
120-
Use: "checkout {<number> | <url> | <branch>}",
121-
Short: "Check out a pull request in Git",
122-
Args: func(cmd *cobra.Command, args []string) error {
123-
if len(args) < 1 {
124-
return errors.New("requires a pull request number as an argument")
125-
}
126-
return nil
127-
},
128-
RunE: prCheckout,
129-
}

0 commit comments

Comments
 (0)
X Tutup