Commit 250c1855 authored by Sean McGivern's avatar Sean McGivern

Move NewMergeRequestWorker methods to a service

This makes it easier to refactor in future commits.
parent ed76ea7d
# frozen_string_literal: true
module MergeRequests
class AfterCreateService < MergeRequests::BaseService
def execute(merge_request)
event_service.open_mr(merge_request, current_user)
notification_service.new_merge_request(merge_request, current_user)
merge_request.diffs(include_stats: false).write_cache
merge_request.create_cross_references!(current_user)
end
end
end
...@@ -13,15 +13,11 @@ module MergeRequests ...@@ -13,15 +13,11 @@ module MergeRequests
create(merge_request) create(merge_request)
end end
def before_create(merge_request)
# current_user (defined in BaseService) is not available within run_after_commit block
user = current_user
merge_request.run_after_commit do
NewMergeRequestWorker.perform_async(merge_request.id, user.id)
end
end
def after_create(issuable) def after_create(issuable)
# Add new items to MergeRequests::AfterCreateService if they can
# be performed in Sidekiq
NewMergeRequestWorker.perform_async(issuable.id, current_user.id)
todo_service.new_merge_request(issuable, current_user) todo_service.new_merge_request(issuable, current_user)
issuable.cache_merge_request_closes_issues!(current_user) issuable.cache_merge_request_closes_issues!(current_user)
create_pipeline_for(issuable, current_user) create_pipeline_for(issuable, current_user)
......
...@@ -12,11 +12,9 @@ class NewMergeRequestWorker # rubocop:disable Scalability/IdempotentWorker ...@@ -12,11 +12,9 @@ class NewMergeRequestWorker # rubocop:disable Scalability/IdempotentWorker
def perform(merge_request_id, user_id) def perform(merge_request_id, user_id)
return unless objects_found?(merge_request_id, user_id) return unless objects_found?(merge_request_id, user_id)
EventCreateService.new.open_mr(issuable, user) MergeRequests::AfterCreateService
NotificationService.new.new_merge_request(issuable, user) .new(issuable.target_project, user)
.execute(issuable)
issuable.diffs(include_stats: false).write_cache
issuable.create_cross_references!(user)
end end
def issuable_class def issuable_class
......
# frozen_string_literal: true
require 'spec_helper'
describe MergeRequests::AfterCreateService do
let_it_be(:merge_request) { create(:merge_request) }
subject(:after_create_service) do
described_class.new(merge_request.target_project, merge_request.author)
end
describe '#execute' do
let(:event_service) { instance_double('EventCreateService', open_mr: true) }
let(:notification_service) { instance_double('NotificationService', new_merge_request: true) }
before do
allow(after_create_service).to receive(:event_service).and_return(event_service)
allow(after_create_service).to receive(:notification_service).and_return(notification_service)
end
it 'creates a merge request open event' do
expect(event_service).to receive(:open_mr).with(merge_request, merge_request.author)
after_create_service.execute(merge_request)
end
it 'creates a new merge request notification' do
expect(notification_service).to receive(:new_merge_request).with(merge_request, merge_request.author)
after_create_service.execute(merge_request)
end
it 'writes diffs to the cache' do
expect(merge_request).to receive_message_chain(:diffs, :write_cache)
after_create_service.execute(merge_request)
end
it 'creates cross references' do
expect(merge_request).to receive(:create_cross_references!).with(merge_request.author)
after_create_service.execute(merge_request)
end
end
end
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