Commit 04f70599 authored by Sean Arnold's avatar Sean Arnold

Merge branch 'sy-reduce-policy-setting-complexity' into 'master'

Clean up IssuableEscalationStatuses::PrepareUpdateService

See merge request gitlab-org/gitlab!78507
parents e5e52996 fa9d19b2
...@@ -2,18 +2,16 @@ ...@@ -2,18 +2,16 @@
module IncidentManagement module IncidentManagement
module IssuableEscalationStatuses module IssuableEscalationStatuses
class PrepareUpdateService class PrepareUpdateService < ::BaseProjectService
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
SUPPORTED_PARAMS = %i[status status_change_reason].freeze SUPPORTED_PARAMS = %i[status status_change_reason].freeze
InvalidParamError = Class.new(StandardError)
def initialize(issuable, current_user, params) def initialize(issuable, current_user, params)
@issuable = issuable @issuable = issuable
@current_user = current_user @param_errors = []
@params = params.dup || {}
@project = issuable.project super(project: issuable.project, current_user: current_user, params: Hash(params))
end end
def execute def execute
...@@ -23,14 +21,14 @@ module IncidentManagement ...@@ -23,14 +21,14 @@ module IncidentManagement
filter_attributes filter_attributes
filter_redundant_params filter_redundant_params
return invalid_param_error if param_errors.any?
ServiceResponse.success(payload: { escalation_status: params }) ServiceResponse.success(payload: { escalation_status: params })
rescue InvalidParamError
invalid_param_error
end end
private private
attr_reader :issuable, :current_user, :params, :project attr_reader :issuable, :param_errors
def available? def available?
issuable.supports_escalation? && issuable.supports_escalation? &&
...@@ -65,7 +63,7 @@ module IncidentManagement ...@@ -65,7 +63,7 @@ module IncidentManagement
return unless status return unless status
status_event = escalation_status.status_event_for(status) status_event = escalation_status.status_event_for(status)
raise InvalidParamError unless status_event add_param_error(:status) && return unless status_event
params[:status_event] = status_event params[:status_event] = status_event
end end
...@@ -84,12 +82,16 @@ module IncidentManagement ...@@ -84,12 +82,16 @@ module IncidentManagement
end end
end end
def add_param_error(param)
param_errors << param
end
def availability_error def availability_error
ServiceResponse.error(message: 'Escalation status updates are not available for this issue, user, or project.') ServiceResponse.error(message: 'Escalation status updates are not available for this issue, user, or project.')
end end
def invalid_param_error def invalid_param_error
ServiceResponse.error(message: 'Invalid value was provided for a parameter.') ServiceResponse.error(message: "Invalid value was provided for parameters: #{param_errors.join(', ')}")
end end
end end
end end
......
...@@ -21,23 +21,35 @@ module EE ...@@ -21,23 +21,35 @@ module EE
end end
def filter_policy def filter_policy
return unless params.include?(:policy)
policy = params.delete(:policy) policy = params.delete(:policy)
return unless policies_permitted?
policy ? set_policy(policy) : unset_policy
end
return unless ::Gitlab::IncidentManagement.escalation_policies_available?(project) def policies_permitted?
return if issuable.alert_management_alert # Cannot change the policy for an alert ::Gitlab::IncidentManagement.escalation_policies_available?(project) &&
issuable.alert_management_alert.nil? # Cannot change the policy for an alert
end
if policy def set_policy(policy)
return if policy.id == escalation_status.policy_id return if policy.id == escalation_status.policy_id
if policy.project_id != issuable.project_id
raise ::IncidentManagement::IssuableEscalationStatuses::PrepareUpdateService::InvalidParamError
end
# Override any provided status if setting new policy unless policy.project_id == issuable.project_id
params[:status_event] = :trigger add_param_error(:policy)
return
end end
params[:policy] = policy params[:policy] = policy
params[:escalations_started_at] = policy ? Time.current : nil params[:escalations_started_at] = Time.current
params[:status_event] = :trigger # Override any provided status if setting new policy
end
def unset_policy
params[:policy] = nil
params[:escalations_started_at] = nil
end end
override :current_params override :current_params
......
...@@ -67,7 +67,7 @@ RSpec.describe IncidentManagement::IssuableEscalationStatuses::PrepareUpdateServ ...@@ -67,7 +67,7 @@ RSpec.describe IncidentManagement::IssuableEscalationStatuses::PrepareUpdateServ
it 'returns an error response' do it 'returns an error response' do
expect(result).to be_error expect(result).to be_error
expect(result.message).to eq('Invalid value was provided for a parameter.') expect(result.message).to eq('Invalid value was provided for parameters: policy')
end end
end end
...@@ -80,6 +80,12 @@ RSpec.describe IncidentManagement::IssuableEscalationStatuses::PrepareUpdateServ ...@@ -80,6 +80,12 @@ RSpec.describe IncidentManagement::IssuableEscalationStatuses::PrepareUpdateServ
it_behaves_like 'successful response without policy params' it_behaves_like 'successful response without policy params'
end end
context 'when policy is excluded' do
let(:params) { { status: status } }
it_behaves_like 'successful response without policy params'
end
context 'when policy is nil' do context 'when policy is nil' do
let(:params) { { status: status, policy: nil } } let(:params) { { status: status, policy: nil } }
......
...@@ -37,7 +37,7 @@ RSpec.describe IncidentManagement::IssuableEscalationStatuses::PrepareUpdateServ ...@@ -37,7 +37,7 @@ RSpec.describe IncidentManagement::IssuableEscalationStatuses::PrepareUpdateServ
end end
shared_examples 'invalid params error response' do shared_examples 'invalid params error response' do
include_examples 'error response', 'Invalid value was provided for a parameter.' include_examples 'error response', 'Invalid value was provided for parameters: status'
end end
it_behaves_like 'successful response', { status_event: :acknowledge } it_behaves_like 'successful response', { status_event: :acknowledge }
......
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