Skip to content

Commit

Permalink
feat(publish): expose tag_with_git_branch in rake publication task
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Nov 6, 2017
1 parent ebce7d8 commit f28d9ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions lib/pact_broker/client/tasks/publication_task.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rake/tasklib'
require 'pact_broker/client/git'

=begin
require pact_broker/client/tasks
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
23 changes: 18 additions & 5 deletions spec/lib/pact_broker/client/tasks/publication_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -96,8 +112,5 @@ module PactBroker::Client
expect{ Rake::Task['pact:publish:exception'].execute }.to raise_error 'A contrived exception'
end
end


end

end

0 comments on commit f28d9ad

Please sign in to comment.