Commit 1754f1ff authored by Vitali Tatarintev's avatar Vitali Tatarintev

Make PagerDuty::ProcessWebhookService user-less service

Avoid passing `nil` as a user to the `PagerDuty::ProcessWebhookService`
parent 2503be2f
......@@ -26,7 +26,7 @@ module Projects
end
def webhook_processor
::IncidentManagement::PagerDuty::ProcessWebhookService.new(project, nil, payload)
::IncidentManagement::PagerDuty::ProcessWebhookService.new(project, payload)
end
def payload
......
......@@ -2,7 +2,7 @@
module IncidentManagement
module PagerDuty
class ProcessWebhookService < BaseService
class ProcessWebhookService
include Gitlab::Utils::StrongMemoize
include IncidentManagement::Settings
......@@ -12,6 +12,11 @@ module IncidentManagement
# https://developer.pagerduty.com/docs/webhooks/v2-overview/#webhook-types
PAGER_DUTY_PROCESSABLE_EVENT_TYPES = %w(incident.trigger).freeze
def initialize(project, payload)
@project = project
@payload = payload
end
def execute(token)
return forbidden unless webhook_setting_active?
return unauthorized unless valid_token?(token)
......@@ -24,6 +29,8 @@ module IncidentManagement
private
attr_reader :project, :payload
def process_incidents
pager_duty_processable_events.each do |event|
::IncidentManagement::PagerDuty::ProcessIncidentWorker.perform_async(project.id, event['incident'])
......@@ -33,7 +40,7 @@ module IncidentManagement
def pager_duty_processable_events
strong_memoize(:pager_duty_processable_events) do
::PagerDuty::WebhookPayloadParser
.call(params.to_h)
.call(payload.to_h)
.filter { |msg| msg['event'].to_s.in?(PAGER_DUTY_PROCESSABLE_EVENT_TYPES) }
end
end
......@@ -47,7 +54,7 @@ module IncidentManagement
end
def valid_payload_size?
Gitlab::Utils::DeepSize.new(params, max_size: PAGER_DUTY_PAYLOAD_SIZE_LIMIT).valid?
Gitlab::Utils::DeepSize.new(payload, max_size: PAGER_DUTY_PAYLOAD_SIZE_LIMIT).valid?
end
def accepted
......
......@@ -23,7 +23,7 @@ RSpec.describe 'PagerDuty webhook' do
it 'calls PagerDuty webhook processor with correct parameters' do
make_request
expect(webhook_processor_class).to have_received(:new).with(project, nil, payload)
expect(webhook_processor_class).to have_received(:new).with(project, payload)
expect(webhook_processor).to have_received(:execute).with('VALID-TOKEN')
end
......
......@@ -17,7 +17,7 @@ RSpec.describe IncidentManagement::PagerDuty::ProcessWebhookService do
let(:webhook_payload) { Gitlab::Json.parse(fixture_file('pager_duty/webhook_incident_trigger.json')) }
let(:token) { nil }
subject(:execute) { described_class.new(project, nil, webhook_payload).execute(token) }
subject(:execute) { described_class.new(project, webhook_payload).execute(token) }
context 'when PagerDuty webhook setting is active' do
let_it_be(:incident_management_setting) { create(:project_incident_management_setting, project: project, pagerduty_active: true) }
......
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