-
Notifications
You must be signed in to change notification settings - Fork 52
Implement RegisterEntity generic API endpoint #5959
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
base: main
Are you sure you want to change the base?
Conversation
2ed5dc3 to
5760cfa
Compare
evankanderson
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about the delay, it took a little while to go over the related code and understand what was going on here.
Despite the number of comments, I'm pretty bullish on this change -- thanks for doing it!
| projectDeleter projects.ProjectDeleter, | ||
| projectCreator projects.ProjectCreator, | ||
| entityService entitySvc.EntityService, | ||
| entityCreator entitySvc.EntityCreator, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is entityCreator separate from entityService?
In particular, it seems like we might want sub-interfaces like EntityCreator and EntityReader for mocks / other parts, but it feels like there should be a rolled-up interface that can do all the CRUD operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They serve different purposes - EntityCreator orchestrates the full creation lifecycle (fetch → validate → register webhook → persist → events), while EntityService handles reads (list, get, delete). Creation is complex enough that separating them felt cleaner, but we could create a rolled-up interface if you prefer.
| // identifying_properties uniquely identifies the entity in the provider. | ||
| // For example, for a GitHub repository use github/repo_owner and github/repo_name, | ||
| // or use upstream_id to identify by provider's internal ID. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do users discover the appropriate property? Is it expected that there are several different possible properties that might be combined to locate an entity (e.g. region=us-east-1 and account=231571814 and registry=ecr.us-east-1/myname and image=abc)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing that this would be handled by per-provider and per-entity_type documentation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we simplify this to a map<string, string>? Struct is a very broad interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, property discovery would be per-provider/entity-type documentation. For map<string, string> vs Struct - I've addressed the size concern with proto.Size() (32KB limit). Happy to switch to map<string, string> if you prefer the simpler interface - it would also make the API more explicit about what's expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, from generating typescript from the OpenAPI spec, map<string, Struct> makes it very hard to figure out what to put in there unless you go and read the source code. A flatter interface feels more user-friendly if we don't lose anything that we use today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I'm currently checking on whether we put "interesting" struct data into properties. If so, I'd still prefer map<string, Struct> to the current Struct -- we imply that properties: 17.4 would be just peachy.)
| propSvc, | ||
| providerManager, | ||
| evt, | ||
| []entityService.EntityValidator{repoValidator}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the providerManager provide the validators? I'm just thinking that e.g. there might be different providers for the same entity type which use different parameters (e.g. an AWS API probably has a region component, while DockerHub or GHCR.io do not).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Currently validators are global but each checks entity type and returns nil early if not applicable. For provider-specific validation (like AWS regions), I think that should live in the provider's FetchAllProperties which can reject invalid properties. The validator layer is for business rules after properties are fetched (like 'no archived repos').
| ctx, | ||
| entMsg.Originator.Type, entMsg.Originator.GetByProps, entMsg.Hint, | ||
| a.propSvc, | ||
| nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was in a transaction and now isn't. Is that safe (I don't know -- it might well be)?
It looks like we're doing a bunch of reads (parent entity, provider ID) and then starting a transaction which writes data based on the reads. I don't have a strong sense of the semantics (if any) we need here, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reads outside the transaction are for stable data (parent entity, provider). The transaction protects the writes (entity + properties). If there's a race where parent is deleted between read/write, the FK constraint will catch it.
| &entityService.EntityCreationOptions{ | ||
| OriginatingEntityID: &parentEwp.Entity.ID, | ||
| RegisterWithProvider: false, // No webhooks for child entities | ||
| PublishReconciliationEvent: false, // No reconciliation for child entities |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not reconcile the child entities? Is that simply because we didn't publish the events before?
It looks like maybe the GetEntity is called from a message handler already, so we're trying to avoid a loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Matches existing behavior - child entities get persisted but don't trigger reconciliation. The parent repo's reconciliation handles evaluation. Publishing events for children could create loops since this runs from a message handler.
|
OK, I somehow missed that you had reviewed this! I'll get back to this. Sorry about that |
|
This PR needs additional information before we can continue. It is now marked as stale because it has been open for 30 days with no activity. Please provide the necessary details to continue or it will be closed in 30 days. |
Implements the RegisterEntity gRPC endpoint to provide a unified, synchronous API for registering any entity type (repositories, releases, artifacts, pull requests) in Minder. This change extracts common entity creation logic into a reusable EntityCreator service that is used by both synchronous (RegisterEntity) and asynchronous (webhook-based) entity registration flows. Key changes: - Add RegisterEntity RPC handler with generic entity creation - Create EntityCreator service to unify entity creation logic - Implement pluggable validator framework (RepositoryValidator) - Refactor RepositoryService to use EntityCreator (reduced from ~90 to ~30 lines) - Refactor async entity handler to use EntityCreator - Update proto to use google.protobuf.Struct for type-safe properties - Add comprehensive test coverage (27 new tests) Security improvements: - Input validation for property count (max 100) and key length (max 200) - Context cancellation protection in cleanup operations - Improved error wrapping for better debugging The implementation maintains backward compatibility with existing RegisterRepository RPC while providing a foundation for registering other entity types through a single unified API. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
- Use errors.As pattern for UserVisibleError passthrough in handlers - Convert validator errors to util.UserVisibleError for proper gRPC responses - Reduce cyclomatic complexity in CreateEntity by extracting helpers: - runValidators for validator loop - cleanupProviderRegistration for cleanup logic - Remove unused functions: - upsertLegacyEntity in add_originating_entity.go - pushReconcilerEvent and persistRepository in service.go - Unused test helpers in handler_test.go and service_test.go - Fix unused parameters in test files - Update tests to mock EntityCreator instead of internal services - Update expected error messages to match new UserVisibleError format 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
5760cfa to
b52ed29
Compare
- Use proto.Size() check for identifying_properties validation (more robust than counting properties, catches large values) - Remove CustomValidators from EntityCreationOptions (no planned use case, reduces complexity) - Use ReplaceAllProperties instead of SaveAllProperties (ensures clean slate for entity creation) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
evankanderson
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again for getting back to this. I'll have a little research on whether properties needs to be a Struct and should have an answer tomorrow. I'll also see about guarding the RegisterEntity on existing providers so we can call it unconditionally, which seems like a bit of a nice simplicity win.
| // identifying_properties uniquely identifies the entity in the provider. | ||
| // For example, for a GitHub repository use github/repo_owner and github/repo_name, | ||
| // or use upstream_id to identify by provider's internal ID. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, from generating typescript from the OpenAPI spec, map<string, Struct> makes it very hard to figure out what to put in there unless you go and read the source code. A flatter interface feels more user-friendly if we don't lose anything that we use today.
| // Whether to register with provider (e.g., create webhooks) | ||
| RegisterWithProvider bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think extending the provider interface here (and for validation) is probably the right thing to do. Maybe every provider has a registration hook, but for some it's a no-op?
| RegisterWithProvider: entityType == pb.Entity_ENTITY_REPOSITORIES, | ||
| PublishReconciliationEvent: entityType == pb.Entity_ENTITY_REPOSITORIES, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather call these hooks and then have the providers no-op them. We could even have a "default provider" struct that you can embed that implements these registration methods with a no-op.
| // 6. Register with provider if needed (e.g., create webhook) | ||
| var registeredProps *properties.Properties | ||
| if opts.RegisterWithProvider { | ||
| registeredProps, err = prov.RegisterEntity(ctx, entityType, allProps) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll get to this probably tomorrow at this point. I may also add a "common provider tests" suite that makes it easy to check that these are handled consistently.
| ) error { | ||
| // Only validate repositories | ||
| if entType != pb.Entity_ENTITY_REPOSITORIES { | ||
| return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of a registration interface, e.g.
type Validator interface {
Validate(context.Context, pb.Entity, *properties.Properties, uuid.UUID)
}
func AddValidator(pb.Entity, Validator) handle { ... }
// might not need / only for testing(?)
func RemoveValidator(pb.Entry, handle) { ... }And then we'd reject any entity type which didn't have at least one validator registered, otherwise we'd call the validators for the applicable types.
| // 2. Check if provider supports this entity type | ||
| if !prov.SupportsEntity(entityType) { | ||
| return nil, fmt.Errorf("provider %s does not support entity type %s", | ||
| provider.Name, entityType) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about it, if we move the validation into the provider, SupportsEntity could actually do the validation if we pass extra parameters to it. Right now, we're trying to cram both the GitHub and GitLab registration into a single validator, but I'm wondering if it makes more sense to have a common library that both call (so, for example, if there's another variation where registration doesn't make sense in forgejo, like "mirrored", that could go in the forgejo code rather than needing to extend the core for that case).
| // identifying_properties uniquely identifies the entity in the provider. | ||
| // For example, for a GitHub repository use github/repo_owner and github/repo_name, | ||
| // or use upstream_id to identify by provider's internal ID. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I'm currently checking on whether we put "interesting" struct data into properties. If so, I'd still prefer map<string, Struct> to the current Struct -- we imply that properties: 17.4 would be just peachy.)
Summary
Implements the
RegisterEntitygRPC endpoint to provide a unified, synchronous API for registering any entity type (repositories, releases, artifacts, pull requests) in Minder.This PR extracts common entity creation logic into a reusable
EntityCreatorservice that eliminates code duplication between synchronous and asynchronous entity registration flows.Key Changes
Core Implementation
POST /api/v1/entityinternal/entities/service/entity_creator.go)RepositoryValidatorwith extensible designidentifier_propertyfromstringtogoogle.protobuf.Structfor type safetyRefactoring
Security Improvements
Test Coverage
Added 27 new tests across 5 test files:
entity_creator_simple_test.go- Provider validation tests (4 tests)repository_validator_test.go- Validator logic tests (6 tests)handlers_entity_instances_test.go- RegisterEntity handler tests (12 tests)service_integration_test.go- RepositoryService integration tests (5 tests)All tests passing ✅
Benefits
Backward Compatibility
✅ Fully backward compatible
RegisterRepositoryRPC continues to work unchangedCode Review Notes
Both automated code quality and security reviews were conducted:
🤖 Generated with Claude Code