Skip to content
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

Default to github-app in provider enroll #3032

Merged
merged 3 commits into from
Apr 11, 2024
Merged
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
5 changes: 2 additions & 3 deletions cmd/cli/app/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/spf13/cobra"

"github.com/stacklok/minder/cmd/cli/app"
ghclient "github.com/stacklok/minder/internal/providers/github/oauth"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

Expand All @@ -37,9 +36,9 @@ var ProviderCmd = &cobra.Command{
func init() {
app.RootCmd.AddCommand(ProviderCmd)
// Flags for all subcommands
// TODO: remove the provider flag from here and add it only to the subcommands that need it
ProviderCmd.PersistentFlags().StringP("provider", "p", ghclient.Github, "Name of the provider, i.e. github")
ProviderCmd.PersistentFlags().StringP("project", "j", "", "ID of the project")
// TODO: get rid of this
ProviderCmd.PersistentFlags().StringP("provider", "p", "", "DEPRECATED - use `class` flag of `enroll` instead")
Copy link
Member

Choose a reason for hiding this comment

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

Since this was only useful for the provider enroll command, should it be moved there? Otherwise other commands like provider list see the deprecation message

Copy link
Member Author

@dmjb dmjb Apr 11, 2024

Choose a reason for hiding this comment

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

I left it that way for the sake of backwards compatibility. For example, in the current CLI, it is acceptable to write:

minder provider --provider=github enroll

Which would be broken if we moved that flag down to the leaf node commands as far as I can tell.

I also think that we should consider changing the name of the parameter: in some commands elsewhere in the CLI, provider might mean the name of the provider, here it means the class/type

}

func getImplementsAsStrings(p *minderv1.Provider) []string {
Expand Down
27 changes: 21 additions & 6 deletions cmd/cli/app/provider/provider_enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (

// legacyGitHubProvider is the legacy GitHub OAuth provider class
legacyGitHubProvider = minderv1.ProviderClass_PROVIDER_CLASS_GITHUB

// githubAppProvider is the name used to identify the new GitHub App provider class
githubAppProvider = minderv1.ProviderClass_PROVIDER_CLASS_GITHUB_APP
)

var enrollCmd = &cobra.Command{
Expand All @@ -59,7 +62,11 @@ func EnrollProviderCommand(ctx context.Context, cmd *cobra.Command, _ []string,
oauthClient := minderv1.NewOAuthServiceClient(conn)
providerClient := minderv1.NewProvidersServiceClient(conn)

// TODO: get rid of provider flag, only use class
provider := viper.GetString("provider")
if provider == "" {
provider = viper.GetString("class")
}
project := viper.GetString("project")
token := viper.GetString("token")
owner := viper.GetString("owner")
Expand All @@ -76,7 +83,11 @@ func EnrollProviderCommand(ctx context.Context, cmd *cobra.Command, _ []string,
ownerPromptStr = fmt.Sprintf("the %s organisation", owner)
}

if !yesFlag {
// Only show this option for the legacy flow
// TODO: split this into multiple subcommands so we do not need to have
// checks like this per flag
// The Github App flow will ask these questions in the browser
if !yesFlag && provider == legacyGitHubProvider.String() {
yes := cli.PrintYesNoPrompt(cmd,
fmt.Sprintf("You are about to enroll repositories from %s.", ownerPromptStr),
"Do you confirm?",
Expand All @@ -87,7 +98,8 @@ func EnrollProviderCommand(ctx context.Context, cmd *cobra.Command, _ []string,
}
}

if token != "" {
// the token only applies to the old flow
if token != "" && provider == legacyGitHubProvider.String() {
return enrollUsingToken(ctx, cmd, oauthClient, provider, project, token, owner)
}

Expand Down Expand Up @@ -161,9 +173,10 @@ func enrollUsingOAuth2Flow(
}

cmd.Printf("Your browser will now be opened to: %s\n", resp.GetUrl())
cmd.Println("Please follow the instructions on the page to complete the OAuth flow.")
cmd.Println("Please follow the instructions on the page to complete the enrollment flow.")
cmd.Println("Once the flow is complete, the CLI will close")
cmd.Println("If this is a headless environment, please copy and paste the URL into a browser on a different machine.")
cmd.Printf("Enrollment will time out after %.1f minutes.\n", MAX_WAIT.Minutes())

if !skipBrowser {
if err := browser.OpenURL(resp.GetUrl()); err != nil {
Expand Down Expand Up @@ -274,9 +287,11 @@ func callBackServer(ctx context.Context, cmd *cobra.Command, project string, por
func init() {
ProviderCmd.AddCommand(enrollCmd)
// Flags
enrollCmd.Flags().StringP("token", "t", "", "Personal Access Token (PAT) to use for enrollment")
enrollCmd.Flags().StringP("owner", "o", "", "Owner to filter on for provider resources")
enrollCmd.Flags().BoolP("yes", "y", false, "Bypass yes/no prompt when enrolling new provider")
enrollCmd.Flags().StringP("token", "t", "", "Personal Access Token (PAT) to use for enrollment (Legacy GitHub only)")
enrollCmd.Flags().StringP("owner", "o", "", "Owner to filter on for provider resources (Legacy GitHub only)")
enrollCmd.Flags().BoolP("yes", "y", false, "Bypass any yes/no prompts when enrolling a new provider")
enrollCmd.Flags().StringP("class", "c", githubAppProvider.String(), "Provider class, defaults to github-app")

// Bind flags
if err := viper.BindPFlag("token", enrollCmd.Flags().Lookup("token")); err != nil {
enrollCmd.Printf("Error binding flag: %s", err)
Expand Down