pact_broker-client 1.31.0 → 1.36.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +23 -0
  3. data/CHANGELOG.md +49 -0
  4. data/README.md +15 -3
  5. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +151 -8
  6. data/lib/pact_broker/client/base_client.rb +11 -9
  7. data/lib/pact_broker/client/cli/broker.rb +31 -5
  8. data/lib/pact_broker/client/cli/can_i_deploy_long_desc.txt +18 -9
  9. data/lib/pact_broker/client/git.rb +40 -22
  10. data/lib/pact_broker/client/matrix.rb +5 -2
  11. data/lib/pact_broker/client/matrix/text_formatter.rb +44 -11
  12. data/lib/pact_broker/client/pacts/list_latest_versions.rb +1 -1
  13. data/lib/pact_broker/client/publish_pacts.rb +92 -14
  14. data/lib/pact_broker/client/tasks/publication_task.rb +35 -6
  15. data/lib/pact_broker/client/version.rb +1 -1
  16. data/pact-broker-client.gemspec +2 -0
  17. data/script/publish-pact.sh +7 -1
  18. data/spec/lib/pact_broker/client/base_client_spec.rb +62 -4
  19. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +13 -2
  20. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +108 -12
  21. data/spec/lib/pact_broker/client/git_spec.rb +39 -2
  22. data/spec/lib/pact_broker/client/matrix/text_formatter_spec.rb +29 -4
  23. data/spec/lib/pact_broker/client/publish_pacts_spec.rb +119 -7
  24. data/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +88 -10
  25. data/spec/pacts/pact_broker_client-pact_broker.json +154 -8
  26. data/spec/service_providers/pact_broker_client_create_version_spec.rb +89 -0
  27. data/spec/service_providers/pact_broker_client_matrix_spec.rb +4 -0
  28. data/spec/service_providers/pact_broker_client_versions_spec.rb +1 -2
  29. data/spec/support/matrix.json +6 -1
  30. data/spec/support/matrix.txt +3 -3
  31. data/spec/support/matrix_error.txt +3 -3
  32. data/spec/support/matrix_with_results.txt +10 -0
  33. data/tasks/pact.rake +2 -0
  34. metadata +36 -4
  35. data/.travis.yml +0 -11
@@ -19,29 +19,49 @@ module PactBroker
19
19
  class PublicationTask < ::Rake::TaskLib
20
20
 
21
21
  attr_accessor :pattern, :pact_broker_base_url, :consumer_version, :tag, :write_method, :tag_with_git_branch, :pact_broker_basic_auth, :pact_broker_token
22
+ attr_reader :auto_detect_version_properties, :branch, :build_url
22
23
  alias_method :tags=, :tag=
23
24
  alias_method :tags, :tag
24
25
 
25
26
  def initialize name = nil, &block
26
27
  @name = name
28
+ @auto_detect_version_properties = nil
29
+ @version_required = false
27
30
  @pattern = 'spec/pacts/*.json'
28
31
  @pact_broker_base_url = 'http://pact-broker'
29
32
  rake_task &block
30
33
  end
31
34
 
35
+ def auto_detect_version_properties= auto_detect_version_properties
36
+ @version_required = version_required || auto_detect_version_properties
37
+ @auto_detect_version_properties = auto_detect_version_properties
38
+ end
39
+
40
+ def branch= branch
41
+ @version_required = version_required || !!branch
42
+ @branch = branch
43
+ end
44
+
45
+ def build_url= build_url
46
+ @version_required = version_required || !!build_url
47
+ @build_url = build_url
48
+ end
49
+
32
50
  private
33
51
 
52
+ attr_reader :version_required
53
+
34
54
  def rake_task &block
35
55
  namespace :pact do
36
56
  desc "Publish pacts to pact broker"
37
57
  task task_name do
38
58
  block.call(self)
39
59
  require 'pact_broker/client/publish_pacts'
