Commit 226467f2 authored by Matija Čupić's avatar Matija Čupić

Move exposing test reports to standalone endpoint

Moves exposing test reports to PipelinesController#test_report instead
of having it in the PipelineDetailsEntity.
parent d8d3db7f
# frozen_string_literal: true
class Projects::PipelinesController < Projects::ApplicationController
include ::Gitlab::Utils::StrongMemoize
before_action :whitelist_query_limiting, only: [:create, :retry]
before_action :pipeline, except: [:index, :new, :create, :charts]
before_action :set_pipeline_path, only: [:show]
......@@ -151,6 +153,17 @@ class Projects::PipelinesController < Projects::ApplicationController
@counts[:failed] = @project.all_pipelines.failed.count(:all)
end
def test_report
if pipeline_test_report == :error
render json: { status: :error_parsing_report }
return
end
render json: TestReportSerializer
.new(current_user: @current_user)
.represent(pipeline_test_report)
end
private
def serialize_pipelines
......@@ -217,6 +230,14 @@ class Projects::PipelinesController < Projects::ApplicationController
view_context.limited_counter_with_delimiter(finder.execute)
end
def pipeline_test_report
strong_memoize(:pipeline_test_report) do
@pipeline.test_reports
rescue Gitlab::Ci::Parsers::ParserError
:error
end
end
end
Projects::PipelinesController.prepend_if_ee('EE::Projects::PipelinesController')
......@@ -7,11 +7,6 @@ class PipelineDetailsEntity < PipelineEntity
expose :details do
expose :artifacts, using: BuildArtifactEntity
expose :test_reports, using: TestReportEntity do |pipeline|
pipeline.test_reports.total_count.zero? ? nil : pipeline.test_reports
rescue
nil
end
expose :manual_actions, using: BuildActionEntity
expose :scheduled_actions, using: BuildActionEntity
end
......
# frozen_string_literal: true
class TestReportSerializer < BaseSerializer
entity TestReportEntity
end
......@@ -387,6 +387,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
get :builds
get :failures
get :status
get :test_report
Gitlab.ee do
get :security
......
......@@ -398,6 +398,55 @@ describe Projects::PipelinesController do
end
end
describe 'GET test_report.json' do
subject(:get_test_report_json) do
post :test_report, params: {
namespace_id: project.namespace,
project_id: project,
id: pipeline.id
},
format: :json
end
context 'when pipeline does not have a test report' do
let(:pipeline) { create(:ci_pipeline, project: project) }
it 'renders an empty test report' do
get_test_report_json
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['total_count']).to eq(0)
end
end
context 'when pipeline has a test report' do
let(:pipeline) { create(:ci_pipeline, :with_test_reports, project: project) }
it 'renders the test report' do
get_test_report_json
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['total_count']).to eq(4)
end
end
context 'when pipeline has corrupt test reports' do
let(:pipeline) { create(:ci_pipeline, project: project) }
before do
job = create(:ci_build, pipeline: pipeline)
create(:ci_job_artifact, :junit_with_corrupted_data, job: job, project: project)
end
it 'renders the test reports' do
get_test_report_json
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to eq('error_parsing_report')
end
end
end
describe 'GET latest' do
let(:branch_main) { project.repository.branches[0] }
let(:branch_secondary) { project.repository.branches[1] }
......
......@@ -31,7 +31,7 @@ describe PipelineDetailsEntity do
expect(subject[:details])
.to include :duration, :finished_at
expect(subject[:details])
.to include :stages, :artifacts, :test_reports, :manual_actions, :scheduled_actions
.to include :stages, :artifacts, :manual_actions, :scheduled_actions
expect(subject[:details][:status]).to include :icon, :favicon, :text, :label
end
......@@ -113,33 +113,6 @@ describe PipelineDetailsEntity do
end
end
context 'when pipeline has test reports' do
let(:pipeline) { create(:ci_pipeline, :with_test_reports) }
it 'contains test reports' do
expect(subject).to include(:details)
expect(subject[:details]).to include(:test_reports)
expect(subject[:details][:test_reports][:total_time]).to eq(pipeline.test_reports.total_time)
end
context 'when pipeline has corrupt test reports' do
before do
job = create(:ci_build, pipeline: pipeline)
create(:ci_job_artifact, :junit_with_corrupted_data, job: job)
end
it 'does not error out' do
expect { subject }.not_to raise_error
end
it 'shows an empty test_reports section' do
expect(subject).to include(:details)
expect(subject[:details]).to include(:test_reports)
expect(subject[:details][:test_reports]).to eq(nil)
end
end
end
context 'when pipeline has YAML errors' do
let(:pipeline) do
create(:ci_pipeline, config: { rspec: { invalid: :value } })
......
......@@ -158,7 +158,7 @@ describe PipelineSerializer do
it 'verifies number of queries', :request_store do
recorded = ActiveRecord::QueryRecorder.new { subject }
expected_queries = Gitlab.ee? ? 40 : 33
expected_queries = Gitlab.ee? ? 38 : 31
expect(recorded.count).to be_within(2).of(expected_queries)
expect(recorded.cached_count).to eq(0)
......@@ -179,7 +179,7 @@ describe PipelineSerializer do
# pipeline. With the same ref this check is cached but if refs are
# different then there is an extra query per ref
# https://gitlab.com/gitlab-org/gitlab-foss/issues/46368
expected_queries = Gitlab.ee? ? 46 : 40
expected_queries = Gitlab.ee? ? 44 : 38
expect(recorded.count).to be_within(2).of(expected_queries)
expect(recorded.cached_count).to eq(0)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment