Skip to content

Commit

Permalink
fix(create-version-tag): raise an error if the version does not exist…
Browse files Browse the repository at this point in the history
… rather than automatically creating it.

Fixes: #41
  • Loading branch information
bethesque committed Jul 17, 2020
1 parent 89e7c6b commit 2ed7a55
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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', ''
Expand Down Expand Up @@ -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"
Expand All @@ -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."
Expand Down
26 changes: 12 additions & 14 deletions lib/pact_broker/client/create_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/pact_broker/client/versions/describe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/create_version_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2ed7a55

Please sign in to comment.