pact_broker-client 1.38.3 → 1.39.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/README.md +18 -0
  4. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +100 -0
  5. data/example/scripts/publish-pact.sh +1 -1
  6. data/lib/pact_broker/client/cli/broker.rb +23 -7
  7. data/lib/pact_broker/client/cli/record_deployment_long_desc.txt +55 -0
  8. data/lib/pact_broker/client/hal/entity.rb +16 -0
  9. data/lib/pact_broker/client/hal/http_client.rb +6 -2
  10. data/lib/pact_broker/client/hal/link.rb +12 -0
  11. data/lib/pact_broker/client/hal/links.rb +15 -0
  12. data/lib/pact_broker/client/hal_client_methods.rb +8 -0
  13. data/lib/pact_broker/client/pacts.rb +0 -1
  14. data/lib/pact_broker/client/publish_pacts.rb +90 -128
  15. data/lib/pact_broker/client/publish_pacts_the_old_way.rb +194 -0
  16. data/lib/pact_broker/client/tasks/publication_task.rb +3 -3
  17. data/lib/pact_broker/client/version.rb +1 -1
  18. data/lib/pact_broker/client/versions/record_deployment.rb +4 -4
  19. data/lib/pact_broker/client/versions/record_undeployment.rb +45 -68
  20. data/script/publish-pact.sh +17 -4
  21. data/script/record-deployment.sh +1 -3
  22. data/script/record-undeployment.sh +4 -0
  23. data/spec/fixtures/foo-bar.json +31 -0
  24. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +36 -7
  25. data/spec/lib/pact_broker/client/pacticipants/create_spec.rb +3 -0
  26. data/spec/lib/pact_broker/client/{publish_pacts_spec.rb → publish_pacts_the_old_way_spec.rb} +10 -9
  27. data/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +18 -12
  28. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +4 -4
  29. data/spec/pacts/pact_broker_client-pact_broker.json +106 -0
  30. data/spec/service_providers/pact_broker_client_create_version_spec.rb +4 -4
  31. data/spec/service_providers/publish_pacts_spec.rb +115 -0
  32. data/spec/service_providers/record_deployment_spec.rb +5 -5
  33. metadata +12 -5
@@ -63,8 +63,9 @@ module PactBroker
63
63
  pact_broker_client_options[:basic_auth] = pact_broker_basic_auth if pact_broker_basic_auth && pact_broker_basic_auth.any?
64
64
  pact_broker_client_options.compact!
65
65
  consumer_version_params = { number: consumer_version, branch: the_branch, build_url: build_url, tags: all_tags, version_required: version_required }.compact
66
- success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version_params, pact_broker_client_options).call
67
- raise "One or more pacts failed to be published" unless success
66
+ result = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version_params, {}, pact_broker_client_options).call
67
+ $stdout.puts result.message
68
+ raise "One or more pacts failed to be published" unless result.success
68
69
  end
69
70
  end
70
71
  end
@@ -86,7 +87,6 @@ module PactBroker
86
87
  branch
87
88
  end
88
89
  end
89
-
90
90
  end
91
91
  end
92
92
  end
@@ -1,5 +1,5 @@
1
1
  module PactBroker
2
2
  module Client
3
- VERSION = '1.38.3'
3
+ VERSION = '1.39.0'
4
4
  end
5
5
  end
@@ -19,7 +19,7 @@ module PactBroker
19
19
  @pacticipant_name = params.fetch(:pacticipant_name)
20
20
  @version_number = params.fetch(:version_number)
21
21
  @environment_name = params.fetch(:environment_name)
22
- @replaced_previous_deployed_version = params.fetch(:replaced_previous_deployed_version)
22
+ @target = params.fetch(:target)
23
23
  @output = params.fetch(:output)
24
24
  @pact_broker_client_options = pact_broker_client_options
25
25
  end
@@ -36,7 +36,7 @@ module PactBroker
36
36
  private
37
37
 
