Skip to content

Commit da2facf

Browse files
committed
feat: support publishing verification results with a version branch
1 parent c5b8c6d commit da2facf

16 files changed

+113
-37
lines changed

lib/pact/hal/http_client.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def create_request uri, http_method, body = nil, headers = {}
4848
def perform_request request, uri
4949
response = Retry.until_true do
5050
http = Net::HTTP.new(uri.host, uri.port, :ENV)
51-
http.set_debug_output(output_stream) if verbose
51+
http.set_debug_output(output_stream) if verbose || ENV['VERBOSE'] == 'true'
5252
http.use_ssl = (uri.scheme == 'https')
5353
http.ca_file = ENV['SSL_CERT_FILE'] if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != ''
5454
http.ca_path = ENV['SSL_CERT_DIR'] if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != ''

lib/pact/pact_broker/fetch_pact_uris_for_verification.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Pact
1010
module PactBroker
1111
class FetchPactURIsForVerification
1212
include PactSelectionDescription
13-
attr_reader :provider, :consumer_version_selectors, :provider_version_tags, :broker_base_url, :http_client_options, :http_client, :options
13+
attr_reader :provider, :consumer_version_selectors, :provider_version_branch, :provider_version_tags, :broker_base_url, :http_client_options, :http_client, :options
1414

1515
PACTS_FOR_VERIFICATION_RELATION = 'pb:provider-pacts-for-verification'.freeze
1616
PACTS_FOR_VERIFICATION_RELATION_BETA = 'beta:provider-pacts-for-verification'.freeze
@@ -20,18 +20,19 @@ class FetchPactURIsForVerification
2020
SELF = 'self'.freeze
2121
EMBEDDED = '_embedded'.freeze
2222

23-
def initialize(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options, options = {})
23+
def initialize(provider, consumer_version_selectors, provider_version_branch, provider_version_tags, broker_base_url, http_client_options, options = {})
2424
@provider = provider
2525
@consumer_version_selectors = consumer_version_selectors || []
26+
@provider_version_branch = provider_version_branch
2627
@provider_version_tags = [*provider_version_tags]
2728
@http_client_options = http_client_options
2829
@broker_base_url = broker_base_url
2930
@http_client = Pact::Hal::HttpClient.new(http_client_options)
3031
@options = options
3132
end
3233

33-
def self.call(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options, options = {})
34-
new(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options, options).call
34+
def self.call(provider, consumer_version_selectors, provider_version_branch, provider_version_tags, broker_base_url, http_client_options, options = {})
35+
new(provider, consumer_version_selectors, provider_version_branch, provider_version_tags, broker_base_url, http_client_options, options).call
3536
end
3637

3738
def call
@@ -76,6 +77,7 @@ def query
7677
q["includePendingStatus"] = true if options[:include_pending_status]
7778
q["consumerVersionSelectors"] = consumer_version_selectors if consumer_version_selectors.any?
7879
q["providerVersionTags"] = provider_version_tags if provider_version_tags.any?
80+
q["providerVersionBranch"] = provider_version_branch if provider_version_branch
7981
q["includeWipPactsSince"] = options[:include_wip_pacts_since] if options[:include_wip_pacts_since]
8082
q
8183
end

lib/pact/provider/configuration/pact_verification_from_broker.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ class PactVerificationFromBroker
1515
# in parent scope, it will clash with these ones,
1616
# so put an underscore in front of the name to be safer.
1717

18-
attr_accessor :_provider_name, :_pact_broker_base_url, :_consumer_version_tags, :_provider_version_tags, :_basic_auth_options, :_enable_pending, :_include_wip_pacts_since, :_verbose, :_consumer_version_selectors
18+
attr_accessor :_provider_name, :_pact_broker_base_url, :_consumer_version_tags, :_provider_version_branch, :_provider_version_tags, :_basic_auth_options, :_enable_pending, :_include_wip_pacts_since, :_verbose, :_consumer_version_selectors
1919

