Commit 9d6b948c authored by Sean Arnold's avatar Sean Arnold

Create integration if not existing

- Update specs to match
- Remove comment
parent 1a21f2c6
......@@ -10,9 +10,12 @@ module AlertManagement
def execute
http_integration = find_http_integration
return ServiceResponse.success(message: 'HTTP Integration not found') unless http_integration
result = if http_integration
update_integration_data(http_integration)
else
create_integration
end
result = update_integration_data(http_integration)
result ? ServiceResponse.success : ServiceResponse.error(message: 'Update failed')
end
......@@ -29,6 +32,19 @@ module AlertManagement
.first
end
def create_integration
new_integration = AlertManagement::HttpIntegration.create(
project_id: alert_service.project_id,
name: 'HTTP endpoint',
endpoint_identifier: AlertManagement::HttpIntegration::LEGACY_IDENTIFIER,
active: alert_service.active,
encrypted_token: alert_service.data.encrypted_token,
encrypted_token_iv: alert_service.data.encrypted_token_iv
)
new_integration.persisted?
end
def update_integration_data(http_integration)
http_integration.update(
active: alert_service.active,
......
# frozen_string_literal: true
# See https://docs.gitlab.com/ee/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class MigrateServicesToHttpIntegrations < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
ALERT_SERVICE_TYPE = 'AlertsService'
SERVICE_NAMES_IDENTIFIER = {
name: 'Legacy Endpoint',
name: 'HTTP endpoint',
identifier: 'legacy'
}
......
......@@ -3,12 +3,23 @@
require 'spec_helper'
RSpec.describe AlertManagement::SyncAlertServiceDataService do
let_it_be(:alerts_service) { create(:alerts_service, :active) }
let_it_be(:alerts_service) do
AlertsService.skip_callback(:save, :after, :update_http_integration)
service = create(:alerts_service, :active)
AlertsService.set_callback(:save, :after, :update_http_integration)
service
end
describe '#execute' do
subject { described_class.new(alerts_service).execute}
subject(:execute) { described_class.new(alerts_service).execute }
context 'without http integration' do
it 'creates the integration' do
expect { execute }
.to change { AlertManagement::HttpIntegration.count }.by(1)
end
it 'returns a success' do
expect(subject.success?).to eq(true)
end
......@@ -18,18 +29,26 @@ RSpec.describe AlertManagement::SyncAlertServiceDataService do
let_it_be(:integration) { create(:alert_management_http_integration, :legacy, project: alerts_service.project) }
it 'updates the integration' do
expect { subject }
expect { execute }
.to change { integration.reload.encrypted_token }.to(alerts_service.data.encrypted_token)
.and change { integration.encrypted_token_iv }.to(alerts_service.data.encrypted_token_iv)
end
it 'returns a success' do
expect(subject.success?).to eq(true)
end
end
context 'existing other http integration' do
let_it_be(:integration) { create(:alert_management_http_integration, project: alerts_service.project) }
it 'does not update the integration' do
expect { subject }
.not_to change { integration }
it 'creates the integration' do
expect { execute }
.to change { AlertManagement::HttpIntegration.count }.by(1)
end
it 'returns a success' do
expect(subject.success?).to eq(true)
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