Commit 224585c9 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch '291986-avoid-passing-nil-to-pagerduty-process-webhook-service' into 'master'

Make PagerDuty::ProcessWebhookService user-less service

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