Skip to content

Commit f28d9ad

Browse files
committed
feat(publish): expose tag_with_git_branch in rake publication task
1 parent ebce7d8 commit f28d9ad

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

README.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,14 @@ PactBroker::Client::PublicationTask.new do | task |
119119
require 'my_consumer/version'
120120
task.consumer_version = MyConsumer::VERSION
121121
task.pattern = 'custom/path/to/pacts/*.json' # optional, default value is 'spec/pacts/*.json'
122-
task.pact_broker_base_url = "http://pact-broker.my.org"
122+
task.pact_broker_base_url = "http://pact-broker"
123+
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
123124
task.tags = ["dev"] # optional
124125
task.pact_broker_basic_auth = { username: 'basic_auth_user', password: 'basic_auth_pass'} # optional
125126
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.
126127
end
127128
```
128129

129-
## Using tags
130-
131-
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].
132-
133-
If you want to use the git branch name as the tag name, use:
134-
135-
```ruby
136-
task.tag = `git rev-parse --abbrev-ref HEAD`.strip
137-
```
138-
139130
```bash
140131
# In CI script
141132

lib/pact_broker/client/tasks/publication_task.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'rake/tasklib'
2+
require 'pact_broker/client/git'
23

34
=begin
45
require pact_broker/client/tasks
@@ -7,7 +8,8 @@
78
require 'consumer/version'
89
task.pact_broker_base_url = 'http://pact-broker'
910
task.consumer_version = Consumer::VERSION
10-
task.tag = "dev"
11+
task.tags = ["dev"]
12+
task.tag_with_git_branch = true
1113
end
1214
1315
=end
@@ -16,7 +18,8 @@ module PactBroker
1618
module Client
1719
class PublicationTask < ::Rake::TaskLib
1820

19-
attr_accessor :pattern, :pact_broker_base_url, :consumer_version, :tag, :write_method, :pact_broker_basic_auth
21+
attr_accessor :pattern, :pact_broker_base_url, :consumer_version, :tag, :write_method, :tag_with_git_branch, :pact_broker_basic_auth
22+
alias_method :tags=, :tag=
2023
alias_method :tags, :tag
2124

2225
def initialize name = nil, &block
@@ -36,7 +39,7 @@ def rake_task &block
3639
require 'pact_broker/client/publish_pacts'
3740
basic_auth_client_options = pact_broker_basic_auth ? {basic_auth: pact_broker_basic_auth} : {}
3841
pact_broker_client_options = basic_auth_client_options.merge(write_method ? {write: write_method} : {})
39-
success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version, [*tags], pact_broker_client_options).call
42+
success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version, all_tags, pact_broker_client_options).call
4043
raise "One or more pacts failed to be published" unless success
4144
end
4245
end
@@ -46,6 +49,11 @@ def task_name
4649
@name ? "publish:#{@name}" : "publish"
4750
end
4851

52+
def all_tags
53+
t = [*tags]
54+
t << PactBroker::Client::Git.branch if tag_with_git_branch
55+
t.compact.uniq
56+
end
4957
end
5058
end
5159
end

spec/lib/pact_broker/client/tasks/publication_task_spec.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ module PactBroker::Client
1010
@consumer_version = "1.2.3"
1111
end
1212

13-
let(:publish_pacts) { instance_double("PactBroker::ClientSupport::PublishPacts")}
13+
let(:publish_pacts) { instance_double("PactBroker::ClientSupport::PublishPacts", call: true)}
1414
let(:pact_file_list) { ['spec/pact/consumer-provider.json'] }
1515

1616
before do
1717
allow(PactBroker::Client::PublishPacts).to receive(:new).and_return(publish_pacts)
1818
allow(FileList).to receive(:[]).with(pattern).and_return(pact_file_list)
19+
allow(PactBroker::Client::Git).to receive(:branch).and_return('foo')
1920
end
2021

2122
let(:pattern) { "spec/pacts/*.json" }
@@ -59,6 +60,22 @@ module PactBroker::Client
5960
end
6061
end
6162

63+
context "when tag_with_git_branch is true" do
64+
before :all do
65+
PactBroker::Client::PublicationTask.new(:git_branch) do | task |
66+
task.consumer_version = '1.2.3'
67+
task.tag_with_git_branch = true
68+
task.tags = ['bar']
69+
end
70+
end
71+
72+
it "invokes PublishPacts with the git branch name as a tag" do
73+
expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, anything, ['bar', 'foo'], anything).and_return(publish_pacts)
74+
75+
Rake::Task['pact:publish:git_branch'].execute
76+
end
77+
end
78+
6279
describe "custom task" do
6380

6481
before :all do
@@ -85,7 +102,6 @@ module PactBroker::Client
85102
end
86103

87104
describe "timing of block execution" do
88-
89105
before :all do
90106
PactBroker::Client::PublicationTask.new(:exception) do | task |
91107
raise 'A contrived exception'
@@ -96,8 +112,5 @@ module PactBroker::Client
96112
expect{ Rake::Task['pact:publish:exception'].execute }.to raise_error 'A contrived exception'
97113
end
98114
end
99-
100-
101115
end
102-
103116
end

0 commit comments

Comments
 (0)