pact_broker-client 1.38.3 → 1.43.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +47 -0
  4. data/Gemfile +4 -0
  5. data/README.md +57 -18
  6. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +182 -2
  7. data/example/scripts/publish-pact.sh +1 -1
  8. data/lib/pact_broker/client/backports.rb +9 -0
  9. data/lib/pact_broker/client/base_command.rb +95 -0
  10. data/lib/pact_broker/client/can_i_deploy.rb +20 -3
  11. data/lib/pact_broker/client/cli/broker.rb +41 -28
  12. data/lib/pact_broker/client/cli/custom_thor.rb +12 -0
  13. data/lib/pact_broker/client/cli/environment_commands.rb +70 -0
  14. data/lib/pact_broker/client/cli/pacticipant_commands.rb +44 -0
  15. data/lib/pact_broker/client/cli/record_deployment_long_desc.txt +0 -0
  16. data/lib/pact_broker/client/cli/version_selector_options_parser.rb +4 -0
  17. data/lib/pact_broker/client/colorize_notices.rb +31 -0
  18. data/lib/pact_broker/client/environments.rb +3 -0
  19. data/lib/pact_broker/client/environments/create_environment.rb +31 -0
  20. data/lib/pact_broker/client/environments/delete_environment.rb +27 -0
  21. data/lib/pact_broker/client/environments/describe_environment.rb +36 -0
  22. data/lib/pact_broker/client/environments/environment_command.rb +66 -0
  23. data/lib/pact_broker/client/environments/list_environments.rb +30 -0
  24. data/lib/pact_broker/client/environments/text_formatter.rb +30 -0
  25. data/lib/pact_broker/client/environments/update_environment.rb +31 -0
  26. data/lib/pact_broker/client/generate_display_name.rb +27 -0
  27. data/lib/pact_broker/client/hal/entity.rb +26 -3
  28. data/lib/pact_broker/client/hal/http_client.rb +11 -2
  29. data/lib/pact_broker/client/hal/link.rb +20 -0
  30. data/lib/pact_broker/client/hal/links.rb +15 -0
  31. data/lib/pact_broker/client/hal_client_methods.rb +9 -3
  32. data/lib/pact_broker/client/matrix.rb +4 -0
  33. data/lib/pact_broker/client/matrix/abbreviate_version_number.rb +15 -0
  34. data/lib/pact_broker/client/matrix/resource.rb +26 -1
  35. data/lib/pact_broker/client/matrix/text_formatter.rb +28 -17
  36. data/lib/pact_broker/client/pacticipants.rb +6 -0
  37. data/lib/pact_broker/client/pacticipants/create.rb +24 -34
  38. data/lib/pact_broker/client/pacticipants/list.rb +34 -0
  39. data/lib/pact_broker/client/pacticipants/text_formatter.rb +41 -0
  40. data/lib/pact_broker/client/pacts.rb +0 -1
  41. data/lib/pact_broker/client/publish_pacts.rb +94 -128
  42. data/lib/pact_broker/client/publish_pacts_the_old_way.rb +194 -0
  43. data/lib/pact_broker/client/string_refinements.rb +56 -0
  44. data/lib/pact_broker/client/tasks/publication_task.rb +3 -3
  45. data/lib/pact_broker/client/version.rb +1 -1
  46. data/lib/pact_broker/client/versions/record_deployment.rb +14 -38
  47. data/lib/pact_broker/client/versions/record_undeployment.rb +45 -68
  48. data/pact-broker-client.gemspec +2 -0
  49. data/script/approve-all.sh +6 -0
  50. data/script/publish-pact.sh +36 -5
  51. data/script/record-deployment.sh +1 -3
  52. data/script/record-undeployment.sh +4 -0
  53. data/spec/fixtures/approvals/can_i_deploy_ignore.approved.txt +13 -0
  54. data/spec/fixtures/approvals/describe_environment.approved.txt +7 -0
  55. data/spec/fixtures/approvals/list_environments.approved.txt +3 -0
  56. data/spec/fixtures/foo-bar.json +31 -0
  57. data/spec/lib/pact_broker/client/can_i_deploy_spec.rb +47 -5
  58. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +5 -5
  59. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +36 -7
  60. data/spec/lib/pact_broker/client/cli/broker_run_webhook_commands_spec.rb +3 -3
  61. data/spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb +21 -0
  62. data/spec/lib/pact_broker/client/environments/delete_environment_spec.rb +120 -0
  63. data/spec/lib/pact_broker/client/environments/describe_environment_spec.rb +89 -0
  64. data/spec/lib/pact_broker/client/environments/update_environment_spec.rb +167 -0
  65. data/spec/lib/pact_broker/client/generate_display_name_spec.rb +39 -0
  66. data/spec/lib/pact_broker/client/hal/entity_spec.rb +2 -2
  67. data/spec/lib/pact_broker/client/pacticipants/create_spec.rb +5 -2
  68. data/spec/lib/pact_broker/client/{publish_pacts_spec.rb → publish_pacts_the_old_way_spec.rb} +10 -9
  69. data/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +18 -12
  70. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +17 -24
  71. data/spec/pacts/pact_broker_client-pact_broker.json +197 -2
  72. data/spec/service_providers/create_environment_spec.rb +78 -0
  73. data/spec/service_providers/list_environments_spec.rb +77 -0
  74. data/spec/service_providers/pact_broker_client_create_version_spec.rb +4 -4
  75. data/spec/service_providers/pact_broker_client_matrix_ignore_spec.rb +98 -0
  76. data/spec/service_providers/pacticipants_create_spec.rb +5 -4
  77. data/spec/service_providers/publish_pacts_spec.rb +116 -0
  78. data/spec/service_providers/record_deployment_spec.rb +14 -11
  79. data/spec/spec_helper.rb +3 -1
  80. data/spec/support/approvals.rb +26 -0
  81. data/spec/support/shared_context.rb +6 -2
  82. metadata +86 -5
