Repository navigation
feat: add inspection name form field and enforce unique inspection names - #1036
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces an InspectionNameRegistry and a corresponding in-memory implementation to manage unique inspection names and prevent collisions across runs, imports, and updates. It also adds a new form task InputInspectionNameTask to allow users to specify custom inspection names. The review feedback suggests updating header.SuggestedFileName alongside header.InspectionName in the form task's converter to ensure consistency, and replacing defensive khictx.GetValue checks with khictx.MustGetValue for the required InspectionNameRegistryKey dependency in both the default value function and the validator.
| if header, found := typedmap.Get(metadataSet, inspectionmetadata.HeaderMetadataKey); found && header != nil { | ||
| header.InspectionName = trimmed | ||
| } |
There was a problem hiding this comment.
The converter updates header.InspectionName with the trimmed user input, but it does not update header.SuggestedFileName. This means that for any newly run inspection, the suggested file name will remain "unnamed.khi" (its initial default value) even if the user specified a custom inspection name.
Updating header.SuggestedFileName alongside header.InspectionName ensures consistency across the application, matching the behavior in RegisterImportedInspection and UpdateInspection.
| if header, found := typedmap.Get(metadataSet, inspectionmetadata.HeaderMetadataKey); found && header != nil { | |
| header.InspectionName = trimmed | |
| } | |
| if header, found := typedmap.Get(metadataSet, inspectionmetadata.HeaderMetadataKey); found && header != nil { | |
| header.InspectionName = trimmed | |
| header.SuggestedFileName = fmt.Sprintf("%s.khi", trimmed) | |
| } |
| if registry, err := khictx.GetValue(ctx, inspectioncore.InspectionNameRegistryKey); err == nil && registry != nil { | ||
| return registry.ResolveUniqueName(inspectionID, baseName), nil | ||
| } | ||
| return baseName, nil |
There was a problem hiding this comment.
According to the repository's general guidelines, we should avoid defensive nil fallbacks and excessive nil checks on required parameters or dependencies. Since InspectionNameRegistryKey is a required dependency for resolving unique inspection names, we should enforce its presence using khictx.MustGetValue instead of a defensive khictx.GetValue check.
registry := khictx.MustGetValue(ctx, inspectioncore.InspectionNameRegistryKey)
return registry.ResolveUniqueName(inspectionID, baseName), nilReferences
- For internal Go code where backward compatibility is not required, avoid model pollution and defensive nil fallbacks (such as global fallback stores or optional nil fields). Enforce strict non-nil constraints on dependencies and require callers to supply them explicitly.
| if registry, err := khictx.GetValue(ctx, inspectioncore.InspectionNameRegistryKey); err == nil && registry != nil { | ||
| if err := registry.ReserveName(inspectionID, trimmed); err != nil { | ||
| if errors.Is(err, inspectioncore.ErrInspectionNameAlreadyInUse) { | ||
| return fmt.Sprintf("inspection name %q is already in use", trimmed), nil | ||
| } | ||
| return err.Error(), nil | ||
| } | ||
| } |
There was a problem hiding this comment.
Similarly, in the validator, InspectionNameRegistryKey is a required dependency to validate and reserve the inspection name. Silently skipping the reservation if the registry is missing could lead to duplicate names being accepted without any error. Enforce its presence using khictx.MustGetValue.
registry := khictx.MustGetValue(ctx, inspectioncore.InspectionNameRegistryKey)
if err := registry.ReserveName(inspectionID, trimmed); err != nil {
if errors.Is(err, inspectioncore.ErrInspectionNameAlreadyInUse) {
return fmt.Sprintf("inspection name %q is already in use", trimmed), nil
}
return err.Error(), nil
}References
- For internal Go code where backward compatibility is not required, avoid model pollution and defensive nil fallbacks (such as global fallback stores or optional nil fields). Enforce strict non-nil constraints on dependencies and require callers to supply them explicitly.
| return "inspection name must not be empty", nil | ||
| } | ||
| inspectionID := khictx.MustGetValue(ctx, inspectioncore.InspectionTaskInspectionID) | ||
| if registry, err := khictx.GetValue(ctx, inspectioncore.InspectionNameRegistryKey); err == nil && registry != nil { |
There was a problem hiding this comment.
The registry can't be nil. It should use MustGetValue
| if err != nil || baseName == "" { | ||
| baseName = "Inspection" | ||
| } | ||
| if registry, err := khictx.GetValue(ctx, inspectioncore.InspectionNameRegistryKey); err == nil && registry != nil { |
There was a problem hiding this comment.
The registry can't be nil. It should use MustGetValue
aada067
Summary
InputInspectionNameTaskat the top of the inspection parameter form (Priority: 200000) so users can specify a custom display name when creating an inspection.Google Kubernetes Engine,Google Kubernetes Engine(1),Google Kubernetes Engine(2), ...).InspectionNameRegistry(InMemoryInspectionNameRegistry) to atomically reserve, validate, and resolve unique inspection names across the server:InputInspectionNameTask): Validates and reserves the inspection name during form evaluation, displaying an error hint if a name is empty or already reserved by another inspection..khiFile Import (RegisterImportedInspection): Automatically resolves and reserves a unique sequential name when an imported.khifile collides with an existing inspection name.UpdateInspection): Rejects duplicate names withconnect.CodeAlreadyExistsand empty names withconnect.CodeInvalidArgument, releasing the old name upon rename.