diff options
Diffstat (limited to 'app/services/alert_management')
3 files changed, 104 insertions, 3 deletions
diff --git a/app/services/alert_management/alerts/update_service.rb b/app/services/alert_management/alerts/update_service.rb new file mode 100644 index 00000000000..ffabbb37289 --- /dev/null +++ b/app/services/alert_management/alerts/update_service.rb @@ -0,0 +1,98 @@ +# frozen_string_literal: true + +module AlertManagement + module Alerts + class UpdateService + include Gitlab::Utils::StrongMemoize + + # @param alert [AlertManagement::Alert] + # @param current_user [User] + # @param params [Hash] Attributes of the alert + def initialize(alert, current_user, params) + @alert = alert + @current_user = current_user + @params = params + end + + def execute + return error_no_permissions unless allowed? + return error_no_updates if params.empty? + + filter_assignees + old_assignees = alert.assignees.to_a + + if alert.update(params) + process_assignement(old_assignees) + + success + else + error(alert.errors.full_messages.to_sentence) + end + end + + private + + attr_reader :alert, :current_user, :params + + def allowed? + current_user&.can?(:update_alert_management_alert, alert) + end + + def assignee_todo_allowed? + assignee&.can?(:read_alert_management_alert, alert) + end + + def todo_service + strong_memoize(:todo_service) do + TodoService.new + end + end + + def success + ServiceResponse.success(payload: { alert: alert }) + end + + def error(message) + ServiceResponse.error(payload: { alert: alert }, message: message) + end + + def error_no_permissions + error(_('You have no permissions')) + end + + def error_no_updates + error(_('Please provide attributes to update')) + end + + # ----- Assignee-related behavior ------ + def filter_assignees + return if params[:assignees].nil? + + params[:assignees] = Array(assignee) + end + + def assignee + strong_memoize(:assignee) do + # Take first assignee while multiple are not currently supported + params[:assignees]&.first + end + end + + def process_assignement(old_assignees) + assign_todo + add_assignee_system_note(old_assignees) + end + + def assign_todo + # Remove check in follow-up issue https://gitlab.com/gitlab-org/gitlab/-/issues/222672 + return unless assignee_todo_allowed? + + todo_service.assign_alert(alert, current_user) + end + + def add_assignee_system_note(old_assignees) + SystemNoteService.change_issuable_assignees(alert, alert.project, current_user, old_assignees) + end + end + end +end diff --git a/app/services/alert_management/create_alert_issue_service.rb b/app/services/alert_management/create_alert_issue_service.rb index 0197f29145d..beacd240b08 100644 --- a/app/services/alert_management/create_alert_issue_service.rb +++ b/app/services/alert_management/create_alert_issue_service.rb @@ -29,8 +29,7 @@ module AlertManagement delegate :project, to: :alert def allowed? - Feature.enabled?(:alert_management_create_alert_issue, project) && - user.can?(:create_issue, project) + user.can?(:create_issue, project) end def create_issue(alert, user, alert_payload) diff --git a/app/services/alert_management/process_prometheus_alert_service.rb b/app/services/alert_management/process_prometheus_alert_service.rb index af28f1354b3..90fcbd95e4b 100644 --- a/app/services/alert_management/process_prometheus_alert_service.rb +++ b/app/services/alert_management/process_prometheus_alert_service.rb @@ -29,6 +29,7 @@ module AlertManagement def process_firing_alert_management_alert if am_alert.present? + am_alert.register_new_event! reset_alert_management_alert_status else create_alert_management_alert @@ -47,7 +48,10 @@ module AlertManagement def create_alert_management_alert am_alert = AlertManagement::Alert.new(am_alert_params.merge(ended_at: nil)) - return if am_alert.save + if am_alert.save + am_alert.execute_services + return + end logger.warn( message: 'Unable to create AlertManagement::Alert', |