pact_broker-client 1.38.1 → 1.41.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +38 -0
  4. data/Gemfile +4 -0
  5. data/README.md +18 -0
  6. data/bin/pact-broker +6 -0
  7. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +61 -140
  8. data/example/scripts/publish-pact.sh +1 -1
  9. data/lib/pact_broker/client/base_client.rb +1 -1
  10. data/lib/pact_broker/client/can_i_deploy.rb +20 -3
  11. data/lib/pact_broker/client/cli/broker.rb +35 -10
  12. data/lib/pact_broker/client/cli/record_deployment_long_desc.txt +0 -0
  13. data/lib/pact_broker/client/cli/version_selector_options_parser.rb +4 -0
  14. data/lib/pact_broker/client/colorize_notices.rb +31 -0
  15. data/lib/pact_broker/client/hal/entity.rb +16 -0
  16. data/lib/pact_broker/client/hal/http_client.rb +37 -1
  17. data/lib/pact_broker/client/hal/link.rb +12 -0
  18. data/lib/pact_broker/client/hal/links.rb +15 -0
  19. data/lib/pact_broker/client/hal_client_methods.rb +8 -0
  20. data/lib/pact_broker/client/matrix.rb +4 -0
  21. data/lib/pact_broker/client/matrix/abbreviate_version_number.rb +15 -0
  22. data/lib/pact_broker/client/matrix/resource.rb +26 -1
  23. data/lib/pact_broker/client/matrix/text_formatter.rb +11 -8
  24. data/lib/pact_broker/client/pacts.rb +0 -1
  25. data/lib/pact_broker/client/publish_pacts.rb +94 -128
  26. data/lib/pact_broker/client/publish_pacts_the_old_way.rb +194 -0
  27. data/lib/pact_broker/client/tasks/publication_task.rb +3 -3
  28. data/lib/pact_broker/client/version.rb +1 -1
  29. data/lib/pact_broker/client/versions/record_deployment.rb +6 -6
  30. data/lib/pact_broker/client/versions/record_undeployment.rb +45 -68
  31. data/pact-broker-client.gemspec +1 -0
  32. data/script/approve-all.sh +6 -0
  33. data/script/publish-pact.sh +36 -5
  34. data/script/record-deployment.sh +1 -3
  35. data/script/record-undeployment.sh +4 -0
  36. data/spec/fixtures/approvals/can_i_deploy_ignore.approved.txt +13 -0
  37. data/spec/fixtures/foo-bar.json +31 -0
  38. data/spec/lib/pact_broker/client/can_i_deploy_spec.rb +47 -5
  39. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +3 -3
  40. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +36 -7
  41. data/spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb +21 -0
  42. data/spec/lib/pact_broker/client/hal/http_client_spec.rb +64 -7
  43. data/spec/lib/pact_broker/client/pacticipants/create_spec.rb +3 -0
  44. data/spec/lib/pact_broker/client/{publish_pacts_spec.rb → publish_pacts_the_old_way_spec.rb} +10 -9
  45. data/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +18 -12
  46. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +5 -5
  47. data/spec/pacts/pact_broker_client-pact_broker.json +50 -124
  48. data/spec/service_providers/pact_broker_client_create_version_spec.rb +4 -4
  49. data/spec/service_providers/pact_broker_client_matrix_ignore_spec.rb +98 -0
  50. data/spec/service_providers/publish_pacts_spec.rb +116 -0
  51. data/spec/service_providers/record_deployment_spec.rb +6 -7
  52. data/spec/spec_helper.rb +2 -1
  53. data/spec/support/approvals.rb +26 -0
  54. data/spec/support/shared_context.rb +6 -2
  55. metadata +41 -5
@@ -15,10 +15,14 @@ module PactBroker
15
15
  using PactBroker::Client::HashRefinements
16
16
 
17
17
  desc 'can-i-deploy', ''
