flagsmith
flagsmith copied to clipboard
feat: trigger-webhook-events-on-commiting-change-requests
Thanks for submitting a PR! Please check the boxes below:
- [ ] I have added information to
docs/if required so people know about the feature! - [x] I have filled in the "Changes" section below?
- [x] I have filled in the "How did you test this code" section below?
- [x] I have used a Conventional Commit title for this Pull Request
Changes
Solves #2064
Webhook events for FLAG_UPDATED were not triggered when committing a Change Request. This PR resolves that issue.
There are two relevant cases:
Feature versioning v2:
Webhooks were intentionally skipped when environment_feature_version_id is present (cf in test_webhooks_are_not_called_for_feature_state_with_environment_feature_version)
Uncommitted Change Requests (this PR's focus):
When creating a Change Request, associated FeatureState objects are created immediately. At this point, post_save triggers (now suppressed) were sending a half empty event while the state was not live. However, when the Change Request is committed, the associated feature states are bulk updated—bypassing signals—and no webhook is triggered.
I intentionally avoided relying on the post_save signal during commit because Change Request FeatureStates are new records without version when initially created. Their history reflect the draft state, meaning enabled matches the future committed values. This leads to new_state == old_state in the webhook payload. So resolving FeatureState based on the previous live version ensure that we pass in the correct diff
Fix: On commit, we now fetch the latest live FeatureState (from DB, not history) and explicitly call trigger_feature_state_change_webhooks() with both the new and previous live state
How did you test this code?
- Added tests
- Built a local webhook server