Skip to content

Allow auto-completing the --repo values#3942

Merged
mislav merged 2 commits intocli:trunkfrom
dscho:complete--repo-flag
Jul 28, 2021
Merged

Allow auto-completing the --repo values#3942
mislav merged 2 commits intocli:trunkfrom
dscho:complete--repo-flag

Conversation

@dscho
Copy link
Contributor

@dscho dscho commented Jul 2, 2021

I have a local worktree with 28 different remotes, and gh pr create usually tries to pick one of the 27 I don't want to open a PR. So I thought it would be neat to have auto-completion for the --repo option. And here it is.

Please let me know if I made any mistake; This is the first Go code I wrote in at least two years.

@cliAutomation
Copy link
Collaborator

Hi! Thanks for the pull request. Please ensure that this change is linked to an issue by mentioning an issue number in the description of the pull request. If this pull request would close the issue, please put the word 'Fixes' before the issue number somewhere in the pull request body. If this is a tiny change like fixing a typo, feel free to ignore this message.

Copy link

@Rhinowiwina Rhinowiwina left a comment

Choose a reason for hiding this comment

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

Override

@@ -2,6 +2,8 @@ package cmdutil

import (

Choose a reason for hiding this comment

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

``

@@ -19,6 +21,18 @@ func executeParentHooks(cmd *cobra.Command, args []string) error {

func EnableRepoOverride(cmd *cobra.Command, f *Factory) {

Choose a reason for hiding this comment

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

exe

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

The functionality spiked out here looks good to me and works well in my testing!

As described in the PR, this seeds possible -R values from the list of git remotes. That solves your use case, but note that it's possible to use any GitHub repository as the value of -R, and that outside of the context of a local git repository, there will be nothing completed for -R as there will be no git remotes. I wonder should we try to provide some suggestions there, for example using the API to list your recently accessed repositories. Of course, this explodes the scope of this feature quite a bit, so I'd be fine even with just going the git remotes approach and expanding the quality of -R suggestions as follow-up.

What are your thoughts?

_ = cmd.RegisterFlagCompletionFunc("repo", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
remotes, _ := f.Remotes()
for _, remote := range remotes.FilterByHosts([]string{"github.com"}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's best not to hardcode github.com. In case GH_HOST is set via environment, you should get Config from f and call cfg.DefaultHost() and use that. It will be github.com by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, it took a bit of guessing because I am not fluent in Go, but I think I got it now.

}
}
sort.Strings(results)
return results, cobra.ShellCompDirectiveNoSpace
Copy link
Contributor

Choose a reason for hiding this comment

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

ShellCompDirectiveNoSpace is only to be used when no space character should be added after completion. Here, it's better to use:

Suggested change
return results, cobra.ShellCompDirectiveNoSpace
return results, cobra.ShellCompDirectiveNoFileComp

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you!

cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format")
_ = cmd.RegisterFlagCompletionFunc("repo", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
remotes, _ := f.Remotes()
Copy link
Contributor

Choose a reason for hiding this comment

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

You should handle errors here. If there is an error, return early with cobra.ShellCompDirectiveError

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@mislav mislav self-assigned this Jul 27, 2021
Looking at the locally-registered remotes, we have a pretty good idea
what `--repo` values are available. Let's complete them.

Helped by Nate Smith and Mislav Marohnić.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
@dscho dscho force-pushed the complete--repo-flag branch from d2dddb1 to b43f78b Compare July 28, 2021 13:37
@dscho
Copy link
Contributor Author

dscho commented Jul 28, 2021

What are your thoughts?

I would really like to avoid extra network roundtrips if possible, read: I still like my approach, and would want to fall back to querying the REST API only if there is no completion for the current prefix.

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Thanks for the update!

@mislav mislav enabled auto-merge July 28, 2021 15:24
@mislav mislav merged commit 4b499be into cli:trunk Jul 28, 2021
@dscho
Copy link
Contributor Author

dscho commented Jul 28, 2021

Thank you @mislav !!!

@dscho dscho deleted the complete--repo-flag branch July 28, 2021 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants