Skip to content

Commit

Permalink
feat: use beta:wip-provider-pacts rather than pb:wip-provider-pacts t…
Browse files Browse the repository at this point in the history
…o fetch WIP pacts
  • Loading branch information
bethesque committed Jul 24, 2018
1 parent 9e6de46 commit 3bb0501
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
16 changes: 16 additions & 0 deletions lib/pact/pact_broker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'pact/pact_broker/fetch_pacts'
require 'pact/pact_broker/fetch_wip_pacts'

module Pact
module PactBroker
extend self

def fetch_pact_uris *args
Pact::PactBroker::FetchPacts.call(*args)
end

def fetch_wip_pact_uris *args
Pact::PactBroker::FetchWipPacts.call(*args)
end
end
end
28 changes: 14 additions & 14 deletions lib/pact/pact_broker/fetch_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def self.call(provider, tags, broker_base_url, http_client_options)

def call
log_message
if get_index.success?
if index.success?
if any_tags?
get_tagged_pacts_for_provider
tagged_pacts_for_provider
else
get_latest_pacts_for_provider
latest_pacts_for_provider
end
else
raise Pact::Error.new("Error retrieving #{broker_base_url} status=#{index_entity.response.code} #{index_entity.response.raw_body}")
Expand All @@ -51,35 +51,35 @@ def any_tags?
tags && tags.any?
end

def get_tagged_pacts_for_provider
def tagged_pacts_for_provider
tags.collect do |tag|
link = get_link(tag)
urls = get_pact_urls(link.expand(provider: provider, tag: tag[:name]).get)
if urls == [] && tag[:fallback]
urls = get_pact_urls(link.expand(provider: provider, tag: tag[:fallback]).get)
link = link_for(tag)
urls = pact_urls(link.expand(provider: provider, tag: tag[:name]).get)
if urls.empty? && tag[:fallback]
urls = pact_urls(link.expand(provider: provider, tag: tag[:fallback]).get)
end
urls
end.flatten
end

def get_link(tag)
def link_for(tag)
if !tag[:all]
index_entity._link(LATEST_PROVIDER_TAG_RELATION)
else
index_entity._link(ALL_PROVIDER_TAG_RELATION)
end
end

def get_index
@index_entity = Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get
def index
@index_entity ||= Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get
end

def get_latest_pacts_for_provider
def latest_pacts_for_provider
link = index_entity._link(LATEST_PROVIDER_RELATION)
get_pact_urls(link.expand(provider: provider).get)
pact_urls(link.expand(provider: provider).get)
end

def get_pact_urls(link_by_provider)
def pact_urls(link_by_provider)
link_by_provider.fetch(PB_PACTS, PACTS).collect do |pact|
Pact::Provider::PactURI.new(pact[HREF], http_client_options)
end
Expand Down
18 changes: 6 additions & 12 deletions lib/pact/pact_broker/fetch_wip_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module PactBroker
class FetchWipPacts
attr_reader :provider, :tags, :broker_base_url, :http_client_options, :http_client, :index_entity

WIP_PROVIDER_RELATION = 'pb:wip-provider-pacts'.freeze
WIP_PROVIDER_RELATION = 'beta:wip-provider-pacts'.freeze
PACTS = 'pacts'.freeze
PB_PACTS = 'pb:pacts'.freeze
HREF = 'href'.freeze
Expand All @@ -24,21 +24,20 @@ def self.call(provider, broker_base_url, http_client_options)
end

def call
log_message
if get_index.success?
get_wip_pacts_for_provider
if index.success?
wip_pacts_for_provider
else
raise Pact::Error.new("Error retrieving #{broker_base_url} status=#{index_entity.response.code} #{index_entity.response.raw_body}")
end
end

private

def get_index
@index_entity = Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get
def index
@index_entity ||= Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get
end

def get_wip_pacts_for_provider
def wip_pacts_for_provider
link = index_entity._link(WIP_PROVIDER_RELATION)
if link
get_pact_urls(link.expand(provider: provider).get)
Expand All @@ -52,11 +51,6 @@ def get_pact_urls(link_by_provider)
Pact::Provider::PactURI.new(pact[HREF], http_client_options)
end
end

def log_message
message = "INFO: Fetching WIP pacts for #{provider} from #{broker_base_url}"
Pact.configuration.output_stream.puts message
end
end
end
end
1 change: 0 additions & 1 deletion spec/lib/pact/pact_broker/fetch_pacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Pact
module PactBroker
describe FetchPacts do

describe "call" do
before do
allow(Pact.configuration).to receive(:output_stream).and_return(double('output stream').as_null_object)
Expand Down
4 changes: 2 additions & 2 deletions spec/service_providers/pact_ruby_fetch_wip_pacts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

before do
pact_broker
.given('the relations for retrieving WIP pacts exist in the index resource')
.given('the relation for retrieving WIP pacts exists in the index resource')
.upon_receiving('a request for the index resource')
.with(
method: :get,
Expand All @@ -26,7 +26,7 @@
status: 200,
body: {
_links: {
'pb:wip-provider-pacts' => {
'beta:wip-provider-pacts' => {
href: Pact.term(
generate: broker_base_url + 'pacts/provider/{provider}/wip',
matcher: %r{/pacts/provider/{provider}/wip$}
Expand Down

0 comments on commit 3bb0501

Please sign in to comment.