Skip to content

Commit

Permalink
feat: add record-release (feature toggled off)
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 4, 2021
1 parent 75f06b8 commit e32e4e5
Show file tree
Hide file tree
Showing 8 changed files with 466 additions and 109 deletions.
25 changes: 21 additions & 4 deletions lib/pact_broker/client/cli/deployment_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module PactBroker
module Client
module CLI
module DeploymentCommands
HELP_URL = "https://docs.pact.io/pact_broker/recording_deployments_and_releases/"

def self.included(thor)
thor.class_eval do
ignored_and_hidden_potential_options_from_environment_variables
desc "record-deployment", "Record deployment of a pacticipant version to an environment. See https://docs.pact.io/go/record_deployment for more information."
desc "record-deployment", "Record deployment of a pacticipant version to an environment. See #{HELP_URL} for more information."
method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was deployed."
method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was deployed."
method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was deployed to."
Expand All @@ -23,8 +24,8 @@ def record_deployment
execute_deployment_command(params, "RecordDeployment")
end

ignored_and_hidden_potential_options_from_environment_variables
desc "record-undeployment", "Record undeployment of (or the end of support for) a pacticipant version from an environment"
desc "record-undeployment", "Record undeployment of a pacticipant version from an environment."
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."
method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was undeployed."
method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was undeployed."
method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was undeployed from."
Expand All @@ -42,6 +43,22 @@ def record_undeployment
execute_deployment_command(params, "RecordUndeployment")
end

desc "record-release", "Record release of a pacticipant version to an environment. See See #{HELP_URL} for more information."
method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was released."
method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was released."
method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was released to."
output_option_json_or_text
shared_authentication_options

def record_release
params = {
pacticipant_name: options.pacticipant,
version_number: options.version,
environment_name: options.environment
}
execute_deployment_command(params, "RecordRelease")
end

no_commands do
def execute_deployment_command(params, command_class_name)
require 'pact_broker/client/deployments'
Expand Down
73 changes: 13 additions & 60 deletions lib/pact_broker/client/deployments/record_deployment.rb
Original file line number Diff line number Diff line change
@@ -1,82 +1,35 @@
require 'pact_broker/client/base_command'
require 'pact_broker/client/deployments/record_release'

module PactBroker
module Client
module Deployments
class RecordDeployment < PactBroker::Client::BaseCommand

NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support recording deployments. Please upgrade to version 2.80.0 or later."

class RecordDeployment < PactBroker::Client::Deployments::RecordRelease
def initialize(params, options, pact_broker_client_options)
super
@pacticipant_name = params.fetch(:pacticipant_name)
@version_number = params.fetch(:version_number)
@environment_name = params.fetch(:environment_name)
@target = params.fetch(:target)
end

private

def do_call
record_deployment
PactBroker::Client::CommandResult.new(true, result_message)
end

attr_reader :pacticipant_name, :version_number, :environment_name, :target, :output
attr_reader :deployed_version_resource
attr_reader :target

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

def record_deployment
@deployed_version_resource =
get_record_deployment_relation
.post(record_deployment_request_body)
.assert_success!
def action_relation_name
"pb:record-deployment"
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
def record_action_request_body
{ target: target }.compact
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
{ target: target }
end

def result_message
if json_output?
deployed_version_resource.response.raw_body
def result_text_message
if target
"#{super} (target #{target})"
else
message = "Recorded deployment of #{pacticipant_name} version #{version_number} to #{environment_name} environment"
message = "#{message} (target #{target})" if target
green("#{message} in #{pact_broker_name}.")
end
end

def check_if_command_supported
unless index_resource.can?("pb:environments")
raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE)
super
end
end
end
Expand Down
99 changes: 99 additions & 0 deletions lib/pact_broker/client/deployments/record_release.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require 'pact_broker/client/base_command'

module PactBroker
module Client
module Deployments
class RecordRelease < PactBroker::Client::BaseCommand
def initialize(params, options, pact_broker_client_options)
super
@pacticipant_name = params.fetch(:pacticipant_name)
@version_number = params.fetch(:version_number)
@environment_name = params.fetch(:environment_name)
end

private

attr_reader :pacticipant_name, :version_number, :environment_name
attr_reader :deployed_version_resource

def do_call
record_action
PactBroker::Client::CommandResult.new(true, result_message)
end

def action
"release"
end

def action_relation_name
"pb:record-release"
end

def not_supported_message
"This version of the Pact Broker does not support recording #{action}s. Please upgrade to version 2.80.0 or later."
end

def environment_exists?
index_resource
._link!("pb:environments")
.get!
._links("pb:environments")
.find(environment_name)
end

def record_action
@deployed_version_resource =
get_record_action_relation
.post(record_action_request_body)
.assert_success!
end

def record_action_links
get_pacticipant_version._links(action_relation_name) or raise PactBroker::Client::Error.new(not_supported_message)
end

def get_record_action_relation
record_action_links.find(environment_name) or record_action_links.find!(environment_name, environment_relation_not_found_error_message)
end

def environment_relation_not_found_error_message
if environment_exists?
"Environment '#{environment_name}' is not an available option for recording a deployment of #{pacticipant_name}."
else
"No environment found with name '#{environment_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_action_request_body
{}
end

def result_message
if json_output?
deployed_version_resource.response.raw_body
else
green("#{result_text_message} in #{pact_broker_name}.")
end
end

def result_text_message
"Recorded #{action} of #{pacticipant_name} version #{version_number} to #{environment_name} environment"
end

def check_if_command_supported
unless index_resource.can?("pb:environments")
raise PactBroker::Client::Error.new(not_supported_message)
end
end
end
end
end
end
10 changes: 10 additions & 0 deletions script/record-deployments-and-releases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export PACT_BROKER_FEATURES=deployments

bundle exec bin/pact-broker create-or-update-pacticipant --name Foo
bundle exec bin/pact-broker create-version-tag --pacticipant Foo --version 2 --tag main --auto-create-version
bundle exec bin/pact-broker describe-version --pacticipant Foo --version 2
bundle exec bin/pact-broker create-environment --name test
bundle exec bin/pact-broker can-i-deploy --pacticipant Foo --version 2 --to-environment test
bundle exec bin/pact-broker record-deployment --pacticipant Foo --version 2 --environment test --target foo
bundle exec bin/pact-broker record-undeployment --pacticipant Foo --version 2 --environment test --target foo
bundle exec bin/pact-broker record-release --pacticipant Foo --version 2 --environment test
Loading

0 comments on commit e32e4e5

Please sign in to comment.