18
- long_desc File.read(File.join(File.dirname(__FILE__), 'can_i_deploy_long_desc.txt'))
18
+ long_desc File.read(File.join(__dir__, 'can_i_deploy_long_desc.txt'))
19
19
 
20
20
  method_option :pacticipant, required: true, aliases: "-a", desc: "The pacticipant name. Use once for each pacticipant being checked."
21
21
  method_option :version, required: false, aliases: "-e", desc: "The pacticipant version. Must be entered after the --pacticipant that it relates to."
22
+
23
+ if ENV.fetch("PACT_BROKER_FEATURES", "").include?("ignore")
24
+ method_option :ignore, required: false, desc: "The pacticipant name to ignore. Use once for each pacticipant being ignored. A specific version can be ignored by also specifying a --version."
25
+ end
22
26
  method_option :latest, required: false, aliases: "-l", banner: '[TAG]', desc: "Use the latest pacticipant version. Optionally specify a TAG to use the latest version with the specified tag."
23
27
  method_option :to, required: false, banner: 'TAG', desc: "This is too hard to explain in a short sentence. Look at the examples.", default: nil
24
28
  method_option :to_environment, required: false, banner: 'ENVIRONMENT', desc: "The environment into which the pacticipant(s) are to be deployed", default: nil, hide: true
@@ -34,11 +38,17 @@ module PactBroker
34
38
  require 'pact_broker/client/can_i_deploy'
35
39
 
36
40
  validate_credentials
37
- selectors = VersionSelectorOptionsParser.call(ARGV)
41
+ selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| !s[:ignore] }
42
+ ignore_selectors = if ENV.fetch("PACT_BROKER_FEATURES", "").include?("ignore")
43
+ VersionSelectorOptionsParser.call(ARGV).select { |s| s[:ignore] }
44
+ else
45
+ []
46
+ end
38
47
  validate_can_i_deploy_selectors(selectors)
39
48
  can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval }
40
- result = CanIDeploy.call(options.broker_base_url, selectors, { to_tag: options.to, to_environment: options.to_environment, limit: options.limit }, can_i_deploy_options, pact_broker_client_options)
49
+ result = CanIDeploy.call(options.broker_base_url, selectors, { to_tag: options.to, to_environment: options.to_environment, limit: options.limit, ignore_selectors: ignore_selectors }, can_i_deploy_options, pact_broker_client_options)
41
50
  $stdout.puts result.message
51
+ $stdout.flush
42
52
  exit(can_i_deploy_exit_status) unless result.success
43
53
  end
44
54
 
@@ -50,14 +60,16 @@ module PactBroker
50
60
  method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag consumer version with the name of the current git branch. Default: false"
51
61
  method_option :build_url, desc: "The build URL that created the pact"
52
62
  method_option :merge, type: :boolean, default: false, require: false, desc: "If a pact already exists for this consumer version and provider, merge the contents. Useful when running Pact tests concurrently on different build nodes."
63
+ method_option :output, aliases: "-o", desc: "json or text", default: 'text'
53
64
  shared_authentication_options
54
65
 
55
66
  def publish(*pact_files)
56
67
  require 'pact_broker/client/error'
57
68
  validate_credentials
58
69
  validate_pact_files(pact_files)
59
- success = publish_pacts(pact_files)
60
- raise PactPublicationError, "One or more pacts failed to be published" unless success
70
+ result = publish_pacts(pact_files)
71
+ $stdout.puts result.message
72
+ exit(1) unless result.success
61
73
  rescue PactBroker::Client::Error => e
62
74
  raise PactPublicationError, "#{e.class} - #{e.message}"
63
75
  end
@@ -171,11 +183,11 @@ module PactBroker
171
183
  if ENV.fetch("PACT_BROKER_FEATURES", "").include?("deployments")
172
184
 
173
185
  ignored_and_hidden_potential_options_from_environment_variables
