diff --git a/ee/lib/audit/protected_branches_changes_auditor.rb b/ee/lib/audit/protected_branches_changes_auditor.rb index 6563a35939ccb71e2308282c848800b06546fc97..064bae0ebc4eecddfee115b7cb6ef81aafc08ba0 100644 --- a/ee/lib/audit/protected_branches_changes_auditor.rb +++ b/ee/lib/audit/protected_branches_changes_auditor.rb @@ -9,10 +9,18 @@ def initialize(current_user, model, old_merge_access_levels, old_push_access_lev end def execute - audit_changes(:allow_force_push, as: 'allow force push', entity: model.project, - model: model, event_type: event_type) - audit_changes(:code_owner_approval_required, as: 'code owner approval required', - entity: model.project, model: model, event_type: event_type) + audit_changes( + :allow_force_push, + as: 'allow force push', + entity: model.project, + model: model, event_type: 'protected_branch_allow_force_push_updated' + ) + audit_changes( + :code_owner_approval_required, + as: 'code owner approval required', + entity: model.project, model: model, + event_type: 'protected_branch_code_owner_approval_required_updated' + ) audit_access_levels end @@ -32,7 +40,7 @@ def audit_access_levels scope: model.project, target: model, message: "Changed #{change} from #{from} to #{to}", - name: event_type, + name: 'protected_branch_updated', additional_details: { change: change, from: from, @@ -53,11 +61,5 @@ def attributes_from_auditable_model(column) to: new } end - - private - - def event_type - 'protected_branch_updated' - end end end diff --git a/ee/spec/lib/audit/protected_branches_changes_auditor_spec.rb b/ee/spec/lib/audit/protected_branches_changes_auditor_spec.rb index d142ab8ba47720c4ad49970b1d000926d6d60a59..af60093f32179d11d508257bb0f572f6b6047fb6 100644 --- a/ee/spec/lib/audit/protected_branches_changes_auditor_spec.rb +++ b/ee/spec/lib/audit/protected_branches_changes_auditor_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Audit::ProtectedBranchesChangesAuditor, :request_store do +RSpec.describe Audit::ProtectedBranchesChangesAuditor, :request_store, feature_category: :audit_events do let_it_be(:author) { create(:user, :with_sign_ins) } let_it_be(:user) { create(:user, :with_sign_ins) } let_it_be(:group) { create(:group) } @@ -33,14 +33,14 @@ subject(:service) { described_class.new(author, protected_branch, old_merge_access_levels, old_push_access_levels) } - shared_examples 'settings' do |setting| + shared_examples 'settings' do |setting, expected_event_type| context "when #{setting} changed" do + change_text = setting.to_s.humanize(capitalize: false) it 'creates an event' do protected_branch.update_attribute(setting, true) expect { service.execute }.to change(AuditEvent, :count).by(1) event = AuditEvent.last - change_text = setting.to_s.humanize(capitalize: false) expect(event.details).to eq({ change: change_text, author_name: author.name, author_class: author.class.name, @@ -62,7 +62,7 @@ it 'streams correct audit event stream' do protected_branch.update_attribute(setting, true) - expect(AuditEvents::AuditEventStreamingWorker).to receive(:perform_async).with('protected_branch_updated', + expect(AuditEvents::AuditEventStreamingWorker).to receive(:perform_async).with(expected_event_type, anything, anything) @@ -71,8 +71,8 @@ end end - include_examples 'settings', :allow_force_push - include_examples 'settings', :code_owner_approval_required + include_examples 'settings', :allow_force_push, 'protected_branch_allow_force_push_updated' + include_examples 'settings', :code_owner_approval_required, 'protected_branch_code_owner_approval_required_updated' where(:type, :old_access_levels, :new_access_levels, :change_text) do :push | ref(:old_push_access_levels) | ref(:new_push_access_levels) | 'allowed to push'