@@ -0,0 +1,27 @@
1
+ require 'pact_broker/client/environments/environment_command'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Environments
6
+ class DeleteEnvironment < PactBroker::Client::Environments::EnvironmentCommand
7
+ private
8
+
9
+ attr_reader :deletion_request_resource
10
+
11
+ def do_call
12
+ existing_environment_resource!
13
+ @deletion_request_resource = existing_environment_link.delete!
14
+ PactBroker::Client::CommandResult.new(deletion_request_resource.success?, result_message)
15
+ end
16
+
17
+ def result_message
18
+ if json_output?
19
+ deletion_request_resource.response.raw_body
20
+ else
21
+ ::Term::ANSIColor.green("Deleted environment #{existing_environment_resource.name} from #{pact_broker_name}")
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ require 'pact_broker/client/environments/environment_command'
2
+ require 'pact_broker/client/generate_display_name'
3
+ require 'yaml'
4
+
5
+ module PactBroker
6
+ module Client
7
+ module Environments
8
+ class DescribeEnvironment < PactBroker::Client::Environments::EnvironmentCommand
9
+ include PactBroker::Client::GenerateDisplayName
10
+ private
11
+
12
+ def do_call
13
+ existing_environment_resource!
14
+ PactBroker::Client::CommandResult.new(true, result_message)
15
+ end
16
+
17
+ def result_message
18
+ if json_output?
19
+ existing_environment_resource.response.raw_body
20
+ else
21
+ YAML.dump(displayify_keys(existing_environment_resource.response.body.except("_links"))).gsub("---\n", "")
22
+ end
23
+ end
24
+
25
+ def displayify_keys(thing)
26
+ case thing
27
+ when Hash then thing.each_with_object({}) { | (key, value), new_hash | new_hash[generate_display_name(key)] = displayify_keys(value) }
28
+ when Array then thing.collect{ | value | displayify_keys(value) }
29
+ else
30
+ thing
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,66 @@
1
+ require 'pact_broker/client/base_command'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Environments
6
+ class EnvironmentCommand < PactBroker::Client::BaseCommand
7
+ NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support environments. Please upgrade to version 2.80.0 or later."
8
+
9
+ private
10
+
11
+ def new_environment_body
12
+ {
13
+ "name" => params[:name],
14
+ "displayName" => params[:display_name],
15
+ "production" => params[:production],
16
+ "contacts" => contacts
17
+ }.compact
18
+ end
19
+
20
+ def environments_link
21
+ index_resource._link!("pb:environments")
22
+ end
23
+
24
+ def existing_environment_link
25
+ index_resource
26
+ ._link!("pb:environment")
27
+ .expand(uuid: params[:uuid])
28
+ end
29
+
30
+ def existing_environment_resource
31
+ @existing_environment_resource ||= existing_environment_link.get
32
+ end
33
+
34
+ def existing_environment_resource!
35
+ existing_environment_resource.assert_success!
36
+ end
37
+
38
+ def existing_environment_body
39
+ @existing_environment_params ||= existing_environment_resource!
40
+ .response
41
+ .body
42
+ .except("uuid", "_links", "createdAt", "updatedAt")
43
+ end
44
+
45
+ def contacts
46
+ if params[:contact_name] || params[:contact_email_address]
47
+ contact = {}
48
+ contact["name"] = params[:contact_name] || "unknown"
49
+ if params[:contact_email_address]
50
+ contact["details"] = { "emailAddress" => params[:contact_email_address] }
51
+ end
52
+ [contact]
53
+ else
54
+ nil
55
+ end
56
+ end
57
+
58
+ def check_if_command_supported
59
+ unless index_resource.can?("pb:environments")
60
+ raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,30 @@
1
+ require 'pact_broker/client/environments/environment_command'
2
+ require 'pact_broker/client/environments/text_formatter'
3
+
4
+ module PactBroker
5
+ module Client
6
+ module Environments
7
+ class ListEnvironments < PactBroker::Client::Environments::EnvironmentCommand
8
+ private
9
+
10
+ attr_reader :environments_resource
11
+
12
+ def do_call
13
+ PactBroker::Client::CommandResult.new(true, result_message)
14
+ end
15
+
16
+ def environments_resource
17
+ @environments_resource = environments_link.get!
18
+ end
19
+
20
+ def result_message
21
+ if json_output?
22
+ environments_resource.response.raw_body
23
+ else
24
+ PactBroker::Client::Environments::TextFormatter.call(environments_resource._embedded["environments"])
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require 'table_print'
2
+ require 'ostruct'
3
+
4
+ module PactBroker
5
+ module Client
6
+ module Environments
7
+ class TextFormatter
8
+
9
+ def self.call(environments)
10
+ return "" if environments.size == 0
11
+
12
+ data = environments.collect do | environment |
13
+ OpenStruct.new(environment)
14
+ end.sort_by{ | environment | environment.name.downcase }
15
+
16
+ uuid_width = data.collect(&:uuid).collect(&:size).max
17
+
18
+ tp_options = [
19
+ { uuid: { width: uuid_width } },
20
+ { name: {} },
21
+ { displayName: { display_name: "Display name" } },
22
+ { production: {} }
23
+ ]
24
+
25
+ TablePrint::Printer.new(data, tp_options).table_print
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ require 'pact_broker/client/environments/environment_command'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Environments
6
+ class UpdateEnvironment < PactBroker::Client::Environments::EnvironmentCommand
7
+
8
+ private
9
+
10
+ attr_reader :updated_environment_resource
11
+
12
+ def do_call
13
+ @updated_environment_resource = existing_environment_link.put!(request_body)
14
+ PactBroker::Client::CommandResult.new(updated_environment_resource.success?, result_message)
15
+ end
16
+
17
+ def request_body
18
+ @request_body ||= existing_environment_body.merge(new_environment_body)
19
+ end
20
+
21
+ def result_message
22
+ if json_output?
23
+ updated_environment_resource.response.raw_body
24
+ else
25
+ ::Term::ANSIColor.green("Updated #{request_body["name"]} environment in #{pact_broker_name}")
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ require 'pact_broker/client/string_refinements'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module GenerateDisplayName
6
+ using PactBroker::Client::StringRefinements
7
+
8
+ def self.call(name)
9
+ return nil if name.nil?
10
+ name
11
+ .to_s
12
+ .gsub(/([A-Z])([A-Z])([a-z])/,'\1 \2\3')
13
+ .gsub(/([a-z\d])([A-Z])(\S)/,'\1 \2\3')
14
+ .gsub(/(\S)([\-_\s\.])(\S)/, '\1 \3')
15
+ .gsub(/\s+/, " ")
16
+ .strip
17
+ .split(" ")
18
+ .collect{ |word| word.camelcase(true) }
19
+ .join(" ")
20
+ end
21
+
22
+ def generate_display_name(name)
23
+ GenerateDisplayName.call(name)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -8,7 +8,14 @@ module PactBroker
8
8
  module Client