20-
def initialize(provider_name, provider_version_tags)
20+
def initialize(provider_name, provider_version_branch, provider_version_tags)
2121
@_provider_name = provider_name
22+
@_provider_version_branch = provider_version_branch
2223
@_provider_version_tags = provider_version_tags
2324
@_consumer_version_tags = []
2425
@_consumer_version_selectors = []
@@ -69,6 +70,7 @@ def create_pact_verification
6970
fetch_pacts = Pact::PactBroker::FetchPactURIsForVerification.new(
7071
_provider_name,
7172
consumer_version_selectors,
73+
_provider_version_branch,
7274
_provider_version_tags,
7375
_pact_broker_base_url,
7476
_basic_auth_options.merge(verbose: _verbose),

lib/pact/provider/configuration/service_provider_config.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ module Configuration
44
class ServiceProviderConfig
55

66
attr_accessor :application_version
7+
attr_reader :branch
78

8-
def initialize application_version, tags, publish_verification_results, &app_block
9+
def initialize application_version, branch, tags, publish_verification_results, &app_block
910
@application_version = application_version
11+
@branch = branch
1012
@tags = [*tags]
1113
@publish_verification_results = publish_verification_results
1214
@app_block = app_block

lib/pact/provider/configuration/service_provider_dsl.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ServiceProviderDSL
1515

1616
extend Pact::DSL
1717

18-
attr_accessor :name, :app_block, :application_version, :tags, :publish_verification_results
18+
attr_accessor :name, :app_block, :application_version, :branch, :tags, :publish_verification_results
1919

2020
CONFIG_RU_APP = lambda {
2121
unless File.exist? Pact.configuration.config_ru_path
@@ -44,6 +44,10 @@ def app_version_tags tags
4444
self.tags = tags
4545
end
4646

47+
def app_version_branch branch
48+
self.branch = branch
49+
end
50+
4751
def publish_verification_results publish_verification_results
4852
self.publish_verification_results = publish_verification_results
4953
Pact::RSpec.with_rspec_2 do
@@ -65,7 +69,7 @@ def create_pact_verification consumer_name, options, &block
6569
end
6670

6771
def create_pact_verification_from_broker(&block)
68-
PactVerificationFromBroker.build(name, tags, &block)
72+
PactVerificationFromBroker.build(name, branch, tags, &block)
6973
end
7074

7175
def finalize
@@ -85,7 +89,7 @@ def application_version_blank?
8589
end
8690

8791
def create_service_provider
88-
Pact.configuration.provider = ServiceProviderConfig.new(application_version, tags, publish_verification_results, &@app_block)
92+
Pact.configuration.provider = ServiceProviderConfig.new(application_version, branch, tags, publish_verification_results, &@app_block)
8993
end
9094
end
9195
end

lib/pact/provider/verification_results/publish.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Publish
1616
PUBLISH_RELATION = 'pb:publish-verification-results'.freeze
1717
PROVIDER_RELATION = 'pb:provider'.freeze
1818
VERSION_TAG_RELATION = 'pb:version-tag'.freeze
19+
BRANCH_VERSION_RELATION = 'pb:branch-version'.freeze
1920

2021
def self.call pact_source, verification_result, options = {}
2122
new(pact_source, verification_result, options).call
@@ -31,6 +32,7 @@ def initialize pact_source, verification_result, options = {}
3132

3233
def call
3334
if can_publish_verification_results?
35+
create_branch_version_if_configured
3436
tag_versions_if_configured
3537
publish_verification_results
3638
true
@@ -72,8 +74,28 @@ def tag_versions_if_configured
7274
end
7375
end
7476

77+
def create_branch_version_if_configured
78+
if Pact.configuration.provider.branch
79+
branch_version_link = provider_entity._link(BRANCH_VERSION_RELATION)
80+
if branch_version_link
81+
version_number = Pact.configuration.provider.application_version
82+
branch = Pact.configuration.provider.branch
83+
84+
Pact.configuration.output_stream.puts "INFO: Creating #{provider_name} version #{version_number} with branch \"#{branch}\""
85+
branch_entity = branch_version_link.expand(
86+
version: version_number,
87+
branch: branch
88+
).put
89+
unless branch_entity.success?
90+
raise PublicationError.new("Error returned from tagging request: status=#{branch_entity.response.code} body=#{branch_entity.response.body}")
91+
end
92+
else
93+
raise PublicationError.new("This version of the Pact Broker does not support version branches. Please update to version 2.58.0 or later.")
94+
end
95+
end
96+
end
97+
7598
def tag_versions
76-
provider_entity = pact_entity.get(PROVIDER_RELATION)
7799
tag_link = provider_entity._link(VERSION_TAG_RELATION) || hacky_tag_url(provider_entity)
78100
provider_application_version = Pact.configuration.provider.application_version
79101

@@ -111,6 +133,10 @@ def consumer_name
111133
def provider_name
112134
pact_source.pact_hash['provider']['name']
113135
end
136+
137+
def provider_entity
138+
@provider_entity ||= pact_entity.get(PROVIDER_RELATION)
139+
end
114140
end
115141
end
116142
end

spec/integration/publish_verification_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
double('provider_configuration',
1212
application_version: '1.2.3',
1313
publish_verification_results?: true,
14+
branch: nil,
1415
tags: [])
1516
end
1617

spec/lib/pact/pact_broker/fetch_pact_uris_for_verification_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ module PactBroker
1212
let(:broker_base_url) { "http://broker.org" }
1313
let(:http_client_options) { {} }
1414
let(:consumer_version_selectors) { [{ tag: "cmaster", latest: true, fallbackTag: 'blah' }] }
15+
let(:provider_version_branch) { "pbranch" }
1516
let(:provider_version_tags) { ["pmaster"] }
1617

17-
subject { FetchPactURIsForVerification.call(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options)}
18+
subject { FetchPactURIsForVerification.call(provider, consumer_version_selectors, provider_version_branch, provider_version_tags, broker_base_url, http_client_options)}
1819

1920
context "when there is an error retrieving the index resource" do
2021
before do
@@ -39,7 +40,7 @@ module PactBroker
3940

4041
let(:provider_version_tags) { "pmaster" }
4142

42-
subject { FetchPactURIsForVerification.new(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options)}
43+
subject { FetchPactURIsForVerification.new(provider, consumer_version_selectors, provider_version_branch, provider_version_tags, broker_base_url, http_client_options)}
4344

4445
it "wraps an array around it" do
4546
expect(subject.provider_version_tags).to eq ["pmaster"]

spec/lib/pact/provider/configuration/message_provider_dsl_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ module Configuration
158158
end
159159

160160
it "builds a PactVerificationFromBroker" do
161-
expect(PactVerificationFromBroker).to receive(:build).with("some-provider", ["dev"])
161+
expect(PactVerificationFromBroker).to receive(:build).with("some-provider", nil, ["dev"])
162162
subject
163163
end
164164
end

spec/lib/pact/provider/configuration/pact_verification_from_broker_spec.rb

+17-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Configuration
66
describe PactVerificationFromBroker do
77
describe 'build' do
88
let(:provider_name) {'provider-name'}
9+
let(:provider_version_branch) { 'main' }
910
let(:provider_version_tags) { ['master'] }
1011
let(:base_url) { "http://broker.org" }
1112
let(:since) { "2020-01-01" }
@@ -25,7 +26,7 @@ module Configuration
2526

2627
context "with valid values" do
2728
subject do
28-
PactVerificationFromBroker.build(provider_name, provider_version_tags) do
29+
PactVerificationFromBroker.build(provider_name, provider_version_branch, provider_version_tags) do
2930
pact_broker_base_url base_url, basic_auth_options
3031
consumer_version_tags tags
3132
enable_pending true
@@ -43,6 +44,7 @@ module Configuration
4344
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(
4445
provider_name,
4546
consumer_version_selectors,
47+
provider_version_branch,
4648
provider_version_tags,
4749
base_url,
4850
basic_auth_opts,
@@ -66,6 +68,7 @@ module Configuration
6668
anything,
6769
anything,
6870
anything,
71+
anything,
6972
{
7073
include_pending_status: true,
7174
include_wip_pacts_since: since.xmlschema
@@ -78,7 +81,7 @@ module Configuration
7881

7982
context "with a missing base url" do
8083
subject do
81-
PactVerificationFromBroker.build(provider_name, provider_version_tags) do
84+
PactVerificationFromBroker.build(provider_name, provider_version_branch, provider_version_tags) do
8285

8386
end
8487
end
@@ -90,27 +93,27 @@ module Configuration
9093

9194
context "with a non array object for consumer_version_tags" do
9295
subject do
93-
PactVerificationFromBroker.build(provider_name, provider_version_tags) do
96+
PactVerificationFromBroker.build(provider_name, provider_version_branch, provider_version_tags) do
9497
pact_broker_base_url base_url
9598
consumer_version_tags "master"
9699
end
97100
end
98101

99102
it "coerces the value into an array" do
100-
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, [{ tag: "master", latest: true}], anything, anything, anything, anything)
103+
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, [{ tag: "master", latest: true}], anything, anything, anything, anything, anything)
101104
subject
102105
end
103106
end
104107

105108
context "when no consumer_version_tags are provided" do
106109
subject do
107-
PactVerificationFromBroker.build(provider_name, provider_version_tags) do
110+
PactVerificationFromBroker.build(provider_name, provider_version_branch, provider_version_tags) do
108111
pact_broker_base_url base_url
109112
end
110113
end
111114

112-
it "creates an instance of FetchPacts with an emtpy array for the consumer_version_tags" do
113-
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, [], anything, anything, anything, anything)
115+
it "creates an instance of FetchPacts with an empty array for the consumer_version_tags" do
116+
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, [], anything, anything, anything, anything, anything)
114117
subject
115118
end
116119
end
@@ -119,14 +122,14 @@ module Configuration
119122
let(:tags) { [{ name: 'main', all: true, fallback: 'fallback' }] }
120123

