Skip to content

Commit

Permalink
feat(create-version-tag): add CLI to tag a pacticipant version
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 31, 2017
1 parent 0a7118e commit c62d9b8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'pact_broker/client/publish_pacts'
require 'rake/file_list'
require 'thor/error'
require 'pact_broker/client/create_tag'

module PactBroker
module Client
Expand Down Expand Up @@ -49,6 +50,25 @@ def publish(*pact_files)
raise PactPublicationError, "#{e.class} - #{e.message}"
end

desc 'create-version-tag', 'Add a tag to a pacticipant version'
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 :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"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"

def create_version_tag
PactBroker::Client::CreateTag.call(
options.broker_base_url,
options.pacticipant,
options.version,
tags,
pact_broker_client_options)
end

desc 'version', "Show the pact_broker-client gem version"
def version
$stdout.puts PactBroker::Client::VERSION
Expand Down
49 changes: 49 additions & 0 deletions lib/pact_broker/client/create_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'pact_broker/client/error'
require 'pact_broker/client/pact_broker_client'
require 'pact_broker/client/retry'

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
end

def initialize(pact_broker_base_url, pacticipant_name, version, tags, pact_broker_client_options)
@pact_broker_base_url = pact_broker_base_url
@pacticipant_name = pacticipant_name
@version = version
@tags = tags
@pact_broker_client_options = pact_broker_client_options
end

def call
tags.each do | tag |
# todo check that pacticipant exists first
$stdout.puts "Tagging #{pacticipant_name} version #{version} as #{tag}"
Retry.until_true do
pact_broker_client.pacticipants.versions.tag pacticipant: pacticipant_name, version: version, tag: tag
end
end
end

private

attr_reader :pact_broker_base_url, :pacticipant_name, :version, :tags, :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
end
end
end
20 changes: 20 additions & 0 deletions spec/integration/create_version_tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
describe "pact-broker create-version-tag" do
before(:all) do
@pipe = IO.popen("bundle exec pact-stub-service spec/pacts/pact_broker_client-pact_broker.json -p 5001")
sleep 2
end

context "when the version is successfully tagged" do
subject { `bundle exec bin/pact-broker create-version-tag -v --pacticipant Condor --version 1.3.0 --tag prod --broker-base-url http://localhost:5001` }

it "returns a success exit code" do
subject
expect($?.exitstatus).to eq 0
expect(subject).to include 'Tagging Condor version 1.3.0 as prod'
end
end

after(:all) do
Process.kill 'KILL', @pipe.pid
end
end

0 comments on commit c62d9b8

Please sign in to comment.