diff options
Diffstat (limited to 'app/services/incident_management/timeline_events/create_service.rb')
-rw-r--r-- | app/services/incident_management/timeline_events/create_service.rb | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/app/services/incident_management/timeline_events/create_service.rb b/app/services/incident_management/timeline_events/create_service.rb index 5422b4ad6d2..71ff5b64515 100644 --- a/app/services/incident_management/timeline_events/create_service.rb +++ b/app/services/incident_management/timeline_events/create_service.rb @@ -5,6 +5,7 @@ module IncidentManagement DEFAULT_ACTION = 'comment' DEFAULT_EDITABLE = false DEFAULT_AUTO_CREATED = false + AUTOCREATE_TAGS = [TimelineEventTag::START_TIME_TAG_NAME, TimelineEventTag::END_TIME_TAG_NAME].freeze class CreateService < TimelineEvents::BaseService def initialize(incident, user, params) @@ -49,6 +50,15 @@ module IncidentManagement new(incident, user, note: note, occurred_at: occurred_at, action: action, auto_created: true).execute end + def change_severity(incident, user) + severity_label = IssuableSeverity::SEVERITY_LABELS[incident.severity.to_sym] + note = "@#{user.username} changed the incident severity to **#{severity_label}**" + occurred_at = incident.updated_at + action = 'severity' + + new(incident, user, note: note, occurred_at: occurred_at, action: action, auto_created: true).execute + end + def change_labels(incident, user, added_labels: [], removed_labels: []) return if Feature.disabled?(:incident_timeline_events_from_labels, incident.project) @@ -85,10 +95,17 @@ module IncidentManagement editable: params.fetch(:editable, DEFAULT_EDITABLE) } + non_existing_tags = validate_tags(project, params[:timeline_event_tag_names]) + + return error("#{_("Following tags don't exist")}: #{non_existing_tags}") unless non_existing_tags.empty? + timeline_event = IncidentManagement::TimelineEvent.new(timeline_event_params) - if timeline_event.save + if timeline_event.save(context: validation_context) add_system_note(timeline_event) + + create_timeline_event_tag_links(timeline_event, params[:timeline_event_tag_names]) + track_usage_event(:incident_management_timeline_event_created, user.id) success(timeline_event) @@ -112,6 +129,53 @@ module IncidentManagement SystemNoteService.add_timeline_event(timeline_event) end + + def validation_context + :user_input if !auto_created && params[:promoted_from_note].blank? + end + + def create_timeline_event_tag_links(timeline_event, tag_names) + return unless tag_names&.any? + + auto_create_predefined_tags(tag_names) + + # Refetches the tag objects to consider predefined tags as well + tags = project.incident_management_timeline_event_tags.by_names(tag_names) + + tag_links = tags.select(:id).map do |tag| + { + timeline_event_id: timeline_event.id, + timeline_event_tag_id: tag.id, + created_at: DateTime.current + } + end + + IncidentManagement::TimelineEventTagLink.insert_all(tag_links) if tag_links.any? + end + + def auto_create_predefined_tags(new_tags) + new_tags = new_tags.map(&:downcase) + + tags_to_create = AUTOCREATE_TAGS.select { |tag| tag.downcase.in?(new_tags) } + + tags_to_create.each do |name| + project.incident_management_timeline_event_tags.create(name: name) + end + end + + def validate_tags(project, tag_names) + return [] unless tag_names&.any? + + start_time_tag = AUTOCREATE_TAGS[0].downcase + end_time_tag = AUTOCREATE_TAGS[1].downcase + + tag_names_downcased = tag_names.map(&:downcase) + + tags = project.incident_management_timeline_event_tags.by_names(tag_names).pluck_names.map(&:downcase) + + # remove tags from given tag_names and also remove predefined tags which can be auto created + tag_names_downcased - tags - [start_time_tag, end_time_tag] + end end end end |