From 2ed7a559d461b6b35b3ddaec38258f534e6da1d5 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 17 Jul 2020 15:31:21 +1000 Subject: [PATCH] fix(create-version-tag): raise an error if the version does not exist rather than automatically creating it. Fixes: https://github.com/pact-foundation/pact_broker-client/issues/41 --- README.md | 4 +--- lib/pact_broker/client/cli/broker.rb | 5 ++++ lib/pact_broker/client/create_tag.rb | 26 ++++++++++----------- lib/pact_broker/client/versions/describe.rb | 2 +- spec/integration/create_version_tag_spec.rb | 2 +- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b91bdeb5..ca441030 100644 --- a/README.md +++ b/README.md @@ -42,13 +42,11 @@ Publish pacts to a Pact Broker. ### create-version-tag ``` -Usage: - pact-broker create-version-tag -a, --pacticipant=PACTICIPANT -b, --broker-base-url=BROKER_BASE_URL -e, --version=VERSION - Options: -a, --pacticipant=PACTICIPANT # The pacticipant name -e, --version=VERSION # The pacticipant version -t, [--tag=TAG] # Tag name for pacticipant version. Can be specified multiple times. + [--auto-create-version], [--no-auto-create-version] # Automatically create the pacticipant version if it does not exist. Default: false -g, [--tag-with-git-branch], [--no-tag-with-git-branch] # Tag pacticipant version with the name of the current git branch. Default: false -b, --broker-base-url=BROKER_BASE_URL # The base URL of the Pact Broker -u, [--broker-username=BROKER_USERNAME] # Pact Broker basic auth username diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index 2f32c76d..8b88740b 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -8,6 +8,7 @@ module CLI class PactPublicationError < ::Thor::Error; end class WebhookCreationError < ::Thor::Error; end class AuthError < ::Thor::Error; end + class VersionCreationError < ::Thor::Error; end class Broker < CustomThor desc 'can-i-deploy', '' @@ -64,6 +65,7 @@ def publish(*pact_files) method_option :pacticipant, required: true, aliases: "-a", desc: "The pacticipant name" method_option :version, required: true, aliases: "-e", desc: "The pacticipant version" method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for pacticipant version. Can be specified multiple times." + method_option :auto_create_version, type: :boolean, default: false, desc: "Automatically create the pacticipant version if it does not exist. Default: false" method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag pacticipant version with the name of the current git branch. Default: false" method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker" method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username" @@ -80,7 +82,10 @@ def create_version_tag options.pacticipant, options.version, tags, + options.auto_create_version, pact_broker_client_options) + rescue PactBroker::Client::Error => e + raise VersionCreationError.new(e.message) end method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that the version belongs to." diff --git a/lib/pact_broker/client/create_tag.rb b/lib/pact_broker/client/create_tag.rb index a1d6200c..81444668 100644 --- a/lib/pact_broker/client/create_tag.rb +++ b/lib/pact_broker/client/create_tag.rb @@ -6,30 +6,22 @@ module PactBroker module Client class CreateTag - class Result - attr_reader :success, :message - - def initialize success, message = nil - @success = success - @message = message - end - end - - def self.call(pact_broker_base_url, pacticipant_name, version, tags, pact_broker_client_options={}) - new(pact_broker_base_url, pacticipant_name, version, tags, pact_broker_client_options).call + def self.call(pact_broker_base_url, pacticipant_name, version, tags, auto_create_version, pact_broker_client_options={}) + new(pact_broker_base_url, pacticipant_name, version, tags, auto_create_version, pact_broker_client_options).call end - def initialize(pact_broker_base_url, pacticipant_name, version, tags, pact_broker_client_options) + def initialize(pact_broker_base_url, pacticipant_name, version, tags, auto_create_version, pact_broker_client_options) @pact_broker_base_url = pact_broker_base_url @pacticipant_name = pacticipant_name @version = version @tags = tags + @auto_create_version = auto_create_version @pact_broker_client_options = pact_broker_client_options end def call + ensure_version_exists if !auto_create_version tags.each do | tag | - # todo check that pacticipant exists first $stdout.puts "Tagging #{pacticipant_name} version #{version} as #{tag}" Retry.while_error do pact_broker_client.pacticipants.versions.tag pacticipant: pacticipant_name, version: version, tag: tag @@ -39,11 +31,17 @@ def call private - attr_reader :pact_broker_base_url, :pacticipant_name, :version, :tags, :pact_broker_client_options + attr_reader :pact_broker_base_url, :pacticipant_name, :version, :tags, :auto_create_version, :pact_broker_client_options def pact_broker_client @pact_broker_client ||= PactBroker::Client::PactBrokerClient.new(base_url: pact_broker_base_url, client_options: pact_broker_client_options) end + + def ensure_version_exists + if pact_broker_client.pacticipants.versions.find(pacticipant: pacticipant_name, version: version).nil? + raise PactBroker::Client::Error.new("Could not create tag. Version #{version} of #{pacticipant_name} does not exist.") + end + end end end end diff --git a/lib/pact_broker/client/versions/describe.rb b/lib/pact_broker/client/versions/describe.rb index 34b0e3e3..dd63cb5e 100644 --- a/lib/pact_broker/client/versions/describe.rb +++ b/lib/pact_broker/client/versions/describe.rb @@ -28,7 +28,7 @@ def initialize params, options, pact_broker_base_url, pact_broker_client_options def call version_hash = if params[:version] - versions_client.find params + versions_client.find(params) else pact_broker_client.pacticipants.versions.latest(params) end diff --git a/spec/integration/create_version_tag_spec.rb b/spec/integration/create_version_tag_spec.rb index 7d446a59..c5b20b1e 100644 --- a/spec/integration/create_version_tag_spec.rb +++ b/spec/integration/create_version_tag_spec.rb @@ -5,7 +5,7 @@ end context "when the version is successfully tagged" do - subject { `bundle exec bin/pact-broker create-version-tag --pacticipant Condor --version 1.3.0 --tag prod --broker-base-url http://localhost:5001` } + subject { `bundle exec bin/pact-broker create-version-tag --auto-create-version --pacticipant Condor --version 1.3.0 --tag prod --broker-base-url http://localhost:5001` } it "returns a success exit code" do subject