Commit d2f4c3ca authored by Douwe Maan's avatar Douwe Maan

Merge branch 'fix/improve-test-coverage-badge-pipelines' into 'master'

Render coverage badge using latest successful pipeline

## What does this MR do?

This MR make test coverage badge to report value for the latest successful pipeline, instead of the latest one, regardless the status.

Latest pipeline is often running, which makes coverage report inaccurate. Latest pipeline can be also the failed one, which may mean that not all stages got processed, therefore coverage report can be inaccurate as well.

This also improves coverage badge performance because it is not necessary to touch repository to get recent SHA on the branch.

## Why was this MR needed?

See #21013

## What are the relevant issue numbers?

Closes #21013

## Does this MR meet the acceptance criteria?

- [x] [CHANGELOG](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CHANGELOG) entry added
- [x] [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md)
- Tests
  - [x] Added for this feature/bug
  - [x] All builds are passing
- [x] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides)
- [x] Branch has no merge conflicts with `master` (if you do - rebase it please)
- [x] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits)

See merge request !5862
parents d0e77fb5 8527f3fa
Please view this file on the master branch, on stable branches it's out of date. Please view this file on the master branch, on stable branches it's out of date.
v 8.11.0 (unreleased) v 8.11.0 (unreleased)
- Use test coverage value from the latest successful pipeline in badge. !5862
- Add test coverage report badge. !5708 - Add test coverage report badge. !5708
- Remove the http_parser.rb dependency by removing the tinder gem. !5758 (tbalthazar) - Remove the http_parser.rb dependency by removing the tinder gem. !5758 (tbalthazar)
- Ability to specify branches for Pivotal Tracker integration (Egor Lynko) - Ability to specify branches for Pivotal Tracker integration (Egor Lynko)
......
...@@ -67,6 +67,8 @@ use following Markdown code to embed the est coverage report into `README.md`: ...@@ -67,6 +67,8 @@ use following Markdown code to embed the est coverage report into `README.md`:
![coverage](http://gitlab.com/gitlab-org/gitlab-ce/badges/master/coverage.svg?job=coverage) ![coverage](http://gitlab.com/gitlab-org/gitlab-ce/badges/master/coverage.svg?job=coverage)
``` ```
The latest successful pipeline will be used to read the test coverage value.
[builds]: #builds [builds]: #builds
[jobs]: yaml/README.md#jobs [jobs]: yaml/README.md#jobs
[stages]: yaml/README.md#stages [stages]: yaml/README.md#stages
......
...@@ -13,8 +13,7 @@ module Gitlab ...@@ -13,8 +13,7 @@ module Gitlab
@job = job @job = job
@pipeline = @project.pipelines @pipeline = @project.pipelines
.where(ref: @ref) .latest_successful_for(@ref)
.where(sha: @project.commit(@ref).try(:sha))
.first .first
end end
......
...@@ -4,12 +4,6 @@ feature 'test coverage badge' do ...@@ -4,12 +4,6 @@ feature 'test coverage badge' do
given!(:user) { create(:user) } given!(:user) { create(:user) }
given!(:project) { create(:project, :private) } given!(:project) { create(:project, :private) }
given!(:pipeline) do
create(:ci_pipeline, project: project,
ref: 'master',
sha: project.commit.id)
end
context 'when user has access to view badge' do context 'when user has access to view badge' do
background do background do
project.team << [user, :developer] project.team << [user, :developer]
...@@ -17,8 +11,10 @@ feature 'test coverage badge' do ...@@ -17,8 +11,10 @@ feature 'test coverage badge' do
end end
scenario 'user requests coverage badge image for pipeline' do scenario 'user requests coverage badge image for pipeline' do
create_job(coverage: 100, name: 'test:1') create_pipeline do |pipeline|
create_job(coverage: 90, name: 'test:2') create_build(pipeline, coverage: 100, name: 'test:1')
create_build(pipeline, coverage: 90, name: 'test:2')
end
show_test_coverage_badge show_test_coverage_badge
...@@ -26,9 +22,11 @@ feature 'test coverage badge' do ...@@ -26,9 +22,11 @@ feature 'test coverage badge' do
end end
scenario 'user requests coverage badge for specific job' do scenario 'user requests coverage badge for specific job' do
create_job(coverage: 50, name: 'test:1') create_pipeline do |pipeline|
create_job(coverage: 50, name: 'test:2') create_build(pipeline, coverage: 50, name: 'test:1')
create_job(coverage: 85, name: 'coverage') create_build(pipeline, coverage: 50, name: 'test:2')
create_build(pipeline, coverage: 85, name: 'coverage')
end
show_test_coverage_badge(job: 'coverage') show_test_coverage_badge(job: 'coverage')
...@@ -36,7 +34,9 @@ feature 'test coverage badge' do ...@@ -36,7 +34,9 @@ feature 'test coverage badge' do
end end
scenario 'user requests coverage badge for pipeline without coverage' do scenario 'user requests coverage badge for pipeline without coverage' do
create_job(coverage: nil, name: 'test') create_pipeline do |pipeline|
create_build(pipeline, coverage: nil, name: 'test')
end
show_test_coverage_badge show_test_coverage_badge
...@@ -54,10 +54,19 @@ feature 'test coverage badge' do ...@@ -54,10 +54,19 @@ feature 'test coverage badge' do
end end
end end
def create_job(coverage:, name:) def create_pipeline
create(:ci_build, name: name, opts = { project: project, ref: 'master', sha: project.commit.id }
coverage: coverage,
pipeline: pipeline) create(:ci_pipeline, opts).tap do |pipeline|
yield pipeline
pipeline.build_updated
end
end
def create_build(pipeline, coverage:, name:)
opts = { pipeline: pipeline, coverage: coverage, name: name }
create(:ci_build, :success, opts)
end end
def show_test_coverage_badge(job: nil) def show_test_coverage_badge(job: nil)
......
...@@ -44,45 +44,49 @@ describe Gitlab::Badge::Coverage::Report do ...@@ -44,45 +44,49 @@ describe Gitlab::Badge::Coverage::Report do
end end
end end
context 'pipeline exists' do context 'when latest successful pipeline exists' do
let!(:pipeline) do before do
create(:ci_pipeline, project: project, create_pipeline do |pipeline|
sha: project.commit.id, create(:ci_build, :success, pipeline: pipeline, name: 'first', coverage: 40)
ref: 'master') create(:ci_build, :success, pipeline: pipeline, coverage: 60)
end end
context 'builds exist' do create_pipeline do |pipeline|
before do create(:ci_build, :failed, pipeline: pipeline, coverage: 10)
create(:ci_build, name: 'first', pipeline: pipeline, coverage: 40)
create(:ci_build, pipeline: pipeline, coverage: 60)
end end
end
context 'particular job specified' do context 'when particular job specified' do
let(:job_name) { 'first' } let(:job_name) { 'first' }
it 'returns coverage for the particular job' do it 'returns coverage for the particular job' do
expect(badge.status).to eq 40 expect(badge.status).to eq 40
end
end end
end
context 'particular job not specified' do context 'when particular job not specified' do
let(:job_name) { '' } let(:job_name) { '' }
it 'returns arithemetic mean for the pipeline' do
expect(badge.status).to eq 50
end
end
end
it 'returns arithemetic mean for the pipeline' do context 'when only failed pipeline exists' do
expect(badge.status).to eq 50 before do
end create_pipeline do |pipeline|
create(:ci_build, :failed, pipeline: pipeline, coverage: 10)
end end
end end
context 'builds do not exist' do it_behaves_like 'unknown coverage report'
it_behaves_like 'unknown coverage report'
context 'particular job specified' do context 'particular job specified' do
let(:job_name) { 'nonexistent' } let(:job_name) { 'nonexistent' }
it 'retruns nil' do it 'retruns nil' do
expect(badge.status).to be_nil expect(badge.status).to be_nil
end
end end
end end
end end
...@@ -90,4 +94,13 @@ describe Gitlab::Badge::Coverage::Report do ...@@ -90,4 +94,13 @@ describe Gitlab::Badge::Coverage::Report do
context 'pipeline does not exist' do context 'pipeline does not exist' do
it_behaves_like 'unknown coverage report' it_behaves_like 'unknown coverage report'
end end
def create_pipeline
opts = { project: project, sha: project.commit.id, ref: 'master' }
create(:ci_pipeline, opts).tap do |pipeline|
yield pipeline
pipeline.build_updated
end
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