diff options
Diffstat (limited to 'app/services/event_create_service.rb')
-rw-r--r-- | app/services/event_create_service.rb | 132 |
1 files changed, 106 insertions, 26 deletions
diff --git a/app/services/event_create_service.rb b/app/services/event_create_service.rb index 522f36cda46..89c3225dbcd 100644 --- a/app/services/event_create_service.rb +++ b/app/services/event_create_service.rb @@ -11,67 +11,81 @@ class EventCreateService IllegalActionError = Class.new(StandardError) def open_issue(issue, current_user) - create_record_event(issue, current_user, Event::CREATED) + create_resource_event(issue, current_user, :opened) + + create_record_event(issue, current_user, :created) end def close_issue(issue, current_user) - create_record_event(issue, current_user, Event::CLOSED) + create_resource_event(issue, current_user, :closed) + + create_record_event(issue, current_user, :closed) end def reopen_issue(issue, current_user) - create_record_event(issue, current_user, Event::REOPENED) + create_resource_event(issue, current_user, :reopened) + + create_record_event(issue, current_user, :reopened) end def open_mr(merge_request, current_user) - create_record_event(merge_request, current_user, Event::CREATED) + create_resource_event(merge_request, current_user, :opened) + + create_record_event(merge_request, current_user, :created) end def close_mr(merge_request, current_user) - create_record_event(merge_request, current_user, Event::CLOSED) + create_resource_event(merge_request, current_user, :closed) + + create_record_event(merge_request, current_user, :closed) end def reopen_mr(merge_request, current_user) - create_record_event(merge_request, current_user, Event::REOPENED) + create_resource_event(merge_request, current_user, :reopened) + + create_record_event(merge_request, current_user, :reopened) end def merge_mr(merge_request, current_user) - create_record_event(merge_request, current_user, Event::MERGED) + create_resource_event(merge_request, current_user, :merged) + + create_record_event(merge_request, current_user, :merged) end def open_milestone(milestone, current_user) - create_record_event(milestone, current_user, Event::CREATED) + create_record_event(milestone, current_user, :created) end def close_milestone(milestone, current_user) - create_record_event(milestone, current_user, Event::CLOSED) + create_record_event(milestone, current_user, :closed) end def reopen_milestone(milestone, current_user) - create_record_event(milestone, current_user, Event::REOPENED) + create_record_event(milestone, current_user, :reopened) end def destroy_milestone(milestone, current_user) - create_record_event(milestone, current_user, Event::DESTROYED) + create_record_event(milestone, current_user, :destroyed) end def leave_note(note, current_user) - create_record_event(note, current_user, Event::COMMENTED) + create_record_event(note, current_user, :commented) end def join_project(project, current_user) - create_event(project, current_user, Event::JOINED) + create_event(project, current_user, :joined) end def leave_project(project, current_user) - create_event(project, current_user, Event::LEFT) + create_event(project, current_user, :left) end def expired_leave_project(project, current_user) - create_event(project, current_user, Event::EXPIRED) + create_event(project, current_user, :expired) end def create_project(project, current_user) - create_event(project, current_user, Event::CREATED) + create_event(project, current_user, :created) end def push(project, current_user, push_data) @@ -82,11 +96,34 @@ class EventCreateService create_push_event(BulkPushEventPayloadService, project, current_user, push_data) end + def save_designs(current_user, create: [], update: []) + created = create.group_by(&:project).flat_map do |project, designs| + Feature.enabled?(:design_activity_events, project) ? designs : [] + end.to_set + updated = update.group_by(&:project).flat_map do |project, designs| + Feature.enabled?(:design_activity_events, project) ? designs : [] + end.to_set + return [] if created.empty? && updated.empty? + + records = created.zip([:created].cycle) + updated.zip([:updated].cycle) + + create_record_events(records, current_user) + end + + def destroy_designs(designs, current_user) + designs = designs.select do |design| + Feature.enabled?(:design_activity_events, design.project) + end + return [] unless designs.present? + + create_record_events(designs.zip([:destroyed].cycle), current_user) + end + # Create a new wiki page event # # @param [WikiPage::Meta] wiki_page_meta The event target # @param [User] author The event author - # @param [Integer] action One of the Event::WIKI_ACTIONS + # @param [Symbol] action One of the Event::WIKI_ACTIONS # # @return a tuple of event and either :found or :created def wiki_event(wiki_page_meta, author, action) @@ -100,7 +137,7 @@ class EventCreateService event = create_record_event(wiki_page_meta, author, action) # Ensure that the event is linked in time to the metadata, for non-deletes - unless action == Event::DESTROYED + unless event.destroyed_action? time_stamp = wiki_page_meta.updated_at event.update_columns(updated_at: time_stamp, created_at: time_stamp) end @@ -111,16 +148,41 @@ class EventCreateService private def existing_wiki_event(wiki_page_meta, action) - if action == Event::DESTROYED + if Event.actions.fetch(action) == Event.actions[:destroyed] most_recent = Event.for_wiki_meta(wiki_page_meta).recent.first - return most_recent if most_recent.present? && most_recent.action == action + return most_recent if most_recent.present? && Event.actions[most_recent.action] == Event.actions[action] else Event.for_wiki_meta(wiki_page_meta).created_at(wiki_page_meta.updated_at).first end end def create_record_event(record, current_user, status) - create_event(record.resource_parent, current_user, status, target_id: record.id, target_type: record.class.name) + create_event(record.resource_parent, current_user, status, + target_id: record.id, target_type: record.class.name) + end + + # If creating several events, this method will insert them all in a single + # statement + # + # @param [[Eventable, Symbol]] a list of pairs of records and a valid status + # @param [User] the author of the event + def create_record_events(pairs, current_user) + base_attrs = { + created_at: Time.now.utc, + updated_at: Time.now.utc, + author_id: current_user.id + } + + attribute_sets = pairs.map do |record, status| + action = Event.actions[status] + raise IllegalActionError, "#{status} is not a valid status" if action.nil? + + parent_attrs(record.resource_parent) + .merge(base_attrs) + .merge(action: action, target_id: record.id, target_type: record.class.name) + end + + Event.insert_all(attribute_sets, returning: %w[id]) end def create_push_event(service_class, project, current_user, push_data) @@ -128,7 +190,7 @@ class EventCreateService # when creating push payload data will result in the event creation being # rolled back as well. event = Event.transaction do - new_event = create_event(project, current_user, Event::PUSHED) + new_event = create_event(project, current_user, :pushed) service_class.new(new_event, push_data).execute @@ -146,16 +208,34 @@ class EventCreateService action: status, author_id: current_user.id ) + attributes.merge!(parent_attrs(resource_parent)) + + Event.create!(attributes) + end + def parent_attrs(resource_parent) resource_parent_attr = case resource_parent when Project - :project + :project_id when Group - :group + :group_id end - attributes[resource_parent_attr] = resource_parent if resource_parent_attr - Event.create!(attributes) + return {} unless resource_parent_attr + + { resource_parent_attr => resource_parent.id } + end + + def create_resource_event(issuable, current_user, status) + return unless state_change_tracking_enabled?(issuable) + + ResourceEvents::ChangeStateService.new(resource: issuable, user: current_user) + .execute(status) + end + + def state_change_tracking_enabled?(issuable) + issuable&.respond_to?(:resource_state_events) && + ::Feature.enabled?(:track_resource_state_change_events, issuable&.project) end end |