40
- pact_broker_client_options = {}
41
- .merge( pact_broker_basic_auth ? { basic_auth: pact_broker_basic_auth } : {} )
42
- .merge( write_method ? { write: write_method } : {} )
43
- .merge( pact_broker_token ? { token: pact_broker_token } : {} )
44
- success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version, all_tags, pact_broker_client_options).call
60
+ pact_broker_client_options = { write: write_method, token: pact_broker_token }
61
+ pact_broker_client_options[:basic_auth] = pact_broker_basic_auth if pact_broker_basic_auth && pact_broker_basic_auth.any?
62
+ pact_broker_client_options.compact!
63
+ consumer_version_params = { number: consumer_version, branch: the_branch, build_url: build_url, tags: all_tags, version_required: version_required }.compact
64
+ success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version_params, pact_broker_client_options).call
45
65
  raise "One or more pacts failed to be published" unless success
46
66
  end
47
67
  end
@@ -53,9 +73,18 @@ module PactBroker
53
73
 
54
74
  def all_tags
55
75
  t = [*tags]
56
- t << PactBroker::Client::Git.branch if tag_with_git_branch
76
+ t << PactBroker::Client::Git.branch(raise_error: true) if tag_with_git_branch
57
77
  t.compact.uniq
58
78
  end
79
+
80
+ def the_branch
81
+ if branch.nil? && auto_detect_version_properties != false
82
+ PactBroker::Client::Git.branch(raise_error: auto_detect_version_properties == true)
83
+ else
84
+ branch
85
+ end
86
+ end
87
+
59
88
  end
60
89
  end
61
90
  end
@@ -1,5 +1,5 @@
1
1
  module PactBroker
2
2
  module Client
3
- VERSION = '1.31.0'
3
+ VERSION = '1.36.0'
4
4
  end
5
5
  end
@@ -26,9 +26,11 @@ Gem::Specification.new do |gem|
26
26
  gem.add_runtime_dependency 'table_print', '~> 1.5'
27
27
  gem.add_runtime_dependency 'thor', '>= 0.20', '< 2.0'
28
28
  gem.add_runtime_dependency 'rake', '~> 13.0' #For FileList
29
+ gem.add_runtime_dependency 'dig_rb', '~> 1.0'
29
30
 
30
31
  gem.add_development_dependency 'fakefs', '~> 0.4'
31
32
  gem.add_development_dependency 'webmock', '~> 3.0'
32
33
  gem.add_development_dependency 'conventional-changelog', '~>1.3'
33
34
  gem.add_development_dependency 'pact', '~> 1.16'
35
+ gem.add_development_dependency 'pact-support', '1.15.0'
34
36
  end
@@ -1 +1,7 @@
1
- bundle exec bin/pact-broker publish spec/pacts/pact_broker_client-pact_broker.json --consumer-app-version 1.2.3 --broker-base-url http://localhost:9292 --tag-with-git-branch --broker-username localhost --broker-password localhost
1
+ bundle exec bin/pact-broker publish spec/pacts/pact_broker_client-pact_broker.json \
2
+ --consumer-app-version 1.2.7 \
3
+ --broker-base-url http://localhost:9292 \
4
+ --broker-username localhost --broker-password localhost \
5
+ --auto-detect-branch \
6
+ --build-url http://mybuild
7
+
@@ -2,10 +2,11 @@ require 'pact_broker/client/base_client'
2
2
  module PactBroker
3
3
  module Client
4
4
  describe BaseClient do
5
- describe '#initialize' do
6
- subject { BaseClient.new(base_url: base_url) }
5
+ subject { BaseClient.new(base_url: base_url) }
6
+
7
+ let(:base_url) { 'http://pact_broker_base_url'}
7
8
 
8
- let(:base_url) { 'http://pact_broker_base_url'}
9
+ describe '#initialize' do
9
10
  let(:username) { 'pact_repo_username'}
10
11
  let(:password) { 'pact_repo_password'}
11
12
  let(:token) { '123456789' }
@@ -119,6 +120,63 @@ module PactBroker
119
120
  end
120
121
  end
121
122
  end
