pact_broker-client 1.39.0 → 1.44.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +21 -0
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +52 -0
  5. data/Gemfile +4 -0
  6. data/README.md +39 -18
  7. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +79 -280
  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 +20 -75
  12. data/lib/pact_broker/client/cli/custom_thor.rb +12 -0
  13. data/lib/pact_broker/client/cli/deployment_commands.rb +74 -0
  14. data/lib/pact_broker/client/cli/environment_commands.rb +70 -0
  15. data/lib/pact_broker/client/cli/pacticipant_commands.rb +53 -0
  16. data/lib/pact_broker/client/cli/record_deployment_long_desc.txt +0 -55
  17. data/lib/pact_broker/client/cli/version_selector_options_parser.rb +4 -0
  18. data/lib/pact_broker/client/colorize_notices.rb +31 -0
  19. data/lib/pact_broker/client/deployments.rb +4 -0
  20. data/lib/pact_broker/client/deployments/record_deployment.rb +38 -0
  21. data/lib/pact_broker/client/deployments/record_release.rb +99 -0
  22. data/lib/pact_broker/client/deployments/record_undeployment.rb +120 -0
  23. data/lib/pact_broker/client/describe_text_formatter.rb +23 -0
  24. data/lib/pact_broker/client/environments.rb +6 -0
  25. data/lib/pact_broker/client/environments/create_environment.rb +31 -0
  26. data/lib/pact_broker/client/environments/delete_environment.rb +27 -0
  27. data/lib/pact_broker/client/environments/describe_environment.rb +26 -0
  28. data/lib/pact_broker/client/environments/environment_command.rb +66 -0
  29. data/lib/pact_broker/client/environments/list_environments.rb +30 -0
  30. data/lib/pact_broker/client/environments/text_formatter.rb +30 -0
  31. data/lib/pact_broker/client/environments/update_environment.rb +31 -0
  32. data/lib/pact_broker/client/generate_display_name.rb +27 -0
  33. data/lib/pact_broker/client/hal/entity.rb +14 -4
  34. data/lib/pact_broker/client/hal/http_client.rb +8 -2
  35. data/lib/pact_broker/client/hal/link.rb +8 -0
  36. data/lib/pact_broker/client/hal_client_methods.rb +1 -3
  37. data/lib/pact_broker/client/matrix.rb +4 -0
  38. data/lib/pact_broker/client/matrix/abbreviate_version_number.rb +15 -0
  39. data/lib/pact_broker/client/matrix/resource.rb +26 -1
  40. data/lib/pact_broker/client/matrix/text_formatter.rb +28 -17
  41. data/lib/pact_broker/client/pacticipants.rb +6 -0
  42. data/lib/pact_broker/client/pacticipants/create.rb +24 -34
  43. data/lib/pact_broker/client/pacticipants/describe.rb +33 -0
  44. data/lib/pact_broker/client/pacticipants/list.rb +34 -0
  45. data/lib/pact_broker/client/pacticipants/text_formatter.rb +41 -0
  46. data/lib/pact_broker/client/publish_pacts.rb +7 -3
  47. data/lib/pact_broker/client/string_refinements.rb +56 -0
  48. data/lib/pact_broker/client/version.rb +1 -1
  49. data/lib/pact_broker/client/versions.rb +4 -1
  50. data/lib/pact_broker/client/versions/describe.rb +3 -1
  51. data/lib/pact_broker/client/versions/formatter.rb +3 -1
  52. data/lib/pact_broker/client/versions/json_formatter.rb +5 -3
  53. data/lib/pact_broker/client/versions/text_formatter.rb +3 -1
  54. data/pact-broker-client.gemspec +2 -0
  55. data/script/approve-all.sh +6 -0
  56. data/script/publish-pact.sh +24 -6
  57. data/script/record-deployments-and-releases.sh +10 -0
  58. data/spec/fixtures/approvals/can_i_deploy_ignore.approved.txt +13 -0
  59. data/spec/fixtures/approvals/describe_environment.approved.txt +7 -0
  60. data/spec/fixtures/approvals/describe_pacticipant.approved.txt +2 -0
  61. data/spec/fixtures/approvals/list_environments.approved.txt +3 -0
  62. data/spec/integration/describe_environment_spec.rb +31 -0
  63. data/spec/lib/pact_broker/client/can_i_deploy_spec.rb +47 -5
  64. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +5 -5
  65. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +1 -1
  66. data/spec/lib/pact_broker/client/cli/broker_run_webhook_commands_spec.rb +3 -3
  67. data/spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb +21 -0
  68. data/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb +204 -0
  69. data/spec/lib/pact_broker/client/environments/delete_environment_spec.rb +120 -0
  70. data/spec/lib/pact_broker/client/environments/describe_environment_spec.rb +89 -0
  71. data/spec/lib/pact_broker/client/environments/update_environment_spec.rb +167 -0
  72. data/spec/lib/pact_broker/client/generate_display_name_spec.rb +39 -0
  73. data/spec/lib/pact_broker/client/hal/entity_spec.rb +2 -2
  74. data/spec/lib/pact_broker/client/pacticipants/create_spec.rb +2 -2
  75. data/spec/pacts/pact_broker_client-pact_broker.json +88 -287
  76. data/spec/service_providers/create_environment_spec.rb +78 -0
  77. data/spec/service_providers/list_environments_spec.rb +77 -0
  78. data/spec/service_providers/pact_broker_client_matrix_ignore_spec.rb +98 -0
  79. data/spec/service_providers/pacticipants_create_spec.rb +5 -4
  80. data/spec/service_providers/publish_pacts_spec.rb +5 -2
  81. data/spec/service_providers/record_deployment_spec.rb +17 -36
  82. data/spec/service_providers/record_release_spec.rb +135 -0
  83. data/spec/spec_helper.rb +15 -2
  84. data/spec/support/approvals.rb +26 -0
  85. data/spec/support/shared_context.rb +6 -2
  86. data/tasks/pact.rake +19 -1
  87. metadata +93 -7
  88. data/lib/pact_broker/client/versions/record_deployment.rb +0 -109
  89. data/lib/pact_broker/client/versions/record_undeployment.rb +0 -102
  90. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +0 -82
