Commit ec340bff authored by Luke Duncalfe's avatar Luke Duncalfe

Merge branch 'sy-migrate-oncall-services-to-project-service' into 'master'

Swap OncallSchedule services to inherit from BaseProjectService

See merge request gitlab-org/gitlab!78317
parents d822a30a 9729ea36
......@@ -28,9 +28,9 @@ module Mutations
project = authorized_find!(args[:project_path])
response ::IncidentManagement::OncallSchedules::CreateService.new(
project,
current_user,
args.slice(:name, :description, :timezone)
project: project,
current_user: current_user,
params: args.slice(:name, :description, :timezone)
).execute
end
end
......
......@@ -18,9 +18,9 @@ module Mutations
oncall_schedule = authorized_find!(project_path: project_path, iid: iid)
response ::IncidentManagement::OncallSchedules::DestroyService.new(
oncall_schedule,
current_user
).execute
project: oncall_schedule.project,
current_user: current_user
).execute(oncall_schedule)
end
end
end
......
......@@ -30,10 +30,10 @@ module Mutations
oncall_schedule = authorized_find!(project_path: args[:project_path], iid: args[:iid])
response ::IncidentManagement::OncallSchedules::UpdateService.new(
oncall_schedule,
current_user,
args.slice(:name, :description, :timezone)
).execute
project: oncall_schedule.project,
current_user: current_user,
params: args.slice(:name, :description, :timezone)
).execute(oncall_schedule)
end
end
end
......
......@@ -2,9 +2,9 @@
module IncidentManagement
module OncallSchedules
class BaseService
class BaseService < ::BaseProjectService
def allowed?
user&.can?(:admin_incident_management_oncall_schedule, project)
current_user&.can?(:admin_incident_management_oncall_schedule, project)
end
def available?
......
......@@ -3,15 +3,6 @@
module IncidentManagement
module OncallSchedules
class CreateService < OncallSchedules::BaseService
# @param project [Project]
# @param user [User]
# @param params [Hash]
def initialize(project, user, params)
@project = project
@user = user
@params = params
end
def execute
return error_no_license unless available?
return error_no_permissions unless allowed?
......@@ -24,8 +15,6 @@ module IncidentManagement
private
attr_reader :project, :user, :params
def error_no_permissions
error(_('You have insufficient permissions to create an on-call schedule for this project'))
end
......
......@@ -3,15 +3,7 @@
module IncidentManagement
module OncallSchedules
class DestroyService < OncallSchedules::BaseService
# @param oncall_schedule [IncidentManagement::OncallSchedule]
# @param user [User]
def initialize(oncall_schedule, user)
@oncall_schedule = oncall_schedule
@user = user
@project = oncall_schedule.project
end
def execute
def execute(oncall_schedule)
return error_no_license unless available?
return error_no_permissions unless allowed?
......@@ -24,8 +16,6 @@ module IncidentManagement
private
attr_reader :oncall_schedule, :user, :project
def error_no_permissions
error(_('You have insufficient permissions to remove an on-call schedule from this project'))
end
......
......@@ -3,24 +3,13 @@
module IncidentManagement
module OncallSchedules
class UpdateService < OncallSchedules::BaseService
# @param oncall_schedule [IncidentManagement::OncallSchedule]
# @param user [User]
# @param params [Hash]
def initialize(oncall_schedule, user, params)
@oncall_schedule = oncall_schedule
@original_schedule_timezone = oncall_schedule.timezone
@user = user
@params = params
@project = oncall_schedule.project
end
def execute
def execute(oncall_schedule)
return error_no_license unless available?
return error_no_permissions unless allowed?
IncidentManagement::OncallSchedule.transaction do
oncall_schedule.update!(params)
update_rotations!
update_rotations!(oncall_schedule)
end
success(oncall_schedule)
......@@ -32,22 +21,16 @@ module IncidentManagement
private
attr_reader :oncall_schedule, :original_schedule_timezone, :user, :params, :project
def update_rotations!
return if same_schedule_timezone?
update_rotation_active_periods!
end
def update_rotations!(oncall_schedule)
return unless oncall_schedule.timezone_previously_changed?
def same_schedule_timezone?
original_schedule_timezone == oncall_schedule.timezone
update_rotation_active_periods!(oncall_schedule)
end
# Converts & updates the active period to the new timezone
# Ex: 8:00 - 17:00 Europe/Berlin becomes 6:00 - 15:00 UTC
def update_rotation_active_periods!
original_schedule_current_time = Time.current.in_time_zone(original_schedule_timezone)
def update_rotation_active_periods!(oncall_schedule)
original_schedule_current_time = Time.current.in_time_zone(oncall_schedule.timezone_previously_was)
oncall_schedule.rotations.with_active_period.each do |rotation|
active_period = rotation.active_period.for_date(original_schedule_current_time)
......@@ -55,7 +38,7 @@ module IncidentManagement
service = IncidentManagement::OncallRotations::EditService.new(
rotation,
user,
current_user,
{
active_period_start: new_start_time,
active_period_end: new_end_time
......
......@@ -9,7 +9,7 @@ RSpec.describe IncidentManagement::OncallSchedules::CreateService do
let(:current_user) { user_with_permissions }
let(:params) { { name: 'On-call schedule', description: 'On-call schedule description', timezone: 'Europe/Berlin' } }
let(:service) { described_class.new(project, current_user, params) }
let(:service) { described_class.new(project: project, current_user: current_user, params: params) }
before do
stub_licensed_features(oncall_schedules: true)
......
......@@ -10,7 +10,7 @@ RSpec.describe IncidentManagement::OncallSchedules::DestroyService do
let!(:oncall_schedule) { create(:incident_management_oncall_schedule, project: project) }
let(:current_user) { user_with_permissions }
let(:params) { {} }
let(:service) { described_class.new(oncall_schedule, current_user) }
let(:service) { described_class.new(project: project, current_user: current_user) }
before do
stub_licensed_features(oncall_schedules: true)
......@@ -25,7 +25,7 @@ RSpec.describe IncidentManagement::OncallSchedules::DestroyService do
end
end
subject(:execute) { service.execute }
subject(:execute) { service.execute(oncall_schedule) }
context 'when the current_user is anonymous' do
let(:current_user) { nil }
......
......@@ -11,7 +11,7 @@ RSpec.describe IncidentManagement::OncallSchedules::UpdateService do
let(:current_user) { user_with_permissions }
let(:new_timezone) { 'America/New_York' }
let(:params) { { name: 'Updated name', description: 'Updated description', timezone: new_timezone } }
let(:service) { described_class.new(oncall_schedule, current_user, params) }
let(:service) { described_class.new(project: project, current_user: current_user, params: params) }
before do
stub_licensed_features(oncall_schedules: true)
......@@ -26,7 +26,7 @@ RSpec.describe IncidentManagement::OncallSchedules::UpdateService do
end
end
subject(:execute) { service.execute }
subject(:execute) { service.execute(oncall_schedule) }
context 'when the current_user is anonymous' do
let(:current_user) { nil }
......
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