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
@@ -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,38 +9,44 @@ 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
-
13
- OPTIONS = [
14
- { consumer: {} },
15
- { consumer_version: {display_name: 'C.VERSION'} },
16
- { provider: {} },
17
- { provider_version: {display_name: 'P.VERSION'} },
18
- { success: {display_name: 'SUCCESS?'} },
19
- { ref: { display_name: 'RESULT#' }}
20
- ]
12
+ Line = Struct.new(:consumer, :consumer_version, :provider, :provider_version, :success, :ref, :ignored)
21
13
 
22
14
  def self.call(matrix)
23
15
  matrix_rows = matrix[:matrix]
24
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)
25
23
  verification_result_number = 0
26
- data = matrix_rows.each_with_index.collect do | line |
24
+ matrix_rows.each_with_index.collect do | line |
27
25
  has_verification_result_url = lookup(line, nil, :verificationResult, :_links, :self, :href)
28
26
  if has_verification_result_url
29
27
  verification_result_number += 1
30
28
  end
31
29
  Line.new(
32
30
  lookup(line, "???", :consumer, :name),
33
- lookup(line, "???", :consumer, :version, :number),
31
+ AbbreviateVersionNumber.call(lookup(line, "???", :consumer, :version, :number)),
34
32
  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 : ""
33
+ AbbreviateVersionNumber.call(lookup(line, "???", :provider, :version, :number)),
34
+ (lookup(line, "???", :verificationResult, :success)).to_s + ( line[:ignored] ? " [ignored]" : ""),
35
+ has_verification_result_url ? verification_result_number : "",
36
+ lookup(line, nil, :ignored)
38
37
  )
39
38
  end
39
+ end
40
40
 
41
- printer = TablePrint::Printer.new(data, OPTIONS)
42
- 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
+ ]
43
50
  end
44
51
 
45
52
  def self.lookup line, default, *keys
@@ -72,6 +79,10 @@ module PactBroker
72
79
  text
73
80
  end
74
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
75
86
  end
76
87
  end
77
88
  end
@@ -1,3 +1,9 @@
1
+ # New code
2
+ require 'pact_broker/client/pacticipants/create'
3
+ require 'pact_broker/client/pacticipants/describe'
4
+ require 'pact_broker/client/pacticipants/list'
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, { "Content-Type" => "application/json" })
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,33 @@
1
+ require 'pact_broker/client/base_command'
2
+ require 'pact_broker/client/describe_text_formatter'
3
+
4
+ module PactBroker
5
+ module Client
6
+ module Pacticipants2
7
+ class Describe < PactBroker::Client::BaseCommand
8
+
9
+ private
10
+
11
+ def do_call
12
+ PactBroker::Client::CommandResult.new(true, result_message)
13
+ end
14
+
15
+ def pacticipant_entity
16
+ @pacticipant_entity ||= index_resource._link('pb:pacticipant').expand('pacticipant' => params[:name]).get!
17
+ end
18
+
19
+ def result_message
20
+ if json_output?
21
+ pacticipant_entity.response.raw_body
22
+ else
23
+ properties = pacticipant_entity.response.body.except("_links", "_embedded")
24
+ if pacticipant_entity._embedded && pacticipant_entity._embedded["labels"] && pacticipant_entity._embedded["labels"].any?
25
+ properties["labels"] = pacticipant_entity._embedded["labels"]
26
+ end
27
+ PactBroker::Client::DescribeTextFormatter.call(properties)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ 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
@@ -0,0 +1,41 @@
1
+ require 'table_print'
2
+ require 'ostruct'
3
+
4
+ module PactBroker
5
+ module Client
6
+ module Pacticipants2
7
+ class TextFormatter
8
+ def self.call(pacticipants)
9
+ return "" if pacticipants.size == 0
10
+
11
+ data = pacticipants.collect do | pacticipant |
12
+ # Might add a UUID in at some stage. Backwards compatible supporting code.
13
+ OpenStruct.new({ uuid: "" }.merge(pacticipant).merge(url: pacticipant["_links"]["self"]["href"]))
14
+ end.sort_by{ | pacticipant | pacticipant.name.downcase }
15
+
16
+ TablePrint::Printer.new(data, tp_options(data)).table_print
17
+ end
18
+
19
+ def self.tp_options(data)
20
+ uuid_width = max_width(data, :uuid, "")
21
+ name_width = max_width(data, :name, "NAME")
22
+ display_name_width = max_width(data, :displayName, "DISPLAY NAME")
23
+
24
+ tp_options = [
25
+ { name: { width: name_width} },
26
+ { displayName: { display_name: "Display name", width: display_name_width } },
27
+ ]
28
+
29
+ if uuid_width > 0
30
+ tp_options.unshift({ uuid: { width: uuid_width } })
31
+ end
32
+ tp_options
33
+ end
34
+
35
+ def self.max_width(data, column, title)
36
+ (data.collect{ |row| row.send(column) } + [title]).compact.collect(&:size).max
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -2,6 +2,7 @@ require 'term/ansicolor'
2
2
  require 'pact_broker/client/hal_client_methods'
3
3
  require 'base64'
4
4
  require 'pact_broker/client/publish_pacts_the_old_way'
5
+ require 'pact_broker/client/colorize_notices'
5
6
 
6
7
  module PactBroker
