debian-mirror-gitlab/spec/contracts/provider/helpers/contract_source_helper.rb

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

59 lines
2 KiB
Ruby
Raw Normal View History

2023-03-04 22:38:38 +05:30
# frozen_string_literal: true
module Provider
module ContractSourceHelper
QA_PACT_BROKER_HOST = "http://localhost:9292/pacts"
PREFIX_PATHS = {
2023-03-17 16:20:25 +05:30
rake: {
ce: "../../contracts/project",
ee: "../../../../ee/spec/contracts/contracts/project"
},
2023-03-04 22:38:38 +05:30
spec: "../contracts/project"
}.freeze
SUB_PATH_REGEX = %r{project/(?<file_path>.*?)_helper.rb}.freeze
class << self
2023-03-17 16:20:25 +05:30
def contract_location(requester:, file_path:, edition: :ce)
2023-03-04 22:38:38 +05:30
raise ArgumentError, 'requester must be :rake or :spec' unless [:rake, :spec].include? requester
2023-03-17 16:20:25 +05:30
raise ArgumentError, 'edition must be :ce or :ee' unless [:ce, :ee].include? edition
2023-03-04 22:38:38 +05:30
relevant_path = file_path.match(SUB_PATH_REGEX)[:file_path].split('/')
2023-03-17 16:20:25 +05:30
ENV["PACT_BROKER"] ? pact_broker_url(relevant_path) : local_contract_location(requester, relevant_path, edition)
2023-03-04 22:38:38 +05:30
end
def pact_broker_url(file_path)
provider_url = "provider/#{construct_provider_url_path(file_path)}"
consumer_url = "consumer/#{construct_consumer_url_path(file_path)}"
"#{QA_PACT_BROKER_HOST}/#{provider_url}/#{consumer_url}/latest"
end
def construct_provider_url_path(file_path)
split_name = file_path[2].split('_')
split_name[0] = split_name[0].upcase
split_name.join("%20")
end
def construct_consumer_url_path(file_path)
"#{file_path[0].split('_').map(&:capitalize).join}%23#{file_path[1]}"
end
2023-03-17 16:20:25 +05:30
def local_contract_location(requester, file_path, edition)
2023-03-04 22:38:38 +05:30
contract_path = construct_local_contract_path(file_path)
2023-03-17 16:20:25 +05:30
prefix_path = PREFIX_PATHS[requester]
prefix_path = File.expand_path(prefix_path[edition], __dir__) if requester == :rake
2023-03-04 22:38:38 +05:30
"#{prefix_path}#{contract_path}"
end
def construct_local_contract_path(file_path)
contract_file_name = "#{file_path[0].tr('_', '')}##{file_path[1]}-#{file_path[2]}.json"
"/#{file_path[0]}/#{file_path[1]}/#{contract_file_name}"
end
end
end
end