Commit 998bffc4 authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak

Merge branch '346169_batch_delete_events' into 'master'

Delete project events before the project

See merge request gitlab-org/gitlab!76465
parents df1b8200 eb8bb098
# frozen_string_literal: true
module Events
class DestroyService
def initialize(project)
@project = project
end
def execute
project.events.all.delete_all
ServiceResponse.success(message: 'Events were deleted.')
rescue StandardError
ServiceResponse.error(message: 'Failed to remove events.')
end
private
attr_reader :project
end
end
......@@ -75,6 +75,18 @@ module Projects
response.success?
end
def destroy_events!
unless remove_events
raise_error(s_('DeleteProject|Failed to remove events. Please try again or contact administrator.'))
end
end
def remove_events
response = ::Events::DestroyService.new(project).execute
response.success?
end
def remove_repository(repository)
return true unless repository
......@@ -117,6 +129,7 @@ module Projects
log_destroy_event
trash_relation_repositories!
trash_project_repositories!
destroy_events!
destroy_web_hooks!
destroy_project_bots!
......
......@@ -11410,6 +11410,9 @@ msgstr ""
msgid "Delete variable"
msgstr ""
msgid "DeleteProject|Failed to remove events. Please try again or contact administrator."
msgstr ""
msgid "DeleteProject|Failed to remove project repository. Please try again or contact administrator."
msgstr ""
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Events::DestroyService do
subject(:service) { described_class.new(project) }
let_it_be(:project, reload: true) { create(:project, :repository) }
let_it_be(:another_project) { create(:project) }
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
let_it_be(:user) { create(:user) }
let!(:unrelated_event) { create(:event, :merged, project: another_project, target: another_project, author: user) }
before do
create(:event, :created, project: project, target: project, author: user)
create(:event, :created, project: project, target: merge_request, author: user)
create(:event, :merged, project: project, target: merge_request, author: user)
end
let(:events) { project.events }
describe '#execute', :aggregate_failures do
it 'deletes the events' do
response = nil
expect { response = subject.execute }.to change(Event, :count).by(-3)
expect(response).to be_success
expect(unrelated_event.reload).to be_present
end
context 'when an error is raised while deleting the records' do
before do
allow(project).to receive_message_chain(:events, :all, :delete_all).and_raise(ActiveRecord::ActiveRecordError)
end
it 'returns error' do
response = subject.execute
expect(response).to be_error
expect(response.message).to eq 'Failed to remove events.'
end
it 'does not delete events' do
expect { subject.execute }.not_to change(Event, :count)
end
end
end
end
......@@ -545,6 +545,27 @@ RSpec.describe Projects::DestroyService, :aggregate_failures do
end
end
context 'when project has events' do
let!(:event) { create(:event, :created, project: project, target: project, author: user) }
it 'deletes events from the project' do
expect do
destroy_project(project, user)
end.to change(Event, :count).by(-1)
end
context 'when an error is returned while deleting events' do
it 'does not delete project' do
allow_next_instance_of(Events::DestroyService) do |instance|
allow(instance).to receive(:execute).and_return(ServiceResponse.error(message: 'foo'))
end
expect(destroy_project(project, user)).to be_falsey
expect(project.delete_error).to include('Failed to remove events')
end
end
end
context 'error while destroying', :sidekiq_inline do
let!(:pipeline) { create(:ci_pipeline, project: project) }
let!(:builds) { create_list(:ci_build, 2, :artifacts, pipeline: pipeline) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment