diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-09 06:09:01 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-09 06:09:01 +0300 |
commit | 0a9efe02885d9ad4dbdaf61746ae81daea0f575b (patch) | |
tree | ccd02a93eaaadb1c547c5f6e1d239d9276908bc6 /app/services/alert_management | |
parent | f44bf01f69a491d4dfca8d631b390371bd0eec7a (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/alert_management')
-rw-r--r-- | app/services/alert_management/alerts/update_service.rb | 72 | ||||
-rw-r--r-- | app/services/alert_management/update_alert_status_service.rb | 68 |
2 files changed, 57 insertions, 83 deletions
diff --git a/app/services/alert_management/alerts/update_service.rb b/app/services/alert_management/alerts/update_service.rb index 1c1addb4a7a..f99af43cefb 100644 --- a/app/services/alert_management/alerts/update_service.rb +++ b/app/services/alert_management/alerts/update_service.rb @@ -12,19 +12,20 @@ module AlertManagement @alert = alert @current_user = current_user @params = params + @param_errors = [] end def execute return error_no_permissions unless allowed? - return error_no_updates if params.empty? - filter_assignees - return error_no_assignee_permissions if unauthorized_assignees? + filter_params + return error_invalid_params if param_errors.any? + # Save old assignees for system notes old_assignees = alert.assignees.to_a if alert.update(params) - process_assignement(old_assignees) + handle_changes(old_assignees: old_assignees) success else @@ -34,7 +35,8 @@ module AlertManagement private - attr_reader :alert, :current_user, :params + attr_reader :alert, :current_user, :params, :param_errors + delegate :resolved?, to: :alert def allowed? current_user&.can?(:update_alert_management_alert, alert) @@ -58,40 +60,80 @@ module AlertManagement error(_('You have no permissions')) end - def error_no_updates - error(_('Please provide attributes to update')) + def error_invalid_params + error(param_errors.to_sentence) end - def error_no_assignee_permissions - error(_('Assignee has no permissions')) + def add_param_error(message) + param_errors << message end - # ----- Assignee-related behavior ------ - def unauthorized_assignees? - params[:assignees]&.any? { |user| !user.can?(:read_alert_management_alert, alert) } + def filter_params + param_errors << _('Please provide attributes to update') if params.empty? + + filter_status + filter_assignees + end + + def handle_changes(old_assignees:) + handle_assignement(old_assignees) if params[:assignees] + handle_status_change if params[:status_event] end + # ----- Assignee-related behavior ------ def filter_assignees return if params[:assignees].nil? # Always take first assignee while multiple are not currently supported params[:assignees] = Array(params[:assignees].first) + + param_errors << _('Assignee has no permissions') if unauthorized_assignees? + end + + def unauthorized_assignees? + params[:assignees]&.any? { |user| !user.can?(:read_alert_management_alert, alert) } end - def process_assignement(old_assignees) + def handle_assignement(old_assignees) assign_todo add_assignee_system_note(old_assignees) end def assign_todo - return if alert.assignees.empty? - 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 + + # ------ Status-related behavior ------- + def filter_status + return unless status = params.delete(:status) + + status_key = AlertManagement::Alert::STATUSES.key(status) + status_event = AlertManagement::Alert::STATUS_EVENTS[status_key] + + unless status_event + param_errors << _('Invalid status') + return + end + + params[:status_event] = status_event + end + + def handle_status_change + add_status_change_system_note + resolve_todos if resolved? + end + + def add_status_change_system_note + SystemNoteService.change_alert_status(alert, current_user) + end + + def resolve_todos + todo_service.resolve_todos_for_target(alert, current_user) + end end end end diff --git a/app/services/alert_management/update_alert_status_service.rb b/app/services/alert_management/update_alert_status_service.rb deleted file mode 100644 index 427e2e09057..00000000000 --- a/app/services/alert_management/update_alert_status_service.rb +++ /dev/null @@ -1,68 +0,0 @@ -# frozen_string_literal: true - -module AlertManagement - class UpdateAlertStatusService - include Gitlab::Utils::StrongMemoize - - # @param alert [AlertManagement::Alert] - # @param user [User] - # @param status [Integer] Must match a value from AlertManagement::Alert::STATUSES - def initialize(alert, user, status) - @alert = alert - @user = user - @status = status - end - - def execute - return error_no_permissions unless allowed? - return error_invalid_status unless status_key - - if alert.update(status_event: status_event) - resolve_todos if resolved? - success - else - error(alert.errors.full_messages.to_sentence) - end - end - - private - - attr_reader :alert, :user, :status - - delegate :project, :resolved?, to: :alert - - def allowed? - user.can?(:update_alert_management_alert, project) - end - - def resolve_todos - TodoService.new.resolve_todos_for_target(alert, user) - end - - def status_key - strong_memoize(:status_key) do - AlertManagement::Alert::STATUSES.key(status) - end - end - - def status_event - AlertManagement::Alert::STATUS_EVENTS[status_key] - end - - def success - ServiceResponse.success(payload: { alert: alert }) - end - - def error_no_permissions - error(_('You have no permissions')) - end - - def error_invalid_status - error(_('Invalid status')) - end - - def error(message) - ServiceResponse.error(payload: { alert: alert }, message: message) - end - end -end |