Skip to content

Commit 42e0db8

Browse files
authored
feat: Support publish pact with branch (#300)
* feat: support branch * feat: support build-url parameter * chore: renaming test * feat: support auto_detect_version_properties parameter * chore: update comment * feat: set auto_detect_version_property default value to false * feat: support consumer message with branch, build_url and auto_detect_version_property
1 parent 80d7b13 commit 42e0db8

9 files changed

+108
-7
lines changed

.flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[flake8]
22
ignore = E226,E302,E41,W503
33
max-line-length = 160
4-
max-complexity = 10
4+
max-complexity = 15
55
exclude = .git,venv,.venv,.tox,.pytest_cache,.direnv

pact/broker.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def _normalize_consumer_name(name):
4949
return name.lower().replace(' ', '_')
5050

5151
def publish(self, consumer_name, version, pact_dir=None,
52-
tag_with_git_branch=None, consumer_tags=None):
52+
tag_with_git_branch=None, consumer_tags=None, branch=None, build_url=None, auto_detect_version_properties=None):
5353
"""Publish the generated pact files to the specified pact broker."""
5454
if self.broker_base_url is None \
5555
and "PACT_BROKER_BASE_URL" not in os.environ:
@@ -85,6 +85,15 @@ def publish(self, consumer_name, version, pact_dir=None,
8585
for tag in consumer_tags:
8686
command.extend(['-t', tag])
8787

88+
if branch:
89+
command.extend(['--branch={}'.format(branch)])
90+
91+
if build_url:
92+
command.extend(['--build-url={}'.format(build_url)])
93+
94+
if auto_detect_version_properties is True:
95+
command.append('--auto-detect-version-properties')
96+
8897
log.debug(f"PactBroker publish command: {command}")
8998

9099
publish_process = Popen(command)

pact/consumer.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Consumer(object):
1616
"""
1717

1818
def __init__(self, name, service_cls=Pact, tags=None,
19-
tag_with_git_branch=False, version='0.0.0'):
19+
tag_with_git_branch=False, version='0.0.0', branch=None, build_url=None, auto_detect_version_properties=False):
2020
"""
2121
Create the Consumer class.
2222
@@ -37,12 +37,24 @@ def __init__(self, name, service_cls=Pact, tags=None,
3737
:type tag_with_git_branch: bool
3838
:param version: The version of this Consumer. This will be used when
3939
publishing pacts to a pact broker. Defaults to '0.0.0'
40+
:param branch: The branch of this Consumer.
41+
:type branch: str
42+
:param build_url: The build URL that created the pact.
43+
:type build_url: str
44+
:param auto_detect_version_properties: Automatically detect the repository branch from known CI,
45+
environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions,
46+
Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps.'.
47+
Defaults to False.
48+
:type auto_detect_version_properties: bool
4049
"""
4150
self.name = name
4251
self.service_cls = service_cls
4352
self.tags = tags
4453
self.tag_with_git_branch = tag_with_git_branch
4554
self.version = version
55+
self.branch = branch
56+
self.build_url = build_url
57+
self.auto_detect_version_properties = auto_detect_version_properties
4658

4759
def has_pact_with(self, provider, host_name='localhost', port=1234,
4860
log_dir=None, ssl=False, sslcert=None, sslkey=None,

pact/message_consumer.py

+15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def __init__(
2222
tags=None,
2323
tag_with_git_branch=False,
2424
version="0.0.0",
25+
branch=None,
26+
build_url=None,
27+
auto_detect_version_properties=False
2528
):
2629
"""
2730
Create the Message Consumer class.
@@ -43,12 +46,24 @@ def __init__(
4346
:type tag_with_git_branch: bool
4447
:param version: The version of this Consumer. This will be used when
4548
publishing pacts to a pact broker. Defaults to '0.0.0'
49+
:param branch: The branch of this Consumer.
50+
:type branch: str
51+
:param build_url: The build URL that created the pact.
52+
:type build_url: str
53+
:param auto_detect_version_properties: Automatically detect the repository branch from known CI,
54+
environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions,
55+
Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps.'.
56+
Defaults to False.
57+
:type auto_detect_version_properties: bool
4658
"""
4759
self.name = name
4860
self.service_cls = service_cls
4961
self.tags = tags
5062
self.tag_with_git_branch = tag_with_git_branch
5163
self.version = version
64+
self.branch = branch
65+
self.build_url = build_url
66+
self.auto_detect_version_properties = auto_detect_version_properties
5267

5368
def has_pact_with(
5469
self,

pact/message_pact.py

+3
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
208208
pact_dir=self.pact_dir,
209209
tag_with_git_branch=self.consumer.tag_with_git_branch,
210210
consumer_tags=self.consumer.tags,
211+
branch=self.consumer.branch,
212+
build_url=self.consumer.build_url,
213+
auto_detect_version_properties=self.consumer.auto_detect_version_properties
211214
)

pact/pact.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ def stop_service(self):
245245
self.consumer.version,
246246
tag_with_git_branch=self.consumer.tag_with_git_branch,
247247
consumer_tags=self.consumer.tags,
248-
pact_dir=self.pact_dir
248+
branch=self.consumer.branch,
249+
pact_dir=self.pact_dir,
250+
build_url=self.consumer.build_url,
251+
auto_detect_version_properties=self.consumer.auto_detect_version_properties
249252
)
250253

251254
def upon_receiving(self, scenario):

tests/test_broker.py

+45
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,48 @@ def test_manual_tagged_publish(self):
137137
'./TestConsumer-TestProvider.json',
138138
'-t', 'tag1',
139139
'-t', 'tag2'])
140+
141+
def test_branch_publish(self):
142+
broker = Broker(broker_base_url="http://localhost")
143+
144+
broker.publish("TestConsumer",
145+
"2.0.1",
146+
branch='consumer-branch',
147+
pact_dir='.')
148+
149+
self.mock_Popen.assert_called_once_with([
150+
BROKER_CLIENT_PATH, 'publish',
151+
'--consumer-app-version=2.0.1',
152+
'--broker-base-url=http://localhost',
153+
'./TestConsumer-TestProvider.json',
154+
'--branch=consumer-branch'])
155+
156+
def test_build_url_publish(self):
157+
broker = Broker(broker_base_url="http://localhost")
158+
159+
broker.publish("TestConsumer",
160+
"2.0.1",
161+
build_url='http://ci',
162+
pact_dir='.')
163+
164+
self.mock_Popen.assert_called_once_with([
165+
BROKER_CLIENT_PATH, 'publish',
166+
'--consumer-app-version=2.0.1',
167+
'--broker-base-url=http://localhost',
168+
'./TestConsumer-TestProvider.json',
169+
'--build-url=http://ci'])
170+
171+
def test_auto_detect_version_properties_publish(self):
172+
broker = Broker(broker_base_url="http://localhost")
173+
174+
broker.publish("TestConsumer",
175+
"2.0.1",
176+
auto_detect_version_properties=True,
177+
pact_dir='.')
178+
179+
self.mock_Popen.assert_called_once_with([
180+
BROKER_CLIENT_PATH, 'publish',
181+
'--consumer-app-version=2.0.1',
182+
'--broker-base-url=http://localhost',
183+
'./TestConsumer-TestProvider.json',
184+
'--auto-detect-version-properties'])

tests/test_message_pact.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,24 @@ def setUp(self):
146146
def test_successful(self):
147147
pact = MessagePact(
148148
self.consumer, self.provider, publish_to_broker=True,
149-
broker_base_url='http://localhost', broker_username='username', broker_password='password')
149+
broker_base_url='http://localhost', broker_username='username', broker_password='password',
150+
pact_dir='some_dir')
150151

151152
with pact:
152153
pass
153154

154155
self.write_to_pact_file.assert_called_once()
155-
self.mock_publish.assert_called_once()
156+
self.mock_publish.assert_called_once_with(
157+
pact,
158+
'TestConsumer',
159+
'0.0.0',
160+
auto_detect_version_properties=False,
161+
branch=None,
162+
build_url=None,
163+
consumer_tags=None,
164+
pact_dir='some_dir',
165+
tag_with_git_branch=False
166+
)
156167

157168
def test_context_raises_error(self):
158169
pact = MessagePact(

tests/test_pact.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,10 @@ def test_stop_windows(self):
390390
'abc',
391391
consumer_tags=None,
392392
tag_with_git_branch=False,
393-
pact_dir='some_dir')
393+
pact_dir='some_dir',
394+
branch=None,
395+
build_url=None,
396+
auto_detect_version_properties=False)
394397

395398
def test_stop_fails_posix(self):
396399
self.mock_platform.return_value = 'Linux'

0 commit comments

Comments
 (0)