From 81d0146c4cf0d34b1b81da770483ed864482149c Mon Sep 17 00:00:00 2001 From: James Lopez Date: Mon, 14 Nov 2016 11:52:29 +0100 Subject: [PATCH] WIP - refactoring URL builder and events presenter into serializers --- .../cycle_analytics/events_controller.rb | 4 +-- app/serializers/analytics_build_entity.rb | 27 +++++++++++++++++++ app/serializers/analytics_build_serializer.rb | 3 +++ lib/gitlab/cycle_analytics/events.rb | 11 ++------ lib/gitlab/light_url_builder.rb | 2 +- .../analytics_build_entity_spec.rb | 22 +++++++++++++++ .../analytics_build_serializer_spec.rb | 24 +++++++++++++++++ 7 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 app/serializers/analytics_build_entity.rb create mode 100644 app/serializers/analytics_build_serializer.rb create mode 100644 spec/serializers/analytics_build_entity_spec.rb create mode 100644 spec/serializers/analytics_build_serializer_spec.rb diff --git a/app/controllers/projects/cycle_analytics/events_controller.rb b/app/controllers/projects/cycle_analytics/events_controller.rb index bb9f93908ad..77b49d6c4b9 100644 --- a/app/controllers/projects/cycle_analytics/events_controller.rb +++ b/app/controllers/projects/cycle_analytics/events_controller.rb @@ -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 diff --git a/app/serializers/analytics_build_entity.rb b/app/serializers/analytics_build_entity.rb new file mode 100644 index 00000000000..7ed89f1f347 --- /dev/null +++ b/app/serializers/analytics_build_entity.rb @@ -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 diff --git a/app/serializers/analytics_build_serializer.rb b/app/serializers/analytics_build_serializer.rb new file mode 100644 index 00000000000..f172d67d356 --- /dev/null +++ b/app/serializers/analytics_build_serializer.rb @@ -0,0 +1,3 @@ +class AnalyticsBuildSerializer < BaseSerializer + entity AnalyticsBuildEntity +end diff --git a/lib/gitlab/cycle_analytics/events.rb b/lib/gitlab/cycle_analytics/events.rb index e4c9752f3f9..21e295ceeb6 100644 --- a/lib/gitlab/cycle_analytics/events.rb +++ b/lib/gitlab/cycle_analytics/events.rb @@ -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) diff --git a/lib/gitlab/light_url_builder.rb b/lib/gitlab/light_url_builder.rb index e4517b5610b..0c86b122b88 100644 --- a/lib/gitlab/light_url_builder.rb +++ b/lib/gitlab/light_url_builder.rb @@ -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 diff --git a/spec/serializers/analytics_build_entity_spec.rb b/spec/serializers/analytics_build_entity_spec.rb new file mode 100644 index 00000000000..33653d5b1e0 --- /dev/null +++ b/spec/serializers/analytics_build_entity_spec.rb @@ -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 diff --git a/spec/serializers/analytics_build_serializer_spec.rb b/spec/serializers/analytics_build_serializer_spec.rb new file mode 100644 index 00000000000..24fd94810d4 --- /dev/null +++ b/spec/serializers/analytics_build_serializer_spec.rb @@ -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