pact_broker-client 1.40.0 → 1.45.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 (97) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +21 -0
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +57 -0
  5. data/Gemfile +4 -0
  6. data/README.md +39 -22
  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 +98 -0
  10. data/lib/pact_broker/client/can_i_deploy.rb +57 -5
  11. data/lib/pact_broker/client/cli/broker.rb +23 -76
  12. data/lib/pact_broker/client/cli/custom_thor.rb +12 -0
  13. data/lib/pact_broker/client/cli/deployment_commands.rb +94 -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_support_ended.rb +103 -0
  23. data/lib/pact_broker/client/deployments/record_undeployment.rb +127 -0
  24. data/lib/pact_broker/client/describe_text_formatter.rb +23 -0
  25. data/lib/pact_broker/client/environments.rb +6 -0
  26. data/lib/pact_broker/client/environments/create_environment.rb +31 -0
  27. data/lib/pact_broker/client/environments/delete_environment.rb +27 -0
  28. data/lib/pact_broker/client/environments/describe_environment.rb +26 -0
  29. data/lib/pact_broker/client/environments/environment_command.rb +66 -0
  30. data/lib/pact_broker/client/environments/list_environments.rb +30 -0
  31. data/lib/pact_broker/client/environments/text_formatter.rb +30 -0
  32. data/lib/pact_broker/client/environments/update_environment.rb +31 -0
  33. data/lib/pact_broker/client/generate_display_name.rb +27 -0
  34. data/lib/pact_broker/client/hal/entity.rb +31 -6
  35. data/lib/pact_broker/client/hal/http_client.rb +8 -2
  36. data/lib/pact_broker/client/hal/link.rb +8 -0
  37. data/lib/pact_broker/client/hal_client_methods.rb +1 -3
  38. data/lib/pact_broker/client/matrix.rb +4 -0
  39. data/lib/pact_broker/client/matrix/abbreviate_version_number.rb +15 -0
  40. data/lib/pact_broker/client/matrix/resource.rb +26 -1
  41. data/lib/pact_broker/client/matrix/text_formatter.rb +28 -17
  42. data/lib/pact_broker/client/pacticipants.rb +6 -0
  43. data/lib/pact_broker/client/pacticipants/create.rb +24 -34
  44. data/lib/pact_broker/client/pacticipants/describe.rb +33 -0
  45. data/lib/pact_broker/client/pacticipants/list.rb +34 -0
  46. data/lib/pact_broker/client/pacticipants/text_formatter.rb +41 -0
  47. data/lib/pact_broker/client/publish_pacts.rb +6 -2
  48. data/lib/pact_broker/client/string_refinements.rb +56 -0
  49. data/lib/pact_broker/client/version.rb +1 -1
  50. data/lib/pact_broker/client/versions.rb +4 -1
  51. data/lib/pact_broker/client/versions/describe.rb +3 -1
  52. data/lib/pact_broker/client/versions/formatter.rb +3 -1
  53. data/lib/pact_broker/client/versions/json_formatter.rb +5 -3
  54. data/lib/pact_broker/client/versions/text_formatter.rb +3 -1
  55. data/pact-broker-client.gemspec +2 -0
  56. data/script/approve-all.sh +6 -0
  57. data/script/publish-pact.sh +12 -9
  58. data/script/record-deployments-and-releases.sh +18 -0
  59. data/spec/fixtures/approvals/can_i_deploy_failure_dry_run.approved.txt +7 -0
  60. data/spec/fixtures/approvals/can_i_deploy_ignore.approved.txt +13 -0
  61. data/spec/fixtures/approvals/can_i_deploy_success_dry_run.approved.txt +7 -0
  62. data/spec/fixtures/approvals/describe_environment.approved.txt +7 -0
  63. data/spec/fixtures/approvals/describe_pacticipant.approved.txt +2 -0
  64. data/spec/fixtures/approvals/list_environments.approved.txt +3 -0
  65. data/spec/integration/describe_environment_spec.rb +31 -0
  66. data/spec/lib/pact_broker/client/can_i_deploy_spec.rb +109 -7
  67. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +19 -6
  68. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +1 -1
  69. data/spec/lib/pact_broker/client/cli/broker_run_webhook_commands_spec.rb +3 -3
  70. data/spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb +21 -0
  71. data/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb +204 -0
  72. data/spec/lib/pact_broker/client/deployments/record_support_ended_spec.rb +208 -0
  73. data/spec/lib/pact_broker/client/deployments/record_undeployment_spec.rb +219 -0
  74. data/spec/lib/pact_broker/client/environments/delete_environment_spec.rb +120 -0
  75. data/spec/lib/pact_broker/client/environments/describe_environment_spec.rb +89 -0
  76. data/spec/lib/pact_broker/client/environments/update_environment_spec.rb +167 -0
  77. data/spec/lib/pact_broker/client/generate_display_name_spec.rb +39 -0
  78. data/spec/lib/pact_broker/client/hal/entity_spec.rb +2 -2
  79. data/spec/lib/pact_broker/client/pacticipants/create_spec.rb +2 -2
  80. data/spec/pacts/pact_broker_client-pact_broker.json +88 -287
  81. data/spec/service_providers/create_environment_spec.rb +78 -0
  82. data/spec/service_providers/list_environments_spec.rb +77 -0
  83. data/spec/service_providers/pact_broker_client_matrix_ignore_spec.rb +98 -0
  84. data/spec/service_providers/pact_broker_client_register_repository_spec.rb +2 -2
  85. data/spec/service_providers/pacticipants_create_spec.rb +5 -4
  86. data/spec/service_providers/publish_pacts_spec.rb +5 -2
  87. data/spec/service_providers/record_deployment_spec.rb +17 -36
  88. data/spec/service_providers/record_release_spec.rb +132 -0
  89. data/spec/service_providers/record_undeployment_spec.rb +166 -0
  90. data/spec/spec_helper.rb +15 -2
  91. data/spec/support/approvals.rb +26 -0
  92. data/spec/support/shared_context.rb +8 -3
  93. data/tasks/pact.rake +21 -1
  94. metadata +104 -7
  95. data/lib/pact_broker/client/versions/record_deployment.rb +0 -109
  96. data/lib/pact_broker/client/versions/record_undeployment.rb +0 -102
  97. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +0 -82
