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

Fix enabling repo auto registration for providers #5171

Merged
merged 1 commit into from
Dec 10, 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
71 changes: 53 additions & 18 deletions cmd/cli/app/repo/repo_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package repo

import (
"context"
"errors"
"fmt"
"slices"

Expand Down Expand Up @@ -50,13 +51,22 @@ func RegisterCmd(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc

providerClient := minderv1.NewProvidersServiceClient(conn)

err := enableAutoRegistration(ctx, cmd, providerClient, project, provider)
ret, err := enableAutoRegistration(ctx, providerClient, project, provider)
if err != nil {
return cli.MessageAndError("Error enabling auto registration", err)
cmd.Println("Enabling auto registration failed for one or more providers.")
} else {
cmd.Println("Enabling auto registration succeeded.")
}
if len(ret.alreadyEnabled) > 0 {
cmd.Println(fmt.Sprintf("Auto registration is already enabled for: %v", ret.alreadyEnabled))
}
if len(ret.failedProviders) > 0 {
cmd.Println(fmt.Sprintf("Failed to enable: %v", ret.failedProviders))
}
if len(ret.updatedProviders) > 0 {
cmd.Println(fmt.Sprintf("Successfully enabled: %v", ret.updatedProviders))
cmd.Println("Use `minder repo list` to check the list of registered repositories.")
}
cmd.Println("Enabled auto registration for future repositories")
cmd.Println("Issued task to register all currently available repositories")
cmd.Println("Use `minder repo list` to check the list registered repositories")
return nil
}

Expand Down Expand Up @@ -244,26 +254,39 @@ func printRepoRegistrationStatus(cmd *cobra.Command, results []*minderv1.Registe
t.Render()
}

var errAutoRegistrationAlreadyEnabled = errors.New("auto registration is already enabled")

type autoRegisterResult struct {
updatedProviders []string
failedProviders []string
alreadyEnabled []string
}

func enableAutoRegistration(
ctx context.Context,
cmd *cobra.Command,
provCli minderv1.ProvidersServiceClient,
project, provName string,
) error {
) (autoRegisterResult, error) {
if provName != "" {
return enableAutoRegistrationForProvider(ctx, cmd, provCli, project, provName)
err := enableAutoRegistrationForProvider(ctx, provCli, project, provName)
if err != nil {
if errors.Is(err, errAutoRegistrationAlreadyEnabled) {
return autoRegisterResult{alreadyEnabled: []string{provName}}, nil
}
return autoRegisterResult{failedProviders: []string{provName}}, err
}
return autoRegisterResult{updatedProviders: []string{provName}}, nil
}

return enableAutoRegistrationAllProviders(ctx, cmd, provCli, project)
return enableAutoRegistrationAllProviders(ctx, provCli, project)
}

func enableAutoRegistrationAllProviders(
ctx context.Context,
cmd *cobra.Command,
provCli minderv1.ProvidersServiceClient,
project string,
) error {
) (autoRegisterResult, error) {
var cursor string
ret := autoRegisterResult{}

for {
allProviders, err := provCli.ListProviders(ctx, &minderv1.ListProvidersRequest{
Expand All @@ -274,7 +297,7 @@ func enableAutoRegistrationAllProviders(
})

if err != nil {
return cli.MessageAndError("failed to get providers", err)
return ret, cli.MessageAndError("failed to get providers", err)
}

cursor = allProviders.Cursor
Expand All @@ -284,25 +307,37 @@ func enableAutoRegistrationAllProviders(
continue
}

err := enableAutoRegistrationForProvider(ctx, cmd, provCli, project, provider.Name)
err := enableAutoRegistrationForProvider(ctx, provCli, project, provider.Name)
if err != nil {
// we could print a diagnostical message here, but since the legacy github provider doesn't support
if errors.Is(err, errAutoRegistrationAlreadyEnabled) {
// Store the provider name that was already enabled
ret.alreadyEnabled = append(ret.alreadyEnabled, provider.Name)
continue
}
// Store the provider name that failed to update
ret.failedProviders = append(ret.failedProviders, provider.Name)
// we could print a diagnostic message here, but since the legacy github provider doesn't support
// auto-enrollment and we still pre-create it, the user would see the message all the time.
continue
}
// Store the provider name that was successfully updated
ret.updatedProviders = append(ret.updatedProviders, provider.Name)
}

if allProviders.Cursor == "" {
break
}
}

return nil
// If there are failed providers, return an error
if len(ret.failedProviders) > 0 {
return ret, fmt.Errorf("failed to enable auto registration for some providers")
}
return ret, nil
}

func enableAutoRegistrationForProvider(
ctx context.Context,
cmd *cobra.Command,
provCli minderv1.ProvidersServiceClient,
project, providerName string,
) error {
Expand Down Expand Up @@ -333,7 +368,7 @@ func enableAutoRegistrationForProvider(
}

if repoReg.GetEnabled() {
cmd.Printf("Auto registration is already enabled for %s\n", providerName)
return errAutoRegistrationAlreadyEnabled
}

repoReg.Enabled = proto.Bool(true)
Expand Down
3 changes: 2 additions & 1 deletion internal/util/cli/providerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func SetProviderConfig(
Provider: &providerName,
},
Patch: &minderv1.Provider{
Config: cfg,
Config: cfg,
Version: "v1",
},
}

Expand Down
Loading