Commit f5b792e2 authored by James Lopez's avatar James Lopez

refactored updater and updated specs

parent 0aa47788
......@@ -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
module Gitlab
module CycleAnalytics
class AuthorUpdater < Updater
def self.update!(event_result)
new(event_result, User, :author).update!
end
end
end
end
......@@ -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
......
module Gitlab
module CycleAnalytics
class BuildUpdater < Updater
def self.update!(event_result)
new(event_result, ::Ci::Build, :build, 'id').update!
end
end
end
end
......@@ -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
......
......@@ -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
......
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
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
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment