test_pull_request_event.py 5.0 KB

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