summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-11-04 11:56:24 +0100
committerJames Lopez <james@jameslopez.es>2016-11-17 08:22:57 +0100
commiteccb6a5e920920bda11104ca608e652f84a944bf (patch)
treebbe4c84faf28be3e7f73fb3ccffa8aae9b3f246d
parent11bad33a4206e482ffac42d75a121182a52e0cb0 (diff)
downloadgitlab-ce-eccb6a5e920920bda11104ca608e652f84a944bf.tar.gz
Refactored test events
Now test events return the builds instead a list of pipelines to avoid calling pipeline.builds per each and get the info. Also, added missing fields/data, URLs, and fixed specs in events spec.
-rw-r--r--lib/gitlab/cycle_analytics/events.rb11
-rw-r--r--lib/gitlab/cycle_analytics/events_fetcher.rb4
-rw-r--r--lib/gitlab/cycle_analytics/metrics_fetcher.rb4
-rw-r--r--lib/gitlab/cycle_analytics/query_config.rb2
-rw-r--r--lib/gitlab/light_url_builder.rb18
-rw-r--r--spec/lib/gitlab/cycle_analytics/events_spec.rb37
6 files changed, 70 insertions, 6 deletions
diff --git a/lib/gitlab/cycle_analytics/events.rb b/lib/gitlab/cycle_analytics/events.rb
index 0819f0eba4b..b39282b1e0f 100644
--- a/lib/gitlab/cycle_analytics/events.rb
+++ b/lib/gitlab/cycle_analytics/events.rb
@@ -32,8 +32,15 @@ module Gitlab
def test_events
@fetcher.fetch(stage: :test).each do |event|
- event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
- event['pipeline'] = ::Ci::Pipeline.find_by_id(event['ci_commit_id']) # we may not have a pipeline
+ build = ::Ci::Build.find(event['id'])
+ event['name'] = build.name
+ event['url'] = Gitlab::LightUrlBuilder.build(entity: :build_url, project: @project, id: build.id)
+ event['branch'] = build.ref
+ event['branch_url'] = Gitlab::LightUrlBuilder.build(entity: :branch_url, project: @project, id: build.ref)
+ event['sha'] = build.short_sha
+ event['commit_url'] = Gitlab::LightUrlBuilder.build(entity: :commit_url, project: @project, id: build.sha)
+ event['date'] = build.started_at
+ event['total_time'] = build.duration
end
end
diff --git a/lib/gitlab/cycle_analytics/events_fetcher.rb b/lib/gitlab/cycle_analytics/events_fetcher.rb
index db613a2d5c0..069b5873736 100644
--- a/lib/gitlab/cycle_analytics/events_fetcher.rb
+++ b/lib/gitlab/cycle_analytics/events_fetcher.rb
@@ -29,6 +29,10 @@ module Gitlab
def review_custom_query(base_query)
base_query.join(user_table).on(mr_table[:author_id].eq(user_table[:id]))
end
+
+ def test_custom_query(base_query)
+ base_query.join(build_table).on(mr_metrics_table[:ci_commit_id].eq(build_table[:commit_id]))
+ end
end
end
end
diff --git a/lib/gitlab/cycle_analytics/metrics_fetcher.rb b/lib/gitlab/cycle_analytics/metrics_fetcher.rb
index c1cbedc045b..11f923cc27d 100644
--- a/lib/gitlab/cycle_analytics/metrics_fetcher.rb
+++ b/lib/gitlab/cycle_analytics/metrics_fetcher.rb
@@ -79,6 +79,10 @@ module Gitlab
def user_table
User.arel_table
end
+
+ def build_table
+ ::CommitStatus.arel_table
+ end
end
end
end
diff --git a/lib/gitlab/cycle_analytics/query_config.rb b/lib/gitlab/cycle_analytics/query_config.rb
index b1ebb452659..bd8ddf8a638 100644
--- a/lib/gitlab/cycle_analytics/query_config.rb
+++ b/lib/gitlab/cycle_analytics/query_config.rb
@@ -55,7 +55,7 @@ module Gitlab
def test
{ start_time_attrs: mr_metrics_table[:latest_build_started_at],
end_time_attrs: mr_metrics_table[:latest_build_finished_at],
- projections: [mr_metrics_table[:ci_commit_id]],
+ projections: [build_table[:id]],
order: mr_table[:created_at]
}
end
diff --git a/lib/gitlab/light_url_builder.rb b/lib/gitlab/light_url_builder.rb
index 7df14acfe85..b33fad12eec 100644
--- a/lib/gitlab/light_url_builder.rb
+++ b/lib/gitlab/light_url_builder.rb
@@ -16,6 +16,7 @@ module Gitlab
end
def url
+ #TODO refactor this
case @entity
when :issue
issue_url
@@ -27,6 +28,10 @@ module Gitlab
commit_url
when :merge_request
mr_url
+ when :build_url
+ namespace_project_build_url(@project.namespace, @project, @id)
+ when :branch_url
+ branch_url
else
raise NotImplementedError.new("No URL builder defined for #{object.class}")
end
@@ -61,5 +66,18 @@ module Gitlab
id: @id
}.merge!(@opts))
end
+
+
+ def pipeline_url
+ namespace_project_build_url({
+ namespace_id: @project.namespace,
+ project_id: @project,
+ id: @id
+ }.merge!(@opts))
+ end
+
+ def branch_url
+ "#{project_url(@project)}/commits/#{@id}"
+ end
end
end
diff --git a/spec/lib/gitlab/cycle_analytics/events_spec.rb b/spec/lib/gitlab/cycle_analytics/events_spec.rb
index 32e7bd7b12c..c83dafbd256 100644
--- a/spec/lib/gitlab/cycle_analytics/events_spec.rb
+++ b/spec/lib/gitlab/cycle_analytics/events_spec.rb
@@ -112,16 +112,47 @@ describe Gitlab::CycleAnalytics::Events do
end
before do
+ create(:ci_build, pipeline: pipeline, status: :success)
+ create(:ci_build, pipeline: pipeline, status: :success)
+
pipeline.run!
pipeline.succeed!
end
- it 'has the build info as a pipeline' do
- expect(subject.test_events.first['pipeline']).to eq(pipeline)
+ it 'has the name' do
+ 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
+ end
+
+ it 'has the URL' do
+ 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
+ end
+
+ it 'has the branch URL' do
+ 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
+ end
+
+ it 'has the commit URL' do
+ 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
end
it 'has the total time' do
- expect(subject.test_events.first['total_time']).to eq('less than a minute')
+ expect(subject.test_events.first['total_time']).not_to be_nil
end
end