121124
subject do
122-
PactVerificationFromBroker.build(provider_name, provider_version_tags) do
125+
PactVerificationFromBroker.build(provider_name, provider_version_branch, provider_version_tags) do
123126
pact_broker_base_url base_url
124127
consumer_version_tags tags
125128
end
126129
end
127130

128131
it "converts them to selectors" do
129-
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, [{ tag: "main", latest: false, fallbackTag: 'fallback'}], anything, anything, anything, anything)
132+
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, [{ tag: "main", latest: false, fallbackTag: 'fallback'}], anything, anything, anything, anything, anything)
130133
subject
131134
end
132135
end
@@ -135,7 +138,7 @@ module Configuration
135138
let(:tags) { [true] }
136139

137140
subject do
138-
PactVerificationFromBroker.build(provider_name, provider_version_tags) do
141+
PactVerificationFromBroker.build(provider_name, provider_version_branch, provider_version_tags) do
139142
pact_broker_base_url base_url
140143
consumer_version_tags tags
141144
end
@@ -150,27 +153,27 @@ module Configuration
150153
let(:tags) { [{ tag: 'main', latest: true, fallback_tag: 'fallback' }] }
151154

152155
subject do
153-
PactVerificationFromBroker.build(provider_name, provider_version_tags) do
156+
PactVerificationFromBroker.build(provider_name, provider_version_branch, provider_version_tags) do
154157
pact_broker_base_url base_url
155158
consumer_version_selectors tags
156159
end
157160
end
158161

