From 3b179bc37b940521522af6d8bf6762c2a9a0e251 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Mon, 14 Nov 2016 14:53:06 +0100 Subject: [PATCH] WIP - refactored events to use build serializer, related spec passing --- app/serializers/analytics_build_entity.rb | 14 ++- lib/gitlab/cycle_analytics/events.rb | 6 +- .../lib/gitlab/cycle_analytics/events_spec.rb | 118 +++++++++--------- .../analytics_build_entity_spec.rb | 9 +- 4 files changed, 79 insertions(+), 68 deletions(-) diff --git a/app/serializers/analytics_build_entity.rb b/app/serializers/analytics_build_entity.rb index 7ed89f1f347..a07f0beccd4 100644 --- a/app/serializers/analytics_build_entity.rb +++ b/app/serializers/analytics_build_entity.rb @@ -2,19 +2,25 @@ class AnalyticsBuildEntity < Grape::Entity include RequestAwareEntity expose :name + expose :id expose :ref, as: :branch expose :short_sha expose :started_at, as: :date expose :duration, as: :total_time + expose :author, using: UserEntity + + expose :branch do + expose :ref, as: :name + + expose :url do |build| + url_to(:namespace_project_tree, build, build.ref) + end + end 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 diff --git a/lib/gitlab/cycle_analytics/events.rb b/lib/gitlab/cycle_analytics/events.rb index 21e295ceeb6..f2fa54f208f 100644 --- a/lib/gitlab/cycle_analytics/events.rb +++ b/lib/gitlab/cycle_analytics/events.rb @@ -30,7 +30,7 @@ module Gitlab end def test_events - @fetcher.fetch(stage: :test).each do |event| + @fetcher.fetch(stage: :test).map do |event| parse_build_event(event) end end @@ -40,7 +40,7 @@ module Gitlab end def staging_events - @fetcher.fetch(stage: :staging).each do |event| + @fetcher.fetch(stage: :staging).map do |event| parse_build_event(event) end end @@ -64,7 +64,7 @@ module Gitlab def parse_build_event(event) build = ::Ci::Build.find(event['id']) - #event['author_name'] = build.author.try(:name) + AnalyticsBuildSerializer.new(project: @project).represent(build).as_json end def first_time_reference_commit(commits, event) diff --git a/spec/lib/gitlab/cycle_analytics/events_spec.rb b/spec/lib/gitlab/cycle_analytics/events_spec.rb index 0fb6a62f10d..ebeea2be39e 100644 --- a/spec/lib/gitlab/cycle_analytics/events_spec.rb +++ b/spec/lib/gitlab/cycle_analytics/events_spec.rb @@ -14,61 +14,61 @@ describe Gitlab::CycleAnalytics::Events do describe '#issue_events' do it 'has the total time' do - expect(subject.issue_events.first['total_time']).to eq('2 days') + expect(subject.issue_events.first[:total_time]).to eq('2 days') end it 'has a title' do - expect(subject.issue_events.first['title']).to eq(context.title) + expect(subject.issue_events.first[:title]).to eq(context.title) end it 'has the URL' do - expect(subject.issue_events.first['url']).not_to be_nil + expect(subject.issue_events.first[:url]).not_to be_nil end it 'has an iid' do - expect(subject.issue_events.first['iid']).to eq(context.iid.to_s) + expect(subject.issue_events.first[:iid]).to eq(context.iid.to_s) end it 'has a created_at timestamp' do - expect(subject.issue_events.first['created_at']).to end_with('ago') + expect(subject.issue_events.first[:created_at]).to end_with('ago') end it "has the author's URL" do - expect(subject.issue_events.first['author_profile_url']).not_to be_nil + expect(subject.issue_events.first[:author_profile_url]).not_to be_nil end it "has the author's avatar URL" do - expect(subject.issue_events.first['author_avatar_url']).not_to be_nil + expect(subject.issue_events.first[:author_avatar_url]).not_to be_nil end it "has the author's name" do - expect(subject.issue_events.first['author_name']).to eq(context.author.name) + expect(subject.issue_events.first[:author_name]).to eq(context.author.name) end end describe '#plan_events' do it 'has a title' do - expect(subject.plan_events.first['title']).not_to be_nil + expect(subject.plan_events.first[:title]).not_to be_nil end it 'has a sha short ID' do - expect(subject.plan_events.first['sha']).not_to be_nil + expect(subject.plan_events.first[:sha]).not_to be_nil end it 'has the total time' do - expect(subject.plan_events.first['total_time']).to eq('less than a minute') + expect(subject.plan_events.first[:total_time]).to eq('less than a minute') end it "has the author's URL" do - expect(subject.plan_events.first['author_profile_url']).not_to be_nil + expect(subject.plan_events.first[:author_profile_url]).not_to be_nil end it "has the author's avatar URL" do - expect(subject.plan_events.first['author_avatar_url']).not_to be_nil + expect(subject.plan_events.first[:author_avatar_url]).not_to be_nil end it "has the author's name" do - expect(subject.plan_events.first['author_name']).not_to be_nil + expect(subject.plan_events.first[:author_name]).not_to be_nil end end @@ -78,31 +78,31 @@ describe Gitlab::CycleAnalytics::Events do end it 'has the total time' do - expect(subject.code_events.first['total_time']).to eq('less than a minute') + expect(subject.code_events.first[:total_time]).to eq('less than a minute') end it 'has a title' do - expect(subject.code_events.first['title']).to eq('Awesome merge_request') + expect(subject.code_events.first[:title]).to eq('Awesome merge_request') end it 'has an iid' do - expect(subject.code_events.first['iid']).to eq(context.iid.to_s) + expect(subject.code_events.first[:iid]).to eq(context.iid.to_s) end it 'has a created_at timestamp' do - expect(subject.code_events.first['created_at']).to end_with('ago') + expect(subject.code_events.first[:created_at]).to end_with('ago') end it "has the author's URL" do - expect(subject.code_events.first['author_profile_url']).not_to be_nil + expect(subject.code_events.first[:author_profile_url]).not_to be_nil end it "has the author's avatar URL" do - expect(subject.code_events.first['author_avatar_url']).not_to be_nil + expect(subject.code_events.first[:author_avatar_url]).not_to be_nil end it "has the author's name" do - expect(subject.code_events.first['author_name']).to eq(context.author.name) + expect(subject.code_events.first[:author_name]).to eq(context.author.name) end end @@ -116,47 +116,47 @@ describe Gitlab::CycleAnalytics::Events do end before do - create(:ci_build, pipeline: pipeline, status: :success) - create(:ci_build, pipeline: pipeline, status: :success) + create(:ci_build, pipeline: pipeline, status: :success, author: user) + create(:ci_build, pipeline: pipeline, status: :success, author: user) pipeline.run! pipeline.succeed! end it 'has the name' do - expect(subject.test_events.first['name']).not_to be_nil + expect(subject.test_events.first[:name]).not_to be_nil end it 'has the ID' do - expect(subject.test_events.first['id']).not_to be_nil + expect(subject.test_events.first[:id]).not_to be_nil end it 'has the URL' do - expect(subject.test_events.first['url']).not_to be_nil + expect(subject.test_events.first[:url]).not_to be_nil end it 'has the branch name' do - expect(subject.test_events.first['branch']).not_to be_nil + expect(subject.test_events.first[:branch]).not_to be_nil end it 'has the branch URL' do - expect(subject.test_events.first['branch_url']).not_to be_nil + expect(subject.test_events.first[:branch][:url]).not_to be_nil end it 'has the short SHA' do - expect(subject.test_events.first['sha']).not_to be_nil + expect(subject.test_events.first[:short_sha]).not_to be_nil end it 'has the commit URL' do - expect(subject.test_events.first['commit_url']).not_to be_nil + expect(subject.test_events.first[:commit_url]).not_to be_nil end it 'has the date' do - expect(subject.test_events.first['date']).not_to be_nil + expect(subject.test_events.first[:date]).not_to be_nil end it 'has the total time' do - expect(subject.test_events.first['total_time']).not_to be_nil + expect(subject.test_events.first[:total_time]).not_to be_nil end end @@ -164,39 +164,39 @@ describe Gitlab::CycleAnalytics::Events do let!(:context) { create(:issue, project: project, created_at: 2.days.ago) } it 'has the total time' do - expect(subject.review_events.first['total_time']).to eq('less than a minute') + expect(subject.review_events.first[:total_time]).to eq('less than a minute') end it 'has a title' do - expect(subject.review_events.first['title']).to eq('Awesome merge_request') + expect(subject.review_events.first[:title]).to eq('Awesome merge_request') end it 'has an iid' do - expect(subject.review_events.first['iid']).to eq(context.iid.to_s) + expect(subject.review_events.first[:iid]).to eq(context.iid.to_s) end it 'has the URL' do - expect(subject.review_events.first['url']).not_to be_nil + expect(subject.review_events.first[:url]).not_to be_nil end it 'has a state' do - expect(subject.review_events.first['state']).not_to be_nil + expect(subject.review_events.first[:state]).not_to be_nil end it 'has a created_at timestamp' do - expect(subject.review_events.first['opened_at']).not_to be_nil + expect(subject.review_events.first[:opened_at]).not_to be_nil end it "has the author's URL" do - expect(subject.review_events.first['author_profile_url']).not_to be_nil + expect(subject.review_events.first[:author_profile_url]).not_to be_nil end it "has the author's avatar URL" do - expect(subject.review_events.first['author_avatar_url']).not_to be_nil + expect(subject.review_events.first[:author_avatar_url]).not_to be_nil end it "has the author's name" do - expect(subject.review_events.first['author_name']).to eq(MergeRequest.first.author.name) + expect(subject.review_events.first[:author_name]).to eq(MergeRequest.first.author.name) end end @@ -221,43 +221,43 @@ describe Gitlab::CycleAnalytics::Events do end it 'has the name' do - expect(subject.staging_events.first['name']).not_to be_nil + expect(subject.staging_events.first[:name]).not_to be_nil end it 'has the ID' do - expect(subject.staging_events.first['id']).not_to be_nil + expect(subject.staging_events.first[:id]).not_to be_nil end it 'has the URL' do - expect(subject.staging_events.first['url']).not_to be_nil + expect(subject.staging_events.first[:url]).not_to be_nil end it 'has the branch name' do - expect(subject.staging_events.first['branch']).not_to be_nil + expect(subject.staging_events.first[:branch]).not_to be_nil end it 'has the branch URL' do - expect(subject.staging_events.first['branch_url']).not_to be_nil + expect(subject.staging_events.first[:branch_url]).not_to be_nil end it 'has the short SHA' do - expect(subject.staging_events.first['sha']).not_to be_nil + expect(subject.staging_events.first[:sha]).not_to be_nil end it 'has the commit URL' do - expect(subject.staging_events.first['commit_url']).not_to be_nil + expect(subject.staging_events.first[:commit_url]).not_to be_nil end it 'has the date' do - expect(subject.staging_events.first['date']).not_to be_nil + expect(subject.staging_events.first[:date]).not_to be_nil end it 'has the total time' do - expect(subject.staging_events.first['total_time']).not_to be_nil + expect(subject.staging_events.first[:total_time]).not_to be_nil end it 'has the author name' do - expect(subject.staging_events.first['author_name']).not_to be_nil + expect(subject.staging_events.first[:author_name]).not_to be_nil end end @@ -270,35 +270,35 @@ describe Gitlab::CycleAnalytics::Events do end it 'has the total time' do - expect(subject.production_events.first['total_time']).to eq('2 days') + expect(subject.production_events.first[:total_time]).to eq('2 days') end it 'has a title' do - expect(subject.production_events.first['title']).to eq(context.title) + expect(subject.production_events.first[:title]).to eq(context.title) end it 'has the URL' do - expect(subject.production_events.first['url']).not_to be_nil + expect(subject.production_events.first[:url]).not_to be_nil end it 'has an iid' do - expect(subject.production_events.first['iid']).to eq(context.iid.to_s) + expect(subject.production_events.first[:iid]).to eq(context.iid.to_s) end it 'has a created_at timestamp' do - expect(subject.production_events.first['created_at']).to end_with('ago') + expect(subject.production_events.first[:created_at]).to end_with('ago') end it "has the author's URL" do - expect(subject.production_events.first['author_profile_url']).not_to be_nil + expect(subject.production_events.first[:author_profile_url]).not_to be_nil end it "has the author's avatar URL" do - expect(subject.production_events.first['author_avatar_url']).not_to be_nil + expect(subject.production_events.first[:author_avatar_url]).not_to be_nil end it "has the author's name" do - expect(subject.production_events.first['author_name']).to eq(context.author.name) + expect(subject.production_events.first[:author_name]).to eq(context.author.name) end end diff --git a/spec/serializers/analytics_build_entity_spec.rb b/spec/serializers/analytics_build_entity_spec.rb index 33653d5b1e0..24040034f68 100644 --- a/spec/serializers/analytics_build_entity_spec.rb +++ b/spec/serializers/analytics_build_entity_spec.rb @@ -6,14 +6,19 @@ describe AnalyticsBuildEntity do end context 'when build is a regular job' do - let(:build) { create(:ci_build) } + let(:user) { create(:user) } + let(:build) { create(:ci_build, author: user) } subject { entity.as_json } - it 'contains url to build page and retry action' do + it 'contains URLs' do expect(subject).to include(:url, :branch_url, :commit_url) end + it 'contains the author' do + expect(subject).to include(:author) + end + it 'does not contain sensitive information' do expect(subject).not_to include(/token/) expect(subject).not_to include(/variables/)