test_provider.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. from __future__ import absolute_import
  2. import responses
  3. from exam import fixture
  4. from sentry.utils.compat.mock import patch
  5. from social_auth.models import UserSocialAuth
  6. from sentry.models import Integration, OrganizationIntegration, Repository
  7. from sentry.testutils import PluginTestCase
  8. from sentry.utils import json
  9. from sentry_plugins.github.client import GitHubClient, GitHubAppsClient
  10. from sentry_plugins.github.plugin import GitHubAppsRepositoryProvider, GitHubRepositoryProvider
  11. from sentry_plugins.github.testutils import (
  12. COMPARE_COMMITS_EXAMPLE,
  13. GET_LAST_COMMITS_EXAMPLE,
  14. INTSTALLATION_REPOSITORIES_API_RESPONSE,
  15. LIST_INSTALLATION_API_RESPONSE,
  16. )
  17. class GitHubPluginTest(PluginTestCase):
  18. @fixture
  19. def provider(self):
  20. return GitHubRepositoryProvider("github")
  21. def test_compare_commits(self):
  22. repo = Repository.objects.create(provider="github", name="example", organization_id=1)
  23. res = self.provider._format_commits(repo, json.loads(COMPARE_COMMITS_EXAMPLE)["commits"])
  24. assert res == [
  25. {
  26. "author_email": "support@github.com",
  27. "author_name": "Monalisa Octocat",
  28. "message": "Fix all the bugs",
  29. "id": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
  30. "repository": "example",
  31. }
  32. ]
  33. def test_get_last_commits(self):
  34. repo = Repository.objects.create(provider="github", name="example", organization_id=1)
  35. res = self.provider._format_commits(repo, json.loads(GET_LAST_COMMITS_EXAMPLE)[:10])
  36. assert res == [
  37. {
  38. "author_email": "support@github.com",
  39. "author_name": "Monalisa Octocat",
  40. "message": "Fix all the bugs",
  41. "id": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
  42. "repository": "example",
  43. }
  44. ]
  45. @responses.activate
  46. def test_create_repository(self):
  47. responses.add(
  48. responses.POST,
  49. "https://api.github.com/repos/getsentry/example-repo/hooks",
  50. json={"id": "123456", "events": ["push", "pull_request"]},
  51. )
  52. user = self.create_user()
  53. organization = self.create_organization()
  54. UserSocialAuth.objects.create(
  55. user=user, provider="github", extra_data={"access_token": "abcdefg"}
  56. )
  57. data = {"name": "getsentry/example-repo", "external_id": "654321"}
  58. data = self.provider.create_repository(organization, data, user)
  59. assert data == {
  60. "config": {
  61. "name": "getsentry/example-repo",
  62. "webhook_id": "123456",
  63. "webhook_events": ["push", "pull_request"],
  64. },
  65. "external_id": "654321",
  66. "name": "getsentry/example-repo",
  67. "url": "https://github.com/getsentry/example-repo",
  68. }
  69. request = responses.calls[-1].request
  70. req_json = json.loads(request.body)
  71. assert req_json == {
  72. "active": True,
  73. "config": {
  74. "url": "http://testserver/plugins/github/organizations/{}/webhook/".format(
  75. organization.id
  76. ),
  77. "secret": self.provider.get_webhook_secret(organization),
  78. "content_type": "json",
  79. },
  80. "name": "web",
  81. "events": ["push", "pull_request"],
  82. }
  83. @responses.activate
  84. def test_delete_repository(self):
  85. responses.add(
  86. responses.DELETE,
  87. "https://api.github.com/repos/getsentry/example-repo/hooks/123456",
  88. json={},
  89. )
  90. user = self.create_user()
  91. organization = self.create_organization()
  92. UserSocialAuth.objects.create(
  93. user=user, provider="github", extra_data={"access_token": "abcdefg"}
  94. )
  95. repo = Repository.objects.create(
  96. provider="github",
  97. name="example-repo",
  98. organization_id=organization.id,
  99. config={
  100. "name": "getsentry/example-repo",
  101. "external_id": "654321",
  102. "webhook_id": "123456",
  103. },
  104. )
  105. self.provider.delete_repository(repo, user)
  106. @responses.activate
  107. def test_update_repository_without_webhook(self):
  108. responses.add(
  109. responses.POST,
  110. "https://api.github.com/repos/getsentry/example-repo/hooks",
  111. json={"id": "123456", "events": ["push", "pull_request"]},
  112. )
  113. user = self.create_user()
  114. organization = self.create_organization()
  115. UserSocialAuth.objects.create(
  116. user=user, provider="github", extra_data={"access_token": "abcdefg"}
  117. )
  118. repo = Repository.objects.create(
  119. provider="github",
  120. name="example-repo",
  121. organization_id=organization.id,
  122. config={"name": "getsentry/example-repo", "external_id": "654321"},
  123. )
  124. self.provider.update_repository(repo, user)
  125. assert repo.config["webhook_id"] == "123456"
  126. assert repo.config["webhook_events"] == ["push", "pull_request"]
  127. @responses.activate
  128. def test_update_repository_with_webhook(self):
  129. responses.add(
  130. responses.PATCH,
  131. "https://api.github.com/repos/getsentry/example-repo/hooks/123456",
  132. json={"id": "123456", "events": ["push", "pull_request"]},
  133. )
  134. user = self.create_user()
  135. organization = self.create_organization()
  136. UserSocialAuth.objects.create(
  137. user=user, provider="github", extra_data={"access_token": "abcdefg"}
  138. )
  139. repo = Repository.objects.create(
  140. provider="github",
  141. name="example-repo",
  142. organization_id=organization.id,
  143. config={
  144. "name": "getsentry/example-repo",
  145. "external_id": "654321",
  146. "webhook_id": "123456",
  147. },
  148. )
  149. self.provider.update_repository(repo, user)
  150. assert repo.config["webhook_id"] == "123456"
  151. assert repo.config["webhook_events"] == ["push", "pull_request"]
  152. class GitHubAppsProviderTest(PluginTestCase):
  153. @fixture
  154. def provider(self):
  155. return GitHubAppsRepositoryProvider("github_apps")
  156. @patch.object(
  157. GitHubAppsClient,
  158. "get_repositories",
  159. return_value=json.loads(INTSTALLATION_REPOSITORIES_API_RESPONSE),
  160. )
  161. @patch.object(
  162. GitHubClient, "get_installations", return_value=json.loads(LIST_INSTALLATION_API_RESPONSE)
  163. )
  164. def test_link_auth(self, *args):
  165. user = self.create_user()
  166. organization = self.create_organization()
  167. UserSocialAuth.objects.create(
  168. user=user, provider="github_apps", extra_data={"access_token": "abcdefg"}
  169. )
  170. integration = Integration.objects.create(provider="github_apps", external_id="1")
  171. self.provider.link_auth(user, organization, {"integration_id": integration.id})
  172. assert OrganizationIntegration.objects.filter(
  173. organization=organization, integration=integration
  174. ).exists()
  175. def test_delete_repository(self):
  176. user = self.create_user()
  177. organization = self.create_organization()
  178. integration = Integration.objects.create(provider="github_apps", external_id="1")
  179. repo = Repository.objects.create(
  180. name="example-repo",
  181. provider="github_apps",
  182. organization_id=organization.id,
  183. integration_id=integration.id,
  184. )
  185. # just check that it doesn't throw / try to delete a webhook
  186. assert self.provider.delete_repository(repo=repo, actor=user) is None
  187. @patch.object(GitHubAppsClient, "get_last_commits", return_value=[])
  188. def test_compare_commits_no_start(self, mock_get_last_commits):
  189. organization = self.create_organization()
  190. integration = Integration.objects.create(provider="github_apps", external_id="1")
  191. repo = Repository.objects.create(
  192. name="example-repo",
  193. provider="github_apps",
  194. organization_id=organization.id,
  195. integration_id=integration.id,
  196. config={"name": "example-repo"},
  197. )
  198. self.provider.compare_commits(repo, None, "a" * 40)
  199. assert mock_get_last_commits.called
  200. @patch.object(GitHubAppsClient, "compare_commits", return_value={"commits": []})
  201. def test_compare_commits(self, mock_compare_commits):
  202. organization = self.create_organization()
  203. integration = Integration.objects.create(provider="github_apps", external_id="1")
  204. repo = Repository.objects.create(
  205. name="example-repo",
  206. provider="github_apps",
  207. organization_id=organization.id,
  208. integration_id=integration.id,
  209. config={"name": "example-repo"},
  210. )
  211. self.provider.compare_commits(repo, "b" * 40, "a" * 40)
  212. assert mock_compare_commits.called