@@ -0,0 +1,6 @@
1
+ require 'pact_broker/client/environments/create_environment'
2
+ require 'pact_broker/client/environments/delete_environment'
3
+ require 'pact_broker/client/environments/describe_environment'
4
+ require 'pact_broker/client/environments/list_environments'
5
+ require 'pact_broker/client/environments/update_environment'
6
+
@@ -0,0 +1,31 @@
1
+ require 'pact_broker/client/environments/environment_command'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Environments
6
+ class CreateEnvironment < PactBroker::Client::Environments::EnvironmentCommand
7
+
8
+ private
9
+
10
+ attr_reader :created_environment_resource
11
+
12
+ def do_call
13
+ @created_environment_resource = environments_link.post!(new_environment_body)
14
+ PactBroker::Client::CommandResult.new(created_environment_resource.success?, result_message)
15
+ end
16
+
17
+ def result_message
18
+ if json_output?
19
+ created_environment_resource.response.raw_body
20
+ else
21
+ ::Term::ANSIColor.green("Created #{params[:name]} environment in #{pact_broker_name} with UUID #{uuid}")
22
+ end
23
+ end
24
+
25
+ def uuid
26
+ created_environment_resource.uuid
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -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,26 @@
1
+ require 'pact_broker/client/environments/environment_command'
2
+ require 'pact_broker/client/describe_text_formatter'
3
+
4
+ module PactBroker
5
+ module Client
6
+ module Environments
7
+ class DescribeEnvironment < PactBroker::Client::Environments::EnvironmentCommand
8
+ private
9
+
10
+ def do_call
11
+ existing_environment_resource!
12
+ PactBroker::Client::CommandResult.new(true, result_message)
13
+ end
14
+
15
+ def result_message
16
+ if json_output?
17
+ existing_environment_resource.response.raw_body
18
+ else
19
+ properties = existing_environment_resource.response.body.except("_links", "_embedded")
20
+ PactBroker::Client::DescribeTextFormatter.call(properties)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ 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)
@@ -158,14 +165,17 @@ module PactBroker
158
165
  false
159
166
  end
160
167
 
161
- def assert_success!(messages = {})
162
- default_message = "Error retrieving #{@href} status=#{response ? response.status: nil} #{response ? response.raw_body : ''}".strip
168
+ def error_message(messages = {})
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
+ end
176
+
177
+ def assert_success!(messages = {})
178
+ raise ErrorResponseReturned.new(error_message(messages), self)
169
179
  end
170
180
  end
171
181
  end
@@ -18,7 +18,7 @@ module PactBroker
18
18
  end
19
19
 
20
20
  def get href, params = {}, headers = {}
21
- query = params.collect{ |(key, value)| "#{CGI::escape(key.to_s)}=#{CGI::escape(value)}" }.join("&")
21
+ query = params.collect{ |(key, value)| "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}" }.join("&")
22
22
  uri = URI(href)
23
23
  uri.query = query
24
24
  perform_request(create_request(uri, 'Get', nil, headers), uri)
@@ -39,9 +39,15 @@ 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
- request['Content-Type'] = "application/json" if ['Post', 'Put', 'Patch'].include?(http_method)
49
+ request['Content-Type'] ||= "application/json" if ['Post', 'Put'].include?(http_method)
50
+ request['Content-Type'] ||= "application/merge-patch+json" if ['Patch'].include?(http_method)
45
51
  request['Accept'] = "application/hal+json"
46
52
  headers.each do | key, value |
47
53
  request[key] = value
@@ -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?
@@ -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