Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/alert_management/alerts/update_service.rb')
-rw-r--r--app/services/alert_management/alerts/update_service.rb98
1 files changed, 98 insertions, 0 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