Skip to content

Commit 951d334

Browse files
committed
feat: add record-undeployment (feature toggled off)
1 parent abfbee9 commit 951d334

File tree

5 files changed

+70
-33
lines changed

5 files changed

+70
-33
lines changed

lib/pact_broker/client/cli/deployment_commands.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def record_deployment
2727
desc "record-undeployment", "Record undeployment of a pacticipant version from an environment."
2828
long_desc "Note that use of this command is not required if you are deploying over a previous version, as record-deployment will handle that scenario for you. This command is only required if you are permanently removing an application instance from an environment."
2929
method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was undeployed."
30-
method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was undeployed."
3130
method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was undeployed from."
3231
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."
3332
output_option_json_or_text
@@ -36,7 +35,6 @@ def record_deployment
3635
def record_undeployment
3736
params = {
3837
pacticipant_name: options.pacticipant,
39-
version_number: options.version,
4038
environment_name: options.environment,
4139
target: options.target
4240
}

lib/pact_broker/client/deployments/record_undeployment.rb

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,98 @@ module Client
55
module Deployments
66
class RecordUndeployment < PactBroker::Client::BaseCommand
77

8-
NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support recording undeployments. Please upgrade to version 2.80.0 or later."
8+
99

1010
def initialize(params, pact_broker_base_url, pact_broker_client_options)
1111
super
1212
@pacticipant_name = params.fetch(:pacticipant_name)
13-
@version_number = params.fetch(:version_number)
1413
@environment_name = params.fetch(:environment_name)
1514
@target = params.fetch(:target)
1615
end
1716

1817
private
1918

2019
def do_call
21-
check_if_command_supported
22-
if deployed_version_links_for_environment.any?
23-
@undeployment_entities = deployed_version_links_for_environment.collect do | deployed_version_link |
24-
deployed_version_link.get!._link!("pb:record-undeployment").post!
25-
end
20+
if undeployed_versions_resources.empty?
21+
check_pacticipant_exists!
22+
PactBroker::Client::CommandResult.new(false, deployed_version_not_found_error_message)
2623
else
27-
check_environment_exists
28-
raise_not_found_error
24+
PactBroker::Client::CommandResult.new(undeployed_versions_resources.all?(:success?), result_message)
2925
end
30-
31-
PactBroker::Client::CommandResult.new(true, "foo")
3226
end
3327

3428
attr_reader :pacticipant_name, :version_number, :environment_name, :target, :output
3529
attr_reader :deployed_version_resource, :undeployment_entities
3630

37-
def version_resource
38-
index_resource._link!("pb:pacticipant-version").expand(pacticipant: pacticipant_name, version: version_number).get!
31+
def currently_deployed_versions_link
32+
environment_resource._link("pb:currently-deployed-versions") or raise PactBroker::Client::Error.new(not_supported_message)
3933
end
4034

41-
def deployed_version_links
42-
@deployed_version_links ||= version_resource._links!("pb:currently-deployed-versions")
35+
def currently_deployed_versions_resource
36+
@deployed_version_links ||= currently_deployed_versions_link.get!(pacticipant: pacticipant_name, target: target)
4337
end
4438

45-
def deployed_version_links_for_environment
46-
@deployed_version_links_for_environment ||= deployed_version_links.select(environment_name)
39+
def undeployed_versions_resources
40+
@undeployed_versions_resources ||= currently_deployed_versions_resource.embedded_entities("deployedVersions").collect do | entity |
41+
entity._link!("self").patch(currentlyDeployed: false)
42+
end
4743
end
4844

49-
def check_environment_exists
45+
def action
46+
"undeployment"
47+
end
48+
49+
def environment_resource
5050
index_resource
5151
._link!("pb:environments")
5252
.get!
5353
._links("pb:environments")
5454
.find!(environment_name, "No environment found with name '#{environment_name}'")
55+
.get!
5556
end
5657

