summaryrefslogtreecommitdiff
path: root/app/services/resource_events
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-19 18:09:10 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-19 18:09:10 +0000
commit33795139ea8e72756bee3675b4e16387425e6ab1 (patch)
tree3ca568fca61482e57810ee30ad5ce4b964a82c4e /app/services/resource_events
parentc7e385e282bcb8505589bce526e692b7bb819ffa (diff)
downloadgitlab-ce-33795139ea8e72756bee3675b4e16387425e6ab1.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/resource_events')
-rw-r--r--app/services/resource_events/change_milestone_service.rb50
-rw-r--r--app/services/resource_events/merge_into_notes_service.rb9
-rw-r--r--app/services/resource_events/synthetic_milestone_notes_builder_service.rb31
3 files changed, 89 insertions, 1 deletions
diff --git a/app/services/resource_events/change_milestone_service.rb b/app/services/resource_events/change_milestone_service.rb
new file mode 100644
index 00000000000..bad7d002c65
--- /dev/null
+++ b/app/services/resource_events/change_milestone_service.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+module ResourceEvents
+ class ChangeMilestoneService
+ attr_reader :resource, :user, :event_created_at, :resource_args
+
+ def initialize(resource:, user:, created_at: Time.now)
+ @resource = resource
+ @user = user
+ @event_created_at = created_at
+
+ @resource_args = {
+ user_id: user.id,
+ created_at: event_created_at
+ }
+ end
+
+ def execute
+ args = build_resource_args
+
+ action = if milestone.nil?
+ :remove
+ else
+ :add
+ end
+
+ record = args.merge(milestone_id: milestone&.id, action: ResourceMilestoneEvent.actions[action])
+
+ create_event(record)
+ end
+
+ private
+
+ def milestone
+ resource&.milestone
+ end
+
+ def create_event(record)
+ ResourceMilestoneEvent.create(record)
+
+ resource.expire_note_etag_cache
+ end
+
+ def build_resource_args
+ key = resource.class.name.underscore.foreign_key
+
+ resource_args.merge(key => resource.id, state: ResourceMilestoneEvent.states[resource.state])
+ end
+ end
+end
diff --git a/app/services/resource_events/merge_into_notes_service.rb b/app/services/resource_events/merge_into_notes_service.rb
index 47948fcff6e..4aa9bb80229 100644
--- a/app/services/resource_events/merge_into_notes_service.rb
+++ b/app/services/resource_events/merge_into_notes_service.rb
@@ -9,6 +9,11 @@ module ResourceEvents
class MergeIntoNotesService
include Gitlab::Utils::StrongMemoize
+ SYNTHETIC_NOTE_BUILDER_SERVICES = [
+ SyntheticLabelNotesBuilderService,
+ SyntheticMilestoneNotesBuilderService
+ ].freeze
+
attr_reader :resource, :current_user, :params
def initialize(resource, current_user, params = {})
@@ -24,7 +29,9 @@ module ResourceEvents
private
def synthetic_notes
- SyntheticLabelNotesBuilderService.new(resource, current_user, params).execute
+ SYNTHETIC_NOTE_BUILDER_SERVICES.flat_map do |service|
+ service.new(resource, current_user, params).execute
+ end
end
end
end
diff --git a/app/services/resource_events/synthetic_milestone_notes_builder_service.rb b/app/services/resource_events/synthetic_milestone_notes_builder_service.rb
new file mode 100644
index 00000000000..ad58417834e
--- /dev/null
+++ b/app/services/resource_events/synthetic_milestone_notes_builder_service.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+# We store events about resource milestone changes in a separate table,
+# but we still want to display notes about milestone changes
+# as classic system notes in UI. This service generates "synthetic" notes for
+# milestone event changes.
+
+module ResourceEvents
+ class SyntheticMilestoneNotesBuilderService < BaseSyntheticNotesBuilderService
+ private
+
+ def synthetic_notes
+ return [] unless tracking_enabled?
+
+ milestone_change_events.map do |event|
+ MilestoneNote.from_event(event, resource: resource, resource_parent: resource_parent)
+ end
+ end
+
+ def milestone_change_events
+ return [] unless resource.respond_to?(:resource_milestone_events)
+
+ events = resource.resource_milestone_events.includes(user: :status) # rubocop: disable CodeReuse/ActiveRecord
+ since_fetch_at(events)
+ end
+
+ def tracking_enabled?
+ ::Feature.enabled?(:track_resource_milestone_change_events, resource.project)
+ end
+ end
+end