diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2018-12-11 18:42:59 +0300 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2018-12-11 18:42:59 +0300 |
commit | 18a48e348b83f66a1d108a2d6e38ac12c47dcef3 (patch) | |
tree | 219ac450c839c41331aaa87ecc140be463618439 /app | |
parent | de21f2278151be40f3480ed6d06388b3dcfe841a (diff) | |
parent | e6226e8cb3d7e79500482ba2cef604133f667381 (diff) |
Merge branch '7788_parse_license_management_reports_in_be-ce' into 'master'
Generalise test compare service
See merge request gitlab-org/gitlab-ce!22833
Diffstat (limited to 'app')
-rw-r--r-- | app/models/ci/build.rb | 2 | ||||
-rw-r--r-- | app/models/merge_request.rb | 24 | ||||
-rw-r--r-- | app/services/ci/compare_reports_base_service.rb | 47 | ||||
-rw-r--r-- | app/services/ci/compare_test_reports_service.rb | 36 |
4 files changed, 66 insertions, 43 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index d86a6eceb59..e2917049902 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -742,7 +742,7 @@ module Ci def collect_test_reports!(test_reports) test_reports.get_suite(group_name).tap do |test_suite| each_report(Ci::JobArtifact::TEST_REPORT_FILE_TYPES) do |file_type, blob| - Gitlab::Ci::Parsers::Test.fabricate!(file_type).parse!(blob, test_suite) + Gitlab::Ci::Parsers.fabricate!(file_type).parse!(blob, test_suite) end end end diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 77e48ce11e8..a13cac73d04 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -1118,14 +1118,17 @@ class MergeRequest < ActiveRecord::Base end end - # rubocop: disable CodeReuse/ServiceClass def compare_test_reports unless has_test_reports? return { status: :error, status_reason: 'This merge request does not have test reports' } end - with_reactive_cache(:compare_test_results) do |data| - unless Ci::CompareTestReportsService.new(project) + compare_reports(Ci::CompareTestReportsService) + end + + def compare_reports(service_class) + with_reactive_cache(service_class.name) do |data| + unless service_class.new(project) .latest?(base_pipeline, actual_head_pipeline, data) raise InvalidateReactiveCache end @@ -1133,19 +1136,14 @@ class MergeRequest < ActiveRecord::Base data end || { status: :parsing } end - # rubocop: enable CodeReuse/ServiceClass - # rubocop: disable CodeReuse/ServiceClass def calculate_reactive_cache(identifier, *args) - case identifier.to_sym - when :compare_test_results - Ci::CompareTestReportsService.new(project).execute( - base_pipeline, actual_head_pipeline) - else - raise NotImplementedError, "Unknown identifier: #{identifier}" - end + service_class = identifier.constantize + + raise NameError, service_class unless service_class < Ci::CompareReportsBaseService + + service_class.new(project).execute(base_pipeline, actual_head_pipeline) end - # rubocop: enable CodeReuse/ServiceClass def all_commits # MySQL doesn't support LIMIT in a subquery. diff --git a/app/services/ci/compare_reports_base_service.rb b/app/services/ci/compare_reports_base_service.rb new file mode 100644 index 00000000000..d5625857599 --- /dev/null +++ b/app/services/ci/compare_reports_base_service.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Ci + class CompareReportsBaseService < ::BaseService + def execute(base_pipeline, head_pipeline) + comparer = comparer_class.new(get_report(base_pipeline), get_report(head_pipeline)) + { + status: :parsed, + key: key(base_pipeline, head_pipeline), + data: serializer_class + .new(project: project) + .represent(comparer).as_json + } + rescue Gitlab::Ci::Parsers::ParserError => e + { + status: :error, + key: key(base_pipeline, head_pipeline), + status_reason: e.message + } + end + + def latest?(base_pipeline, head_pipeline, data) + data&.fetch(:key, nil) == key(base_pipeline, head_pipeline) + end + + private + + def key(base_pipeline, head_pipeline) + [ + base_pipeline&.id, base_pipeline&.updated_at, + head_pipeline&.id, head_pipeline&.updated_at + ] + end + + def comparer_class + raise NotImplementedError + end + + def serializer_class + raise NotImplementedError + end + + def get_report(pipeline) + raise NotImplementedError + end + end +end diff --git a/app/services/ci/compare_test_reports_service.rb b/app/services/ci/compare_test_reports_service.rb index 2293f95f56b..382d5b8995f 100644 --- a/app/services/ci/compare_test_reports_service.rb +++ b/app/services/ci/compare_test_reports_service.rb @@ -1,39 +1,17 @@ # frozen_string_literal: true module Ci - class CompareTestReportsService < ::BaseService - def execute(base_pipeline, head_pipeline) - # rubocop: disable CodeReuse/Serializer - comparer = Gitlab::Ci::Reports::TestReportsComparer - .new(base_pipeline&.test_reports, head_pipeline.test_reports) - - { - status: :parsed, - key: key(base_pipeline, head_pipeline), - data: TestReportsComparerSerializer - .new(project: project) - .represent(comparer).as_json - } - rescue => e - { - status: :error, - key: key(base_pipeline, head_pipeline), - status_reason: e.message - } - # rubocop: enable CodeReuse/Serializer + class CompareTestReportsService < CompareReportsBaseService + def comparer_class + Gitlab::Ci::Reports::TestReportsComparer end - def latest?(base_pipeline, head_pipeline, data) - data&.fetch(:key, nil) == key(base_pipeline, head_pipeline) + def serializer_class + TestReportsComparerSerializer end - private - - def key(base_pipeline, head_pipeline) - [ - base_pipeline&.id, base_pipeline&.updated_at, - head_pipeline&.id, head_pipeline&.updated_at - ] + def get_report(pipeline) + pipeline&.test_reports end end end |