pact_broker-client 1.43.0 → 1.47.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +35 -0
  3. data/CHANGELOG.md +41 -0
  4. data/README.md +54 -54
  5. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +127 -94
  6. data/doc/pacts/markdown/Pact Broker Client - Pactflow.md +94 -0
  7. data/doc/pacts/markdown/README.md +1 -0
  8. data/lib/pact_broker/client/base_command.rb +3 -0
  9. data/lib/pact_broker/client/can_i_deploy.rb +40 -5
  10. data/lib/pact_broker/client/cli/broker.rb +8 -190
  11. data/lib/pact_broker/client/cli/custom_thor.rb +0 -16
  12. data/lib/pact_broker/client/cli/deployment_commands.rb +94 -0
  13. data/lib/pact_broker/client/cli/matrix_commands.rb +93 -0
  14. data/lib/pact_broker/client/cli/pacticipant_commands.rb +9 -0
  15. data/lib/pact_broker/client/cli/webhook_commands.rb +122 -0
  16. data/lib/pact_broker/client/deployments.rb +4 -0
  17. data/lib/pact_broker/client/deployments/record_deployment.rb +38 -0
  18. data/lib/pact_broker/client/deployments/record_release.rb +99 -0
  19. data/lib/pact_broker/client/deployments/record_support_ended.rb +103 -0
  20. data/lib/pact_broker/client/deployments/record_undeployment.rb +127 -0
  21. data/lib/pact_broker/client/describe_text_formatter.rb +23 -0
  22. data/lib/pact_broker/client/environments.rb +6 -3
  23. data/lib/pact_broker/client/environments/describe_environment.rb +3 -13
  24. data/lib/pact_broker/client/hal/entity.rb +22 -4
  25. data/lib/pact_broker/client/hal/http_client.rb +3 -2
  26. data/lib/pact_broker/client/pacticipants.rb +3 -3
  27. data/lib/pact_broker/client/pacticipants/create.rb +2 -2
  28. data/lib/pact_broker/client/pacticipants/describe.rb +33 -0
  29. data/lib/pact_broker/client/verification_required.rb +32 -0
  30. data/lib/pact_broker/client/version.rb +1 -1
  31. data/lib/pact_broker/client/versions.rb +4 -1
  32. data/lib/pact_broker/client/versions/describe.rb +3 -1
  33. data/lib/pact_broker/client/versions/formatter.rb +3 -1
  34. data/lib/pact_broker/client/versions/json_formatter.rb +5 -3
  35. data/lib/pact_broker/client/versions/text_formatter.rb +3 -1
  36. data/lib/pact_broker/client/webhooks/create.rb +14 -8
  37. data/script/record-deployment.sh +1 -1
  38. data/script/record-deployments-and-releases.sh +16 -0
  39. data/script/record-undeployment.sh +1 -1
  40. data/script/webhook-commands.sh +12 -0
  41. data/spec/fixtures/approvals/can_i_deploy_failure_dry_run.approved.txt +7 -0
  42. data/spec/fixtures/approvals/can_i_deploy_success_dry_run.approved.txt +7 -0
  43. data/spec/fixtures/approvals/describe_pacticipant.approved.txt +2 -0
  44. data/spec/integration/describe_environment_spec.rb +31 -0
  45. data/spec/lib/pact_broker/client/can_i_deploy_spec.rb +62 -2
  46. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +15 -2
  47. data/spec/lib/pact_broker/client/cli/broker_run_webhook_commands_spec.rb +4 -2
  48. data/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb +204 -0
  49. data/spec/lib/pact_broker/client/deployments/record_support_ended_spec.rb +208 -0
  50. data/spec/lib/pact_broker/client/deployments/record_undeployment_spec.rb +219 -0
  51. data/spec/pacts/pact_broker_client-pact_broker.json +140 -112
  52. data/spec/pacts/pact_broker_client-pactflow.json +118 -0
  53. data/spec/service_providers/pact_broker_client_register_repository_spec.rb +2 -2
  54. data/spec/service_providers/pact_helper.rb +15 -10
  55. data/spec/service_providers/pactflow_webhooks_create_spec.rb +86 -0
  56. data/spec/service_providers/publish_pacts_spec.rb +3 -1
  57. data/spec/service_providers/record_deployment_spec.rb +4 -28
  58. data/spec/service_providers/record_release_spec.rb +130 -0
  59. data/spec/service_providers/record_undeployment_spec.rb +164 -0
  60. data/spec/service_providers/webhooks_create_spec.rb +1 -1
  61. data/spec/spec_helper.rb +14 -2
  62. data/spec/support/shared_context.rb +2 -1
  63. data/tasks/pact.rake +21 -1
  64. metadata +39 -7
  65. data/lib/pact_broker/client/versions/record_deployment.rb +0 -85
  66. data/lib/pact_broker/client/versions/record_undeployment.rb +0 -102
  67. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +0 -75