38
38
  attr_reader :pact_broker_base_url, :pact_broker_client_options
39
- attr_reader :pacticipant_name, :version_number, :environment_name, :replaced_previous_deployed_version, :output
39
+ attr_reader :pacticipant_name, :version_number, :environment_name, :target, :output
40
40
  attr_reader :deployed_version_resource
41
41
 
42
42
  def check_environment_exists
@@ -75,13 +75,13 @@ module PactBroker
75
75
  end
76
76
 
77
77
  def record_deployment_request_body
78
- { replacedPreviousDeployedVersion: replaced_previous_deployed_version }
78
+ { replacedPreviousDeployedVersion: target }
79
79
  end
80
80
 
81
81
  def result_message
82
82
  if output == "text"
83
83
  message = "Recorded deployment of #{pacticipant_name} version #{version_number} to #{environment_name} in #{pact_broker_name}."
84
- suffix = replaced_previous_deployed_version ? " Marked previous deployed version as undeployed." : ""
84
+ suffix = target ? " Marked previous deployed version as undeployed." : ""
85
85
  message + suffix
86
86
  elsif output == "json"
87
87
  deployed_version_resource.response.raw_body
@@ -2,6 +2,10 @@ require 'pact_broker/client/hal_client_methods'
2
2
  require 'pact_broker/client/error'
3
3
  require 'pact_broker/client/command_result'
4
4
 
5
+ # TODO
6
+ # --limit 1
7
+ # order by date so that the oldest one gets undeployed first
8
+
5
9
  module PactBroker
6
10
  module Client
7
11
  class Versions
@@ -24,10 +28,17 @@ module PactBroker
24
28
  end
25
29
 
26
30
  def call
27
- check_environment_exists
28
- # record_undeployment
31
+ check_if_command_supported
32
+ if deployed_version_links_for_environment.any?
33
+ @undeployment_entities = deployed_version_links_for_environment.collect do | deployed_version_link |
34
+ deployed_version_link.get!._link!("pb:record-undeployment").post!
35
+ end
36
+ else
37
+ check_environment_exists
38
+ raise_not_found_error
39
+ end
29
40
 
30
- PactBroker::Client::CommandResult.new(true, result_message)
41
+ PactBroker::Client::CommandResult.new(true, "foo")
31
42
  rescue PactBroker::Client::Error => e
32
43
  PactBroker::Client::CommandResult.new(false, e.message)
33
44
  end
@@ -35,83 +46,49 @@ module PactBroker
35
46
  private
36
47
 
37
48
  attr_reader :pact_broker_base_url, :pact_broker_client_options
38
- attr_reader :pacticipant_name, :version_number, :environment_name, :replaced_previous_deployed_version, :output
39
- attr_reader :deployed_version_resource
49
+ attr_reader :pacticipant_name, :version_number, :environment_name, :target, :output
50
+ attr_reader :deployed_version_resource, :undeployment_entities
40
51
 
41
- def check_environment_exists
42
- deployed_versions = currently_deployed_versions_for_pacticipant
43
- .get(version: version_number)
44
- .embedded_entities("deployedVersions")
45
- deployed_versions
52
+ def version_resource
53
+ index_resource._link!("pb:pacticipant-version").expand(pacticipant: pacticipant_name, version: version_number).get!
46
54
  end
47
55
 
48
- def currently_deployed_versions_for_pacticipant
49
- @currently_deployed_versions_for_pacticipant ||= index_resource
50
- ._link!("pb:environments")
51
- .get!(name: environment_name)
52
- .embedded_entities("environments")
53
- .tap { |it| raise "Environment not found '#{environment_name}'" if it.empty? }
54
- .first
55
- ._link!("pb:currently-deployed-versions-for-pacticipant")
56
- .expand(pacticipant: pacticipant_name)
56
+ def deployed_version_links
57
+ @deployed_version_links ||= version_resource._links!("pb:currently-deployed-versions")
57
58
  end
