test_pull_request_event.py 4.4 KB

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