@@ -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
@@ -0,0 +1,93 @@
1
+ module PactBroker
2
+ module Client
3
+ module CLI
4
+ module MatrixCommands
5
+
6
+ def self.included(thor)
7
+ thor.class_eval do
8
+ desc "can-i-deploy", ""
9
+ long_desc File.read(File.join(__dir__, "can_i_deploy_long_desc.txt"))
10
+
11
+ method_option :pacticipant, required: true, aliases: "-a", desc: "The pacticipant name. Use once for each pacticipant being checked."
12
+ method_option :version, required: false, aliases: "-e", desc: "The pacticipant version. Must be entered after the --pacticipant that it relates to."
13
+ 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."
14
+ 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."
15
+ method_option :to, required: false, banner: "TAG", desc: "This is too hard to explain in a short sentence. Look at the examples.", default: nil
16
+ method_option :to_environment, required: false, banner: "ENVIRONMENT", desc: "The environment into which the pacticipant(s) are to be deployed", default: nil, hide: true
17
+ method_option :output, aliases: "-o", desc: "json or table", default: "table"
18
+ method_option :retry_while_unknown, banner: "TIMES", type: :numeric, default: 0, required: false, desc: "The number of times to retry while there is an unknown verification result (ie. the provider verification is likely still running)"
19
+ 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"
20
+ # Allow limit to be set manually until https://github.com/pact-foundation/pact_broker-client/issues/53 is fixed
21
+ method_option :limit, hide: true
22
+ 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."
23
+ shared_authentication_options
24
+
25
+ def can_i_deploy(*ignored_but_necessary)
26
+ require "pact_broker/client/cli/version_selector_options_parser"
27
+ require "pact_broker/client/can_i_deploy"
28
+
29
+ validate_credentials
30
+ selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| !s[:ignore] }
31
+ ignore_selectors = if ENV.fetch("PACT_BROKER_FEATURES", "").include?("ignore")
32
+ VersionSelectorOptionsParser.call(ARGV).select { |s| s[:ignore] }
33
+ else
34
+ []
35
+ end
36
+ validate_can_i_deploy_selectors(selectors)
37
+ dry_run = options.dry_run || ENV["PACT_BROKER_CAN_I_DEPLOY_DRY_RUN"] == "true"
38
+ can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval, dry_run: dry_run, verbose: options.verbose }
39
+ 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)
40
+ $stdout.puts result.message
41
+ $stdout.flush
42
+ exit(can_i_deploy_exit_status) unless result.success
43
+ end
44
+
45
+ if ENV.fetch("PACT_BROKER_FEATURES", "").include?("verification_required")
46
+
47
+ method_option :pacticipant, required: true, aliases: "-a", desc: "The pacticipant name. Use once for each pacticipant being checked."
48
+ method_option :version, required: false, aliases: "-e", desc: "The pacticipant version. Must be entered after the --pacticipant that it relates to."
49
+ 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."
50
+ method_option :to, required: false, banner: "TAG", desc: "This is too hard to explain in a short sentence. Look at the examples.", default: nil
51
+ method_option :in_environment, required: false, banner: "ENVIRONMENT", desc: "The environment into which the pacticipant(s) are to be deployed", default: nil, hide: true
52
+ method_option :output, aliases: "-o", desc: "json or table", default: "table"
53
+
54
+ shared_authentication_options
55
+ desc "verification-required", "Checks if there is a verification required between the given pacticipant versions"
56
+ def verification_required(*ignored_but_necessary)
57
+ require "pact_broker/client/cli/version_selector_options_parser"
58
+ require "pact_broker/client/verification_required"
59
+
60
+ validate_credentials
61
+ selectors = VersionSelectorOptionsParser.call(ARGV)
62
+ validate_can_i_deploy_selectors(selectors)
63
+ verification_required_options = { output: options.output, verbose: options.verbose, retry_while_unknown: 0 }
64
+ result = VerificationRequired.call(options.broker_base_url, selectors, { to_tag: options.to, to_environment: options.in_environment, ignore_selectors: [] }, verification_required_options, pact_broker_client_options)
65
+ $stdout.puts result.message
66
+ $stdout.flush
67
+ exit(1) unless result.success
68
+ end
69
+
70
+ end
71
+
72
+ no_commands do
73
+ def can_i_deploy_exit_status
74
+ exit_code_string = ENV.fetch("PACT_BROKER_CAN_I_DEPLOY_EXIT_CODE_BETA", "")
75
+ if exit_code_string =~ /^\d+$/
76
+ $stderr.puts "Exiting can-i-deploy with configured exit code #{exit_code_string}"
77
+ exit_code_string.to_i
78
+ else
79
+ 1
80
+ end
81
+ end
82
+
83
+ def validate_can_i_deploy_selectors selectors
84
+ pacticipants_without_versions = selectors.select{ |s| s[:version].nil? && s[:latest].nil? && s[:tag].nil? }.collect{ |s| s[:pacticipant] }
85
+ raise ::Thor::RequiredArgumentMissingError, "The version must be specified using `--version VERSION`, `--latest`, `--latest TAG`, or `--all TAG` for pacticipant #{pacticipants_without_versions.join(", ")}" if pacticipants_without_versions.any?
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -27,6 +27,15 @@ module PactBroker
27
27
  execute_pacticipant_command(params_from_options(PACTICIPANT_PARAM_NAMES), 'List')
