summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-11-18 13:00:38 +0100
committerJames Lopez <james@jameslopez.es>2016-11-18 13:00:38 +0100
commitf5b792e22eb7bd4ecafcd2ad3bc7a388abb36ffc (patch)
treec647334269f0d4ae414d13e77711f8279aa57697
parent0aa477884c6ac3298f79f62e08e63294d81735a3 (diff)
downloadgitlab-ce-f5b792e22eb7bd4ecafcd2ad3bc7a388abb36ffc.tar.gz
refactored updater and updated specs
-rw-r--r--app/controllers/projects/cycle_analytics/events_controller.rb10
-rw-r--r--lib/gitlab/cycle_analytics/author_updater.rb9
-rw-r--r--lib/gitlab/cycle_analytics/base_event.rb2
-rw-r--r--lib/gitlab/cycle_analytics/build_updater.rb9
-rw-r--r--lib/gitlab/cycle_analytics/staging_event.rb2
-rw-r--r--lib/gitlab/cycle_analytics/updater.rb14
-rw-r--r--spec/lib/gitlab/cycle_analytics/author_updater_spec.rb12
-rw-r--r--spec/lib/gitlab/cycle_analytics/build_updater_spec.rb12
-rw-r--r--spec/lib/gitlab/cycle_analytics/updater_spec.rb25
9 files changed, 35 insertions, 60 deletions
diff --git a/app/controllers/projects/cycle_analytics/events_controller.rb b/app/controllers/projects/cycle_analytics/events_controller.rb
index d5e98228ddc..13b3eec761f 100644
--- a/app/controllers/projects/cycle_analytics/events_controller.rb
+++ b/app/controllers/projects/cycle_analytics/events_controller.rb
@@ -4,7 +4,7 @@ module Projects
include CycleAnalyticsParams
before_action :authorize_read_cycle_analytics!
- before_action :authorize_builds!, only: [:test, :staging]
+ before_action :authorize_read_build!, only: [:test, :staging]
before_action :authorize_read_issue!, only: [:issue, :production]
before_action :authorize_read_merge_request!, only: [:code, :review]
@@ -60,14 +60,6 @@ module Projects
params[:events].slice(:start_date, :branch_name)
end
-
- def authorize_builds!
- return access_denied! unless can?(current_user, :read_build, project)
- end
-
- def authorize_read_issue!
- return access_denied! unless can?(current_user, :read_issue, project)
- end
end
end
end
diff --git a/lib/gitlab/cycle_analytics/author_updater.rb b/lib/gitlab/cycle_analytics/author_updater.rb
deleted file mode 100644
index 87d26701d3a..00000000000
--- a/lib/gitlab/cycle_analytics/author_updater.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-module Gitlab
- module CycleAnalytics
- class AuthorUpdater < Updater
- def self.update!(event_result)
- new(event_result, User, :author).update!
- end
- end
- end
-end
diff --git a/lib/gitlab/cycle_analytics/base_event.rb b/lib/gitlab/cycle_analytics/base_event.rb
index cedc73142fa..486139b1687 100644
--- a/lib/gitlab/cycle_analytics/base_event.rb
+++ b/lib/gitlab/cycle_analytics/base_event.rb
@@ -30,7 +30,7 @@ module Gitlab
def update_author!
return unless event_result.any? && event_result.first['author_id']
- AuthorUpdater.update!(event_result)
+ Updater.update!(event_result, from: 'author_id', to: 'author', klass: User)
end
def event_result
diff --git a/lib/gitlab/cycle_analytics/build_updater.rb b/lib/gitlab/cycle_analytics/build_updater.rb
deleted file mode 100644
index c1190d9e7a9..00000000000
--- a/lib/gitlab/cycle_analytics/build_updater.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-module Gitlab
- module CycleAnalytics
- class BuildUpdater < Updater
- def self.update!(event_result)
- new(event_result, ::Ci::Build, :build, 'id').update!
- end
- end
- end
-end
diff --git a/lib/gitlab/cycle_analytics/staging_event.rb b/lib/gitlab/cycle_analytics/staging_event.rb
index 6c42b1a3a60..a1f30b716f6 100644
--- a/lib/gitlab/cycle_analytics/staging_event.rb
+++ b/lib/gitlab/cycle_analytics/staging_event.rb
@@ -12,7 +12,7 @@ module Gitlab
end
def fetch
- BuildUpdater.update!(event_result)
+ Updater.update!(event_result, from: 'id', to: 'build', klass: ::Ci::Build)
super
end
diff --git a/lib/gitlab/cycle_analytics/updater.rb b/lib/gitlab/cycle_analytics/updater.rb
index 38cbfa03030..953268ebd46 100644
--- a/lib/gitlab/cycle_analytics/updater.rb
+++ b/lib/gitlab/cycle_analytics/updater.rb
@@ -5,25 +5,25 @@ module Gitlab
new(*args).update!
end
- def initialize(event_result, update_klass, update_key, column = nil)
+ def initialize(event_result, from:, to:, klass:)
@event_result = event_result
- @update_klass = update_klass
- @update_key = update_key.to_s
- @column = column || "#{@update_key}_id"
+ @klass = klass
+ @from = from
+ @to = to
end
def update!
@event_result.each do |event|
- event[@update_key] = items[event.delete(@column).to_i].first
+ event[@to] = items[event.delete(@from).to_i].first
end
end
def result_ids
- @event_result.map { |event| event[@column] }
+ @event_result.map { |event| event[@from] }
end
def items
- @items ||= @update_klass.find(result_ids).group_by { |item| item['id'] }
+ @items ||= @klass.find(result_ids).group_by { |item| item['id'] }
end
end
end
diff --git a/spec/lib/gitlab/cycle_analytics/author_updater_spec.rb b/spec/lib/gitlab/cycle_analytics/author_updater_spec.rb
deleted file mode 100644
index f9e4d1714e6..00000000000
--- a/spec/lib/gitlab/cycle_analytics/author_updater_spec.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-require 'spec_helper'
-
-describe Gitlab::CycleAnalytics::AuthorUpdater do
- let(:user) { create(:user) }
- let(:events) { [{ 'author_id' => user.id }] }
-
- it 'maps the correct user' do
- described_class.update!(events)
-
- expect(events.first['author']).to eq(user)
- end
-end
diff --git a/spec/lib/gitlab/cycle_analytics/build_updater_spec.rb b/spec/lib/gitlab/cycle_analytics/build_updater_spec.rb
deleted file mode 100644
index 70886d7b453..00000000000
--- a/spec/lib/gitlab/cycle_analytics/build_updater_spec.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-require 'spec_helper'
-
-describe Gitlab::CycleAnalytics::BuildUpdater do
- let(:build) { create(:ci_build) }
- let(:events) { [{ 'id' => build.id }] }
-
- it 'maps the correct build' do
- described_class.update!(events)
-
- expect(events.first['build']).to eq(build)
- end
-end
diff --git a/spec/lib/gitlab/cycle_analytics/updater_spec.rb b/spec/lib/gitlab/cycle_analytics/updater_spec.rb
new file mode 100644
index 00000000000..eff54cd3692
--- /dev/null
+++ b/spec/lib/gitlab/cycle_analytics/updater_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe Gitlab::CycleAnalytics::Updater do
+ describe 'updates authors' do
+ let(:user) { create(:user) }
+ let(:events) { [{ 'author_id' => user.id }] }
+
+ it 'maps the correct user' do
+ described_class.update!(events, from: 'author_id', to: 'author', klass: User)
+
+ expect(events.first['author']).to eq(user)
+ end
+ end
+
+ describe 'updates builds' do
+ let(:build) { create(:ci_build) }
+ let(:events) { [{ 'id' => build.id }] }
+
+ it 'maps the correct build' do
+ described_class.update!(events, from: 'id', to: 'build', klass: ::Ci::Build)
+
+ expect(events.first['build']).to eq(build)
+ end
+ end
+end