174
- desc "record-deployment", "Record deployment of a pacticipant version to an environment"
186
+ desc "record-deployment", "Record deployment of a pacticipant version to an environment. See https://docs.pact.io/go/record_deployment for more information."
175
187
  method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was deployed."
176
188
  method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was deployed."
177
189
  method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was deployed to."
178
- method_option :replaced_previous_deployed_version, type: :boolean, default: true, required: false, desc: "Whether or not this deployment replaced the previous deployed version. If it did, the previous deployed version of this pacticipant will be marked as undeployed in the Pact Broker."
190
+ method_option :target, default: nil, required: false, desc: "Optional. The target of the deployment - a logical identifer required to differentiate deployments when there are multiple instances of the same application in an environment."
179
191
  method_option :output, aliases: "-o", desc: "json or text", default: 'text'
180
192
  shared_authentication_options
181
193
 
@@ -185,7 +197,7 @@ module PactBroker
185
197
  pacticipant_name: options.pacticipant,
186
198
  version_number: options.version,
187
199
  environment_name: options.environment,
188
- replaced_previous_deployed_version: options.replaced_previous_deployed_version,
200
+ target: options.target,
189
201
  output: options.output
190
202
  }
191
203
  result = PactBroker::Client::Versions::RecordDeployment.call(
@@ -281,6 +293,7 @@ module PactBroker
281
293
  options.broker_base_url,
282
294
  file_list(pact_files),
283
295
  consumer_version_params,
296
+ { merge: options[:merge], output: options.output }.compact,
284
297
  pact_broker_client_options.merge(write_options)
285
298
  )
286
299
  end
@@ -289,13 +302,26 @@ module PactBroker
289
302
  require 'rake/file_list'
290
303
 
291
304
  correctly_separated_pact_files = pact_files.collect{ |path| path.gsub(/\\+/, '/') }
292
- Rake::FileList[correctly_separated_pact_files].collect do | path |
305
+ paths = Rake::FileList[correctly_separated_pact_files].collect do | path |
293
306
  if File.directory?(path)
294
307
  Rake::FileList[File.join(path, "*.json")]
295
308
  else
296
309
  path
297
310
  end
298
311
  end.flatten
312
+ validate_pact_path_list(paths)
313
+ end
314
+
315
+ def validate_pact_path_list(paths)
316
+ paths.collect do | path |
317
+ if File.exist?(path)
318
+ path
319
+ elsif path.start_with?("-")
320
+ raise Thor::Error.new("ERROR: pact-broker publish was called with invalid arguments #{[path]}")
321
+ else
322
+ raise Thor::Error.new("Specified pact file '#{path}' does not exist. This sometimes indicates one of the arguments has been specified with the wrong name and has been incorrectly identified as a file path.")
323
+ end
324
+ end
299
325
  end
300
326
 
301
327
  def tags
@@ -378,7 +404,6 @@ module PactBroker
378
404
  provider: options.provider,
379
405
  events: events
380
406
  }
381
-
382
407
  end
383
408
 
384
409
  def run_webhook_commands webhook_url
@@ -12,6 +12,8 @@ module PactBroker
12
12
  case word
13
13
  when "--pacticipant", "-a"
14
14
  selectors << {}
15
+ when "--ignore"
16
+ selectors << { ignore: true }
15
17
  when "--latest", "-l"
16
18
  selectors << { pacticipant: nil } if selectors.empty?
17
19
  selectors.last[:latest] = true
@@ -21,6 +23,8 @@ module PactBroker
21
23
  case previous_option
22
24
  when "--pacticipant", "-a"
23
25
  selectors.last[:pacticipant] = word
26
+ when "--ignore"
27
+ selectors.last[:pacticipant] = word
24
28
  when "--version", "-e"
25
29
  selectors << { pacticipant: nil } if selectors.empty?
26
30
  selectors.last[:version] = word
