Skip to content

Commit 2106b3d

Browse files
committed
Publish GH App Installed event
1 parent 584e792 commit 2106b3d

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

webhook_handlers/tests/test_github.py

+78
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,84 @@ def test_installation_creates_new_owner_if_dne_default_app(self, mock_refresh):
583583
repos_affected=[("12321", "R_kgDOG2tZYQ"), ("12343", "R_kgDOG2tABC")],
584584
)
585585

586+
@patch("shared.events.amplitude.AmplitudeEventPublisher.publish")
587+
@patch("services.task.TaskService.refresh")
588+
def test_installation_publishes_amplitude_event_without_installer(
589+
self, mock_refresh, mock_amplitude_publish
590+
):
591+
username, service_id = "newuser", 123456
592+
593+
self._post_event_data(
594+
event=GitHubWebhookEvents.INSTALLATION,
595+
data={
596+
"installation": {
597+
"id": 4,
598+
"repository_selection": "selected",
599+
"account": {"id": service_id, "login": username},
600+
"app_id": DEFAULT_APP_ID,
601+
},
602+
"repositories": [
603+
{"id": "12321", "node_id": "R_kgDOG2tZYQ"},
604+
{"id": "12343", "node_id": "R_kgDOG2tABC"},
605+
],
606+
"sender": {"type": "User"},
607+
},
608+
)
609+
610+
owner_set = Owner.objects.filter(
611+
service="github", service_id=service_id, username=username
612+
)
613+
assert owner_set.exists()
614+
owner = owner_set.first()
615+
616+
mock_amplitude_publish.assert_called_with(
617+
"App Installed",
618+
{
619+
"user_ownerid": owner.ownerid,
620+
"ownerid": owner.ownerid,
621+
},
622+
)
623+
624+
@patch("shared.events.amplitude.AmplitudeEventPublisher.publish")
625+
@patch("services.task.TaskService.refresh")
626+
def test_installation_publishes_amplitude_event_with_installer(
627+
self, mock_refresh, mock_amplitude_publish
628+
):
629+
installer = OwnerFactory(service="github", username="installer_username")
630+
631+
username, service_id = "newuser", 123456
632+
633+
self._post_event_data(
634+
event=GitHubWebhookEvents.INSTALLATION,
635+
data={
636+
"installation": {
637+
"id": 4,
638+
"repository_selection": "selected",
639+
"account": {"id": service_id, "login": username},
640+
"app_id": DEFAULT_APP_ID,
641+
},
642+
"repositories": [
643+
{"id": "12321", "node_id": "R_kgDOG2tZYQ"},
644+
{"id": "12343", "node_id": "R_kgDOG2tABC"},
645+
],
646+
"sender": {"type": "User", "login": "installer_username"},
647+
},
648+
)
649+
650+
owner_set = Owner.objects.filter(
651+
service="github", service_id=service_id, username=username
652+
)
653+
assert owner_set.exists()
654+
owner = owner_set.first()
655+
656+
mock_amplitude_publish.assert_called_with(
657+
"App Installed",
658+
{
659+
"user_ownerid": installer.ownerid,
660+
"ownerid": owner.ownerid,
661+
},
662+
)
663+
586664
@patch(
587665
"services.task.TaskService.refresh",
588666
lambda self,

webhook_handlers/views/github.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from rest_framework.permissions import AllowAny
1414
from rest_framework.response import Response
1515
from rest_framework.views import APIView
16+
from shared.events.amplitude import AmplitudeEventPublisher
1617

1718
from codecov_auth.models import (
1819
GITHUB_APP_INSTALLATION_DEFAULT_NAME,
@@ -498,9 +499,34 @@ def _handle_installation_events(
498499
# GithubWebhookEvents.INSTALLTION_REPOSITORIES also execute this code
499500
# because of deprecated flow. But the GithubAppInstallation shouldn't be changed
500501
if event == GitHubWebhookEvents.INSTALLATION:
501-
ghapp_installation, _ = GithubAppInstallation.objects.get_or_create(
502-
installation_id=installation_id, owner=owner
502+
ghapp_installation, was_created = (
503+
GithubAppInstallation.objects.get_or_create(
504+
installation_id=installation_id, owner=owner
505+
)
503506
)
507+
if was_created:
508+
installer_username = request.data.get("sender", {}).get(
509+
"login", None
510+
)
511+
installer = (
512+
Owner.objects.filter(
513+
service=self.service_name,
514+
username=installer_username,
515+
).first()
516+
if installer_username
517+
else None
518+
)
519+
# If installer does not exist, just attribute the action to the org owner.
520+
AmplitudeEventPublisher().publish(
521+
"App Installed",
522+
{
523+
"user_ownerid": installer.ownerid
524+
if installer is not None
525+
else owner.ownerid,
526+
"ownerid": owner.ownerid,
527+
},
528+
)
529+
504530
app_id = request.data["installation"]["app_id"]
505531
# Either update or set
506532
# But this value shouldn't change for the installation, so doesn't matter

0 commit comments

Comments
 (0)