@@ -10,4 +10,13 @@ class Hash
10
10
  def compact!
11
11
  reject! {|_key, value| value == nil}
12
12
  end unless method_defined? :compact!
13
+
14
+ def except(*keys)
15
+ if keys.size > 4 && size > 4 # index if O(m*n) is big
16
+ h = {}
17
+ keys.each { |key| h[key] = true }
18
+ keys = h
19
+ end
20
+ reject { |key, _value| keys.include? key}
21
+ end unless method_defined? :except
13
22
  end
@@ -0,0 +1,98 @@
1
+ require 'pact_broker/client/hal_client_methods'
2
+ require 'pact_broker/client/error'
3
+ require 'pact_broker/client/command_result'
4
+ require 'term/ansicolor'
5
+ require 'pact_broker/client/backports'
6
+
7
+ module PactBroker
8
+ module Client
9
+ class BaseCommand
10
+ include PactBroker::Client::HalClientMethods
11
+
12
+ def self.call(params, options, pact_broker_client_options)
13
+ new(params, options, pact_broker_client_options).call
14
+ end
15
+
16
+ def initialize(params, options, pact_broker_client_options)
17
+ @params = params
18
+ @options = options
19
+ @pact_broker_base_url = pact_broker_client_options.fetch(:pact_broker_base_url)
20
+ @pact_broker_client_options = pact_broker_client_options
21
+ end
22
+
23
+ def call
24
+ check_if_command_supported
25
+ do_call
26
+ rescue PactBroker::Client::Hal::ErrorResponseReturned => e
27
+ handle_http_error(e)
28
+ rescue PactBroker::Client::Error => e
29
+ handle_ruby_error(e)
30
+ rescue StandardError => e
31
+ handle_ruby_error(e, verbose?)
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :params, :options
37
+ attr_reader :pact_broker_base_url, :pact_broker_client_options
38
+
39
+ def handle_http_error(e)
40
+ message = if json_output?
41
+ body = e.entity.response.raw_body
42
+ (body.nil? || body == "") ? "{}" : body
43
+ else
44
+ red(e.message)
45
+ end
46
+ PactBroker::Client::CommandResult.new(false, message)
47
+ end
48
+
49
+ def handle_ruby_error(e, include_backtrace = false)
50
+ PactBroker::Client::CommandResult.new(false, error_message(e, include_backtrace))
51
+ end
52
+
53
+ def error_message(e, include_backtrace)
54
+ if json_output?
55
+ json_error_message(e, include_backtrace)
56
+ else
57
+ text_error_message(e, include_backtrace)
58
+ end
59
+ end
60
+
61
+ def json_error_message(e, include_backtrace)
62
+ error_hash = { message: e.message }
63
+ error_hash[:class] = e.class.name unless e.is_a?(PactBroker::Client::Error)
64
+ error_hash[:backtrace] = e.backtrace if include_backtrace
65
+ { error: error_hash }.to_json
66
+ end
67
+
68
+ def error_message_as_json(message)
69
+ { error: { message: message } }.to_json
70
+ end
71
+
72
+ def text_error_message(e, include_backtrace)
73
+ maybe_backtrace = (include_backtrace ? "\n" + e.backtrace.join("\n") : "")
74
+ exception_message = e.is_a?(PactBroker::Client::Error) ? e.message : "#{e.class} - #{e.message}"
75
+ red(exception_message) + maybe_backtrace
76
+ end
77
+
78
+ def check_if_command_supported
79
+ end
80
+
81
+ def json_output?
82
+ options[:output] == "json"
83
+ end
84
+
85
+ def verbose?
86
+ options[:verbose]
87
+ end
88
+
89
+ def green(text)
90
+ ::Term::ANSIColor.green(text)
91
+ end
92
+
93
+ def red(text)
94
+ ::Term::ANSIColor.red(text)
95
+ end
96
+ end
97
+ end
98
+ end
@@ -3,6 +3,7 @@ require 'pact_broker/client/pact_broker_client'
3
3
  require 'pact_broker/client/retry'
