Commit 129dfa33 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Check whether PagerDuty setting is active

Check whether PagerDuty setting is active when
creating incident issue
parent 60fa2032
# frozen_string_literal: true # frozen_string_literal: true
module IncidentManagement module IncidentManagement
module Settings module Settings
include Gitlab::Utils::StrongMemoize
def incident_management_setting def incident_management_setting
strong_memoize(:incident_management_setting) do strong_memoize(:incident_management_setting) do
project.incident_management_setting || project.incident_management_setting ||
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
module IncidentManagement module IncidentManagement
module PagerDuty module PagerDuty
class CreateIncidentIssueService < BaseService class CreateIncidentIssueService < BaseService
include IncidentManagement::Settings
def initialize(project, incident_payload) def initialize(project, incident_payload)
super(project, User.alert_bot, incident_payload) super(project, User.alert_bot, incident_payload)
end end
...@@ -38,7 +40,8 @@ module IncidentManagement ...@@ -38,7 +40,8 @@ module IncidentManagement
end end
def webhook_available? def webhook_available?
Feature.enabled?(:pagerduty_webhook, project) Feature.enabled?(:pagerduty_webhook, project) &&
incident_management_setting.pagerduty_active?
end end
def forbidden def forbidden
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe IncidentManagement::PagerDuty::CreateIncidentIssueService do RSpec.describe IncidentManagement::PagerDuty::CreateIncidentIssueService do
let_it_be(:project) { create(:project) } let_it_be(:project, reload: true) { create(:project) }
let_it_be(:user) { User.alert_bot } let_it_be(:user) { User.alert_bot }
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(:parsed_payload) { ::PagerDuty::WebhookPayloadParser.call(webhook_payload) } let(:parsed_payload) { ::PagerDuty::WebhookPayloadParser.call(webhook_payload) }
...@@ -17,6 +17,9 @@ RSpec.describe IncidentManagement::PagerDuty::CreateIncidentIssueService do ...@@ -17,6 +17,9 @@ RSpec.describe IncidentManagement::PagerDuty::CreateIncidentIssueService do
stub_feature_flags(pagerduty_webhook: project) stub_feature_flags(pagerduty_webhook: project)
end end
context 'when PagerDuty webhook setting is active' do
let_it_be(:incident_management_setting) { create(:project_incident_management_setting, project: project, pagerduty_active: true) }
context 'when issue can be created' do context 'when issue can be created' do
it 'creates a new issue' do it 'creates a new issue' do
expect { execute }.to change(Issue, :count).by(1) expect { execute }.to change(Issue, :count).by(1)
...@@ -55,9 +58,13 @@ RSpec.describe IncidentManagement::PagerDuty::CreateIncidentIssueService do ...@@ -55,9 +58,13 @@ RSpec.describe IncidentManagement::PagerDuty::CreateIncidentIssueService do
end end
end end
context 'when issue cannot be created' do context 'when the payload does not contain a title' do
let(:incident_payload) { {} } let(:incident_payload) { {} }
it 'does not create a GitLab issue' do
expect { execute }.not_to change(Issue, :count)
end
it 'responds with error' do it 'responds with error' do
expect(execute).to be_error expect(execute).to be_error
expect(execute.message).to eq("Title can't be blank") expect(execute.message).to eq("Title can't be blank")
...@@ -65,6 +72,20 @@ RSpec.describe IncidentManagement::PagerDuty::CreateIncidentIssueService do ...@@ -65,6 +72,20 @@ RSpec.describe IncidentManagement::PagerDuty::CreateIncidentIssueService do
end end
end end
context 'when PagerDuty webhook setting is not active' do
let_it_be(:incident_management_setting) { create(:project_incident_management_setting, project: project, pagerduty_active: false) }
it 'does not create a GitLab issue' do
expect { execute }.not_to change(Issue, :count)
end
it 'responds with forbidden' do
expect(execute).to be_error
expect(execute.http_status).to eq(:forbidden)
end
end
end
context 'when pagerduty_webhook feature disabled' do context 'when pagerduty_webhook feature disabled' do
before do before do
stub_feature_flags(pagerduty_webhook: false) stub_feature_flags(pagerduty_webhook: false)
......
...@@ -4,6 +4,7 @@ require 'spec_helper' ...@@ -4,6 +4,7 @@ require 'spec_helper'
RSpec.describe IncidentManagement::PagerDuty::ProcessIncidentWorker do RSpec.describe IncidentManagement::PagerDuty::ProcessIncidentWorker do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:incident_management_setting) { create(:project_incident_management_setting, project: project, pagerduty_active: true) }
describe '#perform' do describe '#perform' do
subject(:perform) { described_class.new.perform(project.id, incident_payload) } subject(:perform) { described_class.new.perform(project.id, incident_payload) }
......
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