9
9
  module Hal
10
10
  class RelationNotFoundError < ::PactBroker::Client::Error; end
11
- class ErrorResponseReturned < ::PactBroker::Client::Error; end
11
+ class ErrorResponseReturned < ::PactBroker::Client::Error
12
+ attr_reader :entity
13
+
14
+ def initialize(message, entity)
15
+ super(message)
16
+ @entity = entity
17
+ end
18
+ end
12
19
 
13
20
  class Entity
14
21
  def initialize(href, data, http_client, response = nil)
@@ -23,18 +30,34 @@ module PactBroker
23
30
  _link(key).get(*args)
24
31
  end
25
32
 
33
+ def get!(key, *args)
34
+ get(key, *args).assert_success!
35
+ end
36
+
26
37
  def post(key, *args)
27
38
  _link(key).post(*args)
28
39
  end
29
40
 
41
+ def post!(key, *args)
42
+ post(key, *args).assert_success!
43
+ end
44
+
30
45
  def put(key, *args)
31
46
  _link(key).put(*args)
32
47
  end
33
48
 
49
+ def put!(key, *args)
50
+ put(key, *args).assert_success!
51
+ end
52
+
34
53
  def patch(key, *args)
35
54
  _link(key).patch(*args)
36
55
  end
37
56
 
