diff --git a/README.md b/README.md index a3acc60b..43b77de0 100644 --- a/README.md +++ b/README.md @@ -119,23 +119,14 @@ PactBroker::Client::PublicationTask.new do | task | require 'my_consumer/version' task.consumer_version = MyConsumer::VERSION task.pattern = 'custom/path/to/pacts/*.json' # optional, default value is 'spec/pacts/*.json' - task.pact_broker_base_url = "http://pact-broker.my.org" + task.pact_broker_base_url = "http://pact-broker" + task.tag_with_git_branch = true|false # STRONGLY RECOMMENDED as it will greatly assist with your pact workflow. Optional, will merge result with other specified task.tags task.tags = ["dev"] # optional task.pact_broker_basic_auth = { username: 'basic_auth_user', password: 'basic_auth_pass'} # optional task.write_method = :merge # optional, this will merge the published pact into an existing pact rather than overwriting it if one exists. Not recommended, as it makes a mulch of the workflow on the broker. end ``` -## Using tags - -Tags enable you to test different versions of your consumer and provider against each other (eg. `head` and `prod`) and to use pacts on feature branches without breaking your main line of development. You can read more about using tags on the Pact broker [wiki][wiki-tags]. - -If you want to use the git branch name as the tag name, use: - -```ruby - task.tag = `git rev-parse --abbrev-ref HEAD`.strip -``` - ```bash # In CI script diff --git a/lib/pact_broker/client/tasks/publication_task.rb b/lib/pact_broker/client/tasks/publication_task.rb index f29eccee..87f7bab5 100644 --- a/lib/pact_broker/client/tasks/publication_task.rb +++ b/lib/pact_broker/client/tasks/publication_task.rb @@ -1,4 +1,5 @@ require 'rake/tasklib' +require 'pact_broker/client/git' =begin require pact_broker/client/tasks @@ -7,7 +8,8 @@ require 'consumer/version' task.pact_broker_base_url = 'http://pact-broker' task.consumer_version = Consumer::VERSION - task.tag = "dev" + task.tags = ["dev"] + task.tag_with_git_branch = true end =end @@ -16,7 +18,8 @@ module PactBroker module Client class PublicationTask < ::Rake::TaskLib - attr_accessor :pattern, :pact_broker_base_url, :consumer_version, :tag, :write_method, :pact_broker_basic_auth + attr_accessor :pattern, :pact_broker_base_url, :consumer_version, :tag, :write_method, :tag_with_git_branch, :pact_broker_basic_auth + alias_method :tags=, :tag= alias_method :tags, :tag def initialize name = nil, &block @@ -36,7 +39,7 @@ def rake_task &block require 'pact_broker/client/publish_pacts' basic_auth_client_options = pact_broker_basic_auth ? {basic_auth: pact_broker_basic_auth} : {} pact_broker_client_options = basic_auth_client_options.merge(write_method ? {write: write_method} : {}) - success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version, [*tags], pact_broker_client_options).call + success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version, all_tags, pact_broker_client_options).call raise "One or more pacts failed to be published" unless success end end @@ -46,6 +49,11 @@ def task_name @name ? "publish:#{@name}" : "publish" end + def all_tags + t = [*tags] + t << PactBroker::Client::Git.branch if tag_with_git_branch + t.compact.uniq + end end end end diff --git a/spec/lib/pact_broker/client/tasks/publication_task_spec.rb b/spec/lib/pact_broker/client/tasks/publication_task_spec.rb index ed6cdf55..369209c5 100644 --- a/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +++ b/spec/lib/pact_broker/client/tasks/publication_task_spec.rb @@ -10,12 +10,13 @@ module PactBroker::Client @consumer_version = "1.2.3" end - let(:publish_pacts) { instance_double("PactBroker::ClientSupport::PublishPacts")} + let(:publish_pacts) { instance_double("PactBroker::ClientSupport::PublishPacts", call: true)} let(:pact_file_list) { ['spec/pact/consumer-provider.json'] } before do allow(PactBroker::Client::PublishPacts).to receive(:new).and_return(publish_pacts) allow(FileList).to receive(:[]).with(pattern).and_return(pact_file_list) + allow(PactBroker::Client::Git).to receive(:branch).and_return('foo') end let(:pattern) { "spec/pacts/*.json" } @@ -59,6 +60,22 @@ module PactBroker::Client end end + context "when tag_with_git_branch is true" do + before :all do + PactBroker::Client::PublicationTask.new(:git_branch) do | task | + task.consumer_version = '1.2.3' + task.tag_with_git_branch = true + task.tags = ['bar'] + end + end + + it "invokes PublishPacts with the git branch name as a tag" do + expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, anything, ['bar', 'foo'], anything).and_return(publish_pacts) + + Rake::Task['pact:publish:git_branch'].execute + end + end + describe "custom task" do before :all do @@ -85,7 +102,6 @@ module PactBroker::Client end describe "timing of block execution" do - before :all do PactBroker::Client::PublicationTask.new(:exception) do | task | raise 'A contrived exception' @@ -96,8 +112,5 @@ module PactBroker::Client expect{ Rake::Task['pact:publish:exception'].execute }.to raise_error 'A contrived exception' end end - - end - end