Skip to content

Commit

Permalink
chore: update record-undeployment
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 17, 2021
1 parent 9f2810e commit 9dcede6
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 68 deletions.
16 changes: 16 additions & 0 deletions lib/pact_broker/client/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,34 @@ def get(key, *args)
_link(key).get(*args)
end

def get!(key, *args)
get(key, *args).assert_success!
end

def post(key, *args)
_link(key).post(*args)
end

def post!(key, *args)
post(key, *args).assert_success!
end

def put(key, *args)
_link(key).put(*args)
end

def put!(key, *args)
put(key, *args).assert_success!
end

def patch(key, *args)
_link(key).patch(*args)
end

def patch!(key, *args)
patch(key, *args).assert_success!
end

def can?(key)
@links.key? key.to_s
end
Expand Down
12 changes: 12 additions & 0 deletions lib/pact_broker/client/hal/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,26 @@ def put(payload = nil, headers = {})
wrap_response(href, @http_client.put(href, payload ? JSON.dump(payload) : nil, headers))
end

def put!(*args)
put(*args).assert_success!
end

def post(payload = nil, headers = {})
wrap_response(href, @http_client.post(href, payload ? JSON.dump(payload) : nil, headers))
end

def post!(*args)
post(*args).assert_success!
end

def patch(payload = nil, headers = {})
wrap_response(href, @http_client.patch(href, payload ? JSON.dump(payload) : nil, headers))
end

def patch!(*args)
patch(*args).assert_success!
end

def expand(params)
expanded_url = expand_url(params, href)
new_attrs = @attrs.merge('href' => expanded_url)
Expand Down
15 changes: 15 additions & 0 deletions lib/pact_broker/client/hal/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ def find(name)
links.find{ | link | link.name == name }
end

def select!(name, not_found_message = nil)
selected_links = select(name)
if selected_links.any?
selected_links
else
message = not_found_message || "Could not find relation '#{key}' with name '#{name}' in resource at #{href}."
available_options = names.any? ? names.join(", ") : "<none found>"
raise RelationNotFoundError.new(message.chomp(".") + ". Available options: #{available_options}")
end
end

def select(name)
links.select{ | link | link.name == name }
end

private

attr_reader :links, :key, :href
Expand Down
8 changes: 8 additions & 0 deletions lib/pact_broker/client/hal_client_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def index_resource
index_entry_point.get!
end
end

def is_pactflow?
index_resource.response.headers.keys.any?{ | header_name | header_name.downcase.include?("pactflow") }
end

def pact_broker_name
is_pactflow? ? "Pactflow" : "the Pact Broker"
end
end
end
end
111 changes: 44 additions & 67 deletions lib/pact_broker/client/versions/record_undeployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
require 'pact_broker/client/error'
require 'pact_broker/client/command_result'

# TODO
# --limit 1
# order by date so that the oldest one gets undeployed first

module PactBroker
module Client
class Versions
Expand All @@ -24,10 +28,17 @@ def initialize(params, pact_broker_base_url, pact_broker_client_options)
end

def call
check_environment_exists
# record_undeployment
check_if_command_supported
if deployed_version_links_for_environment.any?
@undeployment_entities = deployed_version_links_for_environment.collect do | deployed_version_link |
deployed_version_link.get!._link!("pb:record-undeployment").post!
end
else
check_environment_exists
raise_not_found_error
end

PactBroker::Client::CommandResult.new(true, result_message)
PactBroker::Client::CommandResult.new(true, "foo")
rescue PactBroker::Client::Error => e
PactBroker::Client::CommandResult.new(false, e.message)
end
Expand All @@ -36,82 +47,48 @@ def call

attr_reader :pact_broker_base_url, :pact_broker_client_options
attr_reader :pacticipant_name, :version_number, :environment_name, :replaced_previous_deployed_version, :output
attr_reader :deployed_version_resource
attr_reader :deployed_version_resource, :undeployment_entities

def check_environment_exists
deployed_versions = currently_deployed_versions_for_pacticipant
.get(version: version_number)
.embedded_entities("deployedVersions")
deployed_versions
def version_resource
index_resource._link!("pb:pacticipant-version").expand(pacticipant: pacticipant_name, version: version_number).get!
end

def currently_deployed_versions_for_pacticipant
@currently_deployed_versions_for_pacticipant ||= index_resource
._link!("pb:environments")
.get!(name: environment_name)
.embedded_entities("environments")
.tap { |it| raise "Environment not found '#{environment_name}'" if it.empty? }
.first
._link!("pb:currently-deployed-versions-for-pacticipant")
.expand(pacticipant: pacticipant_name)
def deployed_version_links
@deployed_version_links ||= version_resource._links!("pb:currently-deployed-versions")
end

def currently_deployed_versions_for_pacticipant_version
@currently_deployed_versions ||= currently_deployed_versions_for_pacticipant
.get(version: version_number)
.embedded_entities("deployedVersions")
def deployed_version_links_for_environment
@deployed_version_links_for_environment ||= deployed_version_links.select(environment_name)
end

# def record_deployment
# @deployed_version_resource =
# get_record_deployment_relation
# .post(record_deployment_request_body)
# .assert_success!
# end

# def get_record_deployment_relation
# record_deployment_links = get_pacticipant_version._links!("pb:record-deployment")
# link_for_environment = record_deployment_links.find(environment_name)
# if link_for_environment
# link_for_environment
# else
# check_environment_exists
# # Force the exception to be raised
# record_deployment_links.find!(environment_name, "Environment '#{environment_name}' is not an available option for recording a deployment of #{pacticipant_name}.")
# end
# end

# def get_pacticipant_version
# index_resource
# ._link!("pb:pacticipant-version")
# .expand(pacticipant: pacticipant_name, version: version_number)
# .get
# .assert_success!(404 => "#{pacticipant_name} version #{version_number} not found")
# end

# def record_deployment_request_body
# { replacedPreviousDeployedVersion: replaced_previous_deployed_version }
# end
def check_environment_exists
index_resource
._link!("pb:environments")
.get!
._links("pb:environments")
.find!(environment_name, "No environment found with name '#{environment_name}'")
end

def result_message
""
# if output == "text"
# message = "Recorded deployment of #{pacticipant_name} version #{version_number} to #{environment_name} in #{pact_broker_name}."
# suffix = replaced_previous_deployed_version ? " Marked previous deployed version as undeployed." : ""
# message + suffix
# elsif output == "json"
# deployed_version_resource.response.raw_body
# else
# ""
# end
def raise_not_found_error
raise PactBroker::Client::Error.new(deployed_version_not_found_message)
end

def pact_broker_name
is_pactflow? ? "Pactflow" : "the Pact Broker"
def deployed_version_not_found_message
if (env_names = deployed_version_links.names).any?
"#{pacticipant_name} version #{version_number} is not currently deployed to #{environment_name}. It is currently deployed to: #{env_names.join(", ")}"
else
"#{pacticipant_name} version #{version_number} is not currently deployed to any environment."
end
end

def is_pactflow?
deployed_version_resource.response.headers.keys.any?{ | header_name | header_name.downcase.include?("pactflow") }
def result_message
if output == "text"
message = "Recorded undeployment of #{pacticipant_name} version #{version_number} from #{environment_name} in #{pact_broker_name}."
elsif output == "json"
undeployment_entities.last.response.raw_body
else
""
end
end

def check_if_command_supported
Expand Down
2 changes: 1 addition & 1 deletion script/record-deployment.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PACT_BROKER_FEATURES=deployments bundle exec bin/pact-broker record-deployment \
--pacticipant Foo --version 1.0.0 --environment prod --broker-base-url http://localhost:9292
--pacticipant foo-consumer --version 1 --environment prod --broker-base-url http://localhost:9292 --no-replaced_previous_deployed_version


4 changes: 4 additions & 0 deletions script/record-undeployment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PACT_BROKER_FEATURES=deployments bundle exec bin/pact-broker record-undeployment \
--pacticipant foo-consumer --version 1 --environment prod --broker-base-url http://localhost:9292 --output json --verbose


0 comments on commit 9dcede6

Please sign in to comment.