Add service to perform some of the issue after creation tasks

parent 55456542
# frozen_string_literal: true
module Issues
class AfterCreateService < Issues::BaseService
def execute(issue)
todo_service.new_issue(issue, current_user)
delete_milestone_total_issue_counter_cache(issue.milestone)
track_incident_action(current_user, issue, :incident_created)
end
end
end
Issues::AfterCreateService.prepend_ee_mod
# frozen_string_literal: true
module EE
module Issues
module AfterCreateService
extend ::Gitlab::Utils::Override
override :execute
def execute(issue)
super
add_issue_sla(issue)
end
private
def add_issue_sla(issue)
return unless issue.sla_available?
::IncidentManagement::Incidents::CreateSlaService.new(issue, current_user).execute
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Issues::AfterCreateService do
include AfterNextHelpers
let_it_be(:project) { create(:project) }
let_it_be(:current_user) { create(:user) }
let_it_be(:issue) { create(:issue, project: project, author: current_user) }
subject(:after_create_service) { described_class.new(project, current_user) }
describe '#execute' do
context 'when issue sla is available' do
it 'calls IncidentManagement::Incidents::CreateSlaService' do
allow(issue).to receive(:sla_available?).and_return(true)
expect_next(::IncidentManagement::Incidents::CreateSlaService, issue, current_user)
.to receive(:execute)
after_create_service.execute(issue)
end
end
context 'when issue sla is not available' do
it 'does not call IncidentManagement::Incidents::CreateSlaService' do
allow(issue).to receive(:sla_available?).and_return(false)
expect(::IncidentManagement::Incidents::CreateSlaService)
.not_to receive(:new)
after_create_service.execute(issue)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Issues::AfterCreateService do
include AfterNextHelpers
let_it_be(:project) { create(:project) }
let_it_be(:current_user) { create(:user) }
let_it_be(:assignee) { create(:user) }
let_it_be(:milestone) { create(:milestone, project: project) }
let_it_be(:issue) { create(:issue, project: project, author: current_user, milestone: milestone, assignee_ids: [assignee.id]) }
subject(:after_create_service) { described_class.new(project, current_user) }
describe '#execute' do
it 'creates a pending todo for new assignee' do
attributes = {
project: project,
author: current_user,
user: assignee,
target_id: issue.id,
target_type: issue.class.name,
action: Todo::ASSIGNED,
state: :pending
}
expect { after_create_service.execute(issue) }.to change { Todo.where(attributes).count }.by(1)
end
it 'deletes milestone issues count cache' do
expect_next(Milestones::IssuesCountService, milestone)
.to receive(:delete_cache).and_call_original
after_create_service.execute(issue)
end
context 'with a regular issue' do
it_behaves_like 'does not track incident management event', :incident_management_incident_created do
subject { after_create_service.execute(issue) }
end
end
context 'with an incident issue' do
let(:issue) { create(:issue, :incident, project: project, author: current_user) }
it_behaves_like 'an incident management tracked event', :incident_management_incident_created do
subject { after_create_service.execute(issue) }
end
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