test_pull_request_event.py 4.4 KB

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