Address reviewer comments

This commit is contained in:
Lennart Kats 2024-07-28 21:45:51 +01:00
parent b384b36066
commit 3ba3c17937
No known key found for this signature in database
GPG Key ID: 1EB8B57673197023
2 changed files with 23 additions and 14 deletions

View File

@ -8,21 +8,22 @@ import (
"github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/diag" "github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/set"
) )
const CheckPermissionsFilename = "permissions.check" const CheckPermissionsFilename = "permissions.check"
type reportPermissionErrors struct{} type permissionDiagnostics struct{}
func PermissionDiagnostics() bundle.Mutator { func PermissionDiagnostics() bundle.Mutator {
return &reportPermissionErrors{} return &permissionDiagnostics{}
} }
func (m *reportPermissionErrors) Name() string { func (m *permissionDiagnostics) Name() string {
return "CheckPermissions" return "CheckPermissions"
} }
func (m *reportPermissionErrors) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { func (m *permissionDiagnostics) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
if len(b.Config.Permissions) == 0 { if len(b.Config.Permissions) == 0 {
// Only warn if there is an explicit top-level permissions section // Only warn if there is an explicit top-level permissions section
return nil return nil
@ -50,11 +51,11 @@ func (m *reportPermissionErrors) Apply(ctx context.Context, b *bundle.Bundle) di
// - assistance: advice on who to contact as to manage this project // - assistance: advice on who to contact as to manage this project
func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) { func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) {
canManageBundle := false canManageBundle := false
otherManagers := make(map[string]bool) otherManagers := set.NewSet[string]()
if b.Config.RunAs != nil && b.Config.RunAs.UserName != "" { if b.Config.RunAs != nil && b.Config.RunAs.UserName != "" {
// The run_as user is another human that could be contacted // The run_as user is another human that could be contacted
// about this bundle. // about this bundle.
otherManagers[b.Config.RunAs.UserName] = true otherManagers.Add(b.Config.RunAs.UserName)
} }
currentUser := b.Config.Workspace.CurrentUser.UserName currentUser := b.Config.Workspace.CurrentUser.UserName
@ -83,17 +84,15 @@ func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) {
// Skip service principals // Skip service principals
continue continue
} }
otherManagers[otherManager] = true otherManagers.Add(otherManager)
}
var managersSlice []string
for manager := range otherManagers {
managersSlice = append(managersSlice, manager)
} }
assistance := "For assistance, contact the owners of this project." assistance := "For assistance, contact the owners of this project."
if len(managersSlice) > 0 { if otherManagers.Size() > 0 {
assistance = fmt.Sprintf("For assistance, users or groups with appropriate permissions may include: %s.", strings.Join(managersSlice, ", ")) assistance = fmt.Sprintf(
"For assistance, users or groups with appropriate permissions may include: %s.",
strings.Join(otherManagers.Values(), ", "),
)
} }
return canManageBundle, assistance return canManageBundle, assistance
} }

View File

@ -14,6 +14,11 @@ type Set[T any] struct {
data map[string]T data map[string]T
} }
// Values returns a slice of the set's values
func (s *Set[T]) Values() []T {
return maps.Values(s.data)
}
// NewSetFromF initialise a new set with initial values and a hash function // NewSetFromF initialise a new set with initial values and a hash function
// to define uniqueness of value // to define uniqueness of value
func NewSetFromF[T any](values []T, f hashFunc[T]) *Set[T] { func NewSetFromF[T any](values []T, f hashFunc[T]) *Set[T] {
@ -69,6 +74,11 @@ func (s *Set[T]) Has(item T) bool {
return ok return ok
} }
// Size returns the number of elements in the set
func (s *Set[T]) Size() int {
return len(s.data)
}
// Returns an iterable slice of values from set // Returns an iterable slice of values from set
func (s *Set[T]) Iter() []T { func (s *Set[T]) Iter() []T {
return maps.Values(s.data) return maps.Values(s.data)