Skip to content

Commit

Permalink
Fix enabling repo auto registration for providers (#5171)
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov authored Dec 10, 2024
1 parent ad08eec commit dc1d2ef
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
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

0 comments on commit dc1d2ef

Please sign in to comment.