From 160559cfc4d08f09f2e20c753c35af3e0871d634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Bompard?= Date: Mon, 10 Jun 2024 07:56:44 +0200 Subject: [PATCH] Minor formatting fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Aurélien Bompard --- LICENSE | 18 ++++++------- bugzilla2fedmsg/__init__.py | 9 +++---- bugzilla2fedmsg/consumer.py | 10 +++---- bugzilla2fedmsg/relay.py | 14 +++++----- tests/conftest.py | 1 - tests/test_consumer.py | 4 +-- tests/test_relay.py | 53 ++++++++++--------------------------- tests/test_schemas.py | 37 +++++++------------------- 8 files changed, 47 insertions(+), 99 deletions(-) diff --git a/LICENSE b/LICENSE index 4362b49..e5ab03e 100644 --- a/LICENSE +++ b/LICENSE @@ -55,7 +55,7 @@ modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. - + Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a @@ -111,7 +111,7 @@ modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. - + GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION @@ -158,7 +158,7 @@ Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. - + 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 @@ -216,7 +216,7 @@ instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. - + Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. @@ -267,7 +267,7 @@ Library will still fall under Section 6.) distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. - + 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work @@ -329,7 +329,7 @@ restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. - + 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined @@ -370,7 +370,7 @@ subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. - + 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or @@ -422,7 +422,7 @@ conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. - + 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is @@ -456,7 +456,7 @@ SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS - + How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest diff --git a/bugzilla2fedmsg/__init__.py b/bugzilla2fedmsg/__init__.py index 060495a..3a30dd5 100644 --- a/bugzilla2fedmsg/__init__.py +++ b/bugzilla2fedmsg/__init__.py @@ -1,7 +1,6 @@ import logging import os import time -import logging import click import fedora_messaging @@ -15,18 +14,16 @@ @click.command() -@click.option( - "-c", "--config", envvar="FEDORA_MESSAGING_CONF", help="Configuration file" -) +@click.option("-c", "--config", envvar="FEDORA_MESSAGING_CONF", help="Configuration file") def cli(config): """Relay Bugzilla changes into Fedora Messaging.""" if config: if not os.path.isfile(config): - raise click.exceptions.BadParameter("{} is not a file".format(config)) + raise click.exceptions.BadParameter(f"{config} is not a file") try: fedora_messaging.config.conf.load_config(config_path=config) except fedora_messaging.exceptions.ConfigurationException as e: - raise click.exceptions.BadParameter(str(e)) + raise click.exceptions.BadParameter(str(e)) from e fedora_messaging.config.conf.setup_logging() # Now start the consumer. diff --git a/bugzilla2fedmsg/consumer.py b/bugzilla2fedmsg/consumer.py index 1388a00..3a2e45f 100644 --- a/bugzilla2fedmsg/consumer.py +++ b/bugzilla2fedmsg/consumer.py @@ -26,9 +26,7 @@ def __init__(self, conf, relay): self._conf = conf # Bugzilla - self.products = self._conf.get("bugzilla", {}).get( - "products", ["Fedora", "Fedora EPEL"] - ) + self.products = self._conf.get("bugzilla", {}).get("products", ["Fedora", "Fedora EPEL"]) # STOMP stomp_config = self._conf.get("stomp", {}) @@ -40,9 +38,7 @@ def __init__(self, conf, relay): # Disable cert validation for demo only ssl_context.check_hostname = False ssl_context.verify_mode = ssl.CERT_NONE - ssl_context.load_cert_chain( - stomp_config["ssl_crt"], stomp_config["ssl_key"] - ) + ssl_context.load_cert_chain(stomp_config["ssl_crt"], stomp_config["ssl_key"]) else: ssl_context = None @@ -96,7 +92,7 @@ def consume(self): continue body = json.loads(frame.body.decode()) msg_id = frame.headers.get(StompSpec.MESSAGE_ID_HEADER) - LOGGER.debug("Received message on STOMP with ID {}".format(msg_id)) + LOGGER.debug(f"Received message on STOMP with ID {msg_id}") try: self.relay.on_stomp_message(body, frame.headers) except Exception: diff --git a/bugzilla2fedmsg/relay.py b/bugzilla2fedmsg/relay.py index 4b016fb..a649810 100644 --- a/bugzilla2fedmsg/relay.py +++ b/bugzilla2fedmsg/relay.py @@ -30,7 +30,7 @@ def _bz4_compat_transform(bug, event, objdict, obj): if bug.get("operating_system"): bug["op_sys"] = bug["operating_system"] if not bug.get("weburl"): - bug["weburl"] = "https://bugzilla.redhat.com/show_bug.cgi?id=%s" % bug["id"] + bug["weburl"] = f"https://bugzilla.redhat.com/show_bug.cgi?id={bug['id']}" event["who"] = event["user"]["login"] event["changes"] = event.get("changes", []) for change in event["changes"]: @@ -78,7 +78,7 @@ def on_stomp_message(self, body, headers): # it. product_name = bug["product"]["name"] if product_name not in self._allowed_products: - LOGGER.debug("DROP: %r not in %r" % (product_name, self._allowed_products)) + LOGGER.debug("DROP: %r not in %r", product_name, self._allowed_products) return body["timestamp"] = datetime.datetime.fromtimestamp( @@ -103,20 +103,18 @@ def on_stomp_message(self, body, headers): ) body.update(objdict) - LOGGER.debug("Republishing #%s" % bug["id"]) + LOGGER.debug("Republishing #%s", bug["id"]) messageclass = MessageV1 if self._bz4_compat_mode: messageclass = MessageV1BZ4 try: message = messageclass( - topic="bugzilla.{}".format(topic), + topic=f"bugzilla.{topic}", body=body, severity=INFO, ) publish(message) except PublishReturned as e: - LOGGER.warning( - "Fedora Messaging broker rejected message {}: {}".format(message.id, e) - ) + LOGGER.warning(f"Fedora Messaging broker rejected message {message.id}: {e}") except ConnectionException as e: - LOGGER.warning("Error sending message {}: {}".format(message.id, e)) + LOGGER.warning(f"Error sending message {message.id}: {e}") diff --git a/tests/conftest.py b/tests/conftest.py index 6fe4176..e57cd12 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ Test fixtures for bugzilla2fedmsg.relay. Authors: Adam Williamson diff --git a/tests/test_consumer.py b/tests/test_consumer.py index ed3161d..f8b52da 100644 --- a/tests/test_consumer.py +++ b/tests/test_consumer.py @@ -95,7 +95,7 @@ def test_connect_twice(consumer, connected_frame, message_frame): try: consumer.consume() except StompConnectionError as e: - assert False, "Must not fail when already connected: {}".format(e) + pytest.fail(f"Must not fail when already connected: {e}") def test_subscribe_twice(consumer, connected_frame, message_frame): @@ -114,4 +114,4 @@ def test_subscribe_twice(consumer, connected_frame, message_frame): try: consumer.consume() except StompProtocolError as e: - assert False, "Must not fail when already subscribed: {}".format(e) + pytest.fail(f"Must not fail when already subscribed: {e}") diff --git a/tests/test_relay.py b/tests/test_relay.py index ca328d3..d2415e3 100644 --- a/tests/test_relay.py +++ b/tests/test_relay.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ Tests for bugzilla2fedmsg.relay. Authors: Adam Williamson @@ -12,7 +11,7 @@ import bugzilla2fedmsg.relay -class TestRelay(object): +class TestRelay: relay = bugzilla2fedmsg.relay.MessageRelay( {"bugzilla": {"products": ["Fedora", "Fedora EPEL"]}} ) @@ -20,9 +19,7 @@ class TestRelay(object): @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) def test_bug_create(self, fakepublish, bug_create_message): """Check correct result for bug.create message.""" - self.relay.on_stomp_message( - bug_create_message["body"], bug_create_message["headers"] - ) + self.relay.on_stomp_message(bug_create_message["body"], bug_create_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] assert message.topic == "bugzilla.bug.new" @@ -35,9 +32,7 @@ def test_bug_create(self, fakepublish, bug_create_message): @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) def test_bug_modify(self, fakepublish, bug_modify_message): """Check correct result for bug.modify message.""" - self.relay.on_stomp_message( - bug_modify_message["body"], bug_modify_message["headers"] - ) + self.relay.on_stomp_message(bug_modify_message["body"], bug_modify_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] assert message.topic == "bugzilla.bug.update" @@ -128,9 +123,7 @@ def test_other_product_drop(self, fakepublish, other_product_message): config, the products we care about are the defaults: 'Fedora' and 'Fedora EPEL'. """ - self.relay.on_stomp_message( - other_product_message["body"], other_product_message["headers"] - ) + self.relay.on_stomp_message(other_product_message["body"], other_product_message["headers"]) assert fakepublish.call_count == 0 @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) @@ -142,9 +135,7 @@ def test_bz4_compat(self, fakepublish, bug_create_message, comment_create_messag bz4relay = bugzilla2fedmsg.relay.MessageRelay( {"bugzilla": {"products": ["Fedora", "Fedora EPEL"], "bz4compat": True}} ) - bz4relay.on_stomp_message( - bug_create_message["body"], bug_create_message["headers"] - ) + bz4relay.on_stomp_message(bug_create_message["body"], bug_create_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] assert message.body["bug"]["assigned_to"] == "lvrabec@redhat.com" @@ -155,8 +146,7 @@ def test_bz4_compat(self, fakepublish, bug_create_message, comment_create_messag assert message.body["bug"]["op_sys"] == "Unspecified" assert message.body["event"]["who"] == "dgunchev@gmail.com" assert ( - message.body["bug"]["weburl"] - == "https://bugzilla.redhat.com/show_bug.cgi?id=1701391" + message.body["bug"]["weburl"] == "https://bugzilla.redhat.com/show_bug.cgi?id=1701391" ) # we need a comment message to test this fakepublish.reset_mock() @@ -168,9 +158,7 @@ def test_bz4_compat(self, fakepublish, bug_create_message, comment_create_messag assert message.body["comment"]["author"] == "smooge@redhat.com" @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) - def test_bz4_compat_disabled( - self, fakepublish, bug_create_message, comment_create_message - ): + def test_bz4_compat_disabled(self, fakepublish, bug_create_message, comment_create_message): """Test that we *don't* make Bugzilla 4 compat modifications if the option is switched off. Tests only the destructive ones as they're the ones we really want to avoid, and if these @@ -179,49 +167,36 @@ def test_bz4_compat_disabled( nobz4relay = bugzilla2fedmsg.relay.MessageRelay( {"bugzilla": {"products": ["Fedora", "Fedora EPEL"], "bz4compat": False}} ) - nobz4relay.on_stomp_message( - bug_create_message["body"], bug_create_message["headers"] - ) + nobz4relay.on_stomp_message(bug_create_message["body"], bug_create_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] assert all( - item in message.body["bug"]["assigned_to"] - for item in ("login", "id", "real_name") + item in message.body["bug"]["assigned_to"] for item in ("login", "id", "real_name") ) assert all(item in message.body["bug"]["component"] for item in ("id", "name")) assert all(item in message.body["bug"]["product"] for item in ("id", "name")) @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) - def test_publish_exception_publishreturned( - self, fakepublish, bug_create_message, caplog - ): + def test_publish_exception_publishreturned(self, fakepublish, bug_create_message, caplog): """Check that we handle PublishReturned exception from publish correctly. """ fakepublish.side_effect = fedora_messaging.exceptions.PublishReturned("oops!") # this should not raise any exception - self.relay.on_stomp_message( - bug_create_message["body"], bug_create_message["headers"] - ) + self.relay.on_stomp_message(bug_create_message["body"], bug_create_message["headers"]) assert fakepublish.call_count == 1 # check the logging worked assert "Fedora Messaging broker rejected message" in caplog.text @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) - def test_publish_exception_connectionexception( - self, fakepublish, bug_create_message, caplog - ): + def test_publish_exception_connectionexception(self, fakepublish, bug_create_message, caplog): """Check that we handle ConnectionException from publish correctly. """ # First test PublishReturned - fakepublish.side_effect = fedora_messaging.exceptions.ConnectionException( - "oops!" - ) + fakepublish.side_effect = fedora_messaging.exceptions.ConnectionException("oops!") # this should not raise any exception - self.relay.on_stomp_message( - bug_create_message["body"], bug_create_message["headers"] - ) + self.relay.on_stomp_message(bug_create_message["body"], bug_create_message["headers"]) assert fakepublish.call_count == 1 # check the logging worked assert "Error sending message" in caplog.text diff --git a/tests/test_schemas.py b/tests/test_schemas.py index 68420a1..8194b5a 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ Tests for bugzilla2fedmsg_schemas. Authors: Adam Williamson @@ -13,7 +12,7 @@ import bugzilla2fedmsg.relay -class TestSchemas(object): +class TestSchemas: # We are basically going to use the relays to construct messages # just as we do in test_relay, then check the messages validate # and test the various schema methods. We parametrize the tests @@ -29,9 +28,7 @@ class TestSchemas(object): @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) def test_bug_create_schema(self, fakepublish, bug_create_message, relay): """Check bug.create message schema bits.""" - relay.on_stomp_message( - bug_create_message["body"], bug_create_message["headers"] - ) + relay.on_stomp_message(bug_create_message["body"], bug_create_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] # this should not raise an exception @@ -68,9 +65,7 @@ def test_bug_create_schema(self, fakepublish, bug_create_message, relay): @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) def test_bug_modify_schema(self, fakepublish, bug_modify_message, relay): """Check bug.modify message schema bits.""" - relay.on_stomp_message( - bug_modify_message["body"], bug_modify_message["headers"] - ) + relay.on_stomp_message(bug_modify_message["body"], bug_modify_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] # this should not raise an exception @@ -152,9 +147,7 @@ def test_bug_modify_no_changes_schema(self, fakepublish, bug_modify_message, rel # wipe the 'changes' dict from the sample message, to simulate # one of these broken messages del bug_modify_message["body"]["event"]["changes"] - relay.on_stomp_message( - bug_modify_message["body"], bug_modify_message["headers"] - ) + relay.on_stomp_message(bug_modify_message["body"], bug_modify_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] # this should not raise an exception @@ -172,9 +165,7 @@ def test_bug_modify_no_changes_schema(self, fakepublish, bug_modify_message, rel @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) def test_comment_create_schema(self, fakepublish, comment_create_message, relay): """Check comment.create message schema bits.""" - relay.on_stomp_message( - comment_create_message["body"], comment_create_message["headers"] - ) + relay.on_stomp_message(comment_create_message["body"], comment_create_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] # this should not raise an exception @@ -186,9 +177,7 @@ def test_comment_create_schema(self, fakepublish, comment_create_message, relay) @pytest.mark.parametrize("relay", (bz4relay, nobz4relay)) @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) - def test_attachment_create_schema( - self, fakepublish, attachment_create_message, relay - ): + def test_attachment_create_schema(self, fakepublish, attachment_create_message, relay): """Check attachment.create message schema bits.""" relay.on_stomp_message( attachment_create_message["body"], attachment_create_message["headers"] @@ -204,9 +193,7 @@ def test_attachment_create_schema( @pytest.mark.parametrize("relay", (bz4relay, nobz4relay)) @mock.patch("bugzilla2fedmsg.relay.publish", autospec=True) - def test_attachment_modify_schema( - self, fakepublish, attachment_modify_message, relay - ): + def test_attachment_modify_schema(self, fakepublish, attachment_modify_message, relay): """Check attachment.modify message schema bits.""" relay.on_stomp_message( attachment_modify_message["body"], attachment_modify_message["headers"] @@ -252,9 +239,7 @@ def test_component_not_package_schema(self, fakepublish, bug_create_message, rel # adjust the component in the sample message to one we should # filter out bug_create_message["body"]["bug"]["component"]["name"] = "distribution" - relay.on_stomp_message( - bug_create_message["body"], bug_create_message["headers"] - ) + relay.on_stomp_message(bug_create_message["body"], bug_create_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] # this should not raise an exception @@ -266,14 +251,12 @@ def test_component_not_package_schema(self, fakepublish, bug_create_message, rel def test_bug_no_qa_contact(self, fakepublish, bug_create_message, relay): """Check bug.create message schema bits when qa_contact is None.""" bug_create_message["body"]["bug"]["qa_contact"] = None - relay.on_stomp_message( - bug_create_message["body"], bug_create_message["headers"] - ) + relay.on_stomp_message(bug_create_message["body"], bug_create_message["headers"]) assert fakepublish.call_count == 1 message = fakepublish.call_args[0][0] # this should not raise an exception try: message.validate() except ValidationError as e: - assert False, e + pytest.fail(e) assert message.bug["qa_contact"] is None