test_plugin.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. from __future__ import annotations
  2. from datetime import timedelta
  3. from typing import Any
  4. from unittest.mock import Mock, patch
  5. import pytest
  6. from django.utils import timezone
  7. from sentry.exceptions import HookValidationError
  8. from sentry.models.commit import Commit
  9. from sentry.models.deploy import Deploy
  10. from sentry.models.environment import Environment
  11. from sentry.models.options.project_option import ProjectOption
  12. from sentry.models.release import Release
  13. from sentry.models.releasecommit import ReleaseCommit
  14. from sentry.models.releaseheadcommit import ReleaseHeadCommit
  15. from sentry.models.repository import Repository
  16. from sentry.models.user import User
  17. from sentry.testutils.cases import TestCase
  18. from sentry.utils import json
  19. from sentry_plugins.heroku.plugin import HerokuReleaseHook
  20. class SetRefsTest(TestCase):
  21. """
  22. tests that when finish_release is called on a release hook,
  23. we try to get the previous commits based on the version ref
  24. and that we create `ReleaseHeadCommit`s for the version
  25. """
  26. @patch("sentry.tasks.commits.fetch_commits")
  27. def test_minimal(self, mock_fetch_commits):
  28. project = self.create_project()
  29. version = "bbee5b51f84611e4b14834363b8514c2"
  30. data_list = [
  31. {
  32. "id": "c7155651831549cf8a5e47889fce17eb",
  33. "message": "foo",
  34. "author_email": "jane@example.com",
  35. },
  36. {
  37. "id": "62de626b7c7cfb8e77efb4273b1a3df4123e6216",
  38. "message": "hello",
  39. "author_name": "Jess",
  40. },
  41. {
  42. "id": "58de626b7c7cfb8e77efb4273b1a3df4123e6345",
  43. "message": "bar",
  44. "author_name": "Joe^^",
  45. },
  46. {
  47. "id": "bbee5b51f84611e4b14834363b8514c2",
  48. "message": "blah",
  49. "author_email": "katie@example.com",
  50. },
  51. ]
  52. user = User.objects.create(email="stebe@sentry.io")
  53. repo = Repository.objects.create(
  54. organization_id=project.organization_id, name=project.name, provider="dummy"
  55. )
  56. ProjectOption.objects.set_value(key="heroku:repository", project=project, value=repo.name)
  57. for data in data_list:
  58. Commit.objects.create(
  59. key=data["id"], organization_id=self.project.organization_id, repository_id=repo.id
  60. )
  61. old_release = Release.objects.create(
  62. version="a" * 40,
  63. organization_id=project.organization_id,
  64. date_added=timezone.now() - timedelta(minutes=30),
  65. )
  66. old_release.add_project(project)
  67. ReleaseCommit.objects.create(
  68. organization_id=project.organization_id,
  69. project_id=project.id,
  70. release=old_release,
  71. commit=Commit.objects.get(key="c7155651831549cf8a5e47889fce17eb"),
  72. order=0,
  73. )
  74. ReleaseHeadCommit.objects.create(
  75. organization_id=project.organization_id,
  76. repository_id=repo.id,
  77. release=old_release,
  78. commit=Commit.objects.get(key="c7155651831549cf8a5e47889fce17eb"),
  79. )
  80. release_heads = ReleaseHeadCommit.objects.filter(
  81. organization_id=project.organization_id,
  82. repository_id=repo.id,
  83. commit=Commit.objects.get(key="bbee5b51f84611e4b14834363b8514c2"),
  84. )
  85. assert len(release_heads) == 0
  86. hook = HerokuReleaseHook(project)
  87. hook.finish_release(version=version, owner_id=user.id)
  88. release = Release.objects.get(projects=project, version=version)
  89. new_release_heads = ReleaseHeadCommit.objects.filter(
  90. organization_id=project.organization_id,
  91. repository_id=repo.id,
  92. release=release,
  93. commit=Commit.objects.get(key="bbee5b51f84611e4b14834363b8514c2"),
  94. )
  95. assert len(new_release_heads) == 1
  96. assert release.version == "bbee5b51f84611e4b14834363b8514c2"
  97. deploy = Deploy.objects.filter(
  98. organization_id=project.organization_id,
  99. release=release,
  100. environment_id=Environment.objects.get(
  101. organization_id=project.organization_id, name="production"
  102. ).id,
  103. )
  104. assert len(deploy) == 1
  105. mock_fetch_commits.apply_async.assert_called_with(
  106. kwargs={
  107. "release_id": release.id,
  108. "user_id": user.id,
  109. "refs": [{"commit": "bbee5b51f84611e4b14834363b8514c2", "repository": repo.name}],
  110. "prev_release_id": old_release.id,
  111. }
  112. )
  113. class HookHandleTest(TestCase):
  114. @pytest.fixture(autouse=True)
  115. def patch_is_valid_signature(self):
  116. with patch.object(HerokuReleaseHook, "is_valid_signature"):
  117. yield
  118. @patch.object(HerokuReleaseHook, "set_refs")
  119. def test_user_success(self, set_refs_mock):
  120. user = self.create_user()
  121. organization = self.create_organization(owner=user)
  122. project = self.create_project(organization=organization)
  123. hook = HerokuReleaseHook(project)
  124. req = Mock()
  125. body: dict[str, Any] = {
  126. "data": {
  127. "user": {"email": user.email},
  128. "slug": {"commit": "abcd123"},
  129. "app": {"name": "example"},
  130. },
  131. "action": "update",
  132. }
  133. req.body = bytes(json.dumps(body), "utf-8")
  134. hook.handle(req)
  135. assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
  136. assert set_refs_mock.call_count == 1
  137. @patch.object(HerokuReleaseHook, "set_refs")
  138. def test_only_run_on_update(self, set_refs_mock):
  139. user = self.create_user()
  140. organization = self.create_organization(owner=user)
  141. project = self.create_project(organization=organization)
  142. hook = HerokuReleaseHook(project)
  143. req = Mock()
  144. body: dict[str, Any] = {
  145. "data": {
  146. "user": {"email": user.email},
  147. "slug": {"commit": "abcd123"},
  148. "app": {"name": "example"},
  149. },
  150. "action": "create",
  151. }
  152. req.body = bytes(json.dumps(body), "utf-8")
  153. hook.handle(req)
  154. assert not Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
  155. assert set_refs_mock.call_count == 0
  156. @patch.object(HerokuReleaseHook, "set_refs")
  157. def test_actor_email_success(self, set_refs_mock):
  158. user = self.create_user()
  159. organization = self.create_organization(owner=user)
  160. project = self.create_project(organization=organization)
  161. hook = HerokuReleaseHook(project)
  162. req = Mock()
  163. body: dict[str, Any] = {
  164. "data": {
  165. "actor": {"email": user.email},
  166. "slug": {"commit": "abcd123"},
  167. "app": {"name": "example"},
  168. },
  169. "action": "update",
  170. }
  171. req.body = bytes(json.dumps(body), "utf-8")
  172. hook.handle(req)
  173. assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
  174. assert set_refs_mock.call_count == 1
  175. def test_email_mismatch(self):
  176. user = self.create_user()
  177. organization = self.create_organization(owner=user)
  178. project = self.create_project(organization=organization)
  179. hook = HerokuReleaseHook(project)
  180. req = Mock()
  181. body: dict[str, Any] = {
  182. "data": {
  183. "user": {"email": "wrong@example.com"},
  184. "slug": {"commit": "v999"},
  185. "app": {"name": "example"},
  186. },
  187. "action": "update",
  188. }
  189. req.body = bytes(json.dumps(body), "utf-8")
  190. hook.handle(req)
  191. assert Release.objects.filter(version=body["data"]["slug"]["commit"]).exists()
  192. def test_bad_version(self):
  193. project = self.create_project()
  194. user = self.create_user()
  195. hook = HerokuReleaseHook(project)
  196. req = Mock()
  197. body = {
  198. "data": {
  199. "actor": {"email": user.email},
  200. "slug": {"commit": ""},
  201. "app": {"name": "example"},
  202. },
  203. "action": "update",
  204. }
  205. req.body = bytes(json.dumps(body), "utf-8")
  206. with pytest.raises(HookValidationError):
  207. hook.handle(req)