[go: up one dir, main page]

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

  1. New Event: Repositories::RepositoryCreatedEvent

    • Created at app/events/repositories/repository_created_event.rb
    • Schema includes container_id and container_type (similar to DefaultBranchChangedEvent)
    • Published when a repository is created for a project
  2. 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.)
  3. New Worker: Security::ProjectTrackedContexts::UpdateDefaultWorker

    • Created at ee/app/workers/security/project_tracked_contexts/update_default_worker.rb
    • EventStore subscriber for both Repositories::DefaultBranchChangedEvent and Repositories::RepositoryCreatedEvent
    • Calls UpdateDefaultService to synchronize the default context
    • Logs errors if the service fails
  4. Updated HasRepository concern

    • Added after_create_repository method to publish RepositoryCreatedEvent
    • Called by Repository#after_create when a repository is created
  5. Updated Repository model

    • Modified after_create method to call container.after_create_repository if 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.)
  6. Updated EventStore subscriptions

    • Subscribed UpdateDefaultWorker to both DefaultBranchChangedEvent and RepositoryCreatedEvent
    • Located in ee/lib/ee/gitlab/event_store.rb

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

  1. Create a new project without a repository
  2. Verify a default tracked context is created with the expected default branch name
  3. Add a repository to the project
  4. Verify the default tracked context name is updated to match the actual default branch
  5. Change the default branch of a project
  6. 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

Merge request reports

Loading