123
+
124
+ describe '#handle_response' do
125
+ let(:response) { double('Response', success?: true) }
126
+
127
+ it 'yields response object' do
128
+ expect { |block| subject.handle_response(response, &block) }.to yield_with_args(response)
129
+ end
130
+
131
+ context 'with 404 response' do
132
+ let(:response) { double('Response', success?: false, code: 404) }
133
+ it 'returns nil' do
134
+ expect(subject.handle_response(response)).to be_nil
135
+ end
136
+ end
137
+
138
+ context 'with 401 response' do
139
+ let(:response) { double('Response', success?: false, code: 401, body: 'body') }
140
+ it 'raise an exception with meaningful message' do
141
+ expect { subject.handle_response(response) }
142
+ .to raise_error(PactBroker::Client::Error, "Authentication failed: body")
143
+ end
144
+ end
145
+
146
+ context 'with 403 response' do
147
+ let(:response) { double('Response', success?: false, code: 403, body: 'body') }
148
+ it 'raise an exception with meaningful message' do
149
+ expect { subject.handle_response(response) }
150
+ .to raise_error(PactBroker::Client::Error, "Authorization failed (insufficient permissions): body")
151
+ end
152
+ end
153
+
154
+ context 'with 409 response' do
155
+ let(:response) { double('Response', success?: false, code: 409, body: 'body') }
156
+ it 'raise an exception with meaningful message' do
157
+ expect { subject.handle_response(response) }
158
+ .to raise_error(PactBroker::Client::Error, "Potential duplicate pacticipants: body")
159
+ end
160
+ end
161
+
162
+ context 'with unsuccessful JSON response' do
163
+ let(:response) do
164
+ double('Response', success?: false, code: 500, body: '{"errors": ["Internal server error"]}')
165
+ end
166
+ it 'raise an exception with meaningful message' do
167
+ expect { subject.handle_response(response) }
168
+ .to raise_error(PactBroker::Client::Error, "Internal server error")
169
+ end
170
+ end
171
+
172
+ context 'with unsucessful nono-JSON response ' do
173
+ let(:response) { double('Response', success?: false, code: 500, body: 'Internal server error') }
174
+ it 'raise an exception with meaningful message' do
175
+ expect { subject.handle_response(response) }
176
+ .to raise_error(PactBroker::Client::Error, "status=500 Internal server error")
177
+ end
178
+ end
179
+ end
122
180
  end
123
181
  end
124
- end
182
+ end
@@ -35,7 +35,7 @@ module PactBroker
35
35
  end
36
36
 
37
37
  it "invokes the CanIDeploy service" do
38
- expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {to_tag: nil, limit: 1000}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, {verbose: 'verbose'})
38
+ expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {to_tag: nil, to_environment: nil, limit: 1000}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, {verbose: 'verbose'})
39
39
  invoke_can_i_deploy
40
40
  end
41
41
 
@@ -53,7 +53,18 @@ module PactBroker
53
53
  end
54
54
 
55
55
  it "passes the value as the matrix options" do
56
- expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: 'prod', limit: 1000}, anything, anything)
56
+ expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: 'prod', to_environment: nil, limit: 1000}, anything, anything)
57
+ invoke_can_i_deploy
58
+ end
59
+ end
60
+
61
+ context "with --to-environment" do
62
+ before do
63
+ subject.options.to_environment = 'prod'
64
+ end
65
+
66
+ it "passes the value as the matrix options" do
67
+ expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: nil, to_environment: 'prod', limit: 1000}, anything, anything)
57
68
  invoke_can_i_deploy
58
69
  end
59
70
  end
@@ -27,9 +27,8 @@ module PactBroker::Client::CLI
27
27
  expect(PactBroker::Client::PublishPacts).to receive(:call).with(
28
28
  "http://pact-broker",
29
29
  ["spec/support/cli_test_pacts/foo.json"],
30
- "1.2.3",
31
- [],
32
- {verbose: nil}
30
+ { number: "1.2.3", tags: [], version_required: false },
31
+ { verbose: nil }
33
32
  )
34
33
  invoke_broker
35
34
  end
@@ -43,7 +42,6 @@ module PactBroker::Client::CLI
43
42
  anything,
44
43
  ["spec/support/cli_test_pacts/bar.json", "spec/support/cli_test_pacts/foo.json"],
45
44
  anything,
46
- anything,
47
45
  anything
48
46
  )
49
47
  invoke_broker
@@ -58,7 +56,6 @@ module PactBroker::Client::CLI
58
56
  anything,
59
57
  ["spec/support/cli_test_pacts/bar.json", "spec/support/cli_test_pacts/foo.json"],
60
58
  anything,
61
- anything,
62
59
  anything
63
60
  )
64
61
  invoke_broker
@@ -73,7 +70,6 @@ module PactBroker::Client::CLI
73
70
  anything,
