Commit cb353d65 authored by James Lopez's avatar James Lopez

added new build updater, specs and refactored allowed_ids

parent c76ef847
......@@ -5,6 +5,8 @@ module Projects
before_action :authorize_read_cycle_analytics!
before_action :authorize_builds!, only: [:test, :staging]
before_action :authorize_read_issue!, only: [:issue, :production]
before_action :authorize_read_merge_request!, only: [:code, :review]
def issue
render_events(events.issue_events)
......@@ -60,7 +62,11 @@ module Projects
end
def authorize_builds!
return access_denied! unless current_user.can?(:read_build, project)
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
......
module Gitlab
module CycleAnalytics
class AuthorUpdater
def self.update!(*args)
new(*args).update!
end
def initialize(event_result)
@event_result = event_result
end
def update!
@event_result.each do |event|
event['author'] = users[event.delete('author_id').to_i].first
end
end
def user_ids
@event_result.map { |event| event['author_id'] }
end
def users
@users ||= User.find(user_ids).group_by { |user| user['id'] }
class AuthorUpdater < Updater
def self.update!(event_result)
new(event_result, User, :author).update!
end
end
end
......
module Gitlab
module CycleAnalytics
class BuildUpdater < Updater
def self.update!(event_result)
new(event_result, ::Ci::Build, :build, 'id').update!
end
end
end
end
module Gitlab
module CycleAnalytics
class CodeEvent < BaseEvent
include MergeRequestAllowed
def initialize(*args)
@stage = :code
@start_time_attrs = issue_metrics_table[:first_mentioned_in_commit_at]
......@@ -21,10 +23,6 @@ module Gitlab
def serialize(event)
AnalyticsMergeRequestSerializer.new(project: @project).represent(event).as_json
end
def allowed_ids
@allowed_ids ||= MergeRequestsFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end
end
module Gitlab
module CycleAnalytics
module IssueAllowed
def allowed_ids
@allowed_ids ||= IssuesFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end
end
module Gitlab
module CycleAnalytics
class IssueEvent < BaseEvent
include IssueAllowed
def initialize(*args)
@stage = :issue
@start_time_attrs = issue_table[:created_at]
......@@ -20,10 +22,6 @@ module Gitlab
def serialize(event)
AnalyticsIssueSerializer.new(project: @project).represent(event).as_json
end
def allowed_ids
@allowed_ids ||= IssuesFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end
end
module Gitlab
module CycleAnalytics
module MergeRequestAllowed
def allowed_ids
@allowed_ids ||= MergeRequestsFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end
end
module Gitlab
module CycleAnalytics
class ProductionEvent < BaseEvent
include IssueAllowed
def initialize(*args)
@stage = :production
@start_time_attrs = issue_table[:created_at]
......@@ -19,10 +21,6 @@ module Gitlab
def serialize(event)
AnalyticsIssueSerializer.new(project: @project).represent(event).as_json
end
def allowed_ids
@allowed_ids ||= IssuesFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end
end
module Gitlab
module CycleAnalytics
class ReviewEvent < BaseEvent
include MergeRequestAllowed
def initialize(*args)
@stage = :review
@start_time_attrs = mr_table[:created_at]
......@@ -18,10 +20,6 @@ module Gitlab
def serialize(event)
AnalyticsMergeRequestSerializer.new(project: @project).represent(event).as_json
end
def allowed_ids
@allowed_ids ||= MergeRequestsFinder.new(@options[:current_user], project_id: @project.id).execute.where(id: event_result_ids).pluck(:id)
end
end
end
end
......@@ -11,6 +11,12 @@ module Gitlab
super(*args)
end
def fetch
BuildUpdater.update!(event_result)
super
end
def custom_query(base_query)
base_query.join(build_table).on(mr_metrics_table[:pipeline_id].eq(build_table[:commit_id]))
end
......@@ -18,9 +24,7 @@ module Gitlab
private
def serialize(event)
build = ::Ci::Build.find(event['id'])
AnalyticsBuildSerializer.new.represent(build).as_json
AnalyticsBuildSerializer.new.represent(event['build']).as_json
end
end
end
......
module Gitlab
module CycleAnalytics
class Updater
def self.update!(*args)
new(*args).update!
end
def initialize(event_result, update_klass, update_key, column = nil)
@event_result = event_result
@update_klass = update_klass
@update_key = update_key.to_s
@column = column || "#{@update_key}_id"
end
def update!
@event_result.each do |event|
event[@update_key] = items[event.delete(@column).to_i].first
end
end
def result_ids
@event_result.map { |event| event[@column] }
end
def items
@items ||= @update_klass.find(result_ids).group_by { |item| item['id'] }
end
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 user' do
described_class.update!(events)
expect(events.first['build']).to eq(build)
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