4
4
  require 'pact_broker/client/matrix/formatter'
5
5
  require 'term/ansicolor'
6
+ require 'pact_broker/client/colorize_notices'
6
7
 
7
8
  module PactBroker
8
9
  module Client
@@ -32,9 +33,9 @@ module PactBroker
32
33
  def call
33
34
  create_result(fetch_matrix_with_retries)
34
35
  rescue PactBroker::Client::Error => e
35
- Result.new(false, e.message)
36
+ Result.new(dry_run_or_false, for_dry_run(Term::ANSIColor.red(e.message)))
36
37
  rescue StandardError => e
37
- Result.new(false, "Error retrieving matrix. #{e.class} - #{e.message}\n#{e.backtrace.join("\n")}")
38
+ Result.new(dry_run_or_false, for_dry_run(Term::ANSIColor.red("Error retrieving matrix. #{e.class} - #{e.message}") + "\n#{e.backtrace.join("\n")}"))
38
39
  end
39
40
 
40
41
  private
@@ -45,14 +46,15 @@ module PactBroker
45
46
  if matrix.deployable?
46
47
  Result.new(true, success_message(matrix))
47
48
  else
48
- Result.new(false, failure_message(matrix))
49
+ Result.new(dry_run_or_false, failure_message(matrix))
49
50
  end
50
51
  end
51
52
 
52
53
  def success_message(matrix)
53
54
  message = format_matrix(matrix)
54
55
  if format != 'json'
55
- message = 'Computer says yes \o/ ' + message + "\n\n" + Term::ANSIColor.green(matrix.reason)
56
+ message = warning(matrix) + computer_says(true) + message + "\n\n" + notice_or_reason(matrix, :green)
57
+ message = for_dry_run(message)
56
58
  end