58
59
 
59
- def currently_deployed_versions_for_pacticipant_version
60
- @currently_deployed_versions ||= currently_deployed_versions_for_pacticipant
61
- .get(version: version_number)
62
- .embedded_entities("deployedVersions")
60
+ def deployed_version_links_for_environment
61
+ @deployed_version_links_for_environment ||= deployed_version_links.select(environment_name)
63
62
  end
64
63
 
65
- # def record_deployment
66
- # @deployed_version_resource =
67
- # get_record_deployment_relation
68
- # .post(record_deployment_request_body)
69
- # .assert_success!
70
- # end
71
-
72
- # def get_record_deployment_relation
73
- # record_deployment_links = get_pacticipant_version._links!("pb:record-deployment")
74
- # link_for_environment = record_deployment_links.find(environment_name)
75
- # if link_for_environment
76
- # link_for_environment
77
- # else
78
- # check_environment_exists
79
- # # Force the exception to be raised
80
- # record_deployment_links.find!(environment_name, "Environment '#{environment_name}' is not an available option for recording a deployment of #{pacticipant_name}.")
81
- # end
82
- # end
83
-
84
- # def get_pacticipant_version
85
- # index_resource
86
- # ._link!("pb:pacticipant-version")
87
- # .expand(pacticipant: pacticipant_name, version: version_number)
88
- # .get
89
- # .assert_success!(404 => "#{pacticipant_name} version #{version_number} not found")
90
- # end
91
-
92
- # def record_deployment_request_body
93
- # { replacedPreviousDeployedVersion: replaced_previous_deployed_version }
94
- # end
64
+ def check_environment_exists
65
+ index_resource
66
+ ._link!("pb:environments")
67
+ .get!
68
+ ._links("pb:environments")
69
+ .find!(environment_name, "No environment found with name '#{environment_name}'")
70
+ end
95
71
 
96
- def result_message
97
- ""
98
- # if output == "text"
99
- # message = "Recorded deployment of #{pacticipant_name} version #{version_number} to #{environment_name} in #{pact_broker_name}."
100
- # suffix = replaced_previous_deployed_version ? " Marked previous deployed version as undeployed." : ""
101
- # message + suffix
102
- # elsif output == "json"
103
- # deployed_version_resource.response.raw_body
104
- # else
105
- # ""
106
- # end
72
+ def raise_not_found_error
73
+ raise PactBroker::Client::Error.new(deployed_version_not_found_message)
107
74
  end
108
75
 
109
- def pact_broker_name
110
- is_pactflow? ? "Pactflow" : "the Pact Broker"
76
+ def deployed_version_not_found_message
77
+ if (env_names = deployed_version_links.names).any?
78
+ "#{pacticipant_name} version #{version_number} is not currently deployed to #{environment_name}. It is currently deployed to: #{env_names.join(", ")}"
79
+ else
80
+ "#{pacticipant_name} version #{version_number} is not currently deployed to any environment."
81
+ end
111
82
  end
112
83
 
113
- def is_pactflow?
114
- deployed_version_resource.response.headers.keys.any?{ | header_name | header_name.downcase.include?("pactflow") }
84
+ def result_message
85
+ if output == "text"
86
+ message = "Recorded undeployment of #{pacticipant_name} version #{version_number} from #{environment_name} in #{pact_broker_name}."
87
+ elsif output == "json"
88
+ undeployment_entities.last.response.raw_body
89
+ else
90
+ ""
91
+ end
115
92
  end
116
93
 
117
94
  def check_if_command_supported
@@ -1,7 +1,20 @@
1
- bundle exec bin/pact-broker publish spec/pacts/pact_broker_client-pact_broker.json \
2
- --consumer-app-version 1.2.7 \
1
+ # bundle exec bin/pact-broker create-or-update-webhook http://localhost:9393 \
2
+ # --uuid d40f38c3-aaa3-47f5-9161-95c07bc16b14 \
3
+ # --request POST \
4
+ # --description "foo webhook" \
5
+ # --contract-published
6
+
7
+ bundle exec bin/pact-broker create-or-update-webhook http://localhost:9393 \
8
+ --uuid d40f38c3-aaa3-47f5-9161-95c07bc16555 \
9
+ --provider Bar \
10
+ --request POST \
11
+ --contract-published
12
+
13
+ bundle exec bin/pact-broker publish spec/pacts/pact_broker_client-pact_broker.json spec/pacts/foo-bar.json \
14
+ --consumer-app-version 1.2.12 \
3
15
  --broker-base-url http://localhost:9292 \
4
16
  --broker-username localhost --broker-password localhost \
5
- --auto-detect-branch \
6
- --build-url http://mybuild
17
+ --auto-detect-version-properties \
18
+ --build-url http://mybuild \
19
+ --branch master --tag foo5
7
20
 
@@ -1,4 +1,2 @@
1
1
  PACT_BROKER_FEATURES=deployments bundle exec bin/pact-broker record-deployment \
2
- --pacticipant Foo --version 1.0.0 --environment prod --broker-base-url http://localhost:9292
3
-
4
-
2
+ --pacticipant foo-consumer --version 1 --environment prod --broker-base-url http://localhost:9292
@@ -0,0 +1,4 @@
1
+ PACT_BROKER_FEATURES=deployments bundle exec bin/pact-broker record-undeployment \
2
+ --pacticipant foo-consumer --version 1 --environment prod --broker-base-url http://localhost:9292 --output json --verbose
3
+
4
+
@@ -0,0 +1,31 @@
1
+ {
2
+ "consumer": {
3
+ "name": "Foo"
4
+ },
5
+ "provider": {
6
+ "name": "Bar"
7
+ },
8
+ "interactions": [
9
+ {
10
+ "description": "an example request",
11
+ "providerState": "a provider state",
12
+ "request": {
13
+ "method": "GET",
14
+ "path": "/",
15
+ "headers": {
16
+ }
17
+ },
18
+ "response": {
19
+ "status": 200,
20
+ "headers": {
21
+ "Content-Type": "application/hal+json"
22
+ }
23
+ }
24
+ }
25
+ ],
26
+ "metadata": {
27
+ "pactSpecification": {
28
+ "version": "2.0.0"
29
+ }
30
+ }
31
+ }
@@ -6,11 +6,14 @@ module PactBroker::Client::CLI
6
6
  describe Broker do
7
7
  describe ".broker" do
8
8
  before do
9
- allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(true)
9
+ allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(result)
10
10
  allow(PactBroker::Client::Git).to receive(:branch).and_return("bar")
11
11
  subject.options = OpenStruct.new(minimum_valid_options)
12
+ allow($stdout).to receive(:puts)
12
13
  end
13
14
 
15
+ let(:success) { true }
16
+ let(:result) { instance_double(PactBroker::Client::CommandResult, success: success, message: "message")}
14
17
  let(:file_list) { ["spec/support/cli_test_pacts/foo.json"] }
15
18
  let(:minimum_valid_options) do
16
19
  {
@@ -28,7 +31,8 @@ module PactBroker::Client::CLI
28
31
  "http://pact-broker",
29
32
  ["spec/support/cli_test_pacts/foo.json"],
30
33
  { number: "1.2.3", tags: [], version_required: false },
31
- { }
34
+ {},
35
+ {}
32
36
  )
33
37
  invoke_broker
34
38
  end
@@ -42,6 +46,7 @@ module PactBroker::Client::CLI
42
46
  anything,
43
47
  ["spec/support/cli_test_pacts/bar.json", "spec/support/cli_test_pacts/foo.json"],
44
48
  anything,
49
+ anything,
45
50
  anything
46
51
  )
47
52
  invoke_broker
@@ -56,6 +61,7 @@ module PactBroker::Client::CLI
56
61
  anything,
