diff options
Diffstat (limited to 'app/services/incident_management/timeline_events')
4 files changed, 70 insertions, 17 deletions
diff --git a/app/services/incident_management/timeline_events/base_service.rb b/app/services/incident_management/timeline_events/base_service.rb index 7168e2fdd38..e0ca4320091 100644 --- a/app/services/incident_management/timeline_events/base_service.rb +++ b/app/services/incident_management/timeline_events/base_service.rb @@ -5,6 +5,8 @@ module IncidentManagement class BaseService include Gitlab::Utils::UsageData + AUTOCREATE_TAGS = [TimelineEventTag::START_TIME_TAG_NAME, TimelineEventTag::END_TIME_TAG_NAME].freeze + def allowed? user&.can?(:admin_incident_management_timeline_event, incident) end @@ -24,6 +26,33 @@ module IncidentManagement def error_in_save(timeline_event) error(timeline_event.errors.full_messages.to_sentence) end + + def track_timeline_event(event, project) + namespace = project.namespace + track_usage_event(event, user.id) + + return unless Feature.enabled?(:route_hll_to_snowplow_phase2, namespace) + + Gitlab::Tracking.event( + self.class.to_s, + event, + project: project, + namespace: namespace, + user: user, + label: 'redis_hll_counters.incident_management.incident_management_total_unique_counts_monthly', + context: [Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: event).to_context] + ) + 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 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 index 71ff5b64515..06e8fc32335 100644 --- a/app/services/incident_management/timeline_events/create_service.rb +++ b/app/services/incident_management/timeline_events/create_service.rb @@ -5,7 +5,6 @@ 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) @@ -106,7 +105,7 @@ module IncidentManagement create_timeline_event_tag_links(timeline_event, params[:timeline_event_tag_names]) - track_usage_event(:incident_management_timeline_event_created, user.id) + track_timeline_event("incident_management_timeline_event_created", project) success(timeline_event) else @@ -153,16 +152,6 @@ module IncidentManagement 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? diff --git a/app/services/incident_management/timeline_events/destroy_service.rb b/app/services/incident_management/timeline_events/destroy_service.rb index e1c6bbbdb85..aba46cdda27 100644 --- a/app/services/incident_management/timeline_events/destroy_service.rb +++ b/app/services/incident_management/timeline_events/destroy_service.rb @@ -18,7 +18,7 @@ module IncidentManagement if timeline_event.destroy add_system_note(incident, user) - track_usage_event(:incident_management_timeline_event_deleted, user.id) + track_timeline_event('incident_management_timeline_event_deleted', project) success(timeline_event) else error_in_save(timeline_event) diff --git a/app/services/incident_management/timeline_events/update_service.rb b/app/services/incident_management/timeline_events/update_service.rb index 8d4e29c6857..4949a5a0bd1 100644 --- a/app/services/incident_management/timeline_events/update_service.rb +++ b/app/services/incident_management/timeline_events/update_service.rb @@ -13,21 +13,41 @@ module IncidentManagement def initialize(timeline_event, user, params) @timeline_event = timeline_event @incident = timeline_event.incident + @project = incident.project @user = user @note = params[:note] @occurred_at = params[:occurred_at] @validation_context = VALIDATION_CONTEXT + @timeline_event_tags = params[:timeline_event_tag_names] end def execute return error_no_permissions unless allowed? - timeline_event.assign_attributes(update_params) + unless timeline_event_tags.nil? + auto_create_predefined_tags(timeline_event_tags) - if timeline_event.save(context: validation_context) + # Refetches the tag objects to consider predefined tags as well + new_tags = timeline_event + .project + .incident_management_timeline_event_tags + .by_names(timeline_event_tags) + + non_existing_tags = validate_tags(new_tags) + + return error("#{_("Following tags don't exist")}: #{non_existing_tags}") if non_existing_tags.any? + end + + begin + timeline_event_saved = update_timeline_event_and_event_tags(new_tags) + rescue ActiveRecord::RecordInvalid + error_in_save(timeline_event) + end + + if timeline_event_saved add_system_note(timeline_event) - track_usage_event(:incident_management_timeline_event_edited, user.id) + track_timeline_event('incident_management_timeline_event_edited', timeline_event.project) success(timeline_event) else error_in_save(timeline_event) @@ -36,7 +56,18 @@ module IncidentManagement private - attr_reader :timeline_event, :incident, :user, :note, :occurred_at, :validation_context + attr_reader :timeline_event, :incident, :project, :user, + :note, :occurred_at, :validation_context, :timeline_event_tags + + def update_timeline_event_and_event_tags(new_tags) + ApplicationRecord.transaction do + timeline_event.timeline_event_tags = new_tags unless timeline_event_tags.nil? + + timeline_event.assign_attributes(update_params) + + timeline_event.save!(context: validation_context) + end + end def update_params { updated_by_user: user, note: note, occurred_at: occurred_at }.compact @@ -61,6 +92,10 @@ module IncidentManagement :none end + def validate_tags(new_tags) + timeline_event_tags.map(&:downcase) - new_tags.map(&:name).map(&:downcase) + end + def allowed? user&.can?(:edit_incident_management_timeline_event, timeline_event) end |