Commit cb353d65 authored by James Lopez's avatar James Lopez

added new build updater, specs and refactored allowed_ids

parent c76ef847
...@@ -5,7 +5,9 @@ module Projects ...@@ -5,7 +5,9 @@ module Projects
before_action :authorize_read_cycle_analytics! before_action :authorize_read_cycle_analytics!
before_action :authorize_builds!, only: [:test, :staging] 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 def issue
render_events(events.issue_events) render_events(events.issue_events)
end end
...@@ -60,7 +62,11 @@ module Projects ...@@ -60,7 +62,11 @@ module Projects
end end
def authorize_builds! 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 end
end end
......
module Gitlab module Gitlab
module CycleAnalytics module CycleAnalytics
class AuthorUpdater class AuthorUpdater < Updater
def self.update!(*args) def self.update!(event_result)
new(*args).update! new(event_result, User, :author).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'] }
end end
end 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 Gitlab
module CycleAnalytics module CycleAnalytics
class CodeEvent < BaseEvent class CodeEvent < BaseEvent
include MergeRequestAllowed
def initialize(*args) def initialize(*args)
@stage = :code @stage = :code
@start_time_attrs = issue_metrics_table[:first_mentioned_in_commit_at] @start_time_attrs = issue_metrics_table[:first_mentioned_in_commit_at]
...@@ -21,10 +23,6 @@ module Gitlab ...@@ -21,10 +23,6 @@ module Gitlab
def serialize(event) def serialize(event)
AnalyticsMergeRequestSerializer.new(project: @project).represent(event).as_json AnalyticsMergeRequestSerializer.new(project: @project).represent(event).as_json
end 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 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 Gitlab
module CycleAnalytics module CycleAnalytics
class IssueEvent < BaseEvent class IssueEvent < BaseEvent
include IssueAllowed
def initialize(*args) def initialize(*args)
@stage = :issue @stage = :issue
@start_time_attrs = issue_table[:created_at] @start_time_attrs = issue_table[:created_at]
...@@ -20,10 +22,6 @@ module Gitlab ...@@ -20,10 +22,6 @@ module Gitlab
def serialize(event) def serialize(event)
AnalyticsIssueSerializer.new(project: @project).represent(event).as_json AnalyticsIssueSerializer.new(project: @project).represent(event).as_json
end 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 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 Gitlab
module CycleAnalytics module CycleAnalytics
class ProductionEvent < BaseEvent class ProductionEvent < BaseEvent
include IssueAllowed
def initialize(*args) def initialize(*args)
@stage = :production @stage = :production
@start_time_attrs = issue_table[:created_at] @start_time_attrs = issue_table[:created_at]
...@@ -19,10 +21,6 @@ module Gitlab ...@@ -19,10 +21,6 @@ module Gitlab
def serialize(event) def serialize(event)
AnalyticsIssueSerializer.new(project: @project).represent(event).as_json AnalyticsIssueSerializer.new(project: @project).represent(event).as_json
end 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 end
end end
module Gitlab module Gitlab
module CycleAnalytics module CycleAnalytics
class ReviewEvent < BaseEvent class ReviewEvent < BaseEvent
include MergeRequestAllowed
def initialize(*args) def initialize(*args)
@stage = :review @stage = :review
@start_time_attrs = mr_table[:created_at] @start_time_attrs = mr_table[:created_at]
...@@ -18,10 +20,6 @@ module Gitlab ...@@ -18,10 +20,6 @@ module Gitlab
def serialize(event) def serialize(event)
AnalyticsMergeRequestSerializer.new(project: @project).represent(event).as_json AnalyticsMergeRequestSerializer.new(project: @project).represent(event).as_json
end 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 end
end end
...@@ -11,6 +11,12 @@ module Gitlab ...@@ -11,6 +11,12 @@ module Gitlab
super(*args) super(*args)
end end
def fetch
BuildUpdater.update!(event_result)
super
end
def custom_query(base_query) def custom_query(base_query)
base_query.join(build_table).on(mr_metrics_table[:pipeline_id].eq(build_table[:commit_id])) base_query.join(build_table).on(mr_metrics_table[:pipeline_id].eq(build_table[:commit_id]))
end end
...@@ -18,9 +24,7 @@ module Gitlab ...@@ -18,9 +24,7 @@ module Gitlab
private private
def serialize(event) def serialize(event)
build = ::Ci::Build.find(event['id']) AnalyticsBuildSerializer.new.represent(event['build']).as_json
AnalyticsBuildSerializer.new.represent(build).as_json
end end
end 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