57
59
  message
58
60
  end
@@ -60,11 +62,36 @@ module PactBroker
60
62
  def failure_message(matrix)
61
63
  message = format_matrix(matrix)
62
64
  if format != 'json'
63
- message = 'Computer says no ¯\_()_/¯ ' + message + "\n\n" + Term::ANSIColor.red(matrix.reason)
65
+ message = warning(matrix) + computer_says(false) + message + "\n\n" + notice_or_reason(matrix, :red)
66
+ message = for_dry_run(message)
64
67
  end
65
68
  message
66
69
  end
67
70
 
71
+ def computer_says(success)
72
+ if success
73
+ if dry_run?
74
+ "Computer says yes \\o/ (and maybe you don't need to enable dry run)"
75
+ else
76
+ Term::ANSIColor.green('Computer says yes \o/ ')
77
+ end
78
+ else
79
+ if dry_run?
80
+ "Computer says no ¯\\_(ツ)_/¯ (but you're ignoring this by enabling dry run)"
81
+ else
82
+ Term::ANSIColor.red("Computer says no ¯\_(ツ)_/¯")
83
+ end
84
+ end
85
+ end
86
+
87
+ def notice_or_reason(matrix, reason_color)
88
+ if matrix.notices
89
+ PactBroker::Client::ColorizeNotices.call(matrix.notices).join("\n")
90
+ else
91
+ Term::ANSIColor.send(reason_color, matrix.reason)
92
+ end
93
+ end
94
+
68
95
  def format_matrix(matrix)
69
96
  formatted_matrix = Matrix::Formatter.call(matrix, format)
70
97
  if format != 'json' && formatted_matrix.size > 0
@@ -105,6 +132,23 @@ module PactBroker
105
132
  options[:retry_while_unknown] > 0
106
133
  end
107
134
 
135
+ def dry_run?
136
+ options[:dry_run]
137
+ end
138
+
139
+ def dry_run_or_false
140
+ dry_run? || false
141
+ end
142
+
143
+ def for_dry_run(lines)
144
+ if dry_run?
145
+ prefix = Term::ANSIColor.yellow("[dry-run] ")
146
+ lines.split("\n").collect { |line| prefix + Term::ANSIColor.uncolor(line) }.join("\n") + "\n" + prefix + "\n" + prefix + Term::ANSIColor.green("Dry run enabled - ignoring any failures")
147
+ else
148
+ lines
149
+ end
150
+ end
151
+
108
152
  def retry_options