57-
def raise_not_found_error
58-
raise PactBroker::Client::Error.new(deployed_version_not_found_message)
58+
def check_pacticipant_exists!
59+
if index_resource._link!("pb:pacticipant").expand(pacticipant: pacticipant_name).get.does_not_exist?
60+
raise PactBroker::Client::Error.new("No pacticipant with name '#{pacticipant_name}' found")
61+
end
5962
end
6063

64+
def result_message
65+
if json_output?
66+
undeployed_versions_resources.collect{ | resource | resource.response.body }.to_a.to_json
67+
else
68+
undeployed_versions_resources.collect do | undeployed_versions_resource |
69+
if undeployed_versions_resource.success?
70+
green("#{success_result_text_message(undeployed_versions_resource)} in #{pact_broker_name}.")
71+
else
72+
red(undeployed_versions_resource.error_message)
73+
end
74+
end.join("\n")
75+
end
76+
end
77+
78+
def success_result_text_message(undeployed_versions_resource)
79+
version_number = undeployed_versions_resource.embedded_entity{ | embedded_entity| embedded_entity['version'] }.number
80+
message = "Recorded #{action} of #{pacticipant_name} version #{version_number} from #{environment_name} environment"
81+
if target
82+
message + " (target #{target})"
83+
else
84+
message
85+
end
86+
end
87+
88+
def deployed_version_not_found_error_message
89+
target_bit = target ? " with target '#{target}'" : ""
90+
message = "#{pacticipant_name} is not currently deployed to #{environment_name}#{target_bit}. Cannot record undeployment."
91+
92+
if json_output?
93+
{ error: message }.to_json
94+
else
95+
red(message)
96+
end
97+
end
98+
99+
61100
def deployed_version_not_found_message
62101
if (env_names = deployed_version_links.names).any?
63102
"#{pacticipant_name} version #{version_number} is not currently deployed to #{environment_name}. It is currently deployed to: #{env_names.join(", ")}"
@@ -66,17 +105,13 @@ def deployed_version_not_found_message
66105
end
67106
end
68107

69-
def result_message
70-
if output_json?
71-
undeployment_entities.last.response.raw_body
72-
else
73-
green("Recorded undeployment of #{pacticipant_name} version #{version_number} from #{environment_name} in #{pact_broker_name}.")
74-
end
108+
def not_supported_message
109+
"This version of the Pact Broker does not support recording undeployments. Please upgrade to version 2.80.0 or later."
75110
end
76111

77112
def check_if_command_supported
78113
unless index_resource.can?("pb:environments")
79-
raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE)
114+
raise PactBroker::Client::Error.new(not_supported_message)
80115
end
81116
end
82117
end

lib/pact_broker/client/hal/entity.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,17 @@ def success?
165165
false
166166
end
167167

168-
def assert_success!(messages = {})
168+
def error_message(messages = {})
169169
default_message = "Error making request to #{@href} status=#{response ? response.status: nil} #{response ? response.raw_body : ''}".strip
170170
message = if response && messages[response.status]
171171
(messages[response.status] || "") + " (#{default_message})"
172172
else
173173
default_message
174174
end
175-
raise ErrorResponseReturned.new(message, self)
175+
end
176+
177+
def assert_success!(messages = {})
178+
raise ErrorResponseReturned.new(error_message(messages), self)
176179
end
177180
end
178181
end

lib/pact_broker/client/hal/http_client.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def delete href, body = nil, headers = {}
4646

4747
def create_request uri, http_method, body = nil, headers = {}
4848
request = Net::HTTP.const_get(http_method).new(uri.request_uri)
49-
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)
5051
request['Accept'] = "application/hal+json"
5152
headers.each do | key, value |
5253
request[key] = value

lib/pact_broker/client/pacticipants/create.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def do_call
1717
index_resource._link!('pb:pacticipants').post!(pacticipant_resource_params)
1818
elsif pacticipant_entity.success?
1919
@action = "updated"
20-
pacticipant_entity._link!('self').patch!(pacticipant_resource_params)
20+
pacticipant_entity._link!('self').patch!(pacticipant_resource_params, { "Content-Type" => "application/json" })
2121
else
2222
pacticipant_entity.assert_success!
2323
end

0 commit comments

Comments
 (0)