Sync default tracked context when default branch changes
What does this MR do?
This MR implements functionality to keep the default tracked context name synchronized with the project's default branch name.
Closes #577338
Implementation Details
This implementation addresses the issue where projects can be created without a repository, and the repository added later, causing the default tracked context name to not match the actual default branch name.
Changes Made
-
New Event:
Repositories::RepositoryCreatedEvent- Created at
app/events/repositories/repository_created_event.rb - Schema includes
container_idandcontainer_type(similar toDefaultBranchChangedEvent) - Published when a repository is created for a project
- Created at
-
New Service:
Security::ProjectTrackedContexts::UpdateDefaultService- Created at
ee/app/services/security/project_tracked_contexts/update_default_service.rb - Finds or initializes the default tracked context for a project
- Updates the context name to match the current default branch
- Handles edge cases (no default branch, context already matches, etc.)
- Created at
-
New Worker:
Security::ProjectTrackedContexts::UpdateDefaultWorker- Created at
ee/app/workers/security/project_tracked_contexts/update_default_worker.rb - EventStore subscriber for both
Repositories::DefaultBranchChangedEventandRepositories::RepositoryCreatedEvent - Calls
UpdateDefaultServiceto synchronize the default context - Logs errors if the service fails
- Created at
-
Updated
HasRepositoryconcern- Added
after_create_repositorymethod to publishRepositoryCreatedEvent - Called by
Repository#after_createwhen a repository is created
- Added
-
Updated
Repositorymodel- Modified
after_createmethod to callcontainer.after_create_repositoryif the container responds to it - Ensures event is published whenever a repository is created, regardless of code path
- Works for any container type (Project, Snippet, etc.)
- Modified
-
Updated EventStore subscriptions
- Subscribed
UpdateDefaultWorkerto bothDefaultBranchChangedEventandRepositoryCreatedEvent - Located in
ee/lib/ee/gitlab/event_store.rb
- Subscribed
Test Coverage
- Unit tests for
UpdateDefaultService - Unit tests for
UpdateDefaultWorker - Unit tests for
RepositoryCreatedEvent - Tests cover various scenarios:
- Creating new default context
- Updating existing context with different name
- Handling missing default branch
- Validation errors
- Event handling for both event types
How to test
- Create a new project without a repository
- Verify a default tracked context is created with the expected default branch name
- Add a repository to the project
- Verify the default tracked context name is updated to match the actual default branch
- Change the default branch of a project
- Verify the default tracked context name is updated accordingly
Database migrations
No database migrations required - uses existing security_project_tracked_contexts table.
Checklist
- Code changes
- Tests added
- Documentation not required (internal implementation)
- Changelog entry added (included in commit message)
- EE-specific changes properly marked
Edited by Schmil Monderer