109
153
  {
110
154
  condition: lambda { |matrix| !matrix.any_unknown? },
@@ -131,6 +175,14 @@ module PactBroker
131
175
  raise PactBroker::Client::Error.new("This version of the Pact Broker does not provide a count of the unknown verification results. Please upgrade your Broker to >= v2.23.4")
132
176
  end
133
177
  end
178
+
179
+ def warning(matrix)
180
+ if matrix_options[:ignore_selectors] && matrix_options[:ignore_selectors].any? && !matrix.supports_ignore?
181
+ Term::ANSIColor.yellow("WARN: This version of the Pact Broker does not support ignoring pacticipants. Please upgrade your Broker to >= 2.80.0") + "\n\n"
182
+ else
183
+ ""
184
+ end
185
+ end
134
186
  end
135
187
  end
136
188
  end
@@ -1,6 +1,9 @@
1
1
  require 'pact_broker/client/cli/custom_thor'
2
2
  require 'pact_broker/client/hash_refinements'
3
3
  require 'thor/error'
4
+ require 'pact_broker/client/cli/environment_commands'
5
+ require 'pact_broker/client/cli/deployment_commands'
6
+ require 'pact_broker/client/cli/pacticipant_commands'
4
7
 
5
8
  module PactBroker
6
9
  module Client
@@ -13,12 +16,19 @@ module PactBroker
13
16
 
14
17
  class Broker < CustomThor
15
18
  using PactBroker::Client::HashRefinements
19
+ if ENV.fetch("PACT_BROKER_FEATURES", "").include?("deployments")
20
+ include PactBroker::Client::CLI::EnvironmentCommands
21
+ include PactBroker::Client::CLI::DeploymentCommands
22
+ end
23
+ include PactBroker::Client::CLI::PacticipantCommands
24
+
16
25
 
17
26
  desc 'can-i-deploy', ''
18
27
  long_desc File.read(File.join(__dir__, 'can_i_deploy_long_desc.txt'))
19
28
 
20
29
  method_option :pacticipant, required: true, aliases: "-a", desc: "The pacticipant name. Use once for each pacticipant being checked."
21
30
  method_option :version, required: false, aliases: "-e", desc: "The pacticipant version. Must be entered after the --pacticipant that it relates to."
31
+ 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 after the pacticipant name option."
22
32
  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
33
  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
34
  method_option :to_environment, required: false, banner: 'ENVIRONMENT', desc: "The environment into which the pacticipant(s) are to be deployed", default: nil, hide: true
@@ -27,6 +37,7 @@ module PactBroker
27
37
  method_option :retry_interval, banner: 'SECONDS', type: :numeric, default: 10, required: false, desc: "The time between retries in seconds. Use in conjuction with --retry-while-unknown"
28
38
  # Allow limit to be set manually until https://github.com/pact-foundation/pact_broker-client/issues/53 is fixed
29
39
  method_option :limit, hide: true
40
+ method_option :dry_run, type: :boolean, default: false, desc: "When dry-run is enabled, always exit process with a success code. Can also be enabled by setting the environment variable PACT_BROKER_CAN_I_DEPLOY_DRY_RUN=true."
30
41
  shared_authentication_options
31
42
 
32
43
  def can_i_deploy(*ignored_but_necessary)
@@ -34,10 +45,16 @@ module PactBroker
34
45
  require 'pact_broker/client/can_i_deploy'
35
46
 
36
47
  validate_credentials
37
- selectors = VersionSelectorOptionsParser.call(ARGV)
48
+ selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| !s[:ignore] }
49
+ ignore_selectors = if ENV.fetch("PACT_BROKER_FEATURES", "").include?("ignore")
50
+ VersionSelectorOptionsParser.call(ARGV).select { |s| s[:ignore] }
51
+ else
52
+ []
53
+ end
38
54
  validate_can_i_deploy_selectors(selectors)
39
- 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)
55
+ dry_run = options.dry_run || ENV["PACT_BROKER_CAN_I_DEPLOY_DRY_RUN"] == "true"
56
+ can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval, dry_run: dry_run }
57
+ 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
58
  $stdout.puts result.message
42
59
  $stdout.flush
43
60
  exit(can_i_deploy_exit_status) unless result.success
@@ -51,7 +68,7 @@ module PactBroker
51
68
  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"
52
69
  method_option :build_url, desc: "The build URL that created the pact"
53
70
  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."
54
- method_option :output, aliases: "-o", desc: "json or text", default: 'text'
71
+ output_option_json_or_text
55
72
  shared_authentication_options
56
73
 
57
74
  def publish(*pact_files)
@@ -148,22 +165,9 @@ module PactBroker
148
165
  puts SecureRandom.uuid
149
166
  end
150
167
 
151
- desc 'create-or-update-pacticipant', 'Create or update pacticipant by name'
152
- method_option :name, type: :string, required: true, desc: "Pacticipant name"
153
- method_option :repository_url, type: :string, required: false, desc: "The repository URL of the pacticipant"
154
- shared_authentication_options
155
- verbose_option
156
- def create_or_update_pacticipant(*required_but_ignored)
157
- raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.name.strip.size == 0
158
- require 'pact_broker/client/pacticipants/create'
159
- result = PactBroker::Client::Pacticipants2::Create.call({ name: options.name, repository_url: options.repository_url }, options.broker_base_url, pact_broker_client_options)
160
- $stdout.puts result.message
161
- exit(1) unless result.success
162
- end
163
-
164
168
  desc 'list-latest-pact-versions', 'List the latest pact for each integration'
