Skip to content

Commit

Permalink
feat: add update-environment command
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 28, 2021
1 parent a9fab50 commit 95276cd
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 14 deletions.
9 changes: 9 additions & 0 deletions lib/pact_broker/client/backports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ def compact
def compact!
reject! {|_key, value| value == nil}
end unless method_defined? :compact!

def except(*keys)
if keys.size > 4 && size > 4 # index if O(m*n) is big
h = {}
keys.each { |key| h[key] = true }
keys = h
end
reject { |key, _value| keys.include? key}
end unless method_defined? :except
end
26 changes: 19 additions & 7 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class VersionCreationError < ::Thor::Error; end
class Broker < CustomThor
using PactBroker::Client::HashRefinements

ENVIRONMENT_PARAM_NAMES = [:name, :display_name, :production, :contact_name, :contact_email_address]

desc 'can-i-deploy', ''
long_desc File.read(File.join(__dir__, 'can_i_deploy_long_desc.txt'))

Expand Down Expand Up @@ -182,22 +184,32 @@ def list_latest_pact_versions(*required_but_ignored)

ignored_and_hidden_potential_options_from_environment_variables
desc "create-environment", "Create an environment resource in the Pact Broker to represent a real world deployment or release environment."
method_option :name, required: true, desc: "The uniquely identifying name of the environment as used in deployment code"
method_option :display_name, desc: "The display name of the environment"
method_option :production, type: :boolean, default: false, desc: "Whether or not this environment is a production environment. Default: false"
method_option :contact_name, required: false, desc: "The name of the team/person responsible for this environment"
method_option :contact_email_address, required: false, desc: "The email address of the team/person responsible for this environment"
shared_environment_options
shared_authentication_options

def create_environment
require 'pact_broker/client/environments/create_environment'
param_names = [:name, :display_name, :production, :contact_name, :contact_email_address]
params = param_names.each_with_object({}) { | key, p | p[key] = options[key] }
params = ENVIRONMENT_PARAM_NAMES.each_with_object({}) { | key, p | p[key] = options[key] }
result = PactBroker::Client::Environments::CreateEnvironment.call(params, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
end

ignored_and_hidden_potential_options_from_environment_variables
desc "update-environment", "Update an environment resource in the Pact Broker."
method_option :uuid, required: true, desc: "The UUID of the environment to update"
shared_environment_options
shared_authentication_options

def update_environment
require 'pact_broker/client/environments/update_environment'
params = (ENVIRONMENT_PARAM_NAMES + [:uuid]).each_with_object({}) { | key, p | p[key] = options[key] }
result = PactBroker::Client::Environments::UpdateEnvironment.call(params, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
end


if ENV.fetch("PACT_BROKER_FEATURES", "").include?("deployments")
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."
Expand Down
8 changes: 8 additions & 0 deletions lib/pact_broker/client/cli/custom_thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def self.shared_options_for_webhook_commands
shared_authentication_options
end

def self.shared_environment_options
method_option :name, required: true, desc: "The uniquely identifying name of the environment as used in deployment code"
method_option :display_name, desc: "The display name of the environment"
method_option :production, type: :boolean, default: false, desc: "Whether or not this environment is a production environment. Default: false"
method_option :contact_name, required: false, desc: "The name of the team/person responsible for this environment"
method_option :contact_email_address, required: false, desc: "The email address of the team/person responsible for this environment"
end

def self.verbose_option
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
end
Expand Down
12 changes: 6 additions & 6 deletions lib/pact_broker/client/environments/create_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Environments
class CreateEnvironment
include PactBroker::Client::HalClientMethods

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

def self.call(params, pact_broker_base_url, pact_broker_client_options)
new(params, pact_broker_base_url, pact_broker_client_options).call
Expand Down Expand Up @@ -42,10 +42,10 @@ def create_environment

def request_body
{
name: params[:name],
displayName: params[:display_name],
production: params[:production],
contacts: contacts
"name" => params[:name],
"displayName" => params[:display_name],
"production" => params[:production],
"contacts" => contacts
}.compact
end

Expand Down Expand Up @@ -75,4 +75,4 @@ def check_if_command_supported
end
end
end
end
end
48 changes: 48 additions & 0 deletions lib/pact_broker/client/environments/update_environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'pact_broker/client/environments/create_environment'
require 'pact_broker/client/backports'

module PactBroker
module Client
module Environments
class UpdateEnvironment < PactBroker::Client::Environments::CreateEnvironment
def call
check_if_command_supported
update_environment
rescue PactBroker::Client::Error => e
PactBroker::Client::CommandResult.new(false, ::Term::ANSIColor.red(e.message))
end

private

def update_environment
index_resource
._link!("pb:environment")
.expand(uuid: params[:uuid])
.put!(request_body)
PactBroker::Client::CommandResult.new(true, result_message)
end

def request_body
@request_body ||= begin
incoming_params = super
existing_environment_params.merge(incoming_params)
end
end

def existing_environment_params
@existing_environment_params ||= index_resource
._link!("pb:environment")
.expand(uuid: params[:uuid])
.get!
.response
.body
.except("_links", "createdAt", "updatedAt")
end

def result_message
::Term::ANSIColor.green("Updated environment #{request_body["name"]} in #{pact_broker_name}")
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pact_broker/client/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def success?
end

def assert_success!(messages = {})
default_message = "Error retrieving #{@href} status=#{response ? response.status: nil} #{response ? response.raw_body : ''}".strip
default_message = "Error making request to #{@href} status=#{response ? response.status: nil} #{response ? response.raw_body : ''}".strip
message = if response && messages[response.status]
(messages[response.status] || "") + " (#{default_message})"
else
Expand Down

0 comments on commit 95276cd

Please sign in to comment.