WIP - refactoring URL builder and events presenter into serializers
This commit is contained in:
parent
1744d633ed
commit
81d0146c4c
7 changed files with 81 additions and 12 deletions
|
@ -35,10 +35,10 @@ class Projects::CycleAnalytics::EventsController < Projects::ApplicationControll
|
|||
|
||||
private
|
||||
|
||||
def render_events(events)
|
||||
def render_events(events_list)
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.json { render json: { events: events } }
|
||||
format.json { render json: { events: events_list } }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
27
app/serializers/analytics_build_entity.rb
Normal file
27
app/serializers/analytics_build_entity.rb
Normal 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
|
3
app/serializers/analytics_build_serializer.rb
Normal file
3
app/serializers/analytics_build_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class AnalyticsBuildSerializer < BaseSerializer
|
||||
entity AnalyticsBuildEntity
|
||||
end
|
|
@ -63,15 +63,8 @@ module Gitlab
|
|||
|
||||
def parse_build_event(event)
|
||||
build = ::Ci::Build.find(event['id'])
|
||||
event['name'] = build.name
|
||||
event['url'] = Gitlab::LightUrlBuilder.build(entity: :build, project: @project, id: build.id)
|
||||
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)
|
||||
|
||||
#event['author_name'] = build.author.try(:name)
|
||||
end
|
||||
|
||||
def first_time_reference_commit(commits, event)
|
||||
|
|
|
@ -53,7 +53,7 @@ module Gitlab
|
|||
end
|
||||
|
||||
def branch_url
|
||||
"#{project_url(@project)}/commits/#{@id}"
|
||||
namespace_project_commit_url(@project.namespace, @project, @id)
|
||||
end
|
||||
|
||||
def user_url
|
||||
|
|
22
spec/serializers/analytics_build_entity_spec.rb
Normal file
22
spec/serializers/analytics_build_entity_spec.rb
Normal 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
|
24
spec/serializers/analytics_build_serializer_spec.rb
Normal file
24
spec/serializers/analytics_build_serializer_spec.rb
Normal 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
|
Loading…
Reference in a new issue