Skip to content

Commit

Permalink
feat: add --display-name to create-or-update-pacticipant
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 30, 2021
1 parent 0ec92d6 commit 76f323b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 60 deletions.
12 changes: 10 additions & 2 deletions lib/pact_broker/client/base_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def handle_http_error(e)
body = e.entity.response.raw_body
(body.nil? || body == "") ? "{}" : body
else
::Term::ANSIColor.red(e.message)
red(e.message)
end
PactBroker::Client::CommandResult.new(false, message)
end
Expand Down Expand Up @@ -69,7 +69,7 @@ def json_error_message(e, include_backtrace)
def text_error_message(e, include_backtrace)
maybe_backtrace = (include_backtrace ? "\n" + e.backtrace.join("\n") : "")
exception_message = e.is_a?(PactBroker::Client::Error) ? e.message : "#{e.class} - #{e.message}"
::Term::ANSIColor.red(exception_message) + maybe_backtrace
red(exception_message) + maybe_backtrace
end

def check_if_command_supported
Expand All @@ -82,6 +82,14 @@ def json_output?
def verbose?
options[:verbose]
end

def green(text)
::Term::ANSIColor.green(text)
end

def red(text)
::Term::ANSIColor.red(text)
end
end
end
end
23 changes: 6 additions & 17 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'pact_broker/client/hash_refinements'
require 'thor/error'
require 'pact_broker/client/cli/environment_commands'
require 'pact_broker/client/cli/pacticipant_commands'

module PactBroker
module Client
Expand All @@ -15,6 +16,7 @@ class VersionCreationError < ::Thor::Error; end
class Broker < CustomThor
using PactBroker::Client::HashRefinements
include PactBroker::Client::CLI::EnvironmentCommands
include PactBroker::Client::CLI::PacticipantCommands

desc 'can-i-deploy', ''
long_desc File.read(File.join(__dir__, 'can_i_deploy_long_desc.txt'))
Expand Down Expand Up @@ -62,7 +64,7 @@ def can_i_deploy(*ignored_but_necessary)
method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag consumer version with the name of the current git branch. Default: false"
method_option :build_url, desc: "The build URL that created the pact"
method_option :merge, type: :boolean, default: false, require: false, desc: "If a pact already exists for this consumer version and provider, merge the contents. Useful when running Pact tests concurrently on different build nodes."
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
output_option_json_or_text
shared_authentication_options

def publish(*pact_files)
Expand Down Expand Up @@ -159,22 +161,9 @@ def generate_uuid
puts SecureRandom.uuid
end

desc 'create-or-update-pacticipant', 'Create or update pacticipant by name'
method_option :name, type: :string, required: true, desc: "Pacticipant name"
method_option :repository_url, type: :string, required: false, desc: "The repository URL of the pacticipant"
shared_authentication_options
verbose_option
def create_or_update_pacticipant(*required_but_ignored)
raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.name.strip.size == 0
require 'pact_broker/client/pacticipants/create'
result = PactBroker::Client::Pacticipants2::Create.call({ name: options.name, repository_url: options.repository_url }, options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
end

desc 'list-latest-pact-versions', 'List the latest pact for each integration'
shared_authentication_options
method_option :output, aliases: "-o", desc: "json or table", default: 'table'
output_option_json_or_table
def list_latest_pact_versions(*required_but_ignored)
require 'pact_broker/client/pacts/list_latest_versions'
result = PactBroker::Client::Pacts::ListLatestVersions.call(options.broker_base_url, options.output, pact_broker_client_options)
Expand All @@ -191,7 +180,7 @@ def list_latest_pact_versions(*required_but_ignored)
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."
method_option :target, default: nil, required: false, desc: "Optional. The target of the deployment - a logical identifer required to differentiate deployments when there are multiple instances of the same application in an environment."
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
output_option_json_or_text
shared_authentication_options

def record_deployment
Expand All @@ -217,7 +206,7 @@ def record_deployment
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."
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
output_option_json_or_text
shared_authentication_options

def record_undeployment
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 @@ -110,6 +110,14 @@ def self.shared_options_for_webhook_commands
def self.verbose_option
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
end

def self.output_option_json_or_text
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
end

def self.output_option_json_or_table
method_option :output, aliases: "-o", desc: "json or table", default: 'table'
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/pact_broker/client/cli/environment_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module EnvironmentCommands

def self.included(thor)
thor.class_eval do

def self.shared_environment_options(name_required: false)
method_option :name, required: name_required, desc: "The uniquely identifying name of the environment as used in deployment code"
method_option :display_name, desc: "The display name of the environment"
Expand Down
30 changes: 30 additions & 0 deletions lib/pact_broker/client/cli/pacticipant_commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module PactBroker
module Client
module CLI
module PacticipantCommands
PACTICIPANT_PARAM_NAMES = [:name, :display_name, :repository_url]

def self.included(thor)
thor.class_eval do
desc 'create-or-update-pacticipant', 'Create or update pacticipant by name'
method_option :name, type: :string, required: true, desc: "Pacticipant name"
method_option :display_name, type: :string, desc: "Display name"
method_option :repository_url, type: :string, required: false, desc: "The repository URL of the pacticipant"
output_option_json_or_text
shared_authentication_options
verbose_option

def create_or_update_pacticipant(*required_but_ignored)
raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.name.strip.size == 0
require 'pact_broker/client/pacticipants/create'
params = PACTICIPANT_PARAM_NAMES.each_with_object({}) { | key, p | p[key] = options[key] }
result = PactBroker::Client::Pacticipants2::Create.call(params, { verbose: options.verbose, output: options.output }, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
end
end
end
end
end
end
end
58 changes: 24 additions & 34 deletions lib/pact_broker/client/pacticipants/create.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,45 @@
require 'pact_broker/client/hal'
require 'json'
require 'pact_broker/client/command_result'
require 'pact_broker/client/hal_client_methods'
require 'pact_broker/client/base_command'

module PactBroker
module Client
module Pacticipants2
class Create
class Create < PactBroker::Client::BaseCommand

include HalClientMethods
private

def self.call(params, pact_broker_base_url, pact_broker_client_options)
new(params, pact_broker_base_url, pact_broker_client_options).call
end
attr_reader :action, :response_entity

def initialize(params, pact_broker_base_url, pact_broker_client_options)
@params = params
@index_entry_point = create_index_entry_point(pact_broker_base_url, pact_broker_client_options)
@verbose = pact_broker_client_options[:verbose]
end
def do_call
pacticipant_entity = index_resource._link('pb:pacticipant').expand('pacticipant' => params[:name]).get

def call
pacticipant_entity = index_entity._link('pb:pacticipant').expand('pacticipant' => params[:name]).get
message = nil
response_entity = if pacticipant_entity.does_not_exist?
message = "Pacticipant \"#{params[:name]}\" created"
index_entity._link!('pb:pacticipants').post(pacticipant_resource_params)
@action = "created"
index_resource._link!('pb:pacticipants').post!(pacticipant_resource_params)
elsif pacticipant_entity.success?
@action = "updated"
pacticipant_entity._link!('self').patch!(pacticipant_resource_params)
else
message = "Pacticipant \"#{params[:name]}\" updated"
pacticipant_entity._link!('self').patch(pacticipant_resource_params)
pacticipant_entity.assert_success!
end

response_entity.assert_success!
PactBroker::Client::CommandResult.new(true, message)
rescue StandardError => e
$stderr.puts("#{e.class} - #{e}\n#{e.backtrace.join("\n")}") if verbose
PactBroker::Client::CommandResult.new(false, "#{e.class} - #{e}")
PactBroker::Client::CommandResult.new(true, result_message)
end

private

attr_reader :index_entry_point, :params, :verbose

def index_entity
@index_entity ||= index_entry_point.get!
def result_message
if json_output?
response_entity.response.raw_body
else
green(message = "Pacticipant \"#{params[:name]}\" #{action}")
end
end

def pacticipant_resource_params
p = { name: params[:name] }
p[:repositoryUrl] = params[:repository_url] if params[:repository_url]
p
{
name: params[:name],
repositoryUrl: params[:repository_url],
displayName: params[:display_name]
}.compact
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact_broker/client/pacticipants/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module Pacticipants2
before do
allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:sleep)
end
let(:pact_broker_client_options) { {} }
let(:pact_broker_client_options) { { pact_broker_base_url: broker_base_url} }
let(:broker_base_url) { "http://url" }
let(:params) { { name: 'Foo' } }

subject { Create.call(params, broker_base_url, pact_broker_client_options)}
subject { Create.call(params, {}, pact_broker_client_options)}

context "when there is an http error" do
let!(:index_request) do
Expand Down
9 changes: 5 additions & 4 deletions spec/service_providers/pacticipants_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@
}
end

let(:pact_broker_client_options) { {} }
let(:pact_broker_client_options) { { pact_broker_base_url: broker_base_url} }
let(:options) { {} }

subject { PactBroker::Client::Pacticipants2::Create.call(params, broker_base_url, pact_broker_client_options) }
subject { PactBroker::Client::Pacticipants2::Create.call(params, options, pact_broker_client_options) }

context "when the pacticipant does not already exist" do
before do
Expand All @@ -81,7 +82,7 @@
it "returns a CommandResult with success = true" do
expect(subject).to be_a PactBroker::Client::CommandResult
expect(subject.success).to be true
expect(subject.message).to eq "Pacticipant \"Foo\" created"
expect(subject.message).to include "Pacticipant \"Foo\" created"
end
end

Expand Down Expand Up @@ -112,7 +113,7 @@
it "returns a CommandResult with success = true" do
expect(subject).to be_a PactBroker::Client::CommandResult
expect(subject.success).to be true
expect(subject.message).to eq "Pacticipant \"Foo\" updated"
expect(subject.message).to include "Pacticipant \"Foo\" updated"
end
end
end

0 comments on commit 76f323b

Please sign in to comment.