Browse Source

fix(heroku) Fix KeyError when trying to read user data (#18046)

In addition to not failing on the `user` key missing add fallbacks
for actor.email as that comes in a lot of our hook events.

Fixes SENTRY-BRW
Mark Story 5 years ago
parent
commit
db784da6df
2 changed files with 46 additions and 1 deletions
  1. 5 1
      src/sentry_plugins/heroku/plugin.py
  2. 41 0
      tests/sentry_plugins/heroku/test_plugin.py

+ 5 - 1
src/sentry_plugins/heroku/plugin.py

@@ -17,7 +17,11 @@ logger = logging.getLogger("sentry.plugins.heroku")
 
 class HerokuReleaseHook(ReleaseHook):
     def handle(self, request):
-        email = request.POST["user"]
+        email = None
+        if "user" in request.POST:
+            email = request.POST["user"]
+        elif "actor" in request.POST:
+            email = request.POST["actor"].get("email")
         try:
             user = User.objects.get(
                 email__iexact=email, sentry_orgmember_set__organization__project=self.project

+ 41 - 0
tests/sentry_plugins/heroku/test_plugin.py

@@ -127,6 +127,47 @@ class SetRefsTest(TestCase):
 
 
 class HookHandleTest(TestCase):
+    def test_user_success(self):
+        user = self.create_user()
+        organization = self.create_organization(owner=user)
+        project = self.create_project(organization=organization)
+        hook = HerokuReleaseHook(project)
+        hook.set_refs = Mock()
+
+        req = Mock()
+        req.POST = {"head_long": "abcd123", "url": "http://example.com", "user": user.email}
+        hook.handle(req)
+        assert Release.objects.filter(version=req.POST["head_long"]).exists()
+        assert hook.set_refs.call_count == 1
+
+    def test_actor_email_success(self):
+        user = self.create_user()
+        organization = self.create_organization(owner=user)
+        project = self.create_project(organization=organization)
+        hook = HerokuReleaseHook(project)
+        hook.set_refs = Mock()
+
+        req = Mock()
+        req.POST = {
+            "head_long": "v999",
+            "url": "http://example.com",
+            "actor": {"email": user.email},
+        }
+        hook.handle(req)
+        assert Release.objects.filter(version=req.POST["head_long"]).exists()
+        assert hook.set_refs.call_count == 1
+
+    def test_email_mismatch(self):
+        user = self.create_user()
+        organization = self.create_organization(owner=user)
+        project = self.create_project(organization=organization)
+        hook = HerokuReleaseHook(project)
+
+        req = Mock()
+        req.POST = {"head_long": "v999", "url": "http://example.com", "user": "wrong@example.com"}
+        hook.handle(req)
+        assert Release.objects.filter(version=req.POST["head_long"]).exists()
+
     def test_bad_version(self):
         project = self.create_project()
         user = self.create_user()