Commit 35383186 authored by Sean Arnold's avatar Sean Arnold

Move common methods into base services

oncall schedules and rotations
parent 92757043
# frozen_string_literal: true
module IncidentManagement
module OncallRotations
class BaseService
def allowed?
user&.can?(:admin_incident_management_oncall_schedule, project)
end
def available?
::Gitlab::IncidentManagement.oncall_schedules_available?(project)
end
def error(message)
ServiceResponse.error(message: message)
end
def success(oncall_rotation)
ServiceResponse.success(payload: { oncall_rotation: oncall_rotation })
end
def error_no_license
error(_('Your license does not support on-call rotations'))
end
end
end
end
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
module IncidentManagement module IncidentManagement
module OncallRotations module OncallRotations
class CreateService class CreateService < BaseService
MAXIMUM_PARTICIPANTS = 100 MAXIMUM_PARTICIPANTS = 100
# @param schedule [IncidentManagement::OncallSchedule] # @param schedule [IncidentManagement::OncallSchedule]
# @param project [Project] # @param project [Project]
# @param current_user [User] # @param user [User]
# @param params [Hash<Symbol,Any>] # @param params [Hash<Symbol,Any>]
# @param params - name [String] The name of the on-call rotation. # @param params - name [String] The name of the on-call rotation.
# @param params - length [Integer] The length of the rotation. # @param params - length [Integer] The length of the rotation.
...@@ -17,10 +17,10 @@ module IncidentManagement ...@@ -17,10 +17,10 @@ module IncidentManagement
# @option opts - participant [User] The user who is part of the rotation # @option opts - participant [User] The user who is part of the rotation
# @option opts - color_palette [String] The color palette to assign to the on-call user, for example: "blue". # @option opts - color_palette [String] The color palette to assign to the on-call user, for example: "blue".
# @option opts - color_weight [String] The color weight to assign to for the on-call user, for example "500". Max 4 chars. # @option opts - color_weight [String] The color weight to assign to for the on-call user, for example "500". Max 4 chars.
def initialize(schedule, project, current_user, params) def initialize(schedule, project, user, params)
@schedule = schedule @schedule = schedule
@project = project @project = project
@current_user = current_user @user = user
@rotation_params = params.except(:participants) @rotation_params = params.except(:participants)
@participants_params = Array(params[:participants]) @participants_params = Array(params[:participants])
end end
...@@ -49,15 +49,7 @@ module IncidentManagement ...@@ -49,15 +49,7 @@ module IncidentManagement
private private
attr_reader :schedule, :project, :current_user, :rotation_params, :participants_params attr_reader :schedule, :project, :user, :rotation_params, :participants_params
def allowed?
Ability.allowed?(current_user, :admin_incident_management_oncall_schedule, project)
end
def available?
::Gitlab::IncidentManagement.oncall_schedules_available?(project)
end
def duplicated_users? def duplicated_users?
users = participants_params.map { |participant| participant[:user] } users = participants_params.map { |participant| participant[:user] }
...@@ -98,14 +90,6 @@ module IncidentManagement ...@@ -98,14 +90,6 @@ module IncidentManagement
OncallParticipant.insert_all(participant_rows(participants)) OncallParticipant.insert_all(participant_rows(participants))
end end
def error(message)
ServiceResponse.error(message: message)
end
def success(oncall_rotation)
ServiceResponse.success(payload: { oncall_rotation: oncall_rotation })
end
def error_participant_has_no_permission def error_participant_has_no_permission
error('A participant has insufficient permissions to access the project') error('A participant has insufficient permissions to access the project')
end end
...@@ -122,10 +106,6 @@ module IncidentManagement ...@@ -122,10 +106,6 @@ module IncidentManagement
error(_('You have insufficient permissions to create an on-call rotation for this project')) error(_('You have insufficient permissions to create an on-call rotation for this project'))
end end
def error_no_license
error(_('Your license does not support on-call rotations'))
end
def error_in_validation(object) def error_in_validation(object)
error(object.errors.full_messages.to_sentence) error(object.errors.full_messages.to_sentence)
end end
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module IncidentManagement module IncidentManagement
module OncallRotations module OncallRotations
class DestroyService class DestroyService < BaseService
# @param oncall_schedule [IncidentManagement::OncallRotation] # @param oncall_schedule [IncidentManagement::OncallRotation]
# @param user [User] # @param user [User]
def initialize(oncall_rotation, user) def initialize(oncall_rotation, user)
...@@ -16,7 +16,7 @@ module IncidentManagement ...@@ -16,7 +16,7 @@ module IncidentManagement
return error_no_permissions unless allowed? return error_no_permissions unless allowed?
if oncall_rotation.destroy if oncall_rotation.destroy
success success(oncall_rotation)
else else
error(oncall_rotation.errors.full_messages.to_sentence) error(oncall_rotation.errors.full_messages.to_sentence)
end end
...@@ -26,29 +26,9 @@ module IncidentManagement ...@@ -26,29 +26,9 @@ module IncidentManagement
attr_reader :oncall_rotation, :user, :project attr_reader :oncall_rotation, :user, :project
def allowed?
user&.can?(:admin_incident_management_oncall_schedule, project)
end
def available?
::Gitlab::IncidentManagement.oncall_schedules_available?(project)
end
def error(message)
ServiceResponse.error(message: message)
end
def success
ServiceResponse.success(payload: { oncall_rotation: oncall_rotation })
end
def error_no_permissions def error_no_permissions
error(_('You have insufficient permissions to remove an on-call rotation from this project')) error(_('You have insufficient permissions to remove an on-call rotation from this project'))
end end
def error_no_license
error(_('Your license does not support on-call rotations'))
end
end end
end end
end end
# frozen_string_literal: true
module IncidentManagement
module OncallSchedules
class BaseService
def allowed?
user&.can?(:admin_incident_management_oncall_schedule, project)
end
def available?
::Gitlab::IncidentManagement.oncall_schedules_available?(project)
end
def error(message)
ServiceResponse.error(message: message)
end
def success(oncall_schedule)
ServiceResponse.success(payload: { oncall_schedule: oncall_schedule })
end
def error_no_license
error(_('Your license does not support on-call schedules'))
end
end
end
end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module IncidentManagement module IncidentManagement
module OncallSchedules module OncallSchedules
class CreateService class CreateService < BaseService
# @param project [Project] # @param project [Project]
# @param user [User] # @param user [User]
# @param params [Hash] # @param params [Hash]
...@@ -26,30 +26,10 @@ module IncidentManagement ...@@ -26,30 +26,10 @@ module IncidentManagement
attr_reader :project, :user, :params attr_reader :project, :user, :params
def allowed?
user&.can?(:admin_incident_management_oncall_schedule, project)
end
def available?
::Gitlab::IncidentManagement.oncall_schedules_available?(project)
end
def error(message)
ServiceResponse.error(message: message)
end
def success(oncall_schedule)
ServiceResponse.success(payload: { oncall_schedule: oncall_schedule })
end
def error_no_permissions def error_no_permissions
error(_('You have insufficient permissions to create an on-call schedule for this project')) error(_('You have insufficient permissions to create an on-call schedule for this project'))
end end
def error_no_license
error(_('Your license does not support on-call schedules'))
end
def error_in_create(oncall_schedule) def error_in_create(oncall_schedule)
error(oncall_schedule.errors.full_messages.to_sentence) error(oncall_schedule.errors.full_messages.to_sentence)
end end
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module IncidentManagement module IncidentManagement
module OncallSchedules module OncallSchedules
class DestroyService class DestroyService < BaseService
# @param oncall_schedule [IncidentManagement::OncallSchedule] # @param oncall_schedule [IncidentManagement::OncallSchedule]
# @param user [User] # @param user [User]
def initialize(oncall_schedule, user) def initialize(oncall_schedule, user)
...@@ -16,7 +16,7 @@ module IncidentManagement ...@@ -16,7 +16,7 @@ module IncidentManagement
return error_no_permissions unless allowed? return error_no_permissions unless allowed?
if oncall_schedule.destroy if oncall_schedule.destroy
success success(oncall_schedule)
else else
error(oncall_schedule.errors.full_messages.to_sentence) error(oncall_schedule.errors.full_messages.to_sentence)
end end
...@@ -26,29 +26,9 @@ module IncidentManagement ...@@ -26,29 +26,9 @@ module IncidentManagement
attr_reader :oncall_schedule, :user, :project attr_reader :oncall_schedule, :user, :project
def allowed?
user&.can?(:admin_incident_management_oncall_schedule, project)
end
def available?
::Gitlab::IncidentManagement.oncall_schedules_available?(project)
end
def error(message)
ServiceResponse.error(message: message)
end
def success
ServiceResponse.success(payload: { oncall_schedule: oncall_schedule })
end
def error_no_permissions def error_no_permissions
error(_('You have insufficient permissions to remove an on-call schedule from this project')) error(_('You have insufficient permissions to remove an on-call schedule from this project'))
end end
def error_no_license
error(_('Your license does not support on-call schedules'))
end
end end
end end
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module IncidentManagement module IncidentManagement
module OncallSchedules module OncallSchedules
class UpdateService class UpdateService < BaseService
# @param oncall_schedule [IncidentManagement::OncallSchedule] # @param oncall_schedule [IncidentManagement::OncallSchedule]
# @param user [User] # @param user [User]
# @param params [Hash] # @param params [Hash]
...@@ -28,29 +28,9 @@ module IncidentManagement ...@@ -28,29 +28,9 @@ module IncidentManagement
attr_reader :oncall_schedule, :user, :params, :project attr_reader :oncall_schedule, :user, :params, :project
def allowed?
user&.can?(:admin_incident_management_oncall_schedule, project)
end
def available?
::Gitlab::IncidentManagement.oncall_schedules_available?(project)
end
def error(message)
ServiceResponse.error(message: message)
end
def success(oncall_schedule)
ServiceResponse.success(payload: { oncall_schedule: oncall_schedule })
end
def error_no_permissions def error_no_permissions
error(_('You have insufficient permissions to update an on-call schedule for this project')) error(_('You have insufficient permissions to update an on-call schedule for this project'))
end end
def error_no_license
error(_('Your license does not support on-call schedules'))
end
end end
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