diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index 183d6eee..9924cb1b 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -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 @@ -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 diff --git a/lib/pact_broker/client/create_tag.rb b/lib/pact_broker/client/create_tag.rb new file mode 100644 index 00000000..5ef9f336 --- /dev/null +++ b/lib/pact_broker/client/create_tag.rb @@ -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 diff --git a/spec/integration/create_version_tag_spec.rb b/spec/integration/create_version_tag_spec.rb new file mode 100644 index 00000000..f4a4b027 --- /dev/null +++ b/spec/integration/create_version_tag_spec.rb @@ -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