test_pull_request_event.py 5.1 KB

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