Repository navigation
Fix GH_REPO usage with secret commands #10535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package cmdutil | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "sort" | ||
| "strings" | ||
|
|
@@ -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 != "" { | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. |
||
Uh oh!
There was an error while loading. Please reload this page.