Browse Source

ref(sentry apps): Add analytics events to sentry app webhooks (#32385)

* feat(sentry apps): Add comment webhook analytics events
Colleen O'Rourke 3 years ago
parent
commit
5e9d93f9ea

+ 30 - 0
src/sentry/analytics/events/comment_webhooks.py

@@ -0,0 +1,30 @@
+import abc
+
+from sentry import analytics
+
+
+class CommentEvent(analytics.Event, abc.ABC):
+    attributes = (
+        analytics.Attribute("user_id", type=int, required=False),
+        analytics.Attribute("group_id", type=int),
+        analytics.Attribute("project_slug", type=str),
+        analytics.Attribute("installation_id", type=int),
+        analytics.Attribute("comment_id", type=int),
+    )
+
+
+class CommentCreatedEvent(CommentEvent):
+    type = "comment.created"
+
+
+class CommentUpdatedEvent(CommentEvent):
+    type = "comment.updated"
+
+
+class CommentDeletedEvent(CommentEvent):
+    type = "comment.deleted"
+
+
+analytics.register(CommentCreatedEvent)
+analytics.register(CommentUpdatedEvent)
+analytics.register(CommentDeletedEvent)

+ 33 - 0
src/sentry/analytics/events/sentryapp_issue_webhooks.py

@@ -0,0 +1,33 @@
+import abc
+
+from sentry import analytics
+
+
+class SentryAppIssueEvent(analytics.Event, abc.ABC):
+    attributes = (
+        analytics.Attribute("user_id", type=int, required=False),
+        analytics.Attribute("group_id", type=int),
+        analytics.Attribute("installation_id", type=int),
+    )
+
+
+class SentryAppIssueAssigned(SentryAppIssueEvent):
+    type = "sentry_app.issue.assigned"
+
+
+class SentryAppIssueCreated(SentryAppIssueEvent):
+    type = "sentry_app.issue.created"
+
+
+class SentryAppIssueIgnored(SentryAppIssueEvent):
+    type = "sentry_app.issue.ignored"
+
+
+class SentryAppIssueResolved(SentryAppIssueEvent):
+    type = "sentry_app.issue.resolved"
+
+
+analytics.register(SentryAppIssueCreated)
+analytics.register(SentryAppIssueAssigned)
+analytics.register(SentryAppIssueIgnored)
+analytics.register(SentryAppIssueResolved)

+ 19 - 2
src/sentry/tasks/sentry_apps.py

@@ -244,6 +244,12 @@ def workflow_notification(installation_id, issue_id, type, user_id, *args, **kwa
     data = kwargs.get("data", {})
     data.update({"issue": serialize(issue)})
     send_webhooks(installation=install, event=f"issue.{type}", data=data, actor=user)
+    analytics.record(
+        f"sentry_app.issue.{type}",
+        user_id=user_id,
+        group_id=issue_id,
+        installation_id=installation_id,
+    )
 
 
 @instrumented_task(name="sentry.tasks.sentry_apps.build_comment_webhook", **TASK_OPTIONS)
@@ -251,14 +257,25 @@ def workflow_notification(installation_id, issue_id, type, user_id, *args, **kwa
 def build_comment_webhook(installation_id, issue_id, type, user_id, *args, **kwargs):
     install, _, user = get_webhook_data(installation_id, issue_id, user_id)
     data = kwargs.get("data", {})
+    project_slug = data.get("project_slug")
+    comment_id = data.get("comment_id")
     payload = {
-        "comment_id": data.get("comment_id"),
+        "comment_id": comment_id,
         "group_id": issue_id,
-        "project_slug": data.get("project_slug"),
+        "project_slug": project_slug,
         "timestamp": data.get("timestamp"),
         "comment": data.get("comment"),
     }
     send_webhooks(installation=install, event=type, data=payload, actor=user)
+    # type is comment.created, comment.updated, or comment.deleted
+    analytics.record(
+        type,
+        user_id=user_id,
+        group_id=issue_id,
+        project_slug=project_slug,
+        installation_id=installation_id,
+        comment_id=comment_id,
+    )
 
 
 def get_webhook_data(installation_id, issue_id, user_id):