test_pull_request_event.py 5.1 KB

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