|
| 1 | +require 'pact/hal/entity' |
| 2 | +require 'pact/hal/http_client' |
| 3 | +require 'pact/provider/pact_uri' |
| 4 | +require 'pact/errors' |
| 5 | +require 'pact/pact_broker/fetch_pacts' |
| 6 | + |
| 7 | +module Pact |
| 8 | + module PactBroker |
| 9 | + class FetchPactURIsForVerification |
| 10 | + attr_reader :provider, :consumer_version_selectors, :provider_version_tags, :broker_base_url, :http_client_options, :http_client |
| 11 | + |
| 12 | + PACTS_FOR_VERIFICATION_RELATION = 'beta:provider-pacts-for-verification'.freeze |
| 13 | + PACTS = 'pacts'.freeze |
| 14 | + HREF = 'href'.freeze |
| 15 | + LINKS = '_links'.freeze |
| 16 | + SELF = 'self'.freeze |
| 17 | + EMBEDDED = '_embedded'.freeze |
| 18 | + |
| 19 | + def initialize(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options) |
| 20 | + @provider = provider |
| 21 | + @consumer_version_selectors = consumer_version_selectors |
| 22 | + @provider_version_tags = provider_version_tags |
| 23 | + @http_client_options = http_client_options |
| 24 | + @broker_base_url = broker_base_url |
| 25 | + @http_client = Pact::Hal::HttpClient.new(http_client_options) |
| 26 | + end |
| 27 | + |
| 28 | + def self.call(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options) |
| 29 | + new(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options).call |
| 30 | + end |
| 31 | + |
| 32 | + def call |
| 33 | + if index.can?(PACTS_FOR_VERIFICATION_RELATION) |
| 34 | + log_message |
| 35 | + pacts_for_verification |
| 36 | + else |
| 37 | + # Fall back to old method of fetching pacts |
| 38 | + consumer_version_tags = consumer_version_selectors.collect{ | selector | selector[:tag] } |
| 39 | + FetchPacts.call(provider, consumer_version_tags, broker_base_url, http_client_options) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + private |
| 44 | + |
| 45 | + def index |
| 46 | + @index_entity ||= Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get.assert_success! |
| 47 | + end |
| 48 | + |
| 49 | + def pacts_for_verification |
| 50 | + pacts_for_verification_entity.response.body[EMBEDDED][PACTS].collect do | pact | |
| 51 | + metadata = { |
| 52 | + pending: pact["verificationProperties"]["pending"], |
| 53 | + pending_reason: pact["verificationProperties"]["pendingReason"], |
| 54 | + inclusion_reason: pact["verificationProperties"]["inclusionReason"], |
| 55 | + } |
| 56 | + Pact::Provider::PactURI.new(pact[LINKS][SELF][HREF], http_client_options, metadata) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + def pacts_for_verification_entity |
| 61 | + index |
| 62 | + ._link(PACTS_FOR_VERIFICATION_RELATION) |
| 63 | + .expand(provider: provider) |
| 64 | + .with_query(query) |
| 65 | + .get! |
| 66 | + end |
| 67 | + |
| 68 | + def query |
| 69 | + q = {} |
| 70 | + if consumer_version_selectors&.any? |
| 71 | + q["consumer_version_selectors"] = consumer_version_selectors |
| 72 | + end |
| 73 | + |
| 74 | + if provider_version_tags&.any? |
| 75 | + q["provider_version_tags"] = provider_version_tags |
| 76 | + end |
| 77 | + q |
| 78 | + end |
| 79 | + |
| 80 | + def log_message |
| 81 | + latest = consumer_version_selectors&.any? ? "" : "latest " |
| 82 | + message = "INFO: Fetching #{latest}pacts for #{provider} from #{broker_base_url}" |
| 83 | + if consumer_version_selectors&.any? |
| 84 | + desc = consumer_version_selectors.collect do |selector| |
| 85 | + all_or_latest = selector[:all] ? "all" : "latest" |
| 86 | + # TODO support fallback |
| 87 | + name = selector[:fallback] ? "#{selector[:tag]} (or #{selector[:fallback]} if not found)" : selector[:tag] |
| 88 | + "#{all_or_latest} #{name}" |
| 89 | + end.join(", ") |
| 90 | + message << " for tags: #{desc}" |
| 91 | + end |
| 92 | + Pact.configuration.output_stream.puts message |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | +end |
0 commit comments