pact_broker-client 1.41.0 → 1.42.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -0
  3. data/lib/pact_broker/client/backports.rb +9 -0
  4. data/lib/pact_broker/client/base_command.rb +95 -0
  5. data/lib/pact_broker/client/cli/broker.rb +11 -19
  6. data/lib/pact_broker/client/cli/custom_thor.rb +12 -0
  7. data/lib/pact_broker/client/cli/environment_commands.rb +70 -0
  8. data/lib/pact_broker/client/cli/pacticipant_commands.rb +44 -0
  9. data/lib/pact_broker/client/environments.rb +3 -0
  10. data/lib/pact_broker/client/environments/create_environment.rb +31 -0
  11. data/lib/pact_broker/client/environments/delete_environment.rb +27 -0
  12. data/lib/pact_broker/client/environments/describe_environment.rb +36 -0
  13. data/lib/pact_broker/client/environments/environment_command.rb +66 -0
  14. data/lib/pact_broker/client/environments/list_environments.rb +30 -0
  15. data/lib/pact_broker/client/environments/text_formatter.rb +30 -0
  16. data/lib/pact_broker/client/environments/update_environment.rb +31 -0
  17. data/lib/pact_broker/client/generate_display_name.rb +27 -0
  18. data/lib/pact_broker/client/hal/entity.rb +10 -3
  19. data/lib/pact_broker/client/hal/http_client.rb +5 -0
  20. data/lib/pact_broker/client/hal/link.rb +8 -0
  21. data/lib/pact_broker/client/hal_client_methods.rb +1 -3
  22. data/lib/pact_broker/client/matrix/text_formatter.rb +21 -13
  23. data/lib/pact_broker/client/pacticipants.rb +6 -0
  24. data/lib/pact_broker/client/pacticipants/create.rb +24 -34
  25. data/lib/pact_broker/client/pacticipants/list.rb +34 -0
  26. data/lib/pact_broker/client/pacticipants/text_formatter.rb +41 -0
  27. data/lib/pact_broker/client/string_refinements.rb +56 -0
  28. data/lib/pact_broker/client/version.rb +1 -1
  29. data/pact-broker-client.gemspec +1 -0
  30. data/spec/fixtures/approvals/describe_environment.approved.txt +7 -0
  31. data/spec/fixtures/approvals/list_environments.approved.txt +3 -0
  32. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +3 -3
  33. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +1 -1
  34. data/spec/lib/pact_broker/client/cli/broker_run_webhook_commands_spec.rb +3 -3
  35. data/spec/lib/pact_broker/client/environments/delete_environment_spec.rb +120 -0
  36. data/spec/lib/pact_broker/client/environments/describe_environment_spec.rb +89 -0
  37. data/spec/lib/pact_broker/client/environments/update_environment_spec.rb +167 -0
  38. data/spec/lib/pact_broker/client/generate_display_name_spec.rb +39 -0
  39. data/spec/lib/pact_broker/client/hal/entity_spec.rb +2 -2
  40. data/spec/lib/pact_broker/client/pacticipants/create_spec.rb +2 -2
  41. data/spec/service_providers/create_environment_spec.rb +78 -0
  42. data/spec/service_providers/list_environments_spec.rb +77 -0
  43. data/spec/service_providers/pacticipants_create_spec.rb +5 -4
  44. data/spec/spec_helper.rb +1 -0
  45. data/spec/support/approvals.rb +1 -1
  46. metadata +48 -3
@@ -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)
@@ -159,13 +166,13 @@ module PactBroker
159
166
  end
160
167
 
161
168
  def assert_success!(messages = {})
162
- 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
163
170
  message = if response && messages[response.status]
164
171
  (messages[response.status] || "") + " (#{default_message})"
165
172
  else
166
173
  default_message
167
174
  end
168
- raise ErrorResponseReturned.new(message)
175
+ raise ErrorResponseReturned.new(message, self)
169
176
  end
170
177
  end
171
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)
@@ -69,6 +69,14 @@ module PactBroker
69
69
  patch(*args).assert_success!
70
70
  end
71
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
+
72
80
  def expand(params)
73
81
  expanded_url = expand_url(params, href)
74
82
  new_attrs = @attrs.merge('href' => expanded_url)
@@ -17,9 +17,7 @@ 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!
23
21
  end
24
22
 
25
23
  def is_pactflow?
@@ -11,21 +11,17 @@ module PactBroker
11
11
 
12
12
  Line = Struct.new(:consumer, :consumer_version, :provider, :provider_version, :success, :ref, :ignored)
13
13
 
14
- TP_OPTIONS = [
15
- { consumer: {} },
16
- { consumer_version: {display_name: 'C.VERSION'} },
17
- { provider: {} },
18
- { provider_version: {display_name: 'P.VERSION'} },
19
- { success: {display_name: 'SUCCESS?'} },
20
- { ref: { display_name: 'RESULT#' } }
21
- ]
22
-
23
14
  def self.call(matrix)
24
- tp_options = TP_OPTIONS.dup
25
15
  matrix_rows = matrix[:matrix]
26
16
  return "" if matrix_rows.size == 0
17
+ data = prepare_data(matrix_rows)
18
+ printer = TablePrint::Printer.new(data, tp_options(data))
19
+ printer.table_print + verification_result_urls_text(matrix)
20
+ end
21
+
22
+ def self.prepare_data(matrix_rows)
27
23
  verification_result_number = 0
28
- data = matrix_rows.each_with_index.collect do | line |
24
+ matrix_rows.each_with_index.collect do | line |
29
25
  has_verification_result_url = lookup(line, nil, :verificationResult, :_links, :self, :href)
