Skip to content

Commit 47cef73

Browse files
committed
Fix GH_REPO override
1 parent aef1a4b commit 47cef73

File tree

5 files changed

+29
-33
lines changed

5 files changed

+29
-33
lines changed

command/root.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ func init() {
4949

5050
// overridden in tests
5151
var initContext = func() context.Context {
52-
ctx := context.New()
53-
if repo := os.Getenv("GH_REPO"); repo != "" {
54-
ctx.SetBaseRepo(repo)
55-
}
56-
return ctx
52+
return context.New()
5753
}
5854

5955
// BasicClient returns an API client for github.com only that borrows from but
@@ -78,11 +74,7 @@ func BasicClient() (*api.Client, error) {
7874
}
7975

8076
func contextForCommand(cmd *cobra.Command) context.Context {
81-
ctx := initContext()
82-
if repo, err := cmd.Flags().GetString("repo"); err == nil && repo != "" {
83-
ctx.SetBaseRepo(repo)
84-
}
85-
return ctx
77+
return initContext()
8678
}
8779

8880
func apiVerboseLog() api.ClientOption {

context/context.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import (
1515

1616
// Context represents the interface for querying information about the current environment
1717
type Context interface {
18-
Branch() (string, error)
19-
SetBranch(string)
20-
SetBaseRepo(string)
2118
Config() (config.Config, error)
2219
}
2320

pkg/cmd/issue/issue.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package issue
22

33
import (
44
"github.com/MakeNowJust/heredoc"
5-
"github.com/cli/cli/internal/ghrepo"
65
cmdClose "github.com/cli/cli/pkg/cmd/issue/close"
76
cmdCreate "github.com/cli/cli/pkg/cmd/issue/create"
87
cmdList "github.com/cli/cli/pkg/cmd/issue/list"
@@ -31,17 +30,9 @@ func NewCmdIssue(f *cmdutil.Factory) *cobra.Command {
3130
- by URL, e.g. "https://github.com/OWNER/REPO/issues/123".
3231
`),
3332
},
34-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
35-
if repo, _ := cmd.Flags().GetString("repo"); repo != "" {
36-
// NOTE: this mutates the factory
37-
f.BaseRepo = func() (ghrepo.Interface, error) {
38-
return ghrepo.FromFullName(repo)
39-
}
40-
}
41-
},
4233
}
4334

44-
cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format")
35+
cmdutil.EnableRepoOverride(cmd, f)
4536

4637
cmd.AddCommand(cmdClose.NewCmdClose(f, nil))
4738
cmd.AddCommand(cmdCreate.NewCmdCreate(f, nil))

pkg/cmd/pr/pr.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package pr
22

33
import (
44
"github.com/MakeNowJust/heredoc"
5-
"github.com/cli/cli/internal/ghrepo"
65
cmdCheckout "github.com/cli/cli/pkg/cmd/pr/checkout"
76
cmdClose "github.com/cli/cli/pkg/cmd/pr/close"
87
cmdCreate "github.com/cli/cli/pkg/cmd/pr/create"
@@ -37,17 +36,9 @@ func NewCmdPR(f *cmdutil.Factory) *cobra.Command {
3736
- by the name of its head branch, e.g. "patch-1" or "OWNER:patch-1".
3837
`),
3938
},
40-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
41-
if repo, _ := cmd.Flags().GetString("repo"); repo != "" {
42-
// NOTE: this mutates the factory
43-
f.BaseRepo = func() (ghrepo.Interface, error) {
44-
return ghrepo.FromFullName(repo)
45-
}
46-
}
47-
},
4839
}
4940

50-
cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format")
41+
cmdutil.EnableRepoOverride(cmd, f)
5142

5243
cmd.AddCommand(cmdCheckout.NewCmdCheckout(f, nil))
5344
cmd.AddCommand(cmdClose.NewCmdClose(f, nil))

pkg/cmdutil/repo_override.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmdutil
2+
3+
import (
4+
"os"
5+
6+
"github.com/cli/cli/internal/ghrepo"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func EnableRepoOverride(cmd *cobra.Command, f *Factory) {
11+
cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format")
12+
13+
cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
14+
repoOverride, _ := cmd.Flags().GetString("repo")
15+
if repoFromEnv := os.Getenv("GH_REPO"); repoOverride == "" && repoFromEnv != "" {
16+
repoOverride = repoFromEnv
17+
}
18+
if repoOverride != "" {
19+
// NOTE: this mutates the factory
20+
f.BaseRepo = func() (ghrepo.Interface, error) {
21+
return ghrepo.FromFullName(repoOverride)
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)