( # type: ignore[no-untyped-def]
instance: FeatureState, event_type: WebhookEventType = WebhookEventType.FLAG_UPDATED
)
| 21 | |
| 22 | |
| 23 | def trigger_feature_state_change_webhooks( # type: ignore[no-untyped-def] |
| 24 | instance: FeatureState, event_type: WebhookEventType = WebhookEventType.FLAG_UPDATED |
| 25 | ): |
| 26 | assert event_type in [WebhookEventType.FLAG_UPDATED, WebhookEventType.FLAG_DELETED] |
| 27 | |
| 28 | history_instance = instance.history.first() |
| 29 | timestamp = ( |
| 30 | history_instance.history_date.strftime(WEBHOOK_DATETIME_FORMAT) |
| 31 | if history_instance and history_instance.history_date |
| 32 | else "" |
| 33 | ) |
| 34 | changed_by = ( |
| 35 | history_instance.history_user.email |
| 36 | if history_instance and history_instance.history_user |
| 37 | else ( |
| 38 | history_instance.master_api_key.name |
| 39 | if history_instance and history_instance.master_api_key |
| 40 | else "" |
| 41 | ) |
| 42 | ) |
| 43 | new_state = ( |
| 44 | None |
| 45 | if event_type == WebhookEventType.FLAG_DELETED |
| 46 | else _get_feature_state_webhook_data(instance) |
| 47 | ) |
| 48 | data = {"new_state": new_state, "changed_by": changed_by, "timestamp": timestamp} |
| 49 | previous_state = _get_previous_state(instance, history_instance, event_type) |
| 50 | |
| 51 | if previous_state: |
| 52 | data.update(previous_state=previous_state) |
| 53 | |
| 54 | call_environment_webhooks.delay( |
| 55 | args=(instance.environment.id, data, event_type.value) # type: ignore[union-attr] |
| 56 | ) |
| 57 | |
| 58 | call_organisation_webhooks.delay( |
| 59 | args=( |
| 60 | instance.environment.project.organisation.id, # type: ignore[union-attr] |
| 61 | data, |
| 62 | event_type.value, |
| 63 | ) |
| 64 | ) |
| 65 | |
| 66 | |
| 67 | def _get_previous_state( |
searching dependent graphs…