diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-11 15:09:03 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-11 15:09:03 +0300 |
commit | b9ab87c14ce9ebe8284aeffa32c1cee934156e58 (patch) | |
tree | 85c5a1c4febe868ad0090ab6795f6fe548714541 /app/services/incident_management | |
parent | fb7b6bceee41fc6e5dba72a24519dec8f2713075 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/incident_management')
4 files changed, 177 insertions, 0 deletions
diff --git a/app/services/incident_management/timeline_events/base_service.rb b/app/services/incident_management/timeline_events/base_service.rb new file mode 100644 index 00000000000..cae58465e4a --- /dev/null +++ b/app/services/incident_management/timeline_events/base_service.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module IncidentManagement + module TimelineEvents + class BaseService + def allowed? + user&.can?(:admin_incident_management_timeline_event, incident) + end + + def success(timeline_event) + ServiceResponse.success(payload: { timeline_event: timeline_event }) + end + + def error(message) + ServiceResponse.error(message: message) + end + + def error_no_permissions + error(_('You have insufficient permissions to manage timeline events for this incident')) + end + + def error_in_save(timeline_event) + error(timeline_event.errors.full_messages.to_sentence) + end + end + end +end diff --git a/app/services/incident_management/timeline_events/create_service.rb b/app/services/incident_management/timeline_events/create_service.rb new file mode 100644 index 00000000000..7d287e1bd82 --- /dev/null +++ b/app/services/incident_management/timeline_events/create_service.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +module IncidentManagement + module TimelineEvents + DEFAULT_ACTION = 'comment' + + class CreateService < TimelineEvents::BaseService + def initialize(incident, user, params) + @project = incident.project + @incident = incident + @user = user + @params = params + end + + def execute + return error_no_permissions unless allowed? + + timeline_event_params = { + project: project, + incident: incident, + author: user, + note: params[:note], + action: params.fetch(:action, DEFAULT_ACTION), + note_html: params[:note_html].presence || params[:note], + occurred_at: params[:occurred_at], + promoted_from_note: params[:promoted_from_note] + } + + timeline_event = IncidentManagement::TimelineEvent.new(timeline_event_params) + + if timeline_event.save + add_system_note(timeline_event) + + success(timeline_event) + else + error_in_save(timeline_event) + end + end + + private + + attr_reader :project, :user, :incident, :params + + def add_system_note(timeline_event) + return unless Feature.enabled?(:incident_timeline, project) + + SystemNoteService.add_timeline_event(timeline_event) + end + end + end +end diff --git a/app/services/incident_management/timeline_events/destroy_service.rb b/app/services/incident_management/timeline_events/destroy_service.rb new file mode 100644 index 00000000000..8bb186c289a --- /dev/null +++ b/app/services/incident_management/timeline_events/destroy_service.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module IncidentManagement + module TimelineEvents + class DestroyService < TimelineEvents::BaseService + # @param timeline_event [IncidentManagement::TimelineEvent] + # @param user [User] + def initialize(timeline_event, user) + @timeline_event = timeline_event + @user = user + @incident = timeline_event.incident + @project = @incident.project + end + + def execute + return error_no_permissions unless allowed? + + if timeline_event.destroy + add_system_note(incident, user) + + success(timeline_event) + else + error_in_save(timeline_event) + end + end + + private + + attr_reader :project, :timeline_event, :user, :incident + + def add_system_note(incident, user) + return unless Feature.enabled?(:incident_timeline, project) + + SystemNoteService.delete_timeline_event(incident, user) + end + end + end +end diff --git a/app/services/incident_management/timeline_events/update_service.rb b/app/services/incident_management/timeline_events/update_service.rb new file mode 100644 index 00000000000..fe8b4879561 --- /dev/null +++ b/app/services/incident_management/timeline_events/update_service.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +module IncidentManagement + module TimelineEvents + # @param timeline_event [IncidentManagement::TimelineEvent] + # @param user [User] + # @param params [Hash] + # @option params [string] note + # @option params [datetime] occurred_at + class UpdateService < TimelineEvents::BaseService + def initialize(timeline_event, user, params) + @timeline_event = timeline_event + @incident = timeline_event.incident + @user = user + @note = params[:note] + @occurred_at = params[:occurred_at] + end + + def execute + return error_no_permissions unless allowed? + + if timeline_event.update(update_params) + add_system_note(timeline_event) + + success(timeline_event) + else + error_in_save(timeline_event) + end + end + + private + + attr_reader :timeline_event, :incident, :user, :note, :occurred_at + + def update_params + { updated_by_user: user, note: note.presence, occurred_at: occurred_at.presence }.compact + end + + def add_system_note(timeline_event) + return unless Feature.enabled?(:incident_timeline, incident.project) + + changes = was_changed(timeline_event) + return if changes == :none + + SystemNoteService.edit_timeline_event(timeline_event, user, was_changed: changes) + end + + def was_changed(timeline_event) + changes = timeline_event.previous_changes + occurred_at_changed = changes.key?('occurred_at') + note_changed = changes.key?('note') + + return :occurred_at_and_note if occurred_at_changed && note_changed + return :occurred_at if occurred_at_changed + return :note if note_changed + + :none + end + end + end +end |