summaryrefslogtreecommitdiff
path: root/app
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
parentbd659f70b18be07dac184ca249c7eee17c703e56 (diff)
downloadgitlab-ce-6b2b3d7f49db024678775da3a3685bbdd9bbee65.tar.gz
Resource event model
Diffstat (limited to 'app')
-rw-r--r--app/models/concerns/label_eventable.rb16
-rw-r--r--app/models/issue.rb1
-rw-r--r--app/models/merge_request.rb1
-rw-r--r--app/models/resource_label_event.rb35
-rw-r--r--app/services/resource_events/change_labels_service.rb43
5 files changed, 96 insertions, 0 deletions
diff --git a/app/models/concerns/label_eventable.rb b/app/models/concerns/label_eventable.rb
new file mode 100644
index 00000000000..d22d93448e4
--- /dev/null
+++ b/app/models/concerns/label_eventable.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+# == LabelEventable concern
+#
+# Contains functionality related to objects that support adding/removing labels.
+#
+# This concern is not used yet, it will be used for:
+# https://gitlab.com/gitlab-org/gitlab-ce/issues/48483
+
+module LabelEventable
+ extend ActiveSupport::Concern
+
+ included do
+ has_many :resource_label_events
+ end
+end
diff --git a/app/models/issue.rb b/app/models/issue.rb
index 4715d942c8d..e4ed06f9a69 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -12,6 +12,7 @@ class Issue < ActiveRecord::Base
include TimeTrackable
include ThrottledTouch
include IgnorableColumn
+ include LabelEventable
ignore_column :assignee_id, :branch_name, :deleted_at
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index b4090fd8baf..124ff6b3f91 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -10,6 +10,7 @@ class MergeRequest < ActiveRecord::Base
include EachBatch
include ThrottledTouch
include Gitlab::Utils::StrongMemoize
+ include LabelEventable
ignore_column :locked_at,
:ref_fetched,
diff --git a/app/models/resource_label_event.rb b/app/models/resource_label_event.rb
new file mode 100644
index 00000000000..42c255fcd1e
--- /dev/null
+++ b/app/models/resource_label_event.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+# This model is not used yet, it will be used for:
+# https://gitlab.com/gitlab-org/gitlab-ce/issues/48483
+class ResourceLabelEvent < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :issue
+ belongs_to :merge_request
+ belongs_to :label
+
+ validates :user, presence: true, on: :create
+ validates :label, presence: true, on: :create
+ validate :exactly_one_issuable
+
+ enum action: {
+ add: 1,
+ remove: 2
+ }
+
+ def self.issuable_columns
+ %i(issue_id merge_request_id).freeze
+ end
+
+ def issuable
+ issue || merge_request
+ end
+
+ private
+
+ def exactly_one_issuable
+ if self.class.issuable_columns.count { |attr| self[attr] } != 1
+ errors.add(:base, "Exactly one of #{self.class.issuable_columns.join(', ')} is required")
+ end
+ end
+end
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