From 95276cd0209799e9125984951901bba27a9b5d2b Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 28 May 2021 11:22:26 +1000 Subject: [PATCH] feat: add update-environment command --- lib/pact_broker/client/backports.rb | 9 ++++ lib/pact_broker/client/cli/broker.rb | 26 +++++++--- lib/pact_broker/client/cli/custom_thor.rb | 8 ++++ .../client/environments/create_environment.rb | 12 ++--- .../client/environments/update_environment.rb | 48 +++++++++++++++++++ lib/pact_broker/client/hal/entity.rb | 2 +- 6 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 lib/pact_broker/client/environments/update_environment.rb diff --git a/lib/pact_broker/client/backports.rb b/lib/pact_broker/client/backports.rb index b7bbca21..126a912b 100644 --- a/lib/pact_broker/client/backports.rb +++ b/lib/pact_broker/client/backports.rb @@ -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 diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index 04da4eef..c7a14409 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -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')) @@ -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." diff --git a/lib/pact_broker/client/cli/custom_thor.rb b/lib/pact_broker/client/cli/custom_thor.rb index 3cfef70a..95ad9e8d 100644 --- a/lib/pact_broker/client/cli/custom_thor.rb +++ b/lib/pact_broker/client/cli/custom_thor.rb @@ -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 diff --git a/lib/pact_broker/client/environments/create_environment.rb b/lib/pact_broker/client/environments/create_environment.rb index e2521ec8..8198afbc 100644 --- a/lib/pact_broker/client/environments/create_environment.rb +++ b/lib/pact_broker/client/environments/create_environment.rb @@ -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 @@ -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 @@ -75,4 +75,4 @@ def check_if_command_supported end end end -end \ No newline at end of file +end diff --git a/lib/pact_broker/client/environments/update_environment.rb b/lib/pact_broker/client/environments/update_environment.rb new file mode 100644 index 00000000..70a5e3c5 --- /dev/null +++ b/lib/pact_broker/client/environments/update_environment.rb @@ -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 diff --git a/lib/pact_broker/client/hal/entity.rb b/lib/pact_broker/client/hal/entity.rb index bd5d0cd5..19b5c334 100644 --- a/lib/pact_broker/client/hal/entity.rb +++ b/lib/pact_broker/client/hal/entity.rb @@ -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