refactor: add optional argument to TodoParser to set configuration

Currently, this is just used to set identifiers
without needing to modify the environment, but this
could (should?) be extended to other options
This commit is contained in:
Robert Alonso 2024-11-11 17:52:38 +00:00
parent 5aa95f6c12
commit e42bca636e

View File

@ -24,22 +24,30 @@ class TodoParser(object):
ISSUE_URL_PATTERN = re.compile(r'(?<=Issue URL:\s).+', re.IGNORECASE)
ISSUE_NUMBER_PATTERN = re.compile(r'/issues/(\d+)', re.IGNORECASE)
def __init__(self):
def __init__(self, options=dict()):
# Determine if the issues should be escaped.
self.should_escape = os.getenv('INPUT_ESCAPE', 'true') == 'true'
# Load any custom identifiers, otherwise use the default.
# Load any custom identifiers specified by the environment,
# falling back to any specified by the constructor argument,
# otherwise using the default.
custom_identifiers = os.getenv('INPUT_IDENTIFIERS')
self.identifiers = ['TODO']
self.identifiers_dict = None
if custom_identifiers:
try:
custom_identifiers_dict = json.loads(custom_identifiers)
except json.JSONDecodeError:
print('Invalid identifiers dict, ignoring.')
else:
custom_identifiers_dict = options.get("identifiers", None)
if custom_identifiers_dict:
try:
for identifier_dict in custom_identifiers_dict:
if type(identifier_dict['name']) is not str or type(identifier_dict['labels']) is not list:
raise TypeError
self.identifiers = [identifier['name'] for identifier in custom_identifiers_dict]
self.identifiers_dict = custom_identifiers_dict
except (json.JSONDecodeError, KeyError, TypeError):
except (KeyError, TypeError):
print('Invalid identifiers dict, ignoring.')
self.languages_dict = None