74
71
  ["spec/support/cli_test_pacts/bar.json", "spec/support/cli_test_pacts/foo.json"],
75
72
  anything,
76
- anything,
77
73
  anything
78
74
  )
79
75
  invoke_broker
@@ -89,8 +85,7 @@ module PactBroker::Client::CLI
89
85
  expect(PactBroker::Client::PublishPacts).to receive(:call).with(
90
86
  anything,
91
87
  anything,
92
- anything,
93
- ['foo'],
88
+ hash_including(tags: ['foo']),
94
89
  anything
95
90
  )
96
91
  invoke_broker
@@ -100,7 +95,7 @@ module PactBroker::Client::CLI
100
95
  context "with tag-with-git-branch" do
101
96
  before do
102
97
  subject.options = OpenStruct.new(
103
- minimum_valid_options.merge(tag_with_git_branch: true)
98
+ minimum_valid_options.merge(tag_with_git_branch: true, tag: ['foo'])
104
99
  )
105
100
  end
106
101
 
@@ -112,9 +107,111 @@ module PactBroker::Client::CLI
112
107
  it "adds it to the list of tags when publishing" do
113
108
  expect(PactBroker::Client::PublishPacts).to receive(:call).with(
114
109
  anything,
110
+ anything,
111
+ hash_including(tags: ['foo', 'bar']),
112
+ anything
113
+ )
114
+ invoke_broker
115
+ end
116
+ end
117
+
118
+ context "with a branch specified" do
119
+ before do
120
+ subject.options = OpenStruct.new(
121
+ minimum_valid_options.merge(branch: "main")
122
+ )
123
+ end
124
+
125
+ it "passes in the branch option" do
126
+ expect(PactBroker::Client::PublishPacts).to receive(:call).with(
127
+ anything,
128
+ anything,
129
+ hash_including(branch: "main", version_required: true),
130
+ anything
131
+ )
132
+ invoke_broker
133
+ end
134
+ end
135
+
136
+ context "with --auto-detect-version-properties on by default" do
137
+ before do
138
+ subject.options = OpenStruct.new(
139
+ minimum_valid_options.merge(auto_detect_version_properties: true)
140
+ )
141
+ allow(subject).to receive(:explict_auto_detect_version_properties).and_return(false)
142
+ end
143
+
144
+ it "determines the git branch name" do
145
+ expect(PactBroker::Client::Git).to receive(:branch).with(raise_error: false)
146
+ invoke_broker
147
+ end
148
+
149
+ it "passes in the auto detected branch option with version_required: false" do
150
+ expect(PactBroker::Client::PublishPacts).to receive(:call).with(
151
+ anything,
152
+ anything,
153
+ hash_including(branch: "bar", version_required: false),
154
+ anything
155
+ )
156
+ invoke_broker
157
+ end
158
+ end
159
+
160
+
161
+ context "with --auto-detect-version-properties specified explicitly" do
162
+ before do
163
+ subject.options = OpenStruct.new(
164
+ minimum_valid_options.merge(auto_detect_version_properties: true)
165
+ )
166
+ allow(subject).to receive(:explict_auto_detect_version_properties).and_return(true)
167
+ end
168
+
169
+ it "determines the git branch name" do
170
+ expect(PactBroker::Client::Git).to receive(:branch).with(raise_error: true)
171
+ invoke_broker
172
+ end
173
+
174
+ it "passes in the auto detected branch option with version_required: true" do
175
+ expect(PactBroker::Client::PublishPacts).to receive(:call).with(
115
176
  anything,
116
177
  anything,
117
- ['bar'],
178
+ hash_including(branch: "bar", version_required: true),
179
+ anything
180
+ )
181
+ invoke_broker
182
+ end
183
+
184
+ context "with the branch specified as well" do
185
+ before do
186
+ subject.options = OpenStruct.new(
187
+ minimum_valid_options.merge(branch: "specified-branch", auto_detect_version_properties: true)
188
+ )
189
+ end
190
+
191
+ it "uses the specified branch" do
192
+ expect(PactBroker::Client::PublishPacts).to receive(:call).with(
193
+ anything,
194
+ anything,
195
+ hash_including(branch: "specified-branch", version_required: true),
196
+ anything
197
+ )
198
+ invoke_broker
199
+ end
200
+ end
201
+ end
202
+
203
+ context "with the build_url specified" do
204
+ before do
205
+ subject.options = OpenStruct.new(
206
+ minimum_valid_options.merge(build_url: "http://ci")
207
+ )
208
+ end
209
+
210
+ it "passes in the branch option" do
211
+ expect(PactBroker::Client::PublishPacts).to receive(:call).with(
212
+ anything,
213
+ anything,
214
+ hash_including(build_url: "http://ci"),
118
215
  anything
119
216
  )