30
26
  if has_verification_result_url
31
27
  verification_result_number += 1
@@ -40,9 +36,17 @@ module PactBroker
40
36
  lookup(line, nil, :ignored)
41
37
  )
42
38
  end
39
+ end
43
40
 
44
- printer = TablePrint::Printer.new(data, tp_options)
45
- printer.table_print + verification_result_urls_text(matrix)
41
+ def self.tp_options(data)
42
+ [
43
+ { consumer: { width: max_width(data, :consumer, 'CONSUMER') } },
44
+ { consumer_version: { display_name: 'C.VERSION', width: max_width(data, :consumer_version, 'C.VERSION') } },
45
+ { provider: { width: max_width(data, :provider, 'PROVIDER') } },
46
+ { provider_version: { display_name: 'P.VERSION', width: max_width(data, :provider_version, 'P.VERSION') } },
47
+ { success: { display_name: 'SUCCESS?' } },
48
+ { ref: { display_name: 'RESULT#' } }
49
+ ]
46
50
  end
47
51
 
48
52
  def self.lookup line, default, *keys
@@ -75,6 +79,10 @@ module PactBroker
75
79
  text
76
80
  end
77
81
  end
82
+
83
+ def self.max_width(data, column, title)
84
+ (data.collect{ |row| row.send(column) } + [title]).compact.collect(&:size).max
85
+ end
78
86
  end
79
87
  end
80
88
  end
@@ -1,3 +1,9 @@
1
+ # New code
2
+ Dir.glob(File.join(__FILE__.gsub(".rb", "/**/*.rb"))).sort.each do | path |
3
+ require path
4
+ end
5
+
6
+ # Old code
1
7
  require 'pact_broker/client/base_client'
2
8
 
3
9
  module PactBroker
@@ -1,55 +1,45 @@
1
- require 'pact_broker/client/hal'
2
- require 'json'
3
- require 'pact_broker/client/command_result'
4
- require 'pact_broker/client/hal_client_methods'
1
+ require 'pact_broker/client/base_command'
5
2
 
6
3
  module PactBroker
7
4
  module Client
8
5
  module Pacticipants2
9
- class Create
6
+ class Create < PactBroker::Client::BaseCommand
10
7
 
11
- include HalClientMethods
8
+ private
12
9
 
13
- def self.call(params, pact_broker_base_url, pact_broker_client_options)
14
- new(params, pact_broker_base_url, pact_broker_client_options).call
15
- end
10
+ attr_reader :action, :response_entity
16
11
 
17
- def initialize(params, pact_broker_base_url, pact_broker_client_options)
18
- @params = params
19
- @index_entry_point = create_index_entry_point(pact_broker_base_url, pact_broker_client_options)
20
- @verbose = pact_broker_client_options[:verbose]
21
- end
12
+ def do_call
13
+ pacticipant_entity = index_resource._link('pb:pacticipant').expand('pacticipant' => params[:name]).get
22
14
 
23
- def call
24
- pacticipant_entity = index_entity._link('pb:pacticipant').expand('pacticipant' => params[:name]).get
25
- message = nil
26
15
  response_entity = if pacticipant_entity.does_not_exist?
27
- message = "Pacticipant \"#{params[:name]}\" created"
28
- index_entity._link!('pb:pacticipants').post(pacticipant_resource_params)
16
+ @action = "created"
17
+ index_resource._link!('pb:pacticipants').post!(pacticipant_resource_params)
18
+ elsif pacticipant_entity.success?
19
+ @action = "updated"
20
+ pacticipant_entity._link!('self').patch!(pacticipant_resource_params)
29
21
  else
30
- message = "Pacticipant \"#{params[:name]}\" updated"
31
- pacticipant_entity._link!('self').patch(pacticipant_resource_params)
22
+ pacticipant_entity.assert_success!
32
23
  end
33
24
 
34
25
  response_entity.assert_success!
35
- PactBroker::Client::CommandResult.new(true, message)
36
- rescue StandardError => e
37
- $stderr.puts("#{e.class} - #{e}\n#{e.backtrace.join("\n")}") if verbose
38
- PactBroker::Client::CommandResult.new(false, "#{e.class} - #{e}")
26
+ PactBroker::Client::CommandResult.new(true, result_message)
39
27
  end
40
28
 
41
- private
42
-
43
- attr_reader :index_entry_point, :params, :verbose
44
-
45
- def index_entity
46
- @index_entity ||= index_entry_point.get!
29
+ def result_message
30
+ if json_output?
31
+ response_entity.response.raw_body
32
+ else
33
+ green(message = "Pacticipant \"#{params[:name]}\" #{action}")
34
+ end
47
35
  end
48
36
 
49
37
  def pacticipant_resource_params
50
- p = { name: params[:name] }
51
- p[:repositoryUrl] = params[:repository_url] if params[:repository_url]
52
- p
38
+ {
39
+ name: params[:name],
40
+ repositoryUrl: params[:repository_url],
41
+ displayName: params[:display_name]
42
+ }.compact
53
43
  end
54
44
  end
55
45
  end
@@ -0,0 +1,34 @@
1
+ require 'pact_broker/client/base_command'
2
+ require 'pact_broker/client/pacticipants/text_formatter'
3
+
4
+ module PactBroker
5
+ module Client
6
+ module Pacticipants2
7
+ class List < PactBroker::Client::BaseCommand
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 environments_link
21
+ index_resource._link!('pb:pacticipants')
22
+ end
23
+
24
+ def result_message
25
+ if json_output?
26
+ environments_resource.response.raw_body
27
+ else
28
+ TextFormatter.call(environments_resource._embedded["pacticipants"])
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end