57
62
  ["spec/support/cli_test_pacts/bar.json", "spec/support/cli_test_pacts/foo.json"],
58
63
  anything,
64
+ anything,
59
65
  anything
60
66
  )
61
67
  invoke_broker
@@ -70,12 +76,29 @@ module PactBroker::Client::CLI
70
76
  anything,
71
77
  ["spec/support/cli_test_pacts/bar.json", "spec/support/cli_test_pacts/foo.json"],
72
78
  anything,
79
+ anything,
73
80
  anything
74
81
  )
75
82
  invoke_broker
76
83
  end
77
84
  end
78
85
 
86
+ context "with an invalid argument specified that gets interpreted as a path" do
87
+ let(:file_list) { ['--wrong'] }
88
+
89
+ it "raises a more helpful error" do
90
+ expect{ invoke_broker }.to raise_error(Thor::Error, 'ERROR: pact-broker publish was called with invalid arguments ["--wrong"]')
91
+ end
92
+ end
93
+
94
+ context "when a specified file does not exist" do
95
+ let(:file_list) { ['no-existy'] }
96
+
97
+ it "raises a more helpful error" do
98
+ expect{ invoke_broker }.to raise_error(Thor::Error, /Specified pact file 'no-existy' does not exist/)
99
+ end
100
+ end
101
+
79
102
  context "with a tag" do
80
103
  before do
81
104
  subject.options = OpenStruct.new(minimum_valid_options.merge(tag: ['foo']))
@@ -86,6 +109,7 @@ module PactBroker::Client::CLI
86
109
  anything,
87
110
  anything,
88
111
  hash_including(tags: ['foo']),
112
+ anything,
89
113
  anything
90
114
  )
91
115
  invoke_broker
@@ -109,6 +133,7 @@ module PactBroker::Client::CLI
109
133
  anything,
110
134
  anything,
111
135
  hash_including(tags: ['foo', 'bar']),
136
+ anything,
112
137
  anything
113
138
  )
114
139
  invoke_broker
@@ -127,6 +152,7 @@ module PactBroker::Client::CLI
127
152
  anything,
128
153
  anything,
129
154
  hash_including(branch: "main", version_required: true),
155
+ anything,
130
156
  anything
131
157
  )
132
158
  invoke_broker
@@ -151,6 +177,7 @@ module PactBroker::Client::CLI
151
177
  anything,
152
178
  anything,
153
179
  hash_including(branch: "bar", version_required: false),
180
+ anything,
154
181
  anything
155
182
  )
156
183
  invoke_broker
@@ -176,6 +203,7 @@ module PactBroker::Client::CLI
176
203
  anything,
177
204
  anything,
178
205
  hash_including(branch: "bar", version_required: true),
206
+ anything,
179
207
  anything
180
208
  )
181
209
  invoke_broker
@@ -193,6 +221,7 @@ module PactBroker::Client::CLI
193
221
  anything,
194
222
  anything,
195
223
  hash_including(branch: "specified-branch", version_required: true),
224
+ anything,
196
225
  anything
197
226
  )
198
227
  invoke_broker
@@ -212,6 +241,7 @@ module PactBroker::Client::CLI
212
241
  anything,
213
242
  anything,
214
243
  hash_including(build_url: "http://ci"),
244
+ anything,
215
245
  anything
216
246
  )
217
247
  invoke_broker
@@ -230,6 +260,7 @@ module PactBroker::Client::CLI
230
260
  anything,
231
261
  anything,
232
262
  anything,
263
+ anything,
233
264
  hash_including(basic_auth: { username: 'foo', password: 'bar' })
234
265
  )
235
266
  invoke_broker
@@ -267,12 +298,10 @@ module PactBroker::Client::CLI
267
298
  end
268
299
 
269
300
  context "when the publish command is not successful" do
270
- before do
271
- allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(false)
272
- end
301
+ let(:success) { false }
273
302
 