57
+ def patch!(key, *args)
58
+ patch(key, *args).assert_success!
59
+ end
60
+
38
61
  def can?(key)
39
62
  @links.key? key.to_s
40
63
  end
@@ -143,13 +166,13 @@ module PactBroker
143
166
  end
144
167
 
145
168
  def assert_success!(messages = {})
146
- default_message = "Error retrieving #{@href} status=#{response ? response.status: nil} #{response ? response.raw_body : ''}".strip
169
+ default_message = "Error making request to #{@href} status=#{response ? response.status: nil} #{response ? response.raw_body : ''}".strip
147
170
  message = if response && messages[response.status]
148
171
  (messages[response.status] || "") + " (#{default_message})"
149
172
  else
150
173
  default_message
151
174
  end
152
- raise ErrorResponseReturned.new(message)
175
+ raise ErrorResponseReturned.new(message, self)
153
176
  end
154
177
  end
155
178
  end
@@ -39,6 +39,11 @@ module PactBroker
39
39
  perform_request(create_request(uri, 'Patch', body, headers), uri)
40
40
  end
41
41
 
42
+ def delete href, body = nil, headers = {}
43
+ uri = URI(href)
44
+ perform_request(create_request(uri, 'Delete', body, headers), uri)
45
+ end
46
+
42
47
  def create_request uri, http_method, body = nil, headers = {}
43
48
  request = Net::HTTP.const_get(http_method).new(uri.request_uri)
44
49
  request['Content-Type'] = "application/json" if ['Post', 'Put', 'Patch'].include?(http_method)
@@ -54,7 +59,7 @@ module PactBroker
54
59
  end
55
60
 
56
61
  def perform_request request, uri
57
- response = until_truthy_or_max_times(times: 5, sleep: 5, condition: ->(resp) { resp.code.to_i < 500 }) do
62
+ response = until_truthy_or_max_times(condition: ->(resp) { resp.code.to_i < 500 }) do
58
63
  http = Net::HTTP.new(uri.host, uri.port, :ENV)
59
64
  http.set_debug_output(output_stream) if verbose
60
65
  http.use_ssl = (uri.scheme == 'https')
@@ -71,7 +76,7 @@ module PactBroker
71
76
  end
72
77
 
73
78
  def until_truthy_or_max_times options = {}
74
- max_tries = options.fetch(:times, 3)
79
+ max_tries = options.fetch(:times, default_max_tries)
75
80
  tries = 0
76
81
  sleep_interval = options.fetch(:sleep, 5)
77
82
  sleep(sleep_interval) if options[:sleep_first]
@@ -97,6 +102,10 @@ module PactBroker
97
102
  end
98
103
  end
99
104
 
105
+ def default_max_tries
106
+ 5
107
+ end
108
+
100
109
  def sleep seconds
101
110
  Kernel.sleep seconds
102
111
  end
@@ -49,14 +49,34 @@ 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
+
72
+ def delete(payload = nil, headers = {})
73
+ wrap_response(href, @http_client.delete(href, payload ? JSON.dump(payload) : nil, headers))
74
+ end
75
+
76
+ def delete!(*args)
77
+ delete(*args).assert_success!
78
+ end
79
+
60
80
  def expand(params)
61
81
  expanded_url = expand_url(params, href)
62
82
  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
@@ -17,9 +17,15 @@ module PactBroker
17
17
  end
18
18
 
19
19
  def index_resource
20
- @index_resource ||= Retry.while_error do
21
- index_entry_point.get!
22
- end
20
+ @index_resource ||= index_entry_point.get!
21
+ end
22
+
23
+ def is_pactflow?
24
+ index_resource.response.headers.keys.any?{ | header_name | header_name.downcase.include?("pactflow") }
25
+ end
26
+
27
+ def pact_broker_name
28
+ is_pactflow? ? "Pactflow" : "the Pact Broker"
23
29
  end
24
30
  end
25
31
  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