diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
commit | 7e9c479f7de77702622631cff2628a9c8dcbc627 (patch) | |
tree | c8f718a08e110ad7e1894510980d2155a6549197 /app/services/alert_management | |
parent | e852b0ae16db4052c1c567d9efa4facc81146e88 (diff) |
Add latest changes from gitlab-org/gitlab@13-6-stable-eev13.6.0-rc42
Diffstat (limited to 'app/services/alert_management')
5 files changed, 219 insertions, 4 deletions
diff --git a/app/services/alert_management/http_integrations/create_service.rb b/app/services/alert_management/http_integrations/create_service.rb new file mode 100644 index 00000000000..576e38c23aa --- /dev/null +++ b/app/services/alert_management/http_integrations/create_service.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module AlertManagement + module HttpIntegrations + class CreateService + # @param project [Project] + # @param current_user [User] + # @param params [Hash] + def initialize(project, current_user, params) + @project = project + @current_user = current_user + @params = params + end + + def execute + return error_no_permissions unless allowed? + return error_multiple_integrations unless creation_allowed? + + integration = project.alert_management_http_integrations.create(params) + return error_in_create(integration) unless integration.valid? + + success(integration) + end + + private + + attr_reader :project, :current_user, :params + + def allowed? + current_user&.can?(:admin_operations, project) + end + + def creation_allowed? + project.alert_management_http_integrations.empty? + end + + def error(message) + ServiceResponse.error(message: message) + end + + def success(integration) + ServiceResponse.success(payload: { integration: integration }) + end + + def error_no_permissions + error(_('You have insufficient permissions to create an HTTP integration for this project')) + end + + def error_multiple_integrations + error(_('Multiple HTTP integrations are not supported for this project')) + end + + def error_in_create(integration) + error(integration.errors.full_messages.to_sentence) + end + end + end +end + +::AlertManagement::HttpIntegrations::CreateService.prepend_if_ee('::EE::AlertManagement::HttpIntegrations::CreateService') diff --git a/app/services/alert_management/http_integrations/destroy_service.rb b/app/services/alert_management/http_integrations/destroy_service.rb new file mode 100644 index 00000000000..aeb3f6cb807 --- /dev/null +++ b/app/services/alert_management/http_integrations/destroy_service.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +module AlertManagement + module HttpIntegrations + class DestroyService + # @param integration [AlertManagement::HttpIntegration] + # @param current_user [User] + def initialize(integration, current_user) + @integration = integration + @current_user = current_user + end + + def execute + return error_no_permissions unless allowed? + + if integration.destroy + success + else + error(integration.errors.full_messages.to_sentence) + end + end + + private + + attr_reader :integration, :current_user + + def allowed? + current_user&.can?(:admin_operations, integration) + end + + def error(message) + ServiceResponse.error(message: message) + end + + def success + ServiceResponse.success(payload: { integration: integration }) + end + + def error_no_permissions + error(_('You have insufficient permissions to remove this HTTP integration')) + end + end + end +end diff --git a/app/services/alert_management/http_integrations/update_service.rb b/app/services/alert_management/http_integrations/update_service.rb new file mode 100644 index 00000000000..220c4e759f0 --- /dev/null +++ b/app/services/alert_management/http_integrations/update_service.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module AlertManagement + module HttpIntegrations + class UpdateService + # @param integration [AlertManagement::HttpIntegration] + # @param current_user [User] + # @param params [Hash] + def initialize(integration, current_user, params) + @integration = integration + @current_user = current_user + @params = params + end + + def execute + return error_no_permissions unless allowed? + + params[:token] = nil if params.delete(:regenerate_token) + + if integration.update(params) + success + else + error(integration.errors.full_messages.to_sentence) + end + end + + private + + attr_reader :integration, :current_user, :params + + def allowed? + current_user&.can?(:admin_operations, integration) + end + + def error(message) + ServiceResponse.error(message: message) + end + + def success + ServiceResponse.success(payload: { integration: integration.reset }) + end + + def error_no_permissions + error(_('You have insufficient permissions to update this HTTP integration')) + end + end + end +end diff --git a/app/services/alert_management/process_prometheus_alert_service.rb b/app/services/alert_management/process_prometheus_alert_service.rb index 5c7698f724a..28ce5401a6c 100644 --- a/app/services/alert_management/process_prometheus_alert_service.rb +++ b/app/services/alert_management/process_prometheus_alert_service.rb @@ -9,6 +9,10 @@ module AlertManagement return bad_request unless incoming_payload.has_required_attributes? process_alert_management_alert + return bad_request unless alert.persisted? + + process_incident_issues if process_issues? + send_alert_email if send_email? ServiceResponse.success end @@ -30,8 +34,6 @@ module AlertManagement else create_alert_management_alert end - - process_incident_issues if process_issues? end def reset_alert_management_alert_status @@ -85,12 +87,17 @@ module AlertManagement end def process_incident_issues - return unless alert.persisted? - return if alert.issue + return if alert.issue || alert.resolved? IncidentManagement::ProcessAlertWorker.perform_async(nil, nil, alert.id) end + def send_alert_email + notification_service + .async + .prometheus_alerts_fired(project, [alert]) + end + def logger @logger ||= Gitlab::AppLogger end diff --git a/app/services/alert_management/sync_alert_service_data_service.rb b/app/services/alert_management/sync_alert_service_data_service.rb new file mode 100644 index 00000000000..1ba197065c5 --- /dev/null +++ b/app/services/alert_management/sync_alert_service_data_service.rb @@ -0,0 +1,56 @@ +# 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 + + result = if http_integration + update_integration_data(http_integration) + else + create_integration + end + + 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 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, + encrypted_token: alert_service.data.encrypted_token, + encrypted_token_iv: alert_service.data.encrypted_token_iv + ) + end + end +end |