274
- it "raises a PactPublicationError" do
275
- expect { invoke_broker }.to raise_error PactPublicationError
303
+ it "raises a SystemExit" do
304
+ expect { invoke_broker }.to raise_error SystemExit
276
305
  end
277
306
  end
278
307
  end
@@ -5,6 +5,9 @@ module PactBroker
5
5
  module Pacticipants2
6
6
  describe Create do
7
7
  describe ".call" do
8
+ before do
9
+ allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:sleep)
10
+ end
8
11
  let(:pact_broker_client_options) { {} }
9
12
  let(:broker_base_url) { "http://url" }
10
13
  let(:params) { { name: 'Foo' } }
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
  require 'fakefs/safe'
3
- require 'pact_broker/client/publish_pacts'
3
+ require 'pact_broker/client/publish_pacts_the_old_way'
4
4
  require 'json'
5
5
 
6
6
  module PactBroker
7
7
  module Client
8
- describe PublishPacts do
8
+ describe PublishPactsTheOldWay do
9
9
 
10
10
  # The amount of stubbing that we have to do here indicates this class is doing
11
11
  # TOO MUCH and needs to be split up!
@@ -23,7 +23,7 @@ module PactBroker
23
23
  File.open("spec/pacts/consumer-provider.json", "w") { |file| file << pact_hash.to_json }
24
24
  File.open("spec/pacts/consumer-provider-2.json", "w") { |file| file << pact_hash.to_json }
25
25
  File.open("spec/pacts/foo-bar.json", "w") { |file| file << pact_hash_2.to_json }
26
- allow_any_instance_of(PublishPacts).to receive(:create_index_entry_point).and_return(index_entry_point)
26
+ allow_any_instance_of(PublishPactsTheOldWay).to receive(:create_index_entry_point).and_return(index_entry_point)
27
27
  end
28
28
 
29
29
  after do
@@ -63,8 +63,9 @@ module PactBroker
63
63
  let(:index_entry_point) { instance_double("PactBroker::Client::Hal::EntryPoint", :get! => index_resource )}
64
64
  let(:index_resource) { instance_double("PactBroker::Client::Hal::Entity", can?: can_create_version ) }
65
65
  let(:can_create_version) { false }
66
+ let(:options) { {} }
66
67
 
67
- subject { PublishPacts.new(pact_broker_base_url, pact_file_paths, consumer_version_params, pact_broker_client_options) }
68
+ subject { PublishPactsTheOldWay.new(pact_broker_base_url, pact_file_paths, consumer_version_params, options, pact_broker_client_options) }
68
69
 
69
70
  describe "call" do
70
71
  it "creates a PactBroker Client" do
@@ -85,7 +86,7 @@ module PactBroker
85
86
  end
86
87
 
87
88
  it "returns true" do
88
- expect(subject.call).to be true
89
+ expect(subject.call.success).to eq true
89
90
  end
90
91
  end
91
92
 
@@ -140,7 +141,7 @@ module PactBroker
140
141
  end
141
142
 
142
143
  it "returns false" do
143
- expect(subject.call).to be false
144
+ expect(subject.call.success).to be false
144
145
  end
145
146
  end
146
147
 
@@ -212,7 +213,7 @@ module PactBroker
212
213
  end
213
214
 
214
215
  it "returns false" do
215
- expect(subject.call).to eq false
216
+ expect(subject.call.success).to eq false
216
217
  end
217
218
  end
218
219
  end
@@ -230,7 +231,7 @@ module PactBroker
230
231
  end
231
232
 
232
233
  it "returns false" do
233
- expect(subject.call).to eq false
234
+ expect(subject.call.success).to eq false
234
235
  end
235
236
  end
236
237
 
@@ -255,7 +256,7 @@ module PactBroker
255
256
  end
256
257
 
257
258
  it "returns true" do
258
- expect(subject.call).to eq true
259
+ expect(subject.call.success).to eq true
259
260
  end
260
261
  end
261
262