Add audit logs for project creation and deletion#8065
Conversation
|
@srijantrpth is attempting to deploy a commit to the Flagsmith Team on Vercel. A member of the Team first needs to authorize it. |
for more information, see https://pre-commit.ci
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughProject audit contracts now include creation and deletion messages and a Estimated code review effort: 2 (Simple) | ~10 minutes Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR adds explicit audit log entries for project lifecycle events (creation and deletion) so that these actions are visible in organisation audit logs, addressing Issue #7571.
Changes:
- Create an
AuditLogrecord when a project is created via the Projects API. - Create an
AuditLogrecord when a project is deleted via the Projects API. - Add unit tests covering the new audit log behaviour.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| api/projects/views.py | Writes audit log records during project create / delete operations. |
| api/audit/constants.py | Introduces audit log message constants for project created / deleted events. |
| api/audit/related_object_type.py | Adds PROJECT as a new related object type for audit log records. |
| api/tests/unit/projects/test_unit_projects_views.py | Adds unit tests asserting audit logs are created for project create / delete. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
api/projects/views.py (1)
110-136: 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick winWrap the project lifecycle writes in a transaction.
perform_create()doesserializer.save(),UserProjectPermission.objects.create(), andAuditLog.objects.create()as separate writes, andperform_destroy()writes the audit row beforeinstance.delete(). A failure part-way through can leave the project, permissions, and audit trail out of sync.transaction.atomic()around each path keeps them consistent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 614623b9-245e-4bd2-ad27-0f0f680b392d
📒 Files selected for processing (4)
api/audit/constants.pyapi/audit/related_object_type.pyapi/projects/views.pyapi/tests/unit/projects/test_unit_projects_views.py
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
for more information, see https://pre-commit.ci
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
api/projects/views.py (1)
130-138: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick winWrap the audit insert and delete in
transaction.atomic(). In autocommit,instance.delete()can fail after the audit row is written, leaving a deletion audit for a project that still exists.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 96aedf97-5c13-461c-9d42-b2e02623c949
📒 Files selected for processing (1)
api/projects/views.py
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
api/projects/views.py (1)
117-125: 🔒 Security & Privacy | 🟠 Major | ⚡ Quick winPreserve the master API-key actor for creation events.
For master API-key requests,
authorbecomesNonebutmaster_api_keyis never set. These audit records consequently have no actor, unlike deletion events. Populatemaster_api_keyconditionally here too.Proposed fix
+ is_master_api_key_user = getattr( + self.request.user, "is_master_api_key_user", False + ) AuditLog.objects.create( project=project, - author=self.request.user - if not getattr(self.request.user, "is_master_api_key_user", False) - else None, + author=None if is_master_api_key_user else self.request.user, + master_api_key=self.request.user.key if is_master_api_key_user else None, related_object_id=project.id,
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 79932fa6-9afe-47ac-bf1e-2d9c8b6e9457
📒 Files selected for processing (1)
api/projects/views.py
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
api/projects/views.py (1)
131-141: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick winClear the project FK on deletion audit rows
AuditLog.projectis a real foreign key withon_delete=models.DO_NOTHING, soproject=instancecan makeinstance.delete()fail at the database constraint. Setproject=Noneand keeprelated_object_id=instance.idfor the historical record.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: ec641dd7-b675-442a-a2c2-2a27f9f3123e
📒 Files selected for processing (1)
api/projects/views.py
Thanks for submitting a PR! Please check the boxes below:
docs/if required so people know about the feature.Changes
Closes #7571
PROJECT_CREATED_MESSAGEandPROJECT_DELETED_MESSAGEconstants inapi/audit/constants.py.ProjectViewSet.perform_createandperform_destroyinapi/projects/views.pyto automatically record anAuditLogentry upon project creation and deletion.api/tests/unit/projects/test_unit_projects_views.pyto verify audit log generation for both actions.How did you test this code?
Ran unit tests locally using
pytestwith the test Django settings and verified that all test cases passed successfully, confirming proper audit log count, message formatting, and object relationship mapping.