Flagsmith / Flagsmith/flagsmith
Centralize feature_state webhooks under audit logs
- Dominant language
- Python
- Stars
- 6.6k
- Forks
- 567
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 121
Description
## Context
Related #2064 #2063
Refactor feature change audit log into a proper audit log based integration ?
Currently, webhook dispatching for feature state changes happens through different paths depending on whether the environment uses feature versioning.
**Non versioned environments**
- `post_save` signal on FeatureState
- Dispatches `trigger_feature_state_change_webhook_signals` skipping manually versioned environments
- Builds event using a historical record
- Audit log created separately via `create_audit_log_from_historical_record` with `related_object_type=FEATURE_STATE`
**Versioned environments**
- `EnvironmentFeatureVersion.publish()` sends `environment_feature_version_published`
- Dispatches both:
- `create_environment_feature_version_published_audit_log_task`
- `trigger_update_version_webhooks`
- webhook event has a different model
- Audit log created with `related_object_type=EF_VERSION`
Additionally, organisation level webhooks for audit logs are called separately and directly from a `post_save` signal
```
@receiver(post_save, sender=AuditLog)
def call_webhooks(sender, instance, **kwargs):
```
To be discussed how relevant it would be to tighten up the 2 types of webhooks.
## Goal
Refactor the flow so that environment-level webhook events for feature state changes are dispatched from the creation of an AuditLog, similar to how organization-level integrations work today.
In other words, have the AuditLog being the entrypoint of further business logic in relation to a feature_state change.
## Proposed Flow
Introducing a centralized `post_save` signal on `AuditLog` tracking only `RelatedObjectType.FEATURE_STATE/EF_VERSION` that would in turn emits the signal `feature_state_change_went_live`, extending what has been done [here](https://github.com/Flagsmith/flagsmith/commit/4d415a10cf3e1b171dca9f9b982dd1c3c09f3e01)
Webhook logic would be centralized for both versioned and non-versioned environment, uniformizing event payload via dedicated mappers:
- `EFV Mapper` → builds payload from previously live version
- `FS Mapper` → builds payload using historical diffs
## Benefits
- Centralizes webhook dispatching logic for feature state changes under the AuditLog system
- Unifies EFV and legacy feature states flows while prioritizing business logic execution over technical grounds
- Reduces risks of duplication
## Considerations
Change Requests and scheduling must be accounted for to avoid duplicate webhook execution
Contributor guide
Assessment
This issue has not been assessed yet.