Commit f92e7c0a authored by Vitali Tatarintev's avatar Vitali Tatarintev

Add PagerDuty incident management worker

parent 24d4cebd
# frozen_string_literal: true
module IncidentManagement
module PagerDuty
class ProcessIncidentWorker # rubocop:disable Scalability/IdempotentWorker
include ApplicationWorker
queue_namespace :incident_management
feature_category :incident_management
def perform(project_id, incident_payload)
return unless project_id
project = find_project(project_id)
return unless project
result = create_issue(project, incident_payload)
log_error(result) if result.error?
end
private
def find_project(project_id)
Project.find_by_id(project_id)
end
def create_issue(project, incident_payload)
::IncidentManagement::PagerDuty::CreateIncidentIssueService
.new(project, incident_payload)
.execute
end
def log_error(result)
Gitlab::AppLogger.warn(
message: 'Cannot create issue for PagerDuty incident',
issue_errors: result.message
)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe IncidentManagement::PagerDuty::ProcessIncidentWorker do
let_it_be(:project) { create(:project) }
describe '#perform' do
subject(:perform) { described_class.new.perform(project.id, incident_payload) }
context 'with valid incident payload' do
let(:incident_payload) do
{
'url' => 'https://webdemo.pagerduty.com/incidents/PRORDTY',
'incident_number' => 33,
'title' => 'My new incident',
'status' => 'triggered',
'created_at' => '2017-09-26T15:14:36Z',
'urgency' => 'high',
'incident_key' => nil,
'assignees' => [{
'summary' => 'Laura Haley', 'url' => 'https://webdemo.pagerduty.com/users/P553OPV'
}],
'impacted_services' => [{
'summary' => 'Production XDB Cluster', 'url' => 'https://webdemo.pagerduty.com/services/PN49J75'
}]
}
end
it 'creates a GitLab issue' do
expect { perform }.to change(Issue, :count).by(1)
end
end
context 'with invalid incident payload' do
let(:incident_payload) { {} }
before do
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
end
it 'does not create a GitLab issue' do
expect { perform }.not_to change(Issue, :count)
end
it 'logs a warning' do
perform
expect(Gitlab::AppLogger).to have_received(:warn).with(
message: 'Cannot create issue for PagerDuty incident',
issue_errors: "Title can't be blank"
)
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