Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pkg/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
},
Args: cobra.ExactArgs(1),
PreRun: func(c *cobra.Command, args []string) {
opts.BaseRepo = cmdutil.OverrideBaseRepoFunc(f, "")
opts.BaseRepo = cmdutil.PrioritiseEnvBaseRepoFunc(f.BaseRepo)
},
RunE: func(c *cobra.Command, args []string) error {
opts.RequestPath = args[0]
Expand Down
13 changes: 3 additions & 10 deletions pkg/cmd/factory/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func Test_SmartBaseRepo(t *testing.T) {
}

// Defined in pkg/cmdutil/repo_override.go but test it along with other BaseRepo functions
func Test_OverrideBaseRepo(t *testing.T) {
func Test_PrioritiseEnvBaseRepoFunc(t *testing.T) {
tests := []struct {
name string
remotes git.RemoteSet
Expand All @@ -267,14 +267,7 @@ func Test_OverrideBaseRepo(t *testing.T) {
wantsHost string
}{
{
name: "override from argument",
argOverride: "override/test",
wantsHost: "github.com",
wantsOwner: "override",
wantsName: "test",
},
{
name: "override from environment",
name: "from environment",
envOverride: "somehost.com/override/test",
wantsHost: "somehost.com",
wantsOwner: "override",
Expand Down Expand Up @@ -307,7 +300,7 @@ func Test_OverrideBaseRepo(t *testing.T) {
},
}
f.Remotes = rr.Resolver()
f.BaseRepo = cmdutil.OverrideBaseRepoFunc(f, tt.argOverride)
f.BaseRepo = cmdutil.PrioritiseEnvBaseRepoFunc(f.BaseRepo)
repo, err := f.BaseRepo()
if tt.wantsErr {
assert.Error(t, err)
Expand Down
51 changes: 41 additions & 10 deletions pkg/cmdutil/repo_override.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmdutil

import (
"fmt"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -51,20 +52,50 @@ func EnableRepoOverride(cmd *cobra.Command, f *Factory) {
if err := executeParentHooks(cmd, args); err != nil {
return err
}
repoOverride, _ := cmd.Flags().GetString("repo")
f.BaseRepo = OverrideBaseRepoFunc(f, repoOverride)

// First, get the value of the flag.
// We can ignore errors here because the flag is guaranteed to exist and be string.
userProvidedRepo, _ := cmd.Flags().GetString("repo")
if userProvidedRepo == "" {
// If there was no flag set, then check the GH_REPO environment variable,
// which has lower precedence.
userProvidedRepo = os.Getenv("GH_REPO")
// If the environment variable was set, then set the value of the `repo` flag,
// this ensures that checks for `HasChanged` work correctly. It's a bit "spooky"
// action at a distance because the flag will not have been set by the user,
// but perhaps it makes sense?
//
// The reason we need this is because there are `secret` commands that need to know whether
// the user set the repo, and the alternative is them having knowledge of the flag and env var,
// or by adjusting all our types so that the "source" of the BaseRepo is surfaced, which is a
// pretty big change.
if userProvidedRepo != "" {
if err := cmd.Flags().Set("repo", userProvidedRepo); err != nil {
return fmt.Errorf("failed to set repo override flag: %v", err)
}
}
}

// If the user provided the repo from either source, then we can return that
// directly inside a new BaseRepo function.
if userProvidedRepo != "" {
Comment thread
williammartin marked this conversation as resolved.
f.BaseRepo = func() (ghrepo.Interface, error) {
return ghrepo.FromFullName(userProvidedRepo)
}
}

return nil
}
}

func OverrideBaseRepoFunc(f *Factory, override string) func() (ghrepo.Interface, error) {
if override == "" {
override = os.Getenv("GH_REPO")
}
if override != "" {
return func() (ghrepo.Interface, error) {
return ghrepo.FromFullName(override)
type baseRepoFn func() (ghrepo.Interface, error)

func PrioritiseEnvBaseRepoFunc(baseRepo baseRepoFn) func() (ghrepo.Interface, error) {
return func() (ghrepo.Interface, error) {
repo := os.Getenv("GH_REPO")
if repo == "" {
return baseRepo()
}
return ghrepo.FromFullName(repo)
}
return f.BaseRepo
}
Comment on lines +91 to 101

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can gather, there aren't any functional changes to this logic but refining it:

  • decouple this from Factory for some arbitrary logic that provides a ghrepo.Interface like the base repo functions
  • focuses this solely on the environmental override
  • defers to the provided default if environment is a no go

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

focuses this solely on the environmental override

That's the functional change. Previously it also accepted a string to be like "just return this", which frankly, is a bad solution because the caller should just choose not to call the function if it were empty.