WIP - refactoring URL builder and events presenter into serializers

This commit is contained in:
James Lopez 2016-11-14 11:52:29 +01:00
parent 1744d633ed
commit 81d0146c4c
7 changed files with 81 additions and 12 deletions

View file

@ -35,10 +35,10 @@ class Projects::CycleAnalytics::EventsController < Projects::ApplicationControll
private private
def render_events(events) def render_events(events_list)
respond_to do |format| respond_to do |format|
format.html format.html
format.json { render json: { events: events } } format.json { render json: { events: events_list } }
end end
end end

View file

@ -0,0 +1,27 @@
class AnalyticsBuildEntity < Grape::Entity
include RequestAwareEntity
expose :name
expose :ref, as: :branch
expose :short_sha
expose :started_at, as: :date
expose :duration, as: :total_time
expose :url do |build|
url_to(:namespace_project_build, build)
end
expose :branch_url do |build|
url_to(:namespace_project_tree, build, build.ref)
end
expose :commit_url do |build|
url_to(:namespace_project_commit, build, build.sha)
end
private
def url_to(route, build, id = nil)
public_send("#{route}_url", build.project.namespace, build.project, id || build)
end
end

View file

@ -0,0 +1,3 @@
class AnalyticsBuildSerializer < BaseSerializer
entity AnalyticsBuildEntity
end

View file

@ -63,15 +63,8 @@ module Gitlab
def parse_build_event(event) def parse_build_event(event)
build = ::Ci::Build.find(event['id']) build = ::Ci::Build.find(event['id'])
event['name'] = build.name
event['url'] = Gitlab::LightUrlBuilder.build(entity: :build, project: @project, id: build.id) #event['author_name'] = build.author.try(:name)
event['branch'] = build.ref
event['branch_url'] = Gitlab::LightUrlBuilder.build(entity: :branch, project: @project, id: build.ref)
event['sha'] = build.short_sha
event['commit_url'] = Gitlab::LightUrlBuilder.build(entity: :commit, project: @project, id: build.sha)
event['date'] = build.started_at
event['total_time'] = build.duration
event['author_name'] = build.author.try(:name)
end end
def first_time_reference_commit(commits, event) def first_time_reference_commit(commits, event)

View file

@ -53,7 +53,7 @@ module Gitlab
end end
def branch_url def branch_url
"#{project_url(@project)}/commits/#{@id}" namespace_project_commit_url(@project.namespace, @project, @id)
end end
def user_url def user_url

View file

@ -0,0 +1,22 @@
require 'spec_helper'
describe AnalyticsBuildEntity do
let(:entity) do
described_class.new(build, request: double)
end
context 'when build is a regular job' do
let(:build) { create(:ci_build) }
subject { entity.as_json }
it 'contains url to build page and retry action' do
expect(subject).to include(:url, :branch_url, :commit_url)
end
it 'does not contain sensitive information' do
expect(subject).not_to include(/token/)
expect(subject).not_to include(/variables/)
end
end
end

View file

@ -0,0 +1,24 @@
require 'spec_helper'
describe AnalyticsBuildSerializer do
let(:serializer) do
described_class
.new(project: project)
.represent(resource)
end
let(:json) { serializer.as_json }
let(:project) { create(:project) }
let(:resource) { create(:ci_build) }
context 'when there is a single object provided' do
it 'it generates payload for single object' do
expect(json).to be_an_instance_of Hash
end
it 'contains important elements of analyticsBuild' do
expect(json)
.to include(:name, :branch, :short_sha, :date, :total_time, :url, :branch_url, :commit_url, :author)
end
end
end