7
8
  module Client
@@ -27,7 +28,7 @@ module PactBroker
27
28
 
28
29
  def call
29
30
  validate
30
- if index_resource.can?("pb:publish-contracts")
31
+ if ENV.fetch("PACT_BROKER_FEATURES", "").include?("publish_contracts") && index_resource.can?("pb:publish-contracts")
31
32
  publish_pacts
32
33
  PactBroker::Client::CommandResult.new(success?, message)
33
34
  else
@@ -71,7 +72,9 @@ module PactBroker
71
72
  def text_message
72
73
  response_entities.flat_map do | response_entity |
73
74
  if response_entity.success?
74
- if response_entity.logs
75
+ if response_entity.notices
76
+ PactBroker::Client::ColorizeNotices.call(response_entity.notices.collect{ |n| OpenStruct.new(n) } )
77
+ elsif response_entity.logs
75
78
  response_entity.logs.collect do | log |
76
79
  colorized_message(log)
77
80
  end
@@ -112,7 +115,8 @@ module PactBroker
112
115
  specification: "pact",
113
116
  contentType: "application/json",
114
117
  content: Base64.strict_encode64(pact_hash.to_json),
115
- writeMode: write_mode
118
+ writeMode: write_mode,
119
+ onConflict: write_mode
116
120
  }
117
121
  end
118
122
  end
@@ -0,0 +1,56 @@
1
+ module PactBroker
2
+ module Client
3
+ module StringRefinements
4
+ refine NilClass do
5
+ def blank?
6
+ true
7
+ end
8
+ end
9
+
10
+ refine String do
11
+ def not_blank?
12
+ !blank?
13
+ end
14
+
15
+ def blank?
16
+ self.strip.size == 0
17
+ end
18
+
19
+ # ripped from rubyworks/facets, thank you
20
+ def snakecase
21
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
22
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
23
+ .tr('-', '_')
24
+ .gsub(/\s/, '_')
25
+ .gsub(/__+/, '_')
26
+ .downcase
27
+ end
28
+
29
+ # ripped from rubyworks/facets, thank you
30
+ def camelcase(*separators)
31
+ case separators.first
32
+ when Symbol, TrueClass, FalseClass, NilClass
33
+ first_letter = separators.shift
34
+ end
35
+
36
+ separators = ['_', '\s'] if separators.empty?
37
+
38
+ str = self.dup
39
+
40
+ separators.each do |s|
41
+ str = str.gsub(/(?:#{s}+)([a-z])/){ $1.upcase }
42
+ end
43
+
44
+ case first_letter
45
+ when :upper, true
46
+ str = str.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase }
47
+ when :lower, false
48
+ str = str.gsub(/(\A|\s)([A-Z])/){ $1 + $2.downcase }
49
+ end
50
+
51
+ str
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,5 @@
1
1
  module PactBroker
2
2
  module Client
3
- VERSION = '1.39.0'
3
+ VERSION = '1.44.0'
4
4
  end
5
5
  end
@@ -1,4 +1,7 @@
1
- require_relative 'base_client'
1
+ require 'pact_broker/client/base_client'
2
+ require 'pact_broker/client/versions/describe'
3
+
4
+ # Old code
2
5
  require 'pact_broker/client/pacts'
3
6
 
4
7
  module PactBroker
@@ -1,9 +1,11 @@
1
+ # Need Versions class to extend BaseClient until we can remove the old Versions code
2
+ require 'pact_broker/client/base_client'
1
3
  require 'pact_broker/client/pact_broker_client'
2
4
  require 'pact_broker/client/versions/formatter'
3
5
 
4
6
  module PactBroker
5
7
  module Client
6
- class Versions
8
+ class Versions < BaseClient
7
9
  class Describe
8
10
 
9
11
  class Result
@@ -1,9 +1,11 @@
1
+ # Need Versions class to extend BaseClient until we can remove the old Versions code
2
+ require 'pact_broker/client/base_client'
1
3
  require 'pact_broker/client/versions/json_formatter'
2
4
  require 'pact_broker/client/versions/text_formatter'
3
5
 
4
6
  module PactBroker
5
7
  module Client
6
- class Versions
8
+ class Versions < BaseClient
7
9
  class Formatter
8
10
  def self.call(matrix_lines, format)
9
11
  formatter = case format
@@ -1,11 +1,13 @@
1
+ # Need Versions class to extend BaseClient until we can remove the old Versions code
2
+ require 'pact_broker/client/base_client'
1
3
  require 'table_print'
2
4
 
3
5
  module PactBroker
4
6
  module Client
5
- class Versions
7
+ class Versions < BaseClient
6
8
  class JsonFormatter
7
- def self.call(matrix)
8
- JSON.pretty_generate(matrix)
9
+ def self.call(version)
10
+ JSON.pretty_generate(version)
9
11
  end
10
12
  end
11
13
  end
@@ -1,8 +1,10 @@
1
+ # Need Versions class to extend BaseClient until we can remove the old Versions code
2
+ require 'pact_broker/client/base_client'
1
3
  require 'table_print'
2
4
 
3
5
  module PactBroker
4
6
  module Client
5
- class Versions
7
+ class Versions < BaseClient
6
8
  class TextFormatter
7
9
 
8
10
  Line = Struct.new(:number, :tags)