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

Don't require the provider name when auto-enrolling repositories #3906

Merged
merged 2 commits into from
Jul 17, 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
93 changes: 76 additions & 17 deletions cmd/cli/app/repo/repo_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package repo
import (
"context"
"fmt"
"slices"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -55,29 +56,18 @@ func RegisterCmd(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc

if registerAll {
if len(inputRepoList) > 0 {
return cli.MessageAndError("Cannot use --all with --name", nil)
fmt.Println("Cannot use --all and --name together")
return nil
}

providerClient := minderv1.NewProvidersServiceClient(conn)

err := enableAutoRegistration(ctx, providerClient, project, provider)
err := enableAutoRegistration(ctx, cmd, providerClient, project, provider)
if err != nil {
return cli.MessageAndError("Error enabling auto registration", err)
}
cmd.Println("Enabled auto registration for future repositories")

_, err = providerClient.ReconcileEntityRegistration(ctx, &minderv1.ReconcileEntityRegistrationRequest{
Context: &minderv1.Context{
Provider: &provider,
Project: &project,
},
Entity: minderv1.Entity_ENTITY_REPOSITORIES.ToString(),
})
if err != nil {
return cli.MessageAndError("Error reconciling provider registration", err)
}

cmd.Println("Issued task to register all available 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 @@ -267,7 +257,65 @@ func printRepoRegistrationStatus(cmd *cobra.Command, results []*minderv1.Registe
}

func enableAutoRegistration(
ctx context.Context, provCli minderv1.ProvidersServiceClient,
ctx context.Context,
cmd *cobra.Command,
provCli minderv1.ProvidersServiceClient,
project, provName string,
) error {
if provName != "" {
return enableAutoRegistrationForProvider(ctx, cmd, provCli, project, provName)
}

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

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

for {
allProviders, err := provCli.ListProviders(ctx, &minderv1.ListProvidersRequest{
Context: &minderv1.Context{
Project: &project,
},
Cursor: cursor,
})

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

cursor = allProviders.Cursor

for _, provider := range allProviders.Providers {
if !slices.Contains(provider.GetImplements(), minderv1.ProviderType_PROVIDER_TYPE_REPO_LISTER) {
continue
}

err := enableAutoRegistrationForProvider(ctx, cmd, provCli, project, provider.Name)
if err != nil {
// we could print a diagnostical 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
}
}

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

return nil
}

func enableAutoRegistrationForProvider(
ctx context.Context,
cmd *cobra.Command,
provCli minderv1.ProvidersServiceClient,
project, providerName string,
) error {
serde, err := cli.GetProviderConfig(ctx, provCli, project, providerName)
Expand Down Expand Up @@ -297,7 +345,7 @@ func enableAutoRegistration(
}

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

repoReg.Enabled = proto.Bool(true)
Expand All @@ -308,6 +356,17 @@ func enableAutoRegistration(
return cli.MessageAndError("failed to update provider", err)
}

_, err = provCli.ReconcileEntityRegistration(ctx, &minderv1.ReconcileEntityRegistrationRequest{
Context: &minderv1.Context{
Provider: &providerName,
Project: &project,
},
Entity: minderv1.Entity_ENTITY_REPOSITORIES.ToString(),
})
if err != nil {
return cli.MessageAndError("Error reconciling provider registration", err)
}

return nil
}

Expand Down
10 changes: 6 additions & 4 deletions docs/docs/understand/repository_registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ First, identify the _name_ of your GitHub Provider. You can list your enrolled p
minder provider list
```

To enable automatic registration for your repositories, set the `auto_registration.entities.repository.enabled` attribute to `true` for your provider. For example, if your provider was named `github-app-myorg`, run:
To enable automatic registration for your future repositories, set the `auto_registration.entities.repository.enabled` attribute to `true` for your provider. For example, if your provider was named `github-app-myorg`, run:

```bash
minder provider update --set-attribute=auto_registration.entities.repository.enabled=true --name=github-app-myorg
```

:::note
Enabling automatic registration only applies to new repositories that are created in your organization, it does not retroactively register existing repositories.
:::
To enable automatic registration for existing repositories and enroll all the currently existing repositories, you can use the `minder repo register` command:
```bash
minder repo register --all
```
You can pass the `--provider` flag to restrict the registration to a specific provider. By default, the `repo register` command will register repositories for all providers.

To disable automatic registration, set the `auto_registration.entities.repository.enabled` attribute to `false`:

Expand Down