Commit 72b83885 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Call PagerDuty webhook processor from controller

parent e82ebd5b
......@@ -11,11 +11,7 @@ module Projects
prepend_before_action :project_without_auth
def create
result = ServiceResponse.success(http_status: :accepted)
unless Feature.enabled?(:pagerduty_webhook, @project)
result = ServiceResponse.error(message: 'Unauthorized', http_status: :unauthorized)
end
result = webhook_processor.execute(params[:token])
head result.http_status
end
......@@ -26,6 +22,14 @@ module Projects
@project ||= Project
.find_by_full_path("#{params[:namespace_id]}/#{params[:project_id]}")
end
def webhook_processor
::IncidentManagement::PagerDuty::ProcessWebhookService.new(project, nil, payload)
end
def payload
@payload ||= params.permit![:pager_duty_incident].to_h
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::IncidentManagement::PagerDutyIncidentsController do
let_it_be(:project) { create(:project) }
describe 'POST #create' do
let(:payload) { { messages: [] } }
def make_request
post :create, params: project_params, body: payload.to_json, as: :json
end
context 'when pagerduty_webhook feature enabled' do
before do
stub_feature_flags(pagerduty_webhook: project)
end
it 'responds with 202 Accepted' do
make_request
expect(response).to have_gitlab_http_status(:accepted)
end
end
context 'when pagerduty_webhook feature disabled' do
before do
stub_feature_flags(pagerduty_webhook: false)
end
it 'responds with 401 Unauthorized' do
make_request
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
private
def project_params(opts = {})
opts.reverse_merge(namespace_id: project.namespace, project_id: project)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'PagerDuty webhook' do
let_it_be(:project) { create(:project) }
describe 'POST /incidents/pagerduty' do
let(:payload) { Gitlab::Json.parse(fixture_file('pager_duty/webhook_incident_trigger.json')) }
let(:webhook_processor_class) { ::IncidentManagement::PagerDuty::ProcessWebhookService }
let(:webhook_processor) { instance_double(webhook_processor_class) }
def make_request
headers = { 'Content-Type' => 'application/json' }
post project_incidents_pagerduty_url(project, token: 'VALID-TOKEN'), params: payload.to_json, headers: headers
end
before do
allow(webhook_processor_class).to receive(:new).and_return(webhook_processor)
allow(webhook_processor).to receive(:execute).and_return(ServiceResponse.success(http_status: :accepted))
end
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).to have_received(:execute).with('VALID-TOKEN')
end
it 'responds with 202 Accepted' do
make_request
expect(response).to have_gitlab_http_status(:accepted)
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