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 ...@@ -10,9 +10,12 @@ module AlertManagement
def execute def execute
http_integration = find_http_integration 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') result ? ServiceResponse.success : ServiceResponse.error(message: 'Update failed')
end end
...@@ -29,6 +32,19 @@ module AlertManagement ...@@ -29,6 +32,19 @@ module AlertManagement
.first .first
end 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) def update_integration_data(http_integration)
http_integration.update( http_integration.update(
active: alert_service.active, active: alert_service.active,
......
# frozen_string_literal: true # 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] class MigrateServicesToHttpIntegrations < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
DOWNTIME = false DOWNTIME = false
ALERT_SERVICE_TYPE = 'AlertsService' ALERT_SERVICE_TYPE = 'AlertsService'
SERVICE_NAMES_IDENTIFIER = { SERVICE_NAMES_IDENTIFIER = {
name: 'Legacy Endpoint', name: 'HTTP endpoint',
identifier: 'legacy' identifier: 'legacy'
} }
......
...@@ -3,12 +3,23 @@ ...@@ -3,12 +3,23 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe AlertManagement::SyncAlertServiceDataService do 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 describe '#execute' do
subject { described_class.new(alerts_service).execute} subject(:execute) { described_class.new(alerts_service).execute }
context 'without http integration' do 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 it 'returns a success' do
expect(subject.success?).to eq(true) expect(subject.success?).to eq(true)
end end
...@@ -18,18 +29,26 @@ RSpec.describe AlertManagement::SyncAlertServiceDataService do ...@@ -18,18 +29,26 @@ RSpec.describe AlertManagement::SyncAlertServiceDataService do
let_it_be(:integration) { create(:alert_management_http_integration, :legacy, project: alerts_service.project) } let_it_be(:integration) { create(:alert_management_http_integration, :legacy, project: alerts_service.project) }
it 'updates the integration' do it 'updates the integration' do
expect { subject } expect { execute }
.to change { integration.reload.encrypted_token }.to(alerts_service.data.encrypted_token) .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) .and change { integration.encrypted_token_iv }.to(alerts_service.data.encrypted_token_iv)
end end
it 'returns a success' do
expect(subject.success?).to eq(true)
end
end end
context 'existing other http integration' do context 'existing other http integration' do
let_it_be(:integration) { create(:alert_management_http_integration, project: alerts_service.project) } let_it_be(:integration) { create(:alert_management_http_integration, project: alerts_service.project) }
it 'does not update the integration' do it 'creates the integration' do
expect { subject } expect { execute }
.not_to change { integration } .to change { AlertManagement::HttpIntegration.count }.by(1)
end
it 'returns a success' do
expect(subject.success?).to eq(true)
end end
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