fix: Move call to `AiRelatedSettingsChangedEvent`

What does this MR do and why?

This MR fixes a potential issue with the AiRelatedSettingsChangedEvent being published prematurely. Previously, the event was being emitted from NamespaceSettings::AssignAttributesService which only assigns attributes but doesn't persist them. This could lead to events being published for changes that weren't actually saved to the database.

Changes made:

  1. Moved event publishing from NamespaceSettings::AssignAttributesService to Groups::UpdateService's after_update callback
  2. Updated the logic to check for actual persisted changes using saved_change_to_attribute?
  3. Added comprehensive specs to verify event publishing behavior

This ensures that events are only published when:

  • The update is successful
  • There are actual persisted changes to AI-related settings
  • The namespace settings exist

References

How to set up and validate locally

  1. Start GDK:

    gdk start
  2. In rails console, set up a test group:

    # Create organization
    organization = Organizations::Organization.create!(
      name: 'Test Organization',
      path: 'test-organization'
    )
    
    # Create group
    user = User.first
    group = Group.create!(
      name: 'test-group',
      path: 'test-group',
      visibility_level: Gitlab::VisibilityLevel::PRIVATE,
      organization: organization
    )
    group.add_owner(user)
  3. Test the AI settings change:

    # Update settings
    service = Groups::UpdateService.new(group, user, { experiment_features_enabled: true })
    result = service.execute
    
    # Verify change
    group.reload.namespace_settings.experiment_features_enabled # Should be true
  4. Monitor logs to verify event publishing:

    gdk tail | grep AiRelatedSettingsChangedEvent

MR acceptance checklist

  • The change is isolated to the event publishing logic
  • Added comprehensive specs for success and failure cases
  • No changes to database schema
  • No changes to API endpoints
  • Event is only published after successful updates
Edited by Yashika Jotwani

Merge request reports

Loading