Commit 333b881d authored by Erick Bajao's avatar Erick Bajao

Return builds with coverage in MR widget JSON

Adds `builds_with_coverage` in the MR widget serializer to be used by
the frontend. These are builds in the head pipeline with
coverage values.
parent fc618d94
......@@ -860,6 +860,10 @@ module Ci
builds.latest.with_reports(reports_scope)
end
def builds_with_coverage
builds.with_coverage
end
def has_reports?(reports_scope)
complete? && latest_report_builds(reports_scope).exists?
end
......
......@@ -295,6 +295,7 @@ class MergeRequest < ApplicationRecord
alias_attribute :auto_merge_enabled, :merge_when_pipeline_succeeds
alias_method :issuing_parent, :target_project
delegate :builds_with_coverage, to: :head_pipeline, allow_nil: true
delegate :active?, to: :head_pipeline, prefix: true, allow_nil: true
delegate :success?, :active?, to: :actual_head_pipeline, prefix: true, allow_nil: true
......
# frozen_string_literal: true
class BuildCoverageEntity < Grape::Entity
expose :name, :coverage
end
......@@ -73,6 +73,8 @@ class MergeRequestPollWidgetEntity < Grape::Entity
presenter(merge_request).pipeline_coverage_delta
end
expose :builds_with_coverage, using: BuildCoverageEntity
expose :cancel_auto_merge_path do |merge_request|
presenter(merge_request).cancel_auto_merge_path
end
......
---
title: Return builds with coverage in MR widget JSON response
merge_request: 40533
author:
type: added
......@@ -49,6 +49,7 @@ RSpec.describe Projects::MergeRequests::ContentController do
do_request(:widget)
expect(response).to match_response_schema('entities/merge_request_poll_widget')
expect(response.headers['Poll-Interval']).to eq('10000')
end
......@@ -64,6 +65,20 @@ RSpec.describe Projects::MergeRequests::ContentController do
expect(response.headers['Poll-Interval']).to eq('300000')
end
end
context 'with coverage data' do
let(:merge_request) { create(:merge_request, target_project: project, source_project: project, head_pipeline: head_pipeline) }
let!(:base_pipeline) { create(:ci_empty_pipeline, project: project, ref: merge_request.target_branch, sha: merge_request.diff_base_sha) }
let!(:head_pipeline) { create(:ci_empty_pipeline, project: project) }
let!(:rspec_base) { create(:ci_build, name: 'rspec', coverage: 93.1, pipeline: base_pipeline) }
let!(:rspec_head) { create(:ci_build, name: 'rspec', coverage: 97.1, pipeline: head_pipeline) }
it 'renders widget MR entity as json' do
do_request(:widget)
expect(response).to match_response_schema('entities/merge_request_poll_widget')
end
end
end
end
......
......@@ -22,6 +22,14 @@
"only_allow_merge_if_pipeline_succeeds": { "type": "boolean" },
"has_ci": { "type": "boolean" },
"ci_status": { "type": ["string", "null"] },
"pipeline_coverage_delta": { "type": ["float", "null"] },
"builds_with_coverage": {
"type": ["array", "null"],
"items": {
"type": "object",
"required": ["name", "coverage"]
}
},
"cancel_auto_merge_path": { "type": ["string", "null"] },
"test_reports_path": { "type": ["string", "null"] },
"create_issue_to_resolve_discussions_path": { "type": ["string", "null"] },
......
......@@ -4112,4 +4112,28 @@ RSpec.describe MergeRequest, factory_default: :keep do
expect(context[:label_url_method]).to eq(:project_merge_requests_url)
end
end
describe '#builds_with_coverage' do
let(:builds) { subject.builds_with_coverage }
let(:head_pipeline) { create(:ci_empty_pipeline) }
let!(:rspec) { create(:ci_build, name: 'rspec', coverage: 97.1, pipeline: head_pipeline) }
let!(:jest) { create(:ci_build, name: 'jest', coverage: 94.1, pipeline: head_pipeline) }
let!(:karma) { create(:ci_build, name: 'karma', coverage: nil, pipeline: head_pipeline) }
context 'when head_pipeline is not present' do
it 'returns nil' do
expect(builds).to be_nil
end
end
context 'when head_pipeline is present' do
before do
subject.update_attribute(:head_pipeline_id, head_pipeline.id)
end
it 'returns the builds with coverage' do
expect(builds).to match_array([rspec, jest])
end
end
end
end
......@@ -285,4 +285,25 @@ RSpec.describe MergeRequestPollWidgetEntity do
end
end
end
describe '#builds_with_coverage' do
let(:result) { subject[:builds_with_coverage] }
let(:builds) do
[
double(name: 'rspec', coverage: 91.5),
double(name: 'jest', coverage: 94.1)
]
end
before do
allow(resource).to receive(:builds_with_coverage).and_return(builds)
end
it 'serializes the builds with coverage' do
expect(result).to eq([
{ name: 'rspec', coverage: 91.5 },
{ name: 'jest', coverage: 94.1 }
])
end
end
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