@@ -0,0 +1,31 @@
1
+ require 'term/ansicolor'
2
+
3
+ module PactBroker
4
+ module Client
5
+ class ColorizeNotices
6
+ def self.call(notices)
7
+ notices.collect do | notice |
8
+ colorized_message(notice)
9
+ end
10
+ end
11
+
12
+ def self.colorized_message(notice)
13
+ color = color_for_type(notice.type)
14
+ if color
15
+ ::Term::ANSIColor.color(color, notice.text || '')
16
+ else
17
+ notice.text
18
+ end
19
+ end
20
+
21
+ def self.color_for_type(type)
22
+ case type
23
+ when "warning", "prompt" then "yellow"
24
+ when "error", "danger" then :red
25
+ when "success" then :green
26
+ else nil
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -23,18 +23,34 @@ module PactBroker
23
23
  _link(key).get(*args)
24
24
  end
25
25
 
26
+ def get!(key, *args)
27
+ get(key, *args).assert_success!
28
+ end
29
+
26
30
  def post(key, *args)
27
31
  _link(key).post(*args)
28
32
  end
29
33
 
34
+ def post!(key, *args)
35
+ post(key, *args).assert_success!
36
+ end
37
+
30
38
  def put(key, *args)
31
39
  _link(key).put(*args)
32
40
  end
33
41
 
42
+ def put!(key, *args)
43
+ put(key, *args).assert_success!
44
+ end
45
+
34
46
  def patch(key, *args)
35
47
  _link(key).patch(*args)
36
48
  end
37
49
 
50
+ def patch!(key, *args)
51
+ patch(key, *args).assert_success!
52
+ end
53
+
38
54
  def can?(key)
39
55
  @links.key? key.to_s
40
56
  end
@@ -7,6 +7,7 @@ module PactBroker
7
7
  module Client
8
8
  module Hal
9
9
  class HttpClient
10
+ RETRYABLE_ERRORS = [Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EHOSTUNREACH, Net::ReadTimeout]
10
11
  attr_accessor :username, :password, :verbose, :token
11
12
 
12
13
  def initialize options
@@ -53,7 +54,7 @@ module PactBroker
53
54
  end
54
55
 
55
56
  def perform_request request, uri
56
- response = Retry.until_truthy_or_max_times do
57
+ response = until_truthy_or_max_times(condition: ->(resp) { resp.code.to_i < 500 }) do
57
58
  http = Net::HTTP.new(uri.host, uri.port, :ENV)
58
59
  http.set_debug_output(output_stream) if verbose
59
60
  http.use_ssl = (uri.scheme == 'https')
@@ -69,6 +70,41 @@ module PactBroker
69
70
  Response.new(response)
70
71
  end
71
72
 
73
+ def until_truthy_or_max_times options = {}
74
+ max_tries = options.fetch(:times, default_max_tries)
75
+ tries = 0
76
+ sleep_interval = options.fetch(:sleep, 5)
77
+ sleep(sleep_interval) if options[:sleep_first]
78
+ while true
79
+ begin
80
+ result = yield
81
+ return result if max_tries < 2
82
+ if options[:condition]
83
+ condition_result = options[:condition].call(result)
84
+ return result if condition_result
85
+ else
86
+ return result if result
87
+ end
88
+ tries += 1
89
+ return result if max_tries == tries
90
+ sleep sleep_interval
91
+ rescue *RETRYABLE_ERRORS => e
92
+ tries += 1
93
+ $stderr.puts "ERROR: Error making request - #{e.class} #{e.message} #{e.backtrace.find{|l| l.include?('pact_broker-client')}}, attempt #{tries} of #{max_tries}"
94
+ raise e if max_tries == tries
95
+ sleep sleep_interval
96
+ end
97
+ end
98
+ end
99
+
100
+ def default_max_tries
101
+ 5
102
+ end
103
+
104
+ def sleep seconds
105
+ Kernel.sleep seconds
106
+ end
107
+
72
108
  def output_stream
