|
| 1 | +from typing import Sequence |
| 2 | + |
| 3 | +from django.db import router, transaction |
| 4 | + |
| 5 | +from sentry.models.organization import Organization |
| 6 | +from sentry.models.project import Project |
| 7 | +from sentry.notifications.models.notificationaction import ActionTarget |
| 8 | +from sentry.notifications.types import FallthroughChoiceType |
| 9 | +from sentry.workflow_engine.defaults.detectors import _ensure_detector |
| 10 | +from sentry.workflow_engine.models import ( |
| 11 | + Action, |
| 12 | + DataCondition, |
| 13 | + DataConditionGroup, |
| 14 | + DataConditionGroupAction, |
| 15 | + DetectorWorkflow, |
| 16 | + Workflow, |
| 17 | + WorkflowDataConditionGroup, |
| 18 | +) |
| 19 | +from sentry.workflow_engine.models.data_condition import Condition |
| 20 | +from sentry.workflow_engine.typings.grouptype import IssueStreamGroupType |
| 21 | + |
| 22 | +DEFAULT_WORKFLOW_LABEL = "Send a notification for high priority issues" |
| 23 | + |
| 24 | + |
| 25 | +def connect_workflows_to_issue_stream( |
| 26 | + project: Project, |
| 27 | + workflows: list[Workflow], |
| 28 | +) -> Sequence[DetectorWorkflow]: |
| 29 | + # Because we don't know if this signal is handled already or not... |
| 30 | + issue_stream_detector = _ensure_detector(project, IssueStreamGroupType.slug) |
| 31 | + |
| 32 | + connections = [ |
| 33 | + DetectorWorkflow( |
| 34 | + workflow=workflow, |
| 35 | + detector=issue_stream_detector, |
| 36 | + ) |
| 37 | + for workflow in workflows |
| 38 | + ] |
| 39 | + return DetectorWorkflow.objects.bulk_create( |
| 40 | + connections, |
| 41 | + ignore_conflicts=True, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def create_priority_workflow(org: Organization) -> Workflow: |
| 46 | + existing = Workflow.objects.filter(organization=org, name=DEFAULT_WORKFLOW_LABEL).first() |
| 47 | + if existing: |
| 48 | + return existing |
| 49 | + |
| 50 | + with transaction.atomic(router.db_for_write(Workflow)): |
| 51 | + when_condition_group = DataConditionGroup.objects.create( |
| 52 | + logic_type=DataConditionGroup.Type.ANY_SHORT_CIRCUIT, |
| 53 | + organization=org, |
| 54 | + ) |
| 55 | + |
| 56 | + workflow = Workflow.objects.create( |
| 57 | + organization=org, |
| 58 | + name=DEFAULT_WORKFLOW_LABEL, |
| 59 | + when_condition_group=when_condition_group, |
| 60 | + config={"frequency": 0}, |
| 61 | + ) |
| 62 | + |
| 63 | + # Create the workflow trigger conditions |
| 64 | + conditions: list[DataCondition] = [] |
| 65 | + conditions.append( |
| 66 | + DataCondition( |
| 67 | + type=Condition.NEW_HIGH_PRIORITY_ISSUE, |
| 68 | + condition_group=workflow.when_condition_group, |
| 69 | + comparison=True, |
| 70 | + condition_result=True, |
| 71 | + ) |
| 72 | + ) |
| 73 | + conditions.append( |
| 74 | + DataCondition( |
| 75 | + type=Condition.EXISTING_HIGH_PRIORITY_ISSUE, |
| 76 | + condition_group=workflow.when_condition_group, |
| 77 | + comparison=True, |
| 78 | + condition_result=True, |
| 79 | + ) |
| 80 | + ) |
| 81 | + DataCondition.objects.bulk_create(conditions) |
| 82 | + |
| 83 | + # Create the Action |
| 84 | + action_filter = DataConditionGroup.objects.create( |
| 85 | + logic_type=DataConditionGroup.Type.ANY_SHORT_CIRCUIT, |
| 86 | + organization=org, |
| 87 | + ) |
| 88 | + |
| 89 | + action = Action.objects.create( |
| 90 | + type=Action.Type.EMAIL, |
| 91 | + config={ |
| 92 | + "target_type": ActionTarget.ISSUE_OWNERS, |
| 93 | + "target_identifier": None, |
| 94 | + }, |
| 95 | + data={ |
| 96 | + "fallthrough_type": FallthroughChoiceType.ACTIVE_MEMBERS.value, |
| 97 | + }, |
| 98 | + ) |
| 99 | + DataConditionGroupAction.objects.create( |
| 100 | + action=action, |
| 101 | + condition_group=action_filter, |
| 102 | + ) |
| 103 | + |
| 104 | + WorkflowDataConditionGroup.objects.create( |
| 105 | + workflow=workflow, |
| 106 | + condition_group=action_filter, |
| 107 | + ) |
| 108 | + |
| 109 | + return workflow |
| 110 | + |
| 111 | + |
| 112 | +def ensure_default_workflows(project: Project) -> list[Workflow]: |
| 113 | + workflows = [create_priority_workflow(project.organization)] |
| 114 | + connect_workflows_to_issue_stream(project, workflows) |
| 115 | + |
| 116 | + return workflows |
0 commit comments