165
169
  shared_authentication_options
166
- method_option :output, aliases: "-o", desc: "json or table", default: 'table'
170
+ output_option_json_or_table
167
171
  def list_latest_pact_versions(*required_but_ignored)
168
172
  require 'pact_broker/client/pacts/list_latest_versions'
169
173
  result = PactBroker::Client::Pacts::ListLatestVersions.call(options.broker_base_url, options.output, pact_broker_client_options)
@@ -171,63 +175,6 @@ module PactBroker
171
175
  exit(1) unless result.success
172
176
  end
173
177
 
174
- if ENV.fetch("PACT_BROKER_FEATURES", "").include?("deployments")
175
-
176
- ignored_and_hidden_potential_options_from_environment_variables
177
- desc "record-deployment", "Record deployment of a pacticipant version to an environment"
178
- long_desc File.read(File.join(__dir__, 'record_deployment_long_desc.txt'))
179
- method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was deployed."
180
- method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was deployed."
181
- method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was deployed to."
182
- method_option :target, default: nil, required: false, desc: "The target of the deployment - a logical identifer that represents where the application version was deployed to. See the usage docs for information on when to use this."
183
- method_option :output, aliases: "-o", desc: "json or text", default: 'text'
184
- shared_authentication_options
185
-
186
- def record_deployment
187
- require 'pact_broker/client/versions/record_deployment'
188
- params = {
189
- pacticipant_name: options.pacticipant,
190
- version_number: options.version,
191
- environment_name: options.environment,
192
- target: options.target,
193
- output: options.output
194
- }
195
- result = PactBroker::Client::Versions::RecordDeployment.call(
196
- params,
197
- options.broker_base_url,
198
- pact_broker_client_options
199
- )
200
- $stdout.puts result.message
201
- exit(1) unless result.success
202
- end
203
-
204
- ignored_and_hidden_potential_options_from_environment_variables
205
- desc "record-undeployment", "Record undeployment of (or the end of support for) a pacticipant version from an environment"
206
- method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was deployed."
207
- method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was deployed."
208
- method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was deployed to."
209
- method_option :output, aliases: "-o", desc: "json or text", default: 'text'
210
- shared_authentication_options
211
-
212
- def record_undeployment
213
- require 'pact_broker/client/versions/record_undeployment'
214
- params = {
215
- pacticipant_name: options.pacticipant,
216
- version_number: options.version,
217
- environment_name: options.environment,
218
- output: options.output
219
- }
220
- result = PactBroker::Client::Versions::RecordUndeployment.call(
221
- params,
222
- options.broker_base_url,
223
- pact_broker_client_options
224
- )
225
- $stdout.puts result.message
226
- exit(1) unless result.success
227
- end
228
-
229
- end
230
-
231
178
  ignored_and_hidden_potential_options_from_environment_variables
232
179
  desc 'version', "Show the pact_broker-client gem version"
233
180
  def version
@@ -339,7 +286,7 @@ module PactBroker
339
286
  end
340
287
 
341
288
  def pact_broker_client_options
342
- client_options = { verbose: options.verbose }
289
+ client_options = { verbose: options.verbose, pact_broker_base_url: options.broker_base_url }
343
290
  client_options[:token] = options.broker_token || ENV['PACT_BROKER_TOKEN']
344
291
  if options.broker_username || ENV['PACT_BROKER_USERNAME']
