Просмотр исходного кода

ref: fix types for sentry.utils.audit (#69319)

<!-- Describe your PR here. -->
anthony sottile 10 месяцев назад
Родитель
Сommit
644e8e5b4a
3 измененных файлов с 18 добавлено и 10 удалено
  1. 1 1
      pyproject.toml
  2. 13 7
      src/sentry/utils/audit.py
  3. 4 2
      tests/sentry/utils/test_audit.py

+ 1 - 1
pyproject.toml

@@ -476,7 +476,6 @@ module = [
     "sentry.tsdb.dummy",
     "sentry.tsdb.inmemory",
     "sentry.types.integrations",
-    "sentry.utils.audit",
     "sentry.utils.auth",
     "sentry.utils.committers",
     "sentry.utils.distutils.commands.base",
@@ -594,6 +593,7 @@ module = [
     "sentry.tasks.commit_context",
     "sentry.tasks.on_demand_metrics",
     "sentry.tasks.reprocessing2",
+    "sentry.utils.audit",
     "sentry.utils.email.*",
     "sentry.utils.iterators",
     "sentry.utils.locking.backends.redis",

+ 13 - 7
src/sentry/utils/audit.py

@@ -53,6 +53,17 @@ def actor_from_audit_entry(entry: AuditLogEntry) -> RpcAuditLogEntryActor:
     )
 
 
+def _org_id(org: Organization | RpcOrganization | None, org_id: int | None) -> int:
+    if org is not None and org_id is not None:
+        raise TypeError("expected organization=... or organization_id=... not both!")
+    elif org is not None:
+        return org.id
+    elif org_id is not None:
+        return org_id
+    else:
+        raise TypeError("expected organization=... or organization_id=...")
+
+
 def create_audit_entry_from_user(
     user: User | RpcUser | None,
     api_key: ApiKey | None = None,
@@ -63,9 +74,7 @@ def create_audit_entry_from_user(
     organization_id: int | None = None,
     **kwargs: Any,
 ) -> AuditLogEntry:
-    if organization:
-        assert organization_id is None
-        organization_id = organization.id
+    organization_id = _org_id(organization, organization_id)
 
     entry = AuditLogEntry(
         actor_id=user.id if user else None,
@@ -203,10 +212,7 @@ def create_system_audit_entry(
     Creates an audit log entry for events that are triggered by Sentry's
     systems and do not have an associated Sentry user as the "actor".
     """
-    if organization:
-        assert organization_id is None
-        organization_id = organization.id
-
+    organization_id = _org_id(organization, organization_id)
     entry = AuditLogEntry(actor_label="Sentry", organization_id=organization_id, **kwargs)
     if entry.event is not None:
         log_service.record_audit_log(event=entry.as_event())

+ 4 - 2
tests/sentry/utils/test_audit.py

@@ -49,7 +49,7 @@ class CreateAuditEntryTest(TestCase):
         req = fake_http_request(AnonymousUser())
         req.auth = apikey
 
-        entry = create_audit_entry(req)
+        entry = create_audit_entry(req, organization_id=org.id)
         assert entry.actor_key == apikey
         assert entry.actor is None
         assert entry.ip_address == req.META["REMOTE_ADDR"]
@@ -57,8 +57,10 @@ class CreateAuditEntryTest(TestCase):
         self.assert_no_delete_log_created()
 
     def test_audit_entry_frontend(self):
+        org = self.create_organization()
+
         req = fake_http_request(self.create_user())
-        entry = create_audit_entry(req)
+        entry = create_audit_entry(req, organization_id=org.id)
 
         assert entry.actor == req.user
         assert entry.actor_key is None