159162
it "converts the casing of the key names" do
160-
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, [{ tag: "main", latest: true, fallbackTag: 'fallback'}], anything, anything, anything, anything)
163+
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, [{ tag: "main", latest: true, fallbackTag: 'fallback'}], anything, anything, anything, anything, anything)
161164
subject
162165
end
163166
end
164167

165168
context "when no verbose flag is provided" do
166169
subject do
167-
PactVerificationFromBroker.build(provider_name, provider_version_tags) do
170+
PactVerificationFromBroker.build(provider_name, provider_version_branch, provider_version_tags) do
168171
pact_broker_base_url base_url
169172
end
170173
end
171174

172175
it "creates an instance of FetchPactURIsForVerification with verbose: false" do
173-
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, anything, anything, anything, hash_including(verbose: false), anything)
176+
expect(Pact::PactBroker::FetchPactURIsForVerification).to receive(:new).with(anything, anything, anything, anything, anything, hash_including(verbose: false), anything)
174177
subject
175178
end
176179
end

spec/lib/pact/provider/configuration/service_provider_config_spec.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
require 'spec_helper'
21
require 'pact/provider/configuration/service_provider_config'
32

43
module Pact
54
module Provider
65
module Configuration
76
describe ServiceProviderConfig do
8-
97
describe "app" do
108

119
let(:app_block) { ->{ Object.new } }
1210

13-
subject { ServiceProviderConfig.new("1.2.3'", [], true, &app_block) }
11+
subject { ServiceProviderConfig.new("1.2.3'", "main", [], true, &app_block) }
1412

1513
it "should execute the app_block each time" do
1614
expect(subject.app.object_id).to_not equal(subject.app.object_id)
1715
end
18-
1916
end
2017
end
2118
end

spec/lib/pact/provider/configuration/service_provider_dsl_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,15 @@ module Configuration
156156
subject do
157157
ServiceProviderDSL.build 'some-provider' do
158158
app {}
159+
app_version_branch 'main'
159160
app_version_tags ['dev']
160161
honours_pacts_from_pact_broker do
161162
end
162163
end
163164
end
164165

165166
it 'builds a PactVerificationFromBroker' do
166-
expect(PactVerificationFromBroker).to receive(:build).with('some-provider', ['dev'])
167+
expect(PactVerificationFromBroker).to receive(:build).with('some-provider', 'main', ['dev'])
167168
subject
168169
end
169170
end

0 commit comments

Comments
 (0)