/Packs/CommonScripts/Scripts/CheckFieldValue/CheckFieldValue.py

https://github.com/demisto/content · Python · 61 lines · 42 code · 18 blank · 1 comment · 16 complexity · 388979df9f6e63b0e8ce123e858d6af3 MD5 · raw file

  1. import re
  2. import demistomock as demisto
  3. from CommonServerPython import * # noqa: F401
  4. from typing import Dict, Any, Tuple
  5. def check_field(field_value, regex=None):
  6. if regex:
  7. if re.match(regex, field_value):
  8. return True
  9. else:
  10. if field_value:
  11. return True
  12. return False
  13. def poll_field(args: Dict[str, Any]) -> Tuple[str, dict, dict]:
  14. field = args.get('field')
  15. regex = args.get('regex')
  16. ignore_case = argToBoolean(args.get('ignore_case', 'False'))
  17. regex_ignore_case_flag = re.IGNORECASE if ignore_case else 0
  18. regex = re.compile(regex, regex_ignore_case_flag) if regex else None
  19. incident = demisto.incidents()[0]
  20. data = {
  21. 'field': field,
  22. 'exists': False
  23. }
  24. if field in incident:
  25. data['exists'] = check_field(incident.get(field), regex)
  26. else:
  27. custom_fields = incident.get('CustomFields', {})
  28. if field in custom_fields:
  29. data['exists'] = check_field(custom_fields.get(field), regex)
  30. context = {
  31. 'CheckFieldValue(val.field == obj.field)': data
  32. }
  33. human_readable = 'The field exists.' if data['exists'] else 'The field does not exist.'
  34. return human_readable, context, data
  35. def main():
  36. try:
  37. return_outputs(*poll_field(demisto.args()))
  38. except Exception as ex:
  39. demisto.error(traceback.format_exc()) # print the traceback
  40. return_error(f'Failed to execute CheckFieldValue script. Error: {str(ex)}')
  41. ''' ENTRY POINT '''
  42. if __name__ in ('__main__', '__builtin__', 'builtins'):
  43. main()