28
28
  end
29
29
 
30
+ desc 'describe-pacticipant', "Describe a pacticipant"
31
+ method_option :name, type: :string, required: true, desc: "Pacticipant name"
32
+ output_option_json_or_text
33
+ shared_authentication_options
34
+ verbose_option
35
+ def describe_pacticipant
36
+ execute_pacticipant_command({ name: options.name }, 'Describe')
37
+ end
38
+
30
39
  no_commands do
31
40
  def execute_pacticipant_command(params, command_class_name)
32
41
  require 'pact_broker/client/pacticipants'
@@ -0,0 +1,122 @@
1
+ module PactBroker
2
+ module Client
3
+ module CLI
4
+ # Thor::Error will have its backtrace hidden
5
+ class WebhookCreationError < ::Thor::Error; end
6
+
7
+ module WebhookCommands
8
+ def self.included(thor)
9
+ thor.class_eval do
10
+
11
+ no_commands do
12
+ def self.shared_options_for_webhook_commands
13
+ method_option :request, banner: "METHOD", aliases: "-X", desc: "Webhook HTTP method", required: true
14
+ method_option :header, aliases: "-H", type: :array, desc: "Webhook Header"
15
+ method_option :data, aliases: "-d", desc: "Webhook payload (file or string)"
16
+ method_option :user, aliases: "-u", desc: "Webhook basic auth username and password eg. username:password"
17
+ method_option :consumer, desc: "Consumer name"
18
+ method_option :provider, desc: "Provider name"
19
+ method_option :description, desc: "Webhook description"
20
+ method_option :contract_content_changed, type: :boolean, desc: "Trigger this webhook when the pact content changes"
21
+ method_option :contract_published, type: :boolean, desc: "Trigger this webhook when a pact is published"
22
+ method_option :provider_verification_published, type: :boolean, desc: "Trigger this webhook when a provider verification result is published"
23
+ method_option :provider_verification_failed, type: :boolean, desc: "Trigger this webhook when a failed provider verification result is published"
24
+ method_option :provider_verification_succeeded, type: :boolean, desc: "Trigger this webhook when a successful provider verification result is published"
25
+ method_option :team_uuid, banner: "UUID", desc: "UUID of the Pactflow team to which the webhook should be assigned (Pactflow only)"
26
+ shared_authentication_options
27
+ end
28
+ end
29
+
30
+ shared_options_for_webhook_commands
31
+
32
+ desc 'create-webhook URL', 'Creates a webhook using the same switches as a curl request.'
33
+ long_desc File.read(File.join(File.dirname(__FILE__), 'create_webhook_long_desc.txt'))
34
+ def create_webhook webhook_url
35
+ run_webhook_commands webhook_url
36
+ end
37
+
38
+ shared_options_for_webhook_commands
39
+ method_option :uuid, type: :string, required: true, desc: "Specify the uuid for the webhook"
40
+
41
+ desc 'create-or-update-webhook URL', 'Creates or updates a webhook with a provided uuid and using the same switches as a curl request.'
42
+ long_desc File.read(File.join(File.dirname(__FILE__), 'create_or_update_webhook_long_desc.txt'))
43
+ def create_or_update_webhook webhook_url
44
+ run_webhook_commands webhook_url
45
+ end
46
+
47
+ desc 'test-webhook', 'Test the execution of a webhook'
48
+ method_option :uuid, type: :string, required: true, desc: "Specify the uuid for the webhook"
49
+ shared_authentication_options
50
+ def test_webhook
51
+ require 'pact_broker/client/webhooks/test'
52
+ result = PactBroker::Client::Webhooks::Test.call(options, pact_broker_client_options)
53
+ $stdout.puts result.message
54
+ end
55
+
56
+ no_commands do
57
+
58
+ def parse_webhook_events
59
+ events = []
60
+ events << 'contract_content_changed' if options.contract_content_changed
61
+ events << 'contract_published' if options.contract_published
62
+ events << 'provider_verification_published' if options.provider_verification_published
63
+ events << 'provider_verification_succeeded' if options.provider_verification_succeeded
64
+ events << 'provider_verification_failed' if options.provider_verification_failed
65
+ events
66
+ end
67
+
68
+ def parse_webhook_options(webhook_url)
69
+ events = parse_webhook_events
70
+
71
+ if events.size == 0
72
+ raise WebhookCreationError.new("You must specify at least one of --contract-content-changed, --contract-published, --provider-verification-published, --provider-verification-succeeded or --provider-verification-failed")
73
+ end
74
+
75
+ username = options.user ? options.user.split(":", 2).first : nil
76
+ password = options.user ? options.user.split(":", 2).last : nil
77
+
78
+ headers = (options.header || []).each_with_object({}) { | header, headers | headers[header.split(":", 2).first.strip] = header.split(":", 2).last.strip }
79
+
80
+ body = options.data
81
+ if body && body.start_with?("@")
82
+ filepath = body[1..-1]
83
+ begin
84
+ body = File.read(filepath)
85
+ rescue StandardError => e
86
+ raise WebhookCreationError.new("Couldn't read data from file \"#{filepath}\" due to #{e.class} #{e.message}")
87
+ end
88
+ end
89
+
90
+ {
91
+ uuid: options.uuid,
92
+ description: options.description,
93
+ http_method: options.request,
94
+ url: webhook_url,
95
+ headers: headers,
96
+ username: username,
97
+ password: password,
98
+ body: body,
99
+ consumer: options.consumer,
100
+ provider: options.provider,
101
+ events: events,
102
+ team_uuid: options.team_uuid
103
+ }
104
+ end
105
+
106
+ def run_webhook_commands webhook_url
107
+ require 'pact_broker/client/webhooks/create'
108
+
109
+ validate_credentials
110
+ result = PactBroker::Client::Webhooks::Create.call(parse_webhook_options(webhook_url), options.broker_base_url, pact_broker_client_options)
111
+ $stdout.puts result.message
112
+ exit(1) unless result.success
113
+ rescue PactBroker::Client::Error => e
114
+ raise WebhookCreationError, "#{e.class} - #{e.message}"
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,4 @@
1
+ require "pact_broker/client/deployments/record_deployment"
2
+ require "pact_broker/client/deployments/record_release"
3
+ require "pact_broker/client/deployments/record_undeployment"
4
+ require "pact_broker/client/deployments/record_support_ended"
@@ -0,0 +1,38 @@
1
+ require 'pact_broker/client/deployments/record_release'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Deployments
6
+ class RecordDeployment < PactBroker::Client::Deployments::RecordRelease
7
+ def initialize(params, options, pact_broker_client_options)
8
+ super
9
+ @target = params.fetch(:target)
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :target
15
+
16
+ def action
17
+ "deployment"
18
+ end
19
+
20
+ def action_relation_name
21
+ "pb:record-deployment"
22
+ end
23
+
24
+ def record_action_request_body
25
+ { target: target }.compact
26
+ end
27
+
28
+ def result_text_message
29
+ if target
30
+ "#{super} (target #{target})"
31
+ else
32
+ super
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,99 @@
1
+ require 'pact_broker/client/base_command'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Deployments
6
+ class RecordRelease < PactBroker::Client::BaseCommand
7
+ def initialize(params, options, pact_broker_client_options)
8
+ super
9
+ @pacticipant_name = params.fetch(:pacticipant_name)
10
+ @version_number = params.fetch(:version_number)
11
+ @environment_name = params.fetch(:environment_name)
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :pacticipant_name, :version_number, :environment_name
17
+ attr_reader :deployed_version_resource
18
+
19
+ def do_call
20
+ record_action
21
+ PactBroker::Client::CommandResult.new(true, result_message)
22
+ end
23
+
24
+ def action
25
+ "release"
26
+ end
27
+
28
+ def action_relation_name
29
+ "pb:record-release"
30
+ end
31
+
32
+ def not_supported_message
33
+ "This version of the Pact Broker does not support recording #{action}s. Please upgrade to version 2.80.0 or later."
34
+ end
35
+
36
+ def environment_exists?
37
+ index_resource
38
+ ._link!("pb:environments")
39
+ .get!
40
+ ._links("pb:environments")
41
+ .find(environment_name)
42
+ end
43
+
44
+ def record_action
45
+ @deployed_version_resource =
46
+ get_record_action_relation
47
+ .post(record_action_request_body)
48
+ .assert_success!
49
+ end
50
+
51
+ def record_action_links
52
+ get_pacticipant_version._links(action_relation_name) or raise PactBroker::Client::Error.new(not_supported_message)
53
+ end
54
+
55
+ def get_record_action_relation
56
+ record_action_links.find(environment_name) or record_action_links.find!(environment_name, environment_relation_not_found_error_message)
57
+ end
58
+
59
+ def environment_relation_not_found_error_message
60
+ if environment_exists?
61
+ "Environment '#{environment_name}' is not an available option for recording a deployment of #{pacticipant_name}."
62
+ else
63
+ "No environment found with name '#{environment_name}'."
64
+ end
65
+ end
66
+
67
+ def get_pacticipant_version
68
+ index_resource
69
+ ._link!("pb:pacticipant-version")
70
+ .expand(pacticipant: pacticipant_name, version: version_number)
71
+ .get
72
+ .assert_success!(404 => "#{pacticipant_name} version #{version_number} not found")
73
+ end
74
+
75
+ def record_action_request_body
76
+ {}
77
+ end
78
+
79
+ def result_message
80
+ if json_output?
81
+ deployed_version_resource.response.raw_body
82
+ else
83
+ green("#{result_text_message} in #{pact_broker_name}.")
84
+ end
85
+ end
86
+
87
+ def result_text_message
88
+ "Recorded #{action} of #{pacticipant_name} version #{version_number} to #{environment_name} environment"
89
+ end
90
+
91
+ def check_if_command_supported
92
+ unless index_resource.can?("pb:environments")
93
+ raise PactBroker::Client::Error.new(not_supported_message)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end