utils.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import hashlib
  2. from typing import List, Optional, Union
  3. from apps.issue_events.models import IssueEventType
  4. from .schema import EventMessage
  5. def default_hash_input(title: str, culprit: str, type: IssueEventType) -> str:
  6. return title + culprit + str(type)
  7. def generate_hash(
  8. title: str, culprit: str, type: IssueEventType, extra: Optional[List[str]] = None
  9. ) -> str:
  10. """Generate insecure hash used for grouping issues"""
  11. if extra:
  12. hash_input = "".join(
  13. [
  14. default_hash_input(title, culprit, type)
  15. if part == "{{ default }}"
  16. else part
  17. for part in extra
  18. ]
  19. )
  20. else:
  21. hash_input = default_hash_input(title, culprit, type)
  22. return hashlib.md5(hash_input.encode()).hexdigest()
  23. def transform_parameterized_message(message: Union[str, EventMessage]) -> str:
  24. """
  25. Accept str or Event Message interface
  26. Returns formatted string with interpolation
  27. Both examples would return "Hello there":
  28. {
  29. "message": "Hello %s",
  30. "params": ["there"]
  31. }
  32. {
  33. "message": "Hello {foo}",
  34. "params": {"foo": "there"}
  35. }
  36. """
  37. if isinstance(message, str):
  38. return message
  39. if not message.formatted and message.message:
  40. params = message.params
  41. if isinstance(params, list):
  42. return message.message % tuple(params)
  43. elif isinstance(params, dict):
  44. return message.message.format(**params)
  45. return message.formatted