debian-mirror-gitlab/app/services/merge_requests/mergeability/run_checks_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
1.9 KiB
Ruby
Raw Normal View History

2021-11-18 22:05:49 +05:30
# frozen_string_literal: true
module MergeRequests
module Mergeability
class RunChecksService
include Gitlab::Utils::StrongMemoize
def initialize(merge_request:, params:)
@merge_request = merge_request
@params = params
end
def execute
2022-08-27 11:52:29 +05:30
@results = merge_request.mergeability_checks.each_with_object([]) do |check_class, result_hash|
2021-11-18 22:05:49 +05:30
check = check_class.new(merge_request: merge_request, params: params)
next if check.skip?
2022-10-11 01:57:18 +05:30
check_result = logger.instrument(mergeability_name: check_class.to_s.demodulize.underscore) do
run_check(check)
end
2022-08-27 11:52:29 +05:30
result_hash << check_result
2021-11-18 22:05:49 +05:30
2022-08-27 11:52:29 +05:30
break result_hash if check_result.failed?
2021-11-18 22:05:49 +05:30
end
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
logger.commit
2022-08-27 11:52:29 +05:30
self
end
def success?
raise 'Execute needs to be called before' if results.nil?
results.all?(&:success?)
end
def failure_reason
raise 'Execute needs to be called before' if results.nil?
results.find(&:failed?)&.payload&.fetch(:reason)
2021-11-18 22:05:49 +05:30
end
private
2022-08-27 11:52:29 +05:30
attr_reader :merge_request, :params, :results
2021-11-18 22:05:49 +05:30
def run_check(check)
2022-07-16 23:28:13 +05:30
return check.execute unless Feature.enabled?(:mergeability_caching, merge_request.project)
2021-11-18 22:05:49 +05:30
return check.execute unless check.cacheable?
2022-08-27 11:52:29 +05:30
cached_result = cached_results.read(merge_check: check)
2021-11-18 22:05:49 +05:30
return cached_result if cached_result.respond_to?(:status)
check.execute.tap do |result|
2022-08-27 11:52:29 +05:30
cached_results.write(merge_check: check, result_hash: result.to_hash)
2021-11-18 22:05:49 +05:30
end
end
2022-08-27 11:52:29 +05:30
def cached_results
strong_memoize(:cached_results) do
2021-11-18 22:05:49 +05:30
Gitlab::MergeRequests::Mergeability::ResultsStore.new(merge_request: merge_request)
end
end
2022-10-11 01:57:18 +05:30
def logger
strong_memoize(:logger) do
MergeRequests::Mergeability::Logger.new(merge_request: merge_request)
end
end
2021-11-18 22:05:49 +05:30
end
end
end