120
217
  invoke_broker
@@ -133,8 +230,7 @@ module PactBroker::Client::CLI
133
230
  anything,
134
231
  anything,
135
232
  anything,
136
- anything,
137
- hash_including({basic_auth: {username: 'foo', password: 'bar'}})
233
+ hash_including(basic_auth: { username: 'foo', password: 'bar' })
138
234
  )
139
235
  invoke_broker
140
236
  end
@@ -9,9 +9,20 @@ module PactBroker
9
9
  Git::BRANCH_ENV_VAR_NAMES.each do | env_var_name|
10
10
  allow(ENV).to receive(:[]).with(env_var_name).and_return(nil)
11
11
  end
12
+ allow(Git).to receive(:execute_git_command).and_return(" origin/HEAD \n origin/foo")
12
13
  end
13
14
 
14
- subject { Git.branch }
15
+ let(:raise_exception) { true }
16
+
17
+ subject { Git.branch(raise_error: raise_exception) }
18
+
19
+ shared_examples_for "when raise_error is false" do
20
+ context "when raise_error is false" do
21
+ let(:raise_exception) { false }
22
+
23
+ it { is_expected.to be nil }
24
+ end
25
+ end
15
26
 
16
27
  context "when there is a known environment variable for the branch" do
17
28
  before do
@@ -24,8 +35,28 @@ module PactBroker
24
35
  end
25
36
  end
26
37
 
38
+ context "when there is one environment variable ending with _BRANCH" do
39
+ before do
40
+ allow(ENV).to receive(:keys).and_return(%w{FOO_BRANCH BAR_BRANCH BLAH})
41
+ allow(ENV).to receive(:[]).with("FOO_BRANCH").and_return("")
42
+ allow(ENV).to receive(:[]).with("BAR_BRANCH").and_return("meep")
43
+ end
44
+
45
+ it "returns the value of that environment variable" do
46
+ expect(subject).to eq "meep"
47
+ end
48
+ end
49
+
50
+ context "when there is more than one environment variable ending with _BRANCH" do
51
+ it "attempts to execute a git command to determine the value" do
52
+ expect(Git).to receive(:execute_git_command)
53
+ expect(subject).to_not be_empty
54
+ end
55
+ end
56
+
27
57
  context "when there is no known environment variable for the branch", skip_ci: true do
28
58
  it "attempts to execute a git command to determine the value" do
59
+ expect(Git).to receive(:execute_git_command)
29
60
  expect(subject).to_not be_empty
30
61
  end
31
62
  end
@@ -35,7 +66,7 @@ module PactBroker
35
66
  allow(Git).to receive(:execute_git_command).and_return(" origin/HEAD \n origin/foo")
36
67
  end
37
68
 
38
- it "raises an error" do
69
+ it "returns the branch" do
39
70
  expect(subject).to eq "foo"
40
71
  end
41
72
  end
@@ -48,6 +79,8 @@ module PactBroker
48
79
  it "raises an error" do
49
80
  expect { subject }.to raise_error PactBroker::Client::Error, /returned multiple branches: foo, bar/
50
81
  end
82
+
83
+ include_examples "when raise_error is false"
51
84
  end
52
85
 
53
86
 
@@ -59,6 +92,8 @@ module PactBroker
59
92
  it "raises an error" do
60
93
  expect { subject }.to raise_error PactBroker::Client::Error, /didn't return anything/
61
94
  end
95
+
96
+ include_examples "when raise_error is false"
62
97
  end
63
98
 
64
99
  context "when there is an error executing the git command" do
@@ -69,6 +104,8 @@ module PactBroker
69
104
  it "raises an error" do
70
105
  expect { subject }.to raise_error PactBroker::Client::Error, /some error/
71
106
  end
107
+
108
+ include_examples "when raise_error is false"
72
109
  end
73
110
  end
74
111
  end