Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(audit): create audit log for deleted conditions in a segment #3577

Conversation

matthewelwell
Copy link
Contributor

Thanks for submitting a PR! Please check the boxes below:

  • I have run pre-commit to check linting
  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

This PR solves an issue noticed in customer testing which showed that audit log records are not properly created when a condition is removed from a segment. The issue was caused by the following exception in the task processor:

...
  File "/Users/matthewelwell/projects/flagsmith/flagsmith/api/segments/models.py", line 206, in _get_project
    return self.rule.get_segment().project
           ^^^^^^^^^
  File "/Users/matthewelwell/projects/flagsmith/flagsmith/.venv/lib/python3.11/site-packages/django/db/models/fields/related_descriptors.py", line 187, in __get__
    rel_obj = self.get_object(instance)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/matthewelwell/projects/flagsmith/flagsmith/.venv/lib/python3.11/site-packages/django/db/models/fields/related_descriptors.py", line 154, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/matthewelwell/projects/flagsmith/flagsmith/.venv/lib/python3.11/site-packages/django/db/models/query.py", line 435, in get
    raise self.model.DoesNotExist(
segments.models.SegmentRule.DoesNotExist: SegmentRule matching query does not exist.

To resolve it, I have simply added soft delete to both the SegmentRule and Condition models.

How did you test this code?

This is really hard to test in unit test because it's essentially a race condition. It's very easy to test when using the Task Processor, however, so I have run the application locally with the task processor and verified the issue exists before then resolving it with the code here and retesting it locally to confirm that the issue no longer exists.

@matthewelwell matthewelwell requested review from a team and gagantrivedi and removed request for a team March 7, 2024 15:58
Copy link

vercel bot commented Mar 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview Mar 7, 2024 3:59pm
flagsmith-frontend-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 7, 2024 3:59pm
flagsmith-frontend-staging ✅ Ready (Inspect) Visit Preview Mar 7, 2024 3:59pm

Copy link
Contributor

github-actions bot commented Mar 7, 2024

Uffizzi Preview deployment-48035 was deleted.

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.86%. Comparing base (0080509) to head (bbfcfa6).
Report is 23 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3577   +/-   ##
=======================================
  Coverage   95.86%   95.86%           
=======================================
  Files        1097     1098    +1     
  Lines       34141    34145    +4     
=======================================
+ Hits        32728    32732    +4     
  Misses       1413     1413           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@matthewelwell matthewelwell added this pull request to the merge queue Mar 8, 2024
Merged via the queue into main with commit 1330b4a Mar 8, 2024
23 checks passed
@matthewelwell matthewelwell deleted the fix(audit)/removing-a-segment-condition-does-not-generate-audit-log branch March 8, 2024 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issue related to the REST API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants