Commit 7e70ed4c authored by Sean Arnold's avatar Sean Arnold

Add service to sync data to http integration

Add new specs + factories
parent ada686ef
...@@ -14,6 +14,8 @@ class AlertsService < Service ...@@ -14,6 +14,8 @@ class AlertsService < Service
before_validation :prevent_token_assignment before_validation :prevent_token_assignment
before_validation :ensure_token, if: :activated? before_validation :ensure_token, if: :activated?
after_save :update_http_integration
def url def url
return if instance? || template? return if instance? || template?
...@@ -77,6 +79,14 @@ class AlertsService < Service ...@@ -77,6 +79,14 @@ class AlertsService < Service
def url_helpers def url_helpers
Gitlab::Routing.url_helpers Gitlab::Routing.url_helpers
end end
def update_http_integration
return unless type == 'AlertsService'
AlertManagement::SyncAlertServiceDataService # rubocop: disable CodeReuse/ServiceClass
.new(self)
.execute
end
end end
AlertsService.prepend_if_ee('EE::AlertsService') AlertsService.prepend_if_ee('EE::AlertsService')
# frozen_string_literal: true
module AlertManagement
class SyncAlertServiceDataService
# @param alert_service [AlertsService]
def initialize(alert_service)
@alert_service = alert_service
end
def execute
http_integration = find_http_integration
return ServiceResponse.success(message: 'HTTP Integration not found') unless http_integration
result = update_integration_data(http_integration)
result ? ServiceResponse.success : ServiceResponse.error(message: 'Update failed')
end
private
attr_reader :alert_service
def find_http_integration
AlertManagement::HttpIntegrationsFinder.new(
alert_service.project,
endpoint_identifier: ::AlertManagement::HttpIntegration::LEGACY_IDENTIFIER
)
.execute
.first
end
def update_integration_data(http_integration)
http_integration.update!(
active: alert_service.active,
encrypted_token: alert_service.data.encrypted_token,
encrypted_token_iv: alert_service.data.encrypted_token_iv
)
end
end
end
# frozen_string_literal: true
FactoryBot.define do
factory :alerts_service_data do
service { association(:alerts_service) }
token { SecureRandom.hex }
end
end
...@@ -56,6 +56,10 @@ FactoryBot.define do ...@@ -56,6 +56,10 @@ FactoryBot.define do
trait :inactive do trait :inactive do
active { false } active { false }
end end
before(:create) do |service|
service.data = build(:alerts_service_data, service: service)
end
end end
factory :drone_ci_service do factory :drone_ci_service do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe AlertManagement::SyncAlertServiceDataService do
let_it_be(:alerts_service) { create(:alerts_service, :active) }
describe '#execute' do
subject { described_class.new(alerts_service).execute}
context 'without http integration' do
it 'returns a success' do
expect(subject.success?).to eq(true)
end
end
context 'existing legacy http integration' do
let_it_be(:integration) { create(:alert_management_http_integration, :legacy, project: alerts_service.project) }
it 'updates the integration' do
expect { subject }
.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
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 }
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