345
292
  client_options[:basic_auth] = {
@@ -110,6 +110,18 @@ module PactBroker
110
110
  def self.verbose_option
111
111
  method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
112
112
  end
113
+
114
+ def self.output_option_json_or_text
115
+ method_option :output, aliases: "-o", desc: "json or text", default: 'text'
116
+ end
117
+
118
+ def self.output_option_json_or_table
119
+ method_option :output, aliases: "-o", desc: "json or table", default: 'table'
120
+ end
121
+
122
+ def params_from_options(keys)
123
+ keys.each_with_object({}) { | key, p | p[key] = options[key] }
124
+ end
113
125
  end
114
126
  end
115
127
  end
@@ -0,0 +1,94 @@
1
+ module PactBroker
2
+ module Client
3
+ module CLI
4
+ module DeploymentCommands
5
+ RECORD_DEPLOYMENT_HELP_URL = "https://docs.pact.io/go/record-deployment"
6
+ RECORD_UNDEPLOYMENT_HELP_URL = "https://docs.pact.io/go/record-undeployment"
7
+ RECORD_RELEASE_HELP_URL = "https://docs.pact.io/go/record-release"
8
+ RECORD_SUPPORT_ENDED_HELP_URL = "https://docs.pact.io/go/record-support-ended"
9
+
10
+
11
+ def self.included(thor)
12
+ thor.class_eval do
13
+ desc "record-deployment", "Record deployment of a pacticipant version to an environment. See #{RECORD_DEPLOYMENT_HELP_URL} for more information."
14
+ method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was deployed."
15
+ method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was deployed."
16
+ method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was deployed to."
17
+ 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."
18
+ output_option_json_or_text
19
+ shared_authentication_options
20
+
21
+ def record_deployment
22
+ params = {
23
+ pacticipant_name: options.pacticipant,
24
+ version_number: options.version,
25
+ environment_name: options.environment,
26
+ target: options.target
27
+ }
28
+ execute_deployment_command(params, "RecordDeployment")
29
+ end
30
+
31
+ desc "record-undeployment", "Record undeployment of a pacticipant from an environment."
32
+ long_desc "Note that use of this command is only required if you are permanently removing an application instance from an environment. It is not required if you are deploying over a previous version, as record-deployment will automatically mark the previously deployed version as undeployed for you. See #{RECORD_UNDEPLOYMENT_HELP_URL} for more information."
33
+ method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was undeployed."
34
+ method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was undeployed from."
35
+ method_option :target, default: nil, required: false, desc: "Optional. The target that the application is being undeployed from - a logical identifer required to differentiate deployments when there are multiple instances of the same application in an environment."
36
+ output_option_json_or_text
37
+ shared_authentication_options
38
+
39
+ def record_undeployment
40
+ params = {
41
+ pacticipant_name: options.pacticipant,
42
+ environment_name: options.environment,
43
+ target: options.target
44
+ }
45
+ execute_deployment_command(params, "RecordUndeployment")
46
+ end
47
+
48
+ desc "record-release", "Record release of a pacticipant version to an environment. See See #{RECORD_RELEASE_HELP_URL} for more information."
49
+ method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was released."
50
+ method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was released."
51
+ method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was released to."
52
+ output_option_json_or_text
53
+ shared_authentication_options
54
+
55
+ def record_release
56
+ params = {
57
+ pacticipant_name: options.pacticipant,
58
+ version_number: options.version,
59
+ environment_name: options.environment
60
+ }
61
+ execute_deployment_command(params, "RecordRelease")
62
+ end
63
+
64
+ desc "record-support-ended", "Record the end of support for a pacticipant version in an environment. See #{RECORD_SUPPORT_ENDED_HELP_URL} for more information."
65
+ method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant."
66
+ method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number for which support is ended."
67
+ method_option :environment, required: true, desc: "The name of the environment in which the support is ended."
68
+ output_option_json_or_text
69
+ shared_authentication_options
70
+
71
+ def record_support_ended
72
+ params = {
73
+ pacticipant_name: options.pacticipant,
74
+ version_number: options.version,
75
+ environment_name: options.environment
76
+ }
77
+ execute_deployment_command(params, "RecordSupportEnded")
78
+ end
79
+
80
+ no_commands do
81
+ def execute_deployment_command(params, command_class_name)
82
+ require 'pact_broker/client/deployments'
83
+ command_options = { verbose: options.verbose, output: options.output }
84
+ result = PactBroker::Client::Deployments.const_get(command_class_name).call(params, command_options, pact_broker_client_options)
85
+ $stdout.puts result.message
86
+ exit(1) unless result.success
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end