test_pull_request_event.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from uuid import uuid4
  2. from sentry.models import OrganizationOption, PullRequest, Repository
  3. from sentry.silo import SiloMode
  4. from sentry.testutils.cases import APITestCase
  5. from sentry.testutils.silo import assume_test_silo_mode, region_silo_test
  6. from sentry_plugins.github.testutils import (
  7. PULL_REQUEST_CLOSED_EVENT_EXAMPLE,
  8. PULL_REQUEST_EDITED_EVENT_EXAMPLE,
  9. PULL_REQUEST_OPENED_EVENT_EXAMPLE,
  10. )
  11. from social_auth.models import UserSocialAuth
  12. @region_silo_test(stable=True)
  13. class PullRequestEventWebhook(APITestCase):
  14. def test_opened(self):
  15. project = self.project # force creation
  16. user = self.create_user(email="alberto@sentry.io")
  17. with assume_test_silo_mode(SiloMode.CONTROL):
  18. UserSocialAuth.objects.create(provider="github", user=user, uid=6752317)
  19. self.create_member(organization=project.organization, user=user, role="member")
  20. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  21. secret = "b3002c3e321d4b7880360d397db2ccfd"
  22. OrganizationOption.objects.set_value(
  23. organization=project.organization, key="github:webhook_secret", value=secret
  24. )
  25. repo = Repository.objects.create(
  26. organization_id=project.organization.id,
  27. external_id="35129377",
  28. provider="github_apps",
  29. name="baxterthehacker/public-repo",
  30. )
  31. response = self.client.post(
  32. path=url,
  33. data=PULL_REQUEST_OPENED_EVENT_EXAMPLE,
  34. content_type="application/json",
  35. HTTP_X_GITHUB_EVENT="pull_request",
  36. HTTP_X_HUB_SIGNATURE="sha1=aa5b11bc52b9fac082cb59f9ee8667cb222c3aff",
  37. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  38. )
  39. assert response.status_code == 204
  40. prs = PullRequest.objects.filter(
  41. repository_id=repo.id, organization_id=project.organization.id
  42. )
  43. assert len(prs) == 1
  44. pr = prs[0]
  45. assert pr.key == "1"
  46. assert pr.message == "This is a pretty simple change that we need to pull into master."
  47. assert pr.title == "Update the README with new information"
  48. assert pr.author.name == "baxterthehacker"
  49. assert pr.author.email == "alberto@sentry.io"
  50. def test_edited(self):
  51. project = self.project # force creation
  52. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  53. secret = "b3002c3e321d4b7880360d397db2ccfd"
  54. OrganizationOption.objects.set_value(
  55. organization=project.organization, key="github:webhook_secret", value=secret
  56. )
  57. repo = Repository.objects.create(
  58. organization_id=project.organization.id,
  59. external_id="35129377",
  60. provider="github_apps",
  61. name="baxterthehacker/public-repo",
  62. )
  63. pr = PullRequest.objects.create(
  64. key="1", repository_id=repo.id, organization_id=project.organization.id
  65. )
  66. response = self.client.post(
  67. path=url,
  68. data=PULL_REQUEST_EDITED_EVENT_EXAMPLE,
  69. content_type="application/json",
  70. HTTP_X_GITHUB_EVENT="pull_request",
  71. HTTP_X_HUB_SIGNATURE="sha1=b50a13afd33b514e8e62e603827ea62530f0690e",
  72. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  73. )
  74. assert response.status_code == 204
  75. pr = PullRequest.objects.get(id=pr.id)
  76. assert pr.key == "1"
  77. assert pr.message == "new edited body"
  78. assert pr.title == "new edited title"
  79. assert pr.author.name == "baxterthehacker"
  80. assert pr.author.email == "baxterthehacker@localhost"
  81. def test_closed(self):
  82. project = self.project # force creation
  83. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  84. secret = "b3002c3e321d4b7880360d397db2ccfd"
  85. OrganizationOption.objects.set_value(
  86. organization=project.organization, key="github:webhook_secret", value=secret
  87. )
  88. repo = Repository.objects.create(
  89. organization_id=project.organization.id,
  90. external_id="35129377",
  91. provider="github_apps",
  92. name="baxterthehacker/public-repo",
  93. )
  94. response = self.client.post(
  95. path=url,
  96. data=PULL_REQUEST_CLOSED_EVENT_EXAMPLE,
  97. content_type="application/json",
  98. HTTP_X_GITHUB_EVENT="pull_request",
  99. HTTP_X_HUB_SIGNATURE="sha1=dff1c803cf1e48c1b9aefe4a17952ea132758806",
  100. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  101. )
  102. assert response.status_code == 204
  103. prs = PullRequest.objects.filter(
  104. repository_id=repo.id, organization_id=project.organization.id
  105. )
  106. assert len(prs) == 1
  107. pr = prs[0]
  108. assert pr.key == "1"
  109. assert pr.message == "new closed body"
  110. assert pr.title == "new closed title"
  111. assert pr.author.name == "baxterthehacker"
  112. assert pr.author.email == "baxterthehacker@localhost"
  113. assert pr.merge_commit_sha == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"