test_pull_request_event.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 is not None
  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 is not None
  82. assert pr.author.name == "baxterthehacker"
  83. assert pr.author.email == "baxterthehacker@localhost"
  84. def test_closed(self):
  85. project = self.project # force creation
  86. url = f"/plugins/github/organizations/{project.organization.id}/webhook/"
  87. secret = "b3002c3e321d4b7880360d397db2ccfd"
  88. OrganizationOption.objects.set_value(
  89. organization=project.organization, key="github:webhook_secret", value=secret
  90. )
  91. repo = Repository.objects.create(
  92. organization_id=project.organization.id,
  93. external_id="35129377",
  94. provider="github_apps",
  95. name="baxterthehacker/public-repo",
  96. )
  97. response = self.client.post(
  98. path=url,
  99. data=PULL_REQUEST_CLOSED_EVENT_EXAMPLE,
  100. content_type="application/json",
  101. HTTP_X_GITHUB_EVENT="pull_request",
  102. HTTP_X_HUB_SIGNATURE="sha1=dff1c803cf1e48c1b9aefe4a17952ea132758806",
  103. HTTP_X_GITHUB_DELIVERY=str(uuid4()),
  104. )
  105. assert response.status_code == 204
  106. prs = PullRequest.objects.filter(
  107. repository_id=repo.id, organization_id=project.organization.id
  108. )
  109. assert len(prs) == 1
  110. pr = prs[0]
  111. assert pr.key == "1"
  112. assert pr.message == "new closed body"
  113. assert pr.title == "new closed title"
  114. assert pr.author is not None
  115. assert pr.author.name == "baxterthehacker"
  116. assert pr.author.email == "baxterthehacker@localhost"
  117. assert pr.merge_commit_sha == "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"