Commit c5d09610 authored by Imre Farkas's avatar Imre Farkas

Merge branch 'mo-fix-codequality-comparer' into 'master'

Fix code quality report comparer

See merge request gitlab-org/gitlab!55123
parents 18d6b770 e36455ae
...@@ -5,7 +5,7 @@ module Gitlab ...@@ -5,7 +5,7 @@ module Gitlab
module Reports module Reports
class CodequalityReportsComparer < ReportsComparer class CodequalityReportsComparer < ReportsComparer
def initialize(base_report, head_report) def initialize(base_report, head_report)
@base_report = base_report || CodequalityReports.new @base_report = base_report
@head_report = head_report @head_report = head_report
end end
...@@ -15,12 +15,16 @@ module Gitlab ...@@ -15,12 +15,16 @@ module Gitlab
def existing_errors def existing_errors
strong_memoize(:existing_errors) do strong_memoize(:existing_errors) do
next [] if not_found?
base_report.all_degradations & head_report.all_degradations base_report.all_degradations & head_report.all_degradations
end end
end end
def new_errors def new_errors
strong_memoize(:new_errors) do strong_memoize(:new_errors) do
next [] if not_found?
fingerprints = head_report.degradations.keys - base_report.degradations.keys fingerprints = head_report.degradations.keys - base_report.degradations.keys
head_report.degradations.fetch_values(*fingerprints) head_report.degradations.fetch_values(*fingerprints)
end end
...@@ -28,6 +32,8 @@ module Gitlab ...@@ -28,6 +32,8 @@ module Gitlab
def resolved_errors def resolved_errors
strong_memoize(:resolved_errors) do strong_memoize(:resolved_errors) do
next [] if not_found?
fingerprints = base_report.degradations.keys - head_report.degradations.keys fingerprints = base_report.degradations.keys - head_report.degradations.keys
base_report.degradations.fetch_values(*fingerprints) base_report.degradations.fetch_values(*fingerprints)
end end
......
...@@ -18,10 +18,10 @@ module Gitlab ...@@ -18,10 +18,10 @@ module Gitlab
end end
def status def status
if success? if base_report.nil? || head_report.nil?
STATUS_SUCCESS
elsif base_report.nil? || head_report.nil?
STATUS_NOT_FOUND STATUS_NOT_FOUND
elsif success?
STATUS_SUCCESS
else else
STATUS_FAILED STATUS_FAILED
end end
...@@ -54,6 +54,10 @@ module Gitlab ...@@ -54,6 +54,10 @@ module Gitlab
def total_count def total_count
existing_errors.size + new_errors.size existing_errors.size + new_errors.size
end end
def not_found?
status == STATUS_NOT_FOUND
end
end end
end end
end end
......
...@@ -27,6 +27,22 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do ...@@ -27,6 +27,22 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do
expect(report_status).to eq(described_class::STATUS_SUCCESS) expect(report_status).to eq(described_class::STATUS_SUCCESS)
end end
end end
context 'when head report does not exist' do
let(:head_report) { nil }
it 'returns status not found' do
expect(report_status).to eq(described_class::STATUS_NOT_FOUND)
end
end
context 'when base report does not exist' do
let(:base_report) { nil }
it 'returns status success' do
expect(report_status).to eq(described_class::STATUS_NOT_FOUND)
end
end
end end
describe '#errors_count' do describe '#errors_count' do
...@@ -93,6 +109,14 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do ...@@ -93,6 +109,14 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do
expect(resolved_count).to be_zero expect(resolved_count).to be_zero
end end
end end
context 'when base report is nil' do
let(:base_report) { nil }
it 'returns zero' do
expect(resolved_count).to be_zero
end
end
end end
describe '#total_count' do describe '#total_count' do
...@@ -140,6 +164,14 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do ...@@ -140,6 +164,14 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do
expect(total_count).to eq(2) expect(total_count).to eq(2)
end end
end end
context 'when base report is nil' do
let(:base_report) { nil }
it 'returns zero' do
expect(total_count).to be_zero
end
end
end end
describe '#existing_errors' do describe '#existing_errors' do
...@@ -177,6 +209,14 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do ...@@ -177,6 +209,14 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do
expect(existing_errors).to be_empty expect(existing_errors).to be_empty
end end
end end
context 'when base report is nil' do
let(:base_report) { nil }
it 'returns an empty array' do
expect(existing_errors).to be_empty
end
end
end end
describe '#new_errors' do describe '#new_errors' do
...@@ -213,6 +253,14 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do ...@@ -213,6 +253,14 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do
expect(new_errors).to eq([degradation_1]) expect(new_errors).to eq([degradation_1])
end end
end end
context 'when base report is nil' do
let(:base_report) { nil }
it 'returns an empty array' do
expect(new_errors).to be_empty
end
end
end end
describe '#resolved_errors' do describe '#resolved_errors' do
...@@ -250,5 +298,13 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do ...@@ -250,5 +298,13 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReportsComparer do
expect(resolved_errors).to be_empty expect(resolved_errors).to be_empty
end end
end end
context 'when base report is nil' do
let(:base_report) { nil }
it 'returns an empty array' do
expect(resolved_errors).to be_empty
end
end
end end
end end
...@@ -49,10 +49,6 @@ RSpec.describe Gitlab::Ci::Reports::ReportsComparer do ...@@ -49,10 +49,6 @@ RSpec.describe Gitlab::Ci::Reports::ReportsComparer do
context 'when base_report is nil' do context 'when base_report is nil' do
let(:base_report) { nil } let(:base_report) { nil }
before do
allow(comparer).to receive(:success?).and_return(false)
end
it 'returns status not_found' do it 'returns status not_found' do
expect(status).to eq('not_found') expect(status).to eq('not_found')
end end
...@@ -61,10 +57,6 @@ RSpec.describe Gitlab::Ci::Reports::ReportsComparer do ...@@ -61,10 +57,6 @@ RSpec.describe Gitlab::Ci::Reports::ReportsComparer do
context 'when head_report is nil' do context 'when head_report is nil' do
let(:head_report) { nil } let(:head_report) { nil }
before do
allow(comparer).to receive(:success?).and_return(false)
end
it 'returns status not_found' do it 'returns status not_found' do
expect(status).to eq('not_found') expect(status).to eq('not_found')
end end
...@@ -118,4 +110,22 @@ RSpec.describe Gitlab::Ci::Reports::ReportsComparer do ...@@ -118,4 +110,22 @@ RSpec.describe Gitlab::Ci::Reports::ReportsComparer do
expect { total_count }.to raise_error(NotImplementedError) expect { total_count }.to raise_error(NotImplementedError)
end end
end end
describe '#not_found?' do
subject(:not_found) { comparer.not_found? }
context 'when base report is nil' do
let(:base_report) { nil }
it { is_expected.to be_truthy }
end
context 'when base report exists' do
before do
allow(comparer).to receive(:success?).and_return(true)
end
it { is_expected.to be_falsey }
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