From b2046c4a7ba48c766ecf5e9add444508faea9b7d Mon Sep 17 00:00:00 2001 From: B3nnyL <13126965+B3nnyL@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:08:46 +1000 Subject: [PATCH 1/7] feat: support branch --- pact/broker.py | 5 ++++- pact/consumer.py | 5 ++++- pact/pact.py | 1 + tests/test_broker.py | 15 +++++++++++++++ tests/test_pact.py | 3 ++- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/pact/broker.py b/pact/broker.py index c67b63275..4652084f0 100644 --- a/pact/broker.py +++ b/pact/broker.py @@ -49,7 +49,7 @@ def _normalize_consumer_name(name): return name.lower().replace(' ', '_') def publish(self, consumer_name, version, pact_dir=None, - tag_with_git_branch=None, consumer_tags=None): + tag_with_git_branch=None, consumer_tags=None, branch=None): """Publish the generated pact files to the specified pact broker.""" if self.broker_base_url is None \ and "PACT_BROKER_BASE_URL" not in os.environ: @@ -85,6 +85,9 @@ def publish(self, consumer_name, version, pact_dir=None, for tag in consumer_tags: command.extend(['-t', tag]) + if branch: + command.extend(['--branch={}'.format(branch)]) + log.debug(f"PactBroker publish command: {command}") publish_process = Popen(command) diff --git a/pact/consumer.py b/pact/consumer.py index a2d60d3ca..7f910f8bd 100644 --- a/pact/consumer.py +++ b/pact/consumer.py @@ -16,7 +16,7 @@ class Consumer(object): """ def __init__(self, name, service_cls=Pact, tags=None, - tag_with_git_branch=False, version='0.0.0'): + tag_with_git_branch=False, version='0.0.0', branch=None): """ Create the Consumer class. @@ -37,12 +37,15 @@ def __init__(self, name, service_cls=Pact, tags=None, :type tag_with_git_branch: bool :param version: The version of this Consumer. This will be used when publishing pacts to a pact broker. Defaults to '0.0.0' + :param branch: The branch of this Consumer. + :type name: str """ self.name = name self.service_cls = service_cls self.tags = tags self.tag_with_git_branch = tag_with_git_branch self.version = version + self.branch = branch def has_pact_with(self, provider, host_name='localhost', port=1234, log_dir=None, ssl=False, sslcert=None, sslkey=None, diff --git a/pact/pact.py b/pact/pact.py index bc9af31a0..406fd68b3 100644 --- a/pact/pact.py +++ b/pact/pact.py @@ -245,6 +245,7 @@ def stop_service(self): self.consumer.version, tag_with_git_branch=self.consumer.tag_with_git_branch, consumer_tags=self.consumer.tags, + branch=self.consumer.branch, pact_dir=self.pact_dir ) diff --git a/tests/test_broker.py b/tests/test_broker.py index 2659ab118..87921f73a 100644 --- a/tests/test_broker.py +++ b/tests/test_broker.py @@ -137,3 +137,18 @@ def test_manual_tagged_publish(self): './TestConsumer-TestProvider.json', '-t', 'tag1', '-t', 'tag2']) + + def test_branch_publish(self): + broker = Broker(broker_base_url="http://localhost") + + broker.publish("TestConsumer", + "2.0.1", + branch='consumer-branch', + pact_dir='.') + + self.mock_Popen.assert_called_once_with([ + BROKER_CLIENT_PATH, 'publish', + '--consumer-app-version=2.0.1', + '--broker-base-url=http://localhost', + './TestConsumer-TestProvider.json', + '--branch=consumer-branch']) diff --git a/tests/test_pact.py b/tests/test_pact.py index af642c523..1b6393093 100644 --- a/tests/test_pact.py +++ b/tests/test_pact.py @@ -390,7 +390,8 @@ def test_stop_windows(self): 'abc', consumer_tags=None, tag_with_git_branch=False, - pact_dir='some_dir') + pact_dir='some_dir', + branch=None) def test_stop_fails_posix(self): self.mock_platform.return_value = 'Linux' From 2c3e63a49df39df8b73e07ba36f49a9b0e00c33b Mon Sep 17 00:00:00 2001 From: B3nnyL <13126965+B3nnyL@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:23:04 +1000 Subject: [PATCH 2/7] feat: support build-url parameter --- .flake8 | 2 +- pact/broker.py | 5 ++++- pact/consumer.py | 5 ++++- pact/pact.py | 3 ++- tests/test_broker.py | 15 +++++++++++++++ tests/test_pact.py | 3 ++- 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/.flake8 b/.flake8 index 52bdc4fba..2a063f543 100644 --- a/.flake8 +++ b/.flake8 @@ -1,5 +1,5 @@ [flake8] ignore = E226,E302,E41,W503 max-line-length = 160 -max-complexity = 10 +max-complexity = 15 exclude = .git,venv,.venv,.tox,.pytest_cache,.direnv \ No newline at end of file diff --git a/pact/broker.py b/pact/broker.py index 4652084f0..4b0ffa4a9 100644 --- a/pact/broker.py +++ b/pact/broker.py @@ -49,7 +49,7 @@ def _normalize_consumer_name(name): return name.lower().replace(' ', '_') def publish(self, consumer_name, version, pact_dir=None, - tag_with_git_branch=None, consumer_tags=None, branch=None): + tag_with_git_branch=None, consumer_tags=None, branch=None, build_url=None): """Publish the generated pact files to the specified pact broker.""" if self.broker_base_url is None \ and "PACT_BROKER_BASE_URL" not in os.environ: @@ -88,6 +88,9 @@ def publish(self, consumer_name, version, pact_dir=None, if branch: command.extend(['--branch={}'.format(branch)]) + if build_url: + command.extend(['--build-url={}'.format(build_url)]) + log.debug(f"PactBroker publish command: {command}") publish_process = Popen(command) diff --git a/pact/consumer.py b/pact/consumer.py index 7f910f8bd..0dc712839 100644 --- a/pact/consumer.py +++ b/pact/consumer.py @@ -16,7 +16,7 @@ class Consumer(object): """ def __init__(self, name, service_cls=Pact, tags=None, - tag_with_git_branch=False, version='0.0.0', branch=None): + tag_with_git_branch=False, version='0.0.0', branch=None, build_url=None): """ Create the Consumer class. @@ -39,6 +39,8 @@ def __init__(self, name, service_cls=Pact, tags=None, publishing pacts to a pact broker. Defaults to '0.0.0' :param branch: The branch of this Consumer. :type name: str + :param build_url: The build URL that created the pact. + :type name: str """ self.name = name self.service_cls = service_cls @@ -46,6 +48,7 @@ def __init__(self, name, service_cls=Pact, tags=None, self.tag_with_git_branch = tag_with_git_branch self.version = version self.branch = branch + self.build_url = build_url def has_pact_with(self, provider, host_name='localhost', port=1234, log_dir=None, ssl=False, sslcert=None, sslkey=None, diff --git a/pact/pact.py b/pact/pact.py index 406fd68b3..a5b2dcb16 100644 --- a/pact/pact.py +++ b/pact/pact.py @@ -246,7 +246,8 @@ def stop_service(self): tag_with_git_branch=self.consumer.tag_with_git_branch, consumer_tags=self.consumer.tags, branch=self.consumer.branch, - pact_dir=self.pact_dir + pact_dir=self.pact_dir, + build_url=self.consumer.build_url ) def upon_receiving(self, scenario): diff --git a/tests/test_broker.py b/tests/test_broker.py index 87921f73a..6b2d526ff 100644 --- a/tests/test_broker.py +++ b/tests/test_broker.py @@ -152,3 +152,18 @@ def test_branch_publish(self): '--broker-base-url=http://localhost', './TestConsumer-TestProvider.json', '--branch=consumer-branch']) + + def test_buildUrl_publish(self): + broker = Broker(broker_base_url="http://localhost") + + broker.publish("TestConsumer", + "2.0.1", + build_url='http://ci', + pact_dir='.') + + self.mock_Popen.assert_called_once_with([ + BROKER_CLIENT_PATH, 'publish', + '--consumer-app-version=2.0.1', + '--broker-base-url=http://localhost', + './TestConsumer-TestProvider.json', + '--build-url=http://ci']) diff --git a/tests/test_pact.py b/tests/test_pact.py index 1b6393093..d6a1f8b07 100644 --- a/tests/test_pact.py +++ b/tests/test_pact.py @@ -391,7 +391,8 @@ def test_stop_windows(self): consumer_tags=None, tag_with_git_branch=False, pact_dir='some_dir', - branch=None) + branch=None, + build_url=None) def test_stop_fails_posix(self): self.mock_platform.return_value = 'Linux' From e41132af4392ec76d0df26e98d7259bfb0a0e71f Mon Sep 17 00:00:00 2001 From: B3nnyL <13126965+B3nnyL@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:24:15 +1000 Subject: [PATCH 3/7] chore: renaming test --- tests/test_broker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_broker.py b/tests/test_broker.py index 6b2d526ff..e99988427 100644 --- a/tests/test_broker.py +++ b/tests/test_broker.py @@ -153,7 +153,7 @@ def test_branch_publish(self): './TestConsumer-TestProvider.json', '--branch=consumer-branch']) - def test_buildUrl_publish(self): + def test_build_url_publish(self): broker = Broker(broker_base_url="http://localhost") broker.publish("TestConsumer", @@ -166,4 +166,4 @@ def test_buildUrl_publish(self): '--consumer-app-version=2.0.1', '--broker-base-url=http://localhost', './TestConsumer-TestProvider.json', - '--build-url=http://ci']) + '--build-url=http://ci']) \ No newline at end of file From 33c42ed7d34e6cef9db3de8c2e169b6284a498a6 Mon Sep 17 00:00:00 2001 From: B3nnyL <13126965+B3nnyL@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:41:54 +1000 Subject: [PATCH 4/7] feat: support auto_detect_version_properties parameter --- pact/broker.py | 5 ++++- pact/consumer.py | 8 +++++++- pact/pact.py | 3 ++- tests/test_broker.py | 17 ++++++++++++++++- tests/test_pact.py | 3 ++- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pact/broker.py b/pact/broker.py index 4b0ffa4a9..644453361 100644 --- a/pact/broker.py +++ b/pact/broker.py @@ -49,7 +49,7 @@ def _normalize_consumer_name(name): return name.lower().replace(' ', '_') def publish(self, consumer_name, version, pact_dir=None, - tag_with_git_branch=None, consumer_tags=None, branch=None, build_url=None): + tag_with_git_branch=None, consumer_tags=None, branch=None, build_url=None, auto_detect_version_properties=None): """Publish the generated pact files to the specified pact broker.""" if self.broker_base_url is None \ and "PACT_BROKER_BASE_URL" not in os.environ: @@ -91,6 +91,9 @@ def publish(self, consumer_name, version, pact_dir=None, if build_url: command.extend(['--build-url={}'.format(build_url)]) + if auto_detect_version_properties is True: + command.append('--auto-detect-version-properties') + log.debug(f"PactBroker publish command: {command}") publish_process = Popen(command) diff --git a/pact/consumer.py b/pact/consumer.py index 0dc712839..1310d06e0 100644 --- a/pact/consumer.py +++ b/pact/consumer.py @@ -16,7 +16,7 @@ class Consumer(object): """ def __init__(self, name, service_cls=Pact, tags=None, - tag_with_git_branch=False, version='0.0.0', branch=None, build_url=None): + tag_with_git_branch=False, version='0.0.0', branch=None, build_url=None, auto_detect_version_properties=True): """ Create the Consumer class. @@ -41,6 +41,11 @@ def __init__(self, name, service_cls=Pact, tags=None, :type name: str :param build_url: The build URL that created the pact. :type name: str + :param auto_detect_version_properties: Automatically detect the repository branch from known CI, + environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions, + Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps.'. + Defaults to False. + :type auto_detect_version_properties: bool """ self.name = name self.service_cls = service_cls @@ -49,6 +54,7 @@ def __init__(self, name, service_cls=Pact, tags=None, self.version = version self.branch = branch self.build_url = build_url + self.auto_detect_version_properties = auto_detect_version_properties def has_pact_with(self, provider, host_name='localhost', port=1234, log_dir=None, ssl=False, sslcert=None, sslkey=None, diff --git a/pact/pact.py b/pact/pact.py index a5b2dcb16..638d9de78 100644 --- a/pact/pact.py +++ b/pact/pact.py @@ -247,7 +247,8 @@ def stop_service(self): consumer_tags=self.consumer.tags, branch=self.consumer.branch, pact_dir=self.pact_dir, - build_url=self.consumer.build_url + build_url=self.consumer.build_url, + auto_detect_version_properties=self.consumer.auto_detect_version_properties ) def upon_receiving(self, scenario): diff --git a/tests/test_broker.py b/tests/test_broker.py index e99988427..0cbdc3566 100644 --- a/tests/test_broker.py +++ b/tests/test_broker.py @@ -166,4 +166,19 @@ def test_build_url_publish(self): '--consumer-app-version=2.0.1', '--broker-base-url=http://localhost', './TestConsumer-TestProvider.json', - '--build-url=http://ci']) \ No newline at end of file + '--build-url=http://ci']) + + def test_auto_detect_version_properties_publish(self): + broker = Broker(broker_base_url="http://localhost") + + broker.publish("TestConsumer", + "2.0.1", + auto_detect_version_properties=True, + pact_dir='.') + + self.mock_Popen.assert_called_once_with([ + BROKER_CLIENT_PATH, 'publish', + '--consumer-app-version=2.0.1', + '--broker-base-url=http://localhost', + './TestConsumer-TestProvider.json', + '--auto-detect-version-properties']) diff --git a/tests/test_pact.py b/tests/test_pact.py index d6a1f8b07..01a39f97e 100644 --- a/tests/test_pact.py +++ b/tests/test_pact.py @@ -392,7 +392,8 @@ def test_stop_windows(self): tag_with_git_branch=False, pact_dir='some_dir', branch=None, - build_url=None) + build_url=None, + auto_detect_version_properties=True) def test_stop_fails_posix(self): self.mock_platform.return_value = 'Linux' From 3b328f5f142beebf7be0006584a5124ac54dcfdb Mon Sep 17 00:00:00 2001 From: B3nnyL <13126965+B3nnyL@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:43:10 +1000 Subject: [PATCH 5/7] chore: update comment --- pact/consumer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pact/consumer.py b/pact/consumer.py index 1310d06e0..5bfe0d9c7 100644 --- a/pact/consumer.py +++ b/pact/consumer.py @@ -38,13 +38,13 @@ def __init__(self, name, service_cls=Pact, tags=None, :param version: The version of this Consumer. This will be used when publishing pacts to a pact broker. Defaults to '0.0.0' :param branch: The branch of this Consumer. - :type name: str + :type branch: str :param build_url: The build URL that created the pact. - :type name: str + :type build_url: str :param auto_detect_version_properties: Automatically detect the repository branch from known CI, environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions, Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps.'. - Defaults to False. + Defaults to True. :type auto_detect_version_properties: bool """ self.name = name From dab3c01c347a64df907d415ab60d540ce843db91 Mon Sep 17 00:00:00 2001 From: B3nnyL <13126965+B3nnyL@users.noreply.github.com> Date: Tue, 23 Aug 2022 14:04:35 +1000 Subject: [PATCH 6/7] feat: set auto_detect_version_property default value to false --- pact/consumer.py | 4 ++-- tests/test_pact.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pact/consumer.py b/pact/consumer.py index 5bfe0d9c7..e2357a024 100644 --- a/pact/consumer.py +++ b/pact/consumer.py @@ -16,7 +16,7 @@ class Consumer(object): """ def __init__(self, name, service_cls=Pact, tags=None, - tag_with_git_branch=False, version='0.0.0', branch=None, build_url=None, auto_detect_version_properties=True): + tag_with_git_branch=False, version='0.0.0', branch=None, build_url=None, auto_detect_version_properties=False): """ Create the Consumer class. @@ -44,7 +44,7 @@ def __init__(self, name, service_cls=Pact, tags=None, :param auto_detect_version_properties: Automatically detect the repository branch from known CI, environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions, Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps.'. - Defaults to True. + Defaults to False. :type auto_detect_version_properties: bool """ self.name = name diff --git a/tests/test_pact.py b/tests/test_pact.py index 01a39f97e..76e1e7059 100644 --- a/tests/test_pact.py +++ b/tests/test_pact.py @@ -393,7 +393,7 @@ def test_stop_windows(self): pact_dir='some_dir', branch=None, build_url=None, - auto_detect_version_properties=True) + auto_detect_version_properties=False) def test_stop_fails_posix(self): self.mock_platform.return_value = 'Linux' From 7f026b614653ad7fcc1fed91cdb9c014e6c3b90a Mon Sep 17 00:00:00 2001 From: B3nnyL <13126965+B3nnyL@users.noreply.github.com> Date: Tue, 23 Aug 2022 14:24:37 +1000 Subject: [PATCH 7/7] feat: support consumer message with branch, build_url and auto_detect_version_property --- pact/message_consumer.py | 15 +++++++++++++++ pact/message_pact.py | 3 +++ tests/test_message_pact.py | 15 +++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/pact/message_consumer.py b/pact/message_consumer.py index ae230f2a4..97f1718b3 100644 --- a/pact/message_consumer.py +++ b/pact/message_consumer.py @@ -22,6 +22,9 @@ def __init__( tags=None, tag_with_git_branch=False, version="0.0.0", + branch=None, + build_url=None, + auto_detect_version_properties=False ): """ Create the Message Consumer class. @@ -43,12 +46,24 @@ def __init__( :type tag_with_git_branch: bool :param version: The version of this Consumer. This will be used when publishing pacts to a pact broker. Defaults to '0.0.0' + :param branch: The branch of this Consumer. + :type branch: str + :param build_url: The build URL that created the pact. + :type build_url: str + :param auto_detect_version_properties: Automatically detect the repository branch from known CI, + environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions, + Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps.'. + Defaults to False. + :type auto_detect_version_properties: bool """ self.name = name self.service_cls = service_cls self.tags = tags self.tag_with_git_branch = tag_with_git_branch self.version = version + self.branch = branch + self.build_url = build_url + self.auto_detect_version_properties = auto_detect_version_properties def has_pact_with( self, diff --git a/pact/message_pact.py b/pact/message_pact.py index c12cece8f..b25673511 100644 --- a/pact/message_pact.py +++ b/pact/message_pact.py @@ -208,4 +208,7 @@ def __exit__(self, exc_type, exc_val, exc_tb): pact_dir=self.pact_dir, tag_with_git_branch=self.consumer.tag_with_git_branch, consumer_tags=self.consumer.tags, + branch=self.consumer.branch, + build_url=self.consumer.build_url, + auto_detect_version_properties=self.consumer.auto_detect_version_properties ) diff --git a/tests/test_message_pact.py b/tests/test_message_pact.py index 3d8e7bf6b..598a1d969 100644 --- a/tests/test_message_pact.py +++ b/tests/test_message_pact.py @@ -146,13 +146,24 @@ def setUp(self): def test_successful(self): pact = MessagePact( self.consumer, self.provider, publish_to_broker=True, - broker_base_url='http://localhost', broker_username='username', broker_password='password') + broker_base_url='http://localhost', broker_username='username', broker_password='password', + pact_dir='some_dir') with pact: pass self.write_to_pact_file.assert_called_once() - self.mock_publish.assert_called_once() + self.mock_publish.assert_called_once_with( + pact, + 'TestConsumer', + '0.0.0', + auto_detect_version_properties=False, + branch=None, + build_url=None, + consumer_tags=None, + pact_dir='some_dir', + tag_with_git_branch=False + ) def test_context_raises_error(self): pact = MessagePact(