summaryrefslogtreecommitdiff
path: root/app/services/resource_events
diff options
context:
space:
mode:
authorJan Provaznik <jprovaznik@gitlab.com>2018-08-01 08:58:49 +0000
committerSean McGivern <sean@mcgivern.me.uk>2018-08-01 08:58:49 +0000
commit6b2b3d7f49db024678775da3a3685bbdd9bbee65 (patch)
tree2108bf9c394c2e3197ba994ef40e1b02d799bf55 /app/services/resource_events
parentbd659f70b18be07dac184ca249c7eee17c703e56 (diff)
downloadgitlab-ce-6b2b3d7f49db024678775da3a3685bbdd9bbee65.tar.gz
Resource event model
Diffstat (limited to 'app/services/resource_events')
-rw-r--r--app/services/resource_events/change_labels_service.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/app/services/resource_events/change_labels_service.rb b/app/services/resource_events/change_labels_service.rb
new file mode 100644
index 00000000000..8edb0ddb3ed
--- /dev/null
+++ b/app/services/resource_events/change_labels_service.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+# This service is not used yet, it will be used for:
+# https://gitlab.com/gitlab-org/gitlab-ce/issues/48483
+module ResourceEvents
+ class ChangeLabelsService
+ attr_reader :resource, :user
+
+ def initialize(resource, user)
+ @resource, @user = resource, user
+ end
+
+ def execute(added_labels: [], removed_labels: [])
+ label_hash = {
+ resource_column(resource) => resource.id,
+ user_id: user.id,
+ created_at: Time.now
+ }
+
+ labels = added_labels.map do |label|
+ label_hash.merge(label_id: label.id, action: ResourceLabelEvent.actions['add'])
+ end
+ labels += removed_labels.map do |label|
+ label_hash.merge(label_id: label.id, action: ResourceLabelEvent.actions['remove'])
+ end
+
+ Gitlab::Database.bulk_insert(ResourceLabelEvent.table_name, labels)
+ end
+
+ private
+
+ def resource_column(resource)
+ case resource
+ when Issue
+ :issue_id
+ when MergeRequest
+ :merge_request_id
+ else
+ raise ArgumentError, "Unknown resource type #{resource.class.name}"
+ end
+ end
+ end
+end