Commit 76eb63a2 authored by Maxime Orefice's avatar Maxime Orefice Committed by Shinya Maeda

Introduce ReportsComparer class

This commits extracts the business logic required to build a diff
report between the head and the base pipeline. This logic has been
extracted to an abstract class to facilitate future report comparison.
parent af5ab940
...@@ -3,21 +3,14 @@ ...@@ -3,21 +3,14 @@
module Gitlab module Gitlab
module Ci module Ci
module Reports module Reports
class AccessibilityReportsComparer class AccessibilityReportsComparer < ReportsComparer
include Gitlab::Utils::StrongMemoize
STATUS_SUCCESS = 'success'
STATUS_FAILED = 'failed'
attr_reader :base_report, :head_report
def initialize(base_report, head_report) def initialize(base_report, head_report)
@base_report = base_report || AccessibilityReports.new @base_report = base_report || AccessibilityReports.new
@head_report = head_report @head_report = head_report
end end
def status def success?
head_report.errors_count > 0 ? STATUS_FAILED : STATUS_SUCCESS head_report.errors_count == 0
end end
def existing_errors def existing_errors
......
...@@ -3,21 +3,14 @@ ...@@ -3,21 +3,14 @@
module Gitlab module Gitlab
module Ci module Ci
module Reports module Reports
class CodequalityReportsComparer class CodequalityReportsComparer < ReportsComparer
include Gitlab::Utils::StrongMemoize
STATUS_SUCCESS = 'success'
STATUS_FAILED = 'failed'
attr_reader :base_report, :head_report
def initialize(base_report, head_report) def initialize(base_report, head_report)
@base_report = base_report || CodequalityReports.new @base_report = base_report || CodequalityReports.new
@head_report = head_report @head_report = head_report
end end
def status def success?
head_report.degradations_count > 0 ? STATUS_FAILED : STATUS_SUCCESS head_report.degradations_count == 0
end end
def existing_errors def existing_errors
...@@ -40,17 +33,15 @@ module Gitlab ...@@ -40,17 +33,15 @@ module Gitlab
end end
end end
def errors_count
head_report.degradations_count
end
def resolved_count def resolved_count
resolved_errors.size resolved_errors.size
end end
def total_count def total_count
existing_errors.size + new_errors.size head_report.degradations_count
end end
alias_method :errors_count, :total_count
end end
end end
end end
......
# frozen_string_literal: true
module Gitlab
module Ci
module Reports
class ReportsComparer
include Gitlab::Utils::StrongMemoize
STATUS_SUCCESS = 'success'
STATUS_FAILED = 'failed'
attr_reader :base_report, :head_report
def initialize(base_report, head_report)
@base_report = base_report
@head_report = head_report
end
def status
success? ? STATUS_SUCCESS : STATUS_FAILED
end
def success?
raise NotImplementedError
end
def existing_errors
raise NotImplementedError
end
def new_errors
raise NotImplementedError
end
def resolved_errors
raise NotImplementedError
end
def errors_count
raise NotImplementedError
end
def resolved_count
resolved_errors.size
end
def total_count
existing_errors.size + new_errors.size
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Ci::Reports::ReportsComparer do
let(:comparer) { described_class.new(base_report, head_report) }
let(:base_report) { Gitlab::Ci::Reports::CodequalityReports.new }
let(:head_report) { Gitlab::Ci::Reports::CodequalityReports.new }
describe '#initialize' do
context 'sets getter for the report comparer' do
it 'return base report' do
expect(comparer.base_report).to be_an_instance_of(Gitlab::Ci::Reports::CodequalityReports)
end
it 'return head report' do
expect(comparer.head_report).to be_an_instance_of(Gitlab::Ci::Reports::CodequalityReports)
end
end
end
describe '#status' do
subject(:status) { comparer.status }
it 'returns not implemented error' do
expect { status }.to raise_error(NotImplementedError)
end
context 'when success? is true' do
before do
allow(comparer).to receive(:success?).and_return(true)
end
it 'returns status success' do
expect(status).to eq('success')
end
end
context 'when success? is false' do
before do
allow(comparer).to receive(:success?).and_return(false)
end
it 'returns status failed' do
expect(status).to eq('failed')
end
end
end
describe '#success?' do
subject(:success?) { comparer.success? }
it 'returns not implemented error' do
expect { success? }.to raise_error(NotImplementedError)
end
end
describe '#existing_errors' do
subject(:existing_errors) { comparer.existing_errors }
it 'returns not implemented error' do
expect { existing_errors }.to raise_error(NotImplementedError)
end
end
describe '#resolved_errors' do
subject(:resolved_errors) { comparer.resolved_errors }
it 'returns not implemented error' do
expect { resolved_errors }.to raise_error(NotImplementedError)
end
end
describe '#errors_count' do
subject(:errors_count) { comparer.errors_count }
it 'returns not implemented error' do
expect { errors_count }.to raise_error(NotImplementedError)
end
end
describe '#resolved_count' do
subject(:resolved_count) { comparer.resolved_count }
it 'returns not implemented error' do
expect { resolved_count }.to raise_error(NotImplementedError)
end
end
describe '#total_count' do
subject(:total_count) { comparer.total_count }
it 'returns not implemented error' do
expect { total_count }.to raise_error(NotImplementedError)
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