Skip to content

Commit bdccc5a

Browse files
committed
refactor!: fail fast with invalid setting
The happy-path behavior for the CODE_OWNER_MAPPINGS is untouched. This change now fails fast on misconfigurations. BREAKING CHANGE: Since edX.org is relying heavily on the code_owner custom attributes, we will now fail fast, rather than just logging an error, when CODE_OWNER_MAPPINGS is misconfigured.
1 parent c022565 commit bdccc5a

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Added
2626

2727
* Added new ``code_owner_theme`` and ``code_owner_squad`` custom attributes. This is useful in cases where the ``code_owner`` combines a theme and squad name, because monitoring can instead reference ``code_owner_squad`` to be resilient to theme name updates. For the decision doc, see edx_django_utils/monitoring/docs/decisions/0004-code-owner-theme-and-squad.rst.
2828

29+
Updated
30+
-------
31+
32+
* Misconfigurations of CODE_OWNER_MAPPINGS will now fail fast, rather than just logging. Although technically a breaking change, if CODE_OWNER_MAPPINGS is in use, it is probably correctly configured and this change should be a no-op.
33+
2934
[3.16.0] - 2021-03-24
3035
---------------------
3136

edx_django_utils/monitoring/internal/code_owner/utils.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_code_owner_from_module(module):
2929
return None
3030

3131
code_owner_mappings = get_code_owner_mappings()
32-
if code_owner_mappings is None:
32+
if not code_owner_mappings:
3333
return None
3434

3535
module_parts = module.split('.')
@@ -88,9 +88,7 @@ def get_code_owner_mappings():
8888
# .. setting_description: Used for monitoring and reporting of ownership. Use a
8989
# dict with keys of code owner name and value as a list of dotted path
9090
# module names owned by the code owner.
91-
code_owner_mappings = getattr(settings, 'CODE_OWNER_MAPPINGS', None)
92-
if code_owner_mappings is None:
93-
return None
91+
code_owner_mappings = getattr(settings, 'CODE_OWNER_MAPPINGS', {})
9492

9593
try:
9694
for code_owner in code_owner_mappings:
@@ -102,10 +100,9 @@ def get_code_owner_mappings():
102100
if optional_module_prefix_match:
103101
path_without_prefix = path[optional_module_prefix_match.end():]
104102
path_to_code_owner_mapping[path_without_prefix] = code_owner
105-
except Exception as e: # pylint: disable=broad-except
106-
# will remove broad exceptions after ensuring all proper cases are covered
107-
set_custom_attribute('deprecated_broad_except_get_code_owner_mappings', e.__class__)
108-
log.exception('Error processing code_owner_mappings. {}'.format(e))
103+
except TypeError as e:
104+
log.exception('Error processing CODE_OWNER_MAPPINGS. {}'.format(e))
105+
raise e
109106

110107
_PATH_TO_CODE_OWNER_MAPPINGS = path_to_code_owner_mapping
111108
return _PATH_TO_CODE_OWNER_MAPPINGS

edx_django_utils/monitoring/tests/code_owner/test_middleware.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,8 @@ def test_catch_all_with_errors(self, mock_set_custom_attribute, _):
285285
)
286286
def test_load_config_with_invalid_dict(self):
287287
request = RequestFactory().get('/test/')
288-
self.middleware(request)
289-
expected_path_module = self._REQUEST_PATH_TO_MODULE_PATH['/test/']
290-
self._assert_code_owner_custom_attributes(
291-
mock_set_custom_attribute, path_module=expected_path_module,
292-
)
288+
with self.assertRaises(TypeError):
289+
self.middleware(request)
293290

294291
def _assert_code_owner_custom_attributes(self, mock_set_custom_attribute, expected_code_owner=None,
295292
path_module=None, has_path_error=False,

edx_django_utils/monitoring/tests/code_owner/test_utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ def test_code_owner_mapping_hits_and_misses(self, module, expected_owner):
6262
@override_settings(CODE_OWNER_MAPPINGS=['invalid_setting_as_list'])
6363
@patch('edx_django_utils.monitoring.internal.code_owner.utils.log')
6464
def test_code_owner_mapping_with_invalid_dict(self, mock_logger):
65-
with self.assertRaises(AssertionError):
65+
with self.assertRaises(TypeError):
6666
get_code_owner_from_module('xblock')
67-
mock_logger.exception.assert_called_with(
68-
'Error processing code_owner_mappings.',
69-
)
67+
68+
mock_logger.exception.assert_called_with(
69+
'Error processing CODE_OWNER_MAPPINGS. list indices must be integers or slices, not str',
70+
)
7071

7172
def test_code_owner_mapping_with_no_settings(self):
7273
self.assertIsNone(get_code_owner_from_module('xblock'))

0 commit comments

Comments
 (0)