Skip to content

Commit 794769d

Browse files
committed
fix type issue and make sure we dont have audit log if unchanged+
1 parent d41bc9d commit 794769d

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

src/sentry/api/serializers/models/organization.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,13 @@ def serialize( # type: ignore[override]
740740
.first()
741741
)
742742
context["hasGranularReplayPermissions"] = bool(permissions_enabled)
743-
context["replayAccessMembers"] = list(
744-
OrganizationMemberReplayAccess.objects.filter(
743+
context["replayAccessMembers"] = [
744+
user_id
745+
for user_id in OrganizationMemberReplayAccess.objects.filter(
745746
organizationmember__organization=obj
746747
).values_list("organizationmember__user_id", flat=True)
747-
)
748+
if user_id is not None
749+
]
748750

749751
if has_custom_dynamic_sampling(obj, actor=user):
750752
context["targetSampleRate"] = float(

src/sentry/core/endpoints/organization_details.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,16 @@ def save(self, **kwargs):
591591
if "hasGranularReplayPermissions" in data:
592592
option_key = "sentry:granular-replay-permissions"
593593
new_value = data["hasGranularReplayPermissions"]
594-
option_inst, created = OrganizationOption.objects.update_or_create(
594+
option_inst, created = OrganizationOption.objects.get_or_create(
595595
organization=org, key=option_key, defaults={"value": new_value}
596596
)
597-
changed_data["hasGranularReplayPermissions"] = f"to {new_value}"
597+
if not created and option_inst.value != new_value:
598+
old_val = option_inst.value
599+
option_inst.value = new_value
600+
option_inst.save()
601+
changed_data["hasGranularReplayPermissions"] = f"from {old_val} to {new_value}"
602+
elif created:
603+
changed_data["hasGranularReplayPermissions"] = f"to {new_value}"
598604

599605
if "replayAccessMembers" in data:
600606
user_ids = data["replayAccessMembers"]

tests/sentry/core/endpoints/test_organization_details.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,39 @@ def test_granular_replay_permissions_flag_unset(self) -> None:
15151515

15161516
assert "to False" in log.data["hasGranularReplayPermissions"]
15171517

1518+
@with_feature("organizations:granular-replay-permissions")
1519+
def test_granular_replay_permissions_no_spurious_audit_log(self) -> None:
1520+
self.organization.update_option("sentry:granular-replay-permissions", True)
1521+
with assume_test_silo_mode_of(AuditLogEntry):
1522+
AuditLogEntry.objects.filter(organization_id=self.organization.id).delete()
1523+
1524+
data = {"hasGranularReplayPermissions": True}
1525+
with outbox_runner():
1526+
self.get_success_response(self.organization.slug, **data)
1527+
1528+
with assume_test_silo_mode_of(AuditLogEntry):
1529+
audit_logs = AuditLogEntry.objects.filter(organization_id=self.organization.id)
1530+
assert audit_logs.count() == 0
1531+
1532+
@with_feature("organizations:granular-replay-permissions")
1533+
def test_granular_replay_permissions_change_logs_old_value(self) -> None:
1534+
self.organization.update_option("sentry:granular-replay-permissions", False)
1535+
with assume_test_silo_mode_of(AuditLogEntry):
1536+
AuditLogEntry.objects.filter(organization_id=self.organization.id).delete()
1537+
1538+
data = {"hasGranularReplayPermissions": True}
1539+
with outbox_runner():
1540+
self.get_success_response(self.organization.slug, **data)
1541+
1542+
option_value = OrganizationOption.objects.get(
1543+
organization=self.organization, key="sentry:granular-replay-permissions"
1544+
)
1545+
assert option_value.value is True
1546+
1547+
with assume_test_silo_mode_of(AuditLogEntry):
1548+
log = AuditLogEntry.objects.get(organization_id=self.organization.id)
1549+
assert log.data["hasGranularReplayPermissions"] == "from False to True"
1550+
15181551
def test_granular_replay_permissions_flag_requires_feature(self) -> None:
15191552
data = {"hasGranularReplayPermissions": True}
15201553
self.get_error_response(self.organization.slug, **data, status_code=404)

0 commit comments

Comments
 (0)