From 830597d7e1a1d5bc93d5a486386e1b38e0e9ebf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cleto=20Mart=C3=ADn?= Date: Tue, 14 Feb 2023 11:40:32 +0100 Subject: [PATCH] Fix notifier counts for single checks (#152) For those checks that only have one check_ method, the current code didn't show up correctly the number of errors/warnings correctly. --- CHANGES.md | 4 ++++ compliance/__init__.py | 2 +- compliance/notify.py | 22 +++++-------------- demo/demo_examples/checks/test_github.py | 2 +- doc-source/notifiers.rst | 4 ++-- test/t_compliance/t_controls/test_controls.py | 2 +- 6 files changed, 15 insertions(+), 21 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 8c1218d0..d08c3261 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +# [1.24.1](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/v1.24.1) + +- [FIXED] Number of errors/warnings shown correctly for single checks. + # [1.24.0](https://github.com/ComplianceAsCode/auditree-framework/releases/tag/v1.24.0) - [FIXED] Update pre-commit dependencies. diff --git a/compliance/__init__.py b/compliance/__init__.py index 13d248c1..cf2d7688 100644 --- a/compliance/__init__.py +++ b/compliance/__init__.py @@ -13,4 +13,4 @@ # limitations under the License. """Compliance automation package.""" -__version__ = "1.24.0" +__version__ = "1.24.1" diff --git a/compliance/notify.py b/compliance/notify.py index 7aa57fb6..99ecc86e 100644 --- a/compliance/notify.py +++ b/compliance/notify.py @@ -66,12 +66,10 @@ def messages(self): test_obj = test_desc["test"].test method_name = parse_test_id(test_id)["method"] - msg_method = "get_notification_message" - if len(test_obj.tests) > 1: - candidate = method_name.replace("test_", "msg_", 1) - if hasattr(test_obj, candidate): - msg_method = candidate - + msg_method = "get_notification_message" # default notification method + candidate = method_name.replace("test_", "msg_", 1) + if hasattr(test_obj, candidate): + msg_method = candidate # set body to None if the notification function hasn't been # defined or if it returns None. # use a predefined error message for error status. @@ -96,19 +94,11 @@ def messages(self): if msg and "subtitle" in msg and msg["subtitle"]: title += f' - {msg["subtitle"]}' - failure_count = 0 - if msg and test_obj.failures: - failure_count = test_obj.failures_count() - - warning_count = 0 - if msg and test_obj.warnings: - warning_count = test_obj.warnings_count() - msg = { "title": title, "body": body, - "failure_count": failure_count, - "warning_count": warning_count, + "failure_count": test_obj.failures_count(), + "warning_count": test_obj.warnings_count(), } yield test_id, test_desc, msg diff --git a/demo/demo_examples/checks/test_github.py b/demo/demo_examples/checks/test_github.py index 3c0f2378..da074963 100644 --- a/demo/demo_examples/checks/test_github.py +++ b/demo/demo_examples/checks/test_github.py @@ -100,7 +100,7 @@ def test_members_is_not_empty(self, org): if not members: self.add_failures(org, "There is nobody!") elif len(members) < 5: - self.add_warnings(org, "There are people int there, but less than 5!") + self.add_warnings(org, "There are people in there, but less than 5!") def get_reports(self): """Return GitHub report name.""" diff --git a/doc-source/notifiers.rst b/doc-source/notifiers.rst index 71188fce..28bc8711 100644 --- a/doc-source/notifiers.rst +++ b/doc-source/notifiers.rst @@ -14,8 +14,8 @@ system is that each ``test_`` can generate a short notification that has the following components: **NOTE:** When configuring notifiers, you should be aware of the - possibilitythat notifications may contain sensitive information that can be - sent to less trusted stores like Slack or public git issue trackers. So be + possibility that notifications may contain sensitive information that can be + sent to less trusted stores like Slack or public git issue trackers. So be mindful of check notification content as well as the nature of the forum you intend to send these notifications to. diff --git a/test/t_compliance/t_controls/test_controls.py b/test/t_compliance/t_controls/test_controls.py index 3026c422..9395c636 100644 --- a/test/t_compliance/t_controls/test_controls.py +++ b/test/t_compliance/t_controls/test_controls.py @@ -57,7 +57,7 @@ def test_as_dict_immutability(self): """Ensure that control content cannot be changed through as_dict.""" with self.assertRaises(AttributeError) as ar: self.cd.as_dict = {"foo": "bar"} - self.assertEqual(str(ar.exception), "can't set attribute") + self.assertTrue(str(ar.exception).startswith("can't set attribute")) controls_copy = self.cd.as_dict self.assertEqual(controls_copy, self.cd.as_dict) controls_copy.update({"foo": "bar"})