73
109
  AuthorizationHeaderRedactor.new($stdout)
74
110
  end
@@ -49,14 +49,26 @@ module PactBroker
49
49
  wrap_response(href, @http_client.put(href, payload ? JSON.dump(payload) : nil, headers))
50
50
  end
51
51
 
52
+ def put!(*args)
53
+ put(*args).assert_success!
54
+ end
55
+
52
56
  def post(payload = nil, headers = {})
53
57
  wrap_response(href, @http_client.post(href, payload ? JSON.dump(payload) : nil, headers))
54
58
  end
55
59
 
60
+ def post!(*args)
61
+ post(*args).assert_success!
62
+ end
63
+
56
64
  def patch(payload = nil, headers = {})
57
65
  wrap_response(href, @http_client.patch(href, payload ? JSON.dump(payload) : nil, headers))
58
66
  end
59
67
 
68
+ def patch!(*args)
69
+ patch(*args).assert_success!
70
+ end
71
+
60
72
  def expand(params)
61
73
  expanded_url = expand_url(params, href)
62
74
  new_attrs = @attrs.merge('href' => expanded_url)
@@ -30,6 +30,21 @@ module PactBroker
30
30
  links.find{ | link | link.name == name }
31
31
  end
32
32
 
33
+ def select!(name, not_found_message = nil)
34
+ selected_links = select(name)
35
+ if selected_links.any?
36
+ selected_links
37
+ else
38
+ message = not_found_message || "Could not find relation '#{key}' with name '#{name}' in resource at #{href}."
39
+ available_options = names.any? ? names.join(", ") : "<none found>"
40
+ raise RelationNotFoundError.new(message.chomp(".") + ". Available options: #{available_options}")
41
+ end
42
+ end
43
+
44
+ def select(name)
45
+ links.select{ | link | link.name == name }
46
+ end
47
+
33
48
  private
34
49
 
35
50
  attr_reader :links, :key, :href
@@ -21,6 +21,14 @@ module PactBroker
21
21
  index_entry_point.get!
22
22
  end
23
23
  end
24
+
25
+ def is_pactflow?
26
+ index_resource.response.headers.keys.any?{ | header_name | header_name.downcase.include?("pactflow") }
27
+ end
28
+
29
+ def pact_broker_name
30
+ is_pactflow? ? "Pactflow" : "the Pact Broker"
31
+ end
24
32
  end
25
33
  end
26
34
  end
@@ -60,6 +60,9 @@ module PactBroker
60
60
  elsif selectors.size == 1 && !options[:to_environment]
61
61
  opts[:latest] = 'true'
62
62
  end
63
+ if options[:ignore_selectors] && options[:ignore_selectors].any?
64
+ opts[:ignore] = convert_selector_hashes_to_params(options[:ignore_selectors])
65
+ end
63
66
  opts
64
67
  end
65
68
 
@@ -69,6 +72,7 @@ module PactBroker
69
72
  hash[:version] = selector[:version] if selector[:version]
70
73
  hash[:latest] = 'true' if selector[:latest]
71
74
  hash[:tag] = selector[:tag] if selector[:tag]
75
+ hash[:branch] = selector[:branch] if selector[:branch]
72
76
  end
73
77
  end
74
78
  end
@@ -0,0 +1,15 @@
1
+ module PactBroker
2
+ module Client
3
+ class Matrix
4
+ class AbbreviateVersionNumber
5
+ def self.call version_number
6
+ if version_number
7
+ version_number.gsub(/[A-Za-z0-9]{40}/) do | val |
8
+ val[0...7] + "..."
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -2,9 +2,22 @@ require 'pact_broker/client/base_client'
2
2
 
3
3
  module PactBroker
4
4
  module Client
5
+ class Notice < Hash
6
+ def initialize(hash)
7
+ self.merge!(hash)
8
+ end
9
+
10
+ def text
11
+ self[:text]
12
+ end
13
+
14
+ def type
15
+ self[:type]
16
+ end
17
+ end
5
18
  class Matrix < BaseClient
6
- class Resource < Hash
7
19
 
20
+ class Resource < Hash
8
21
  def initialize hash
9
22
  self.merge!(hash)
10
23
  end
@@ -21,6 +34,10 @@ module PactBroker
21
34
  !!(self[:summary] && Integer === self[:summary][:unknown] )
22
35
  end
23
36
 
37
+ def supports_ignore?
38
+ !!(self[:summary] && Integer === self[:summary][:ignored] )
39
+ end
40
+
24
41
  def unknown_count
25
42
  supports_unknown_count? ? self[:summary][:unknown] : nil
26
43
  end
@@ -32,6 +49,14 @@ module PactBroker
32
49
  def deployable?
33
50
  self[:summary][:deployable]
34
51
  end
52
+
53
+ def notices
54
+ if self[:notices].is_a?(Array)
55
+ self[:notices].collect { | notice_hash | Notice.new(notice_hash) }
56
+ else
57
+ nil
58
+ end
59
+ end
35
60
  end
36
61
  end
37
62
  end
@@ -1,6 +1,7 @@
1
1
  require 'table_print'
2
2
  require 'dig_rb'
3
3
  require 'pact_broker/client/hash_refinements'
4
+ require 'pact_broker/client/matrix/abbreviate_version_number'
4
5
 
5
6
  module PactBroker
6
7
  module Client
@@ -8,18 +9,19 @@ module PactBroker
8
9
  class TextFormatter
9
10
  using PactBroker::Client::HashRefinements
10
11
 
11
- Line = Struct.new(:consumer, :consumer_version, :provider, :provider_version, :success, :ref)
12
+ Line = Struct.new(:consumer, :consumer_version, :provider, :provider_version, :success, :ref, :ignored)
12
13
 
13
- OPTIONS = [
14
+ TP_OPTIONS = [
14
15
  { consumer: {} },
15
16
  { consumer_version: {display_name: 'C.VERSION'} },
16
17
  { provider: {} },
17
18
  { provider_version: {display_name: 'P.VERSION'} },
18
19
  { success: {display_name: 'SUCCESS?'} },
19
- { ref: { display_name: 'RESULT#' }}
20
+ { ref: { display_name: 'RESULT#' } }
20
21
  ]
21
22
 
22
23
  def self.call(matrix)
24
+ tp_options = TP_OPTIONS.dup
23
25
  matrix_rows = matrix[:matrix]
24
26
  return "" if matrix_rows.size == 0
25
27
  verification_result_number = 0
@@ -30,15 +32,16 @@ module PactBroker
30
32
  end
31
33
  Line.new(
32
34
  lookup(line, "???", :consumer, :name),
33
- lookup(line, "???", :consumer, :version, :number),
35
+ AbbreviateVersionNumber.call(lookup(line, "???", :consumer, :version, :number)),
34
36
  lookup(line, "???", :provider, :name) ,
35
- lookup(line, "???", :provider, :version, :number),
36
- (lookup(line, "???", :verificationResult, :success)).to_s,
37
- has_verification_result_url ? verification_result_number : ""
37
+ AbbreviateVersionNumber.call(lookup(line, "???", :provider, :version, :number)),
38
+ (lookup(line, "???", :verificationResult, :success)).to_s + ( line[:ignored] ? " [ignored]" : ""),
39
+ has_verification_result_url ? verification_result_number : "",
40
+ lookup(line, nil, :ignored)
38
41
  )
39
42
  end
40
43
 
41
- printer = TablePrint::Printer.new(data, OPTIONS)
44
+ printer = TablePrint::Printer.new(data, tp_options)
42
45
  printer.table_print + verification_result_urls_text(matrix)
43
46
  end
44
47