2020-08-22 15:50:15 +02:00
from unittest . mock import patch
2017-11-16 00:43:10 +01:00
2020-09-01 13:52:36 +02:00
import orjson
2016-10-25 14:50:42 +02:00
from zerver . lib . test_classes import WebhookTestCase
2017-11-16 00:43:10 +01:00
from zerver . lib . webhooks . git import COMMITS_LIMIT
2016-10-25 14:50:42 +02:00
2020-08-22 17:16:32 +02:00
TOPIC_REPO = " public-repo "
TOPIC_ISSUE = " public-repo / Issue #2 Spelling error in the README file "
TOPIC_PR = " public-repo / PR #1 Update the README with new information "
TOPIC_DEPLOYMENT = " public-repo / Deployment on production "
TOPIC_ORGANIZATION = " baxterandthehackers organization "
TOPIC_BRANCH = " public-repo / changes "
TOPIC_WIKI = " public-repo / Wiki Pages "
2020-01-14 22:06:24 +01:00
2016-10-25 14:50:42 +02:00
class GithubWebhookTest ( WebhookTestCase ) :
STREAM_NAME = ' github '
2016-11-14 21:06:39 +01:00
URL_TEMPLATE = " /api/v1/external/github?stream= {stream} &api_key= {api_key} "
2018-04-18 21:34:30 +02:00
FIXTURE_DIR_NAME = ' github '
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_ping_event ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " GitHub webhook has been successfully configured by TomaszKolek. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " ping " , TOPIC_REPO , expected_message )
2017-02-01 00:37:13 +01:00
2019-06-06 06:51:38 +02:00
def test_star_event ( self ) - > None :
2020-06-27 13:19:32 +02:00
expected_message = " Codertocat starred the repository [Codertocat/Hello-World](https://github.com/Codertocat/Hello-World). "
2020-04-09 21:51:58 +02:00
expected_topic = " Hello-World "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " star " , expected_topic , expected_message )
2019-06-06 06:51:38 +02:00
2017-11-04 07:47:46 +01:00
def test_ping_organization_event ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " GitHub webhook has been successfully configured by eeshangarg. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " ping__organization " , " zulip-test-org " , expected_message )
2017-07-17 04:03:54 +02:00
2017-11-04 07:47:46 +01:00
def test_push_delete_branch ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " eeshangarg [deleted](https://github.com/eeshangarg/public-repo/compare/2e8cf535fb38...000000000000) the branch feature. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__delete_branch " , " public-repo / feature " , expected_message )
2017-05-16 04:38:25 +02:00
2017-11-04 07:47:46 +01:00
def test_push_local_branch_without_commits ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " eeshangarg [pushed](https://github.com/eeshangarg/public-repo/compare/feature) the branch feature. "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" push__local_branch_without_commits " , " public-repo / feature " , expected_message
)
2017-05-17 02:19:33 +02:00
2017-11-04 07:47:46 +01:00
def test_push_1_commit ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker [pushed](https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f) 1 commit to branch changes. \n \n * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__1_commit " , TOPIC_BRANCH , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_push_1_commit_without_username ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " eeshangarg [pushed](https://github.com/eeshangarg/public-repo/compare/0383613da871...2e8cf535fb38) 1 commit to branch changes. Commits by John Snow (1). \n \n * Update the README ([2e8cf53](https://github.com/eeshangarg/public-repo/commit/2e8cf535fb38a3dab2476cdf856efda904ad4c94)) "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__1_commit_without_username " , TOPIC_BRANCH , expected_message )
2017-04-29 08:16:07 +02:00
2017-11-04 07:47:46 +01:00
def test_push_1_commit_filtered_by_branches ( self ) - > None :
2017-03-25 03:23:15 +01:00
self . url = self . build_webhook_url ( ' master,changes ' )
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker [pushed](https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f) 1 commit to branch changes. \n \n * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__1_commit " , TOPIC_BRANCH , expected_message )
2017-03-25 03:23:15 +01:00
2017-11-04 07:47:46 +01:00
def test_push_multiple_comitters ( self ) - > None :
2020-04-09 21:51:58 +02:00
commits_info = ' * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) \n '
2020-06-09 00:25:09 +02:00
expected_message = f """ baxterthehacker [pushed](https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f) 6 commits to branch changes. Commits by Tomasz (3), Ben (2) and baxterthehacker (1). \n \n { commits_info * 5 } * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) """
2017-04-05 09:12:19 +02:00
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__multiple_committers " , TOPIC_BRANCH , expected_message )
2017-04-05 09:12:19 +02:00
2017-11-04 07:47:46 +01:00
def test_push_multiple_comitters_with_others ( self ) - > None :
2020-04-09 21:51:58 +02:00
commits_info = ' * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) \n '
2020-06-09 00:25:09 +02:00
expected_message = f """ baxterthehacker [pushed](https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f) 10 commits to branch changes. Commits by Tomasz (4), Ben (3), James (2) and others (1). \n \n { commits_info * 9 } * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) """
2017-04-05 09:12:19 +02:00
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__multiple_committers_with_others " , TOPIC_BRANCH , expected_message )
2017-04-05 09:12:19 +02:00
2017-11-04 07:47:46 +01:00
def test_push_multiple_comitters_filtered_by_branches ( self ) - > None :
2017-04-21 23:35:40 +02:00
self . url = self . build_webhook_url ( ' master,changes ' )
2020-04-09 21:51:58 +02:00
commits_info = ' * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) \n '
2020-06-09 00:25:09 +02:00
expected_message = f """ baxterthehacker [pushed](https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f) 6 commits to branch changes. Commits by Tomasz (3), Ben (2) and baxterthehacker (1). \n \n { commits_info * 5 } * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) """
2017-04-21 23:35:40 +02:00
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__multiple_committers " , TOPIC_BRANCH , expected_message )
2017-04-21 23:35:40 +02:00
2017-11-04 07:47:46 +01:00
def test_push_multiple_comitters_with_others_filtered_by_branches ( self ) - > None :
2017-04-21 23:35:40 +02:00
self . url = self . build_webhook_url ( ' master,changes ' )
2020-04-09 21:51:58 +02:00
commits_info = ' * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) \n '
2020-06-09 00:25:09 +02:00
expected_message = f """ baxterthehacker [pushed](https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f) 10 commits to branch changes. Commits by Tomasz (4), Ben (3), James (2) and others (1). \n \n { commits_info * 9 } * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) """
2017-04-21 23:35:40 +02:00
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__multiple_committers_with_others " , TOPIC_BRANCH , expected_message )
2017-04-21 23:35:40 +02:00
2017-11-04 07:47:46 +01:00
def test_push_50_commits ( self ) - > None :
2017-03-15 10:15:03 +01:00
commit_info = " * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) \n "
2020-06-10 06:40:53 +02:00
expected_message = f " baxterthehacker [pushed](https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f) 50 commits to branch changes. \n \n { commit_info * COMMITS_LIMIT } [and 30 more commit(s)] "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__50_commits " , TOPIC_BRANCH , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_push_50_commits_filtered_by_branches ( self ) - > None :
2017-04-21 23:35:40 +02:00
self . url = self . build_webhook_url ( branches = ' master,changes ' )
2017-03-25 03:23:15 +01:00
commit_info = " * Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c)) \n "
2020-06-10 06:40:53 +02:00
expected_message = f " baxterthehacker [pushed](https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f) 50 commits to branch changes. \n \n { commit_info * COMMITS_LIMIT } [and 30 more commit(s)] "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__50_commits " , TOPIC_BRANCH , expected_message )
2017-03-25 03:23:15 +01:00
2017-11-04 07:47:46 +01:00
def test_commit_comment_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker [commented](https://github.com/baxterthehacker/public-repo/commit/9049f1265b7d61be4a8904a9a27120d2064dab3b#commitcomment-11056394) on [9049f12](https://github.com/baxterthehacker/public-repo/commit/9049f1265b7d61be4a8904a9a27120d2064dab3b): \n ~~~ quote \n This is a really good change! :+1: \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " commit_comment " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_create_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker created tag 0.0.1. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " create " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_delete_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker deleted tag simple-tag. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " delete " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_deployment_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker created new deployment. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " deployment " , TOPIC_DEPLOYMENT , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_deployment_status_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " Deployment changed status to success. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " deployment_status " , TOPIC_DEPLOYMENT , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_fork_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterandthehackers forked [public-repo](https://github.com/baxterandthehackers/public-repo). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " fork " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_issue_comment_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker [commented](https://github.com/baxterthehacker/public-repo/issues/2#issuecomment-99262140) on [Issue #2](https://github.com/baxterthehacker/public-repo/issues/2): \n \n ~~~ quote \n You are totally right! I ' ll get this fixed right away. \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " issue_comment " , TOPIC_ISSUE , expected_message )
2016-10-25 14:50:42 +02:00
2018-09-27 18:57:09 +02:00
def test_issue_comment_deleted_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " Scheduler / Issue #5 This is a new issue "
expected_message = " eeshangarg deleted a [comment](https://github.com/eeshangarg/Scheduler/issues/5#issuecomment-425164194) on [Issue #5](https://github.com/eeshangarg/Scheduler/issues/5): \n \n ~~~ quote \n This is a comment on this new issue. \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " issue_comment__deleted " , expected_topic , expected_message )
2018-09-27 18:57:09 +02:00
2018-07-20 21:36:18 +02:00
def test_issue_comment_msg_with_custom_topic_in_url ( self ) - > None :
self . url = self . build_webhook_url ( topic = ' notifications ' )
2020-04-09 21:51:58 +02:00
expected_topic = " notifications "
expected_message = " baxterthehacker [commented](https://github.com/baxterthehacker/public-repo/issues/2#issuecomment-99262140) on [Issue #2 Spelling error in the README file](https://github.com/baxterthehacker/public-repo/issues/2): \n \n ~~~ quote \n You are totally right! I ' ll get this fixed right away. \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " issue_comment " , expected_topic , expected_message )
2018-07-20 21:36:18 +02:00
2017-11-04 07:47:46 +01:00
def test_issue_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker opened [Issue #2](https://github.com/baxterthehacker/public-repo/issues/2): \n \n ~~~ quote \n It looks like you accidentally spelled ' commit ' with two ' t ' s. \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " issues " , TOPIC_ISSUE , expected_message )
2016-10-25 14:50:42 +02:00
2018-07-20 21:36:18 +02:00
def test_issue_msg_with_custom_topic_in_url ( self ) - > None :
self . url = self . build_webhook_url ( topic = ' notifications ' )
2020-04-09 21:51:58 +02:00
expected_topic = " notifications "
expected_message = " baxterthehacker opened [Issue #2 Spelling error in the README file](https://github.com/baxterthehacker/public-repo/issues/2): \n \n ~~~ quote \n It looks like you accidentally spelled ' commit ' with two ' t ' s. \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " issues " , expected_topic , expected_message )
2018-07-20 21:36:18 +02:00
2017-11-04 07:47:46 +01:00
def test_membership_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker added [kdaigle](https://github.com/kdaigle) to the Contractors team. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " membership " , TOPIC_ORGANIZATION , expected_message )
2019-03-09 21:06:44 +01:00
def test_membership_removal_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker removed [kdaigle](https://github.com/kdaigle) from the Contractors team. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " membership__removal " , TOPIC_ORGANIZATION , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_member_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker added [octocat](https://github.com/octocat) to [public-repo](https://github.com/baxterthehacker/public-repo). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " member " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_pull_request_opened_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker opened [PR #1](https://github.com/baxterthehacker/public-repo/pull/1) from `changes` to `master`: \n \n ~~~ quote \n This is a pretty simple change that we need to pull into master. \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__opened " , TOPIC_PR , expected_message )
2016-10-25 14:50:42 +02:00
2018-09-26 02:45:29 +02:00
def test_pull_request_opened_with_preassigned_assignee_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " Scheduler / PR #4 Improve README "
expected_message = " eeshangarg opened [PR #4](https://github.com/eeshangarg/Scheduler/pull/4) (assigned to eeshangarg) from `improve-readme-2` to `master`. "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" pull_request__opened_with_preassigned_assignee " , expected_topic , expected_message
)
2018-09-26 02:45:29 +02:00
2018-07-20 21:36:18 +02:00
def test_pull_request_opened_msg_with_custom_topic_in_url ( self ) - > None :
self . url = self . build_webhook_url ( topic = ' notifications ' )
2020-04-09 21:51:58 +02:00
expected_topic = " notifications "
expected_message = " baxterthehacker opened [PR #1 Update the README with new information](https://github.com/baxterthehacker/public-repo/pull/1) from `changes` to `master`: \n \n ~~~ quote \n This is a pretty simple change that we need to pull into master. \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__opened " , expected_topic , expected_message )
2018-07-20 21:36:18 +02:00
2017-11-04 07:47:46 +01:00
def test_pull_request_synchronized_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker updated [PR #1](https://github.com/baxterthehacker/public-repo/pull/1) from `changes` to `master`. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__synchronized " , TOPIC_PR , expected_message )
2017-02-02 20:09:57 +01:00
2017-11-04 07:47:46 +01:00
def test_pull_request_closed_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker closed without merge [PR #1](https://github.com/baxterthehacker/public-repo/pull/1). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__closed " , TOPIC_PR , expected_message )
2016-10-25 14:50:42 +02:00
2018-07-20 21:36:18 +02:00
def test_pull_request_closed_msg_with_custom_topic_in_url ( self ) - > None :
self . url = self . build_webhook_url ( topic = ' notifications ' )
2020-04-09 21:51:58 +02:00
expected_topic = " notifications "
expected_message = " baxterthehacker closed without merge [PR #1 Update the README with new information](https://github.com/baxterthehacker/public-repo/pull/1). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__closed " , expected_topic , expected_message )
2018-07-20 21:36:18 +02:00
2017-11-04 07:47:46 +01:00
def test_pull_request_merged_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker merged [PR #1](https://github.com/baxterthehacker/public-repo/pull/1). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__merged " , TOPIC_PR , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_public_msg ( self ) - > None :
2020-06-27 13:19:32 +02:00
expected_message = " baxterthehacker made the repository [baxterthehacker/public-repo](https://github.com/baxterthehacker/public-repo) public. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " public " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_wiki_pages_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " jasonrudolph: \n * created [Home](https://github.com/baxterthehacker/public-repo/wiki/Home) \n * created [Home](https://github.com/baxterthehacker/public-repo/wiki/Home) "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " gollum__wiki_pages " , TOPIC_WIKI , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_watch_msg ( self ) - > None :
2020-06-27 13:19:32 +02:00
expected_message = " baxterthehacker starred the repository [baxterthehacker/public-repo](https://github.com/baxterthehacker/public-repo). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " watch__repository " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_repository_msg ( self ) - > None :
2020-06-27 13:19:32 +02:00
expected_message = " baxterthehacker created the repository [baxterandthehackers/public-repo](https://github.com/baxterandthehackers/public-repo). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " repository " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_team_add_msg ( self ) - > None :
2020-06-27 13:19:32 +02:00
expected_message = " The repository [baxterandthehackers/public-repo](https://github.com/baxterandthehackers/public-repo) was added to team github. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " team_add " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_release_msg ( self ) - > None :
2020-05-12 02:39:57 +02:00
expected_message = " baxterthehacker published release [0.0.1](https://github.com/baxterthehacker/public-repo/releases/tag/0.0.1) for tag 0.0.1. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " release " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_page_build_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " Github Pages build, triggered by baxterthehacker, has finished building. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " page_build " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2017-11-04 07:47:46 +01:00
def test_status_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " [9049f12](https://github.com/baxterthehacker/public-repo/commit/9049f1265b7d61be4a8904a9a27120d2064dab3b) changed its status to success. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " status " , TOPIC_REPO , expected_message )
2016-10-25 14:50:42 +02:00
2018-09-27 22:41:43 +02:00
def test_status_with_target_url_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " [9049f12](https://github.com/baxterthehacker/public-repo/commit/9049f1265b7d61be4a8904a9a27120d2064dab3b) changed its status to [success](https://example.com/build/status). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " status__with_target_url " , TOPIC_REPO , expected_message )
2018-09-27 22:41:43 +02:00
2017-11-04 07:47:46 +01:00
def test_pull_request_review_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker submitted [PR Review](https://github.com/baxterthehacker/public-repo/pull/1#pullrequestreview-2626884). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request_review " , TOPIC_PR , expected_message )
2016-10-25 14:50:42 +02:00
2018-07-20 21:36:18 +02:00
def test_pull_request_review_msg_with_custom_topic_in_url ( self ) - > None :
self . url = self . build_webhook_url ( topic = ' notifications ' )
2020-04-09 21:51:58 +02:00
expected_topic = " notifications "
expected_message = " baxterthehacker submitted [PR Review for #1 Update the README with new information](https://github.com/baxterthehacker/public-repo/pull/1#pullrequestreview-2626884). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request_review " , expected_topic , expected_message )
2018-07-20 21:36:18 +02:00
2017-11-04 07:47:46 +01:00
def test_pull_request_review_comment_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker created [PR Review Comment](https://github.com/baxterthehacker/public-repo/pull/1#discussion_r29724692): \n \n ~~~ quote \n Maybe you should use more emojji on this line. \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request_review_comment " , TOPIC_PR , expected_message )
2016-10-25 14:50:42 +02:00
2018-07-20 21:36:18 +02:00
def test_pull_request_review_comment_with_custom_topic_in_url ( self ) - > None :
self . url = self . build_webhook_url ( topic = ' notifications ' )
2020-04-09 21:51:58 +02:00
expected_topic = " notifications "
expected_message = " baxterthehacker created [PR Review Comment on #1 Update the README with new information](https://github.com/baxterthehacker/public-repo/pull/1#discussion_r29724692): \n \n ~~~ quote \n Maybe you should use more emojji on this line. \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request_review_comment " , expected_topic , expected_message )
2018-07-20 21:36:18 +02:00
2017-11-04 07:47:46 +01:00
def test_push_tag_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker pushed tag abc. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " push__tag " , TOPIC_REPO , expected_message )
2017-02-08 20:41:43 +01:00
2017-11-04 07:47:46 +01:00
def test_pull_request_edited_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker edited [PR #1](https://github.com/baxterthehacker/public-repo/pull/1) from `changes` to `master`. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__edited " , TOPIC_PR , expected_message )
2017-02-08 20:41:43 +01:00
2017-11-04 07:47:46 +01:00
def test_pull_request_assigned_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " baxterthehacker assigned [PR #1](https://github.com/baxterthehacker/public-repo/pull/1) to baxterthehacker. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__assigned " , TOPIC_PR , expected_message )
2017-02-08 20:41:43 +01:00
2018-07-20 21:36:18 +02:00
def test_pull_request_assigned_msg_with_custom_topic_in_url ( self ) - > None :
self . url = self . build_webhook_url ( topic = ' notifications ' )
2020-04-09 21:51:58 +02:00
expected_topic = " notifications "
expected_message = " baxterthehacker assigned [PR #1 Update the README with new information](https://github.com/baxterthehacker/public-repo/pull/1) to baxterthehacker. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__assigned " , expected_topic , expected_message )
2018-07-20 21:36:18 +02:00
2017-11-04 07:47:46 +01:00
def test_pull_request_unassigned_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " eeshangarg unassigned [PR #1](https://github.com/zulip-test-org/helloworld/pull/1). "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" pull_request__unassigned " ,
" helloworld / PR #1 Mention that Zulip rocks! " ,
expected_message ,
)
2017-02-08 21:30:25 +01:00
2020-05-12 15:25:08 +02:00
def test_pull_request_ready_for_review_msg ( self ) - > None :
expected_message = " **Hypro999** has marked [PR #2](https://github.com/Hypro999/temp-test-github-webhook/pull/2) as ready for review. "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" pull_request__ready_for_review " ,
" temp-test-github-webhook / PR #2 Test " ,
expected_message ,
)
2020-05-12 15:25:08 +02:00
2018-07-01 20:40:08 +02:00
def test_pull_request_review_requested_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " **eeshangarg** requested [showell](https://github.com/showell) for a review on [PR #1](https://github.com/eeshangarg/Scheduler/pull/1). "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" pull_request__review_requested " ,
" Scheduler / PR #1 This is just a test commit " ,
expected_message ,
)
2018-07-01 20:40:08 +02:00
2018-10-23 20:05:14 +02:00
def test_pull_request_review_requested_singular_key_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " **eeshangarg** requested [rishig](https://github.com/rishig) for a review on [PR #6](https://github.com/eeshangarg/Scheduler/pull/6). "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" pull_request__review_requested_singular_key " ,
" Scheduler / PR #6 Mention how awesome this project is in ... " ,
expected_message ,
)
2018-10-23 20:05:14 +02:00
2018-07-01 20:40:08 +02:00
def test_pull_request_review_requested_multiple_reviwers_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " **eeshangarg** requested [showell](https://github.com/showell) and [timabbott](https://github.com/timabbott) for a review on [PR #1](https://github.com/eeshangarg/Scheduler/pull/1). "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" pull_request__review_requested_multiple_reviewers " ,
" Scheduler / PR #1 This is just a test commit " ,
expected_message ,
)
2018-07-01 20:40:08 +02:00
2020-03-11 00:29:59 +01:00
def test_pull_request__review_requested_team_reviewer_msg ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " **singhsourabh** requested [shreyaskargit](https://github.com/shreyaskargit), [bajaj99prashant](https://github.com/bajaj99prashant), [review-team](https://github.com/orgs/test-org965/teams/review-team), [authority](https://github.com/orgs/test-org965/teams/authority) and [management](https://github.com/orgs/test-org965/teams/management) for a review on [PR #4](https://github.com/test-org965/webhook-test/pull/4). "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" pull_request__review_requested_team_reviewer " ,
" webhook-test / PR #4 testing webhook " ,
expected_message ,
)
2020-03-11 00:29:59 +01:00
2018-07-20 21:36:18 +02:00
def test_pull_request_review_requested_with_custom_topic_in_url ( self ) - > None :
self . url = self . build_webhook_url ( topic = ' notifications ' )
2020-04-09 21:51:58 +02:00
expected_topic = " notifications "
expected_message = " **eeshangarg** requested [showell](https://github.com/showell) for a review on [PR #1 This is just a test commit](https://github.com/eeshangarg/Scheduler/pull/1). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pull_request__review_requested " , expected_topic , expected_message )
2018-07-20 21:36:18 +02:00
2019-02-19 21:16:41 +01:00
def test_check_run ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " hello-world / checks "
expected_message = """
2019-02-19 21:16:41 +01:00
Check [ randscape ] ( http : / / github . com / github / hello - world / runs / 4 ) completed ( success ) . ( [ d6fde92 ] ( http : / / github . com / github / hello - world / commit / d6fde92930d4715a2b49857d24b940956b26d2d3 ) )
""" .strip()
2020-08-23 15:49:24 +02:00
self . check_webhook ( " check_run__completed " , expected_topic , expected_message )
2019-02-19 21:16:41 +01:00
2020-05-14 14:20:05 +02:00
def test_team_edited_description ( self ) - > None :
expected_topic = " team Testing "
expected_message = """ \
* * Hypro999 * * changed the team description to :
` ` ` quote
A temporary team so that I can get some webhook fixtures !
` ` ` """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " team__edited_description " , expected_topic , expected_message )
2020-05-14 14:20:05 +02:00
def test_team_edited_name ( self ) - > None :
expected_topic = " team Testing Team "
expected_message = """ Team `Testing` was renamed to `Testing Team`. """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " team__edited_name " , expected_topic , expected_message )
2020-05-14 14:20:05 +02:00
def test_team_edited_privacy ( self ) - > None :
expected_topic = " team Testing Team "
expected_message = """ Team visibility changed to `secret` """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " team__edited_privacy_secret " , expected_topic , expected_message )
2020-05-14 14:20:05 +02:00
2020-08-22 15:50:15 +02:00
def verify_post_is_ignored ( self , payload : str , http_x_github_event : str ) - > None :
with patch ( " zerver.webhooks.github.view.check_send_webhook_message " ) as m :
result = self . client_post (
self . url ,
payload ,
HTTP_X_GITHUB_EVENT = http_x_github_event ,
content_type = " application/json " ,
)
self . assertFalse ( m . called )
2019-02-19 21:16:41 +01:00
self . assert_json_success ( result )
2020-08-22 15:50:15 +02:00
def test_check_run_in_progress_ignore ( self ) - > None :
payload = self . get_body ( ' check_run__in_progress ' )
self . verify_post_is_ignored ( payload , " check_run " )
2020-09-01 13:52:36 +02:00
def test_ignored_pull_request_actions ( self ) - > None :
ignored_actions = [
2020-09-01 14:19:33 +02:00
" approved " ,
" converted_to_draft " ,
2020-09-01 13:52:36 +02:00
" labeled " ,
" review_request_removed " ,
" unlabeled " ,
]
for action in ignored_actions :
data = dict ( action = action )
payload = orjson . dumps ( data ) . decode ( )
self . verify_post_is_ignored ( payload , " pull_request " )
2017-03-25 03:23:15 +01:00
2020-09-01 15:07:08 +02:00
def test_ignored_team_actions ( self ) - > None :
ignored_actions = [
" added_to_repository " ,
" created " ,
" deleted " ,
" removed_from_repository " ,
]
for action in ignored_actions :
data = dict ( action = action )
payload = orjson . dumps ( data ) . decode ( )
self . verify_post_is_ignored ( payload , " team " )
2020-08-22 15:50:15 +02:00
def test_push_1_commit_filtered_by_branches_ignore ( self ) - > None :
2017-04-21 23:35:40 +02:00
self . url = self . build_webhook_url ( branches = ' master,development ' )
2019-06-06 08:47:24 +02:00
payload = self . get_body ( ' push__1_commit ' )
2020-08-22 15:50:15 +02:00
self . verify_post_is_ignored ( payload , " push " )
2017-03-25 03:23:15 +01:00
2020-08-22 15:50:15 +02:00
def test_push_50_commits_filtered_by_branches_ignore ( self ) - > None :
2017-04-21 23:35:40 +02:00
self . url = self . build_webhook_url ( branches = ' master,development ' )
2019-06-06 08:47:24 +02:00
payload = self . get_body ( ' push__50_commits ' )
2020-08-22 15:50:15 +02:00
self . verify_post_is_ignored ( payload , " push " )
2017-04-21 23:35:40 +02:00
2020-08-22 15:50:15 +02:00
def test_push_multiple_comitters_filtered_by_branches_ignore ( self ) - > None :
2017-04-21 23:35:40 +02:00
self . url = self . build_webhook_url ( branches = ' master,development ' )
2019-06-06 08:47:24 +02:00
payload = self . get_body ( ' push__multiple_committers ' )
2020-08-22 15:50:15 +02:00
self . verify_post_is_ignored ( payload , " push " )
2017-04-21 23:35:40 +02:00
2020-08-22 15:50:15 +02:00
def test_push_multiple_comitters_with_others_filtered_by_branches_ignore ( self ) - > None :
2017-04-21 23:35:40 +02:00
self . url = self . build_webhook_url ( branches = ' master,development ' )
2019-06-06 08:47:24 +02:00
payload = self . get_body ( ' push__multiple_committers_with_others ' )
2020-08-22 15:50:15 +02:00
self . verify_post_is_ignored ( payload , " push " )
2019-02-19 20:17:40 +01:00
2020-08-22 15:50:15 +02:00
def test_repository_vulnerability_alert_ignore ( self ) - > None :
2019-02-19 20:17:40 +01:00
self . url = self . build_webhook_url ( )
payload = self . get_body ( ' repository_vulnerability_alert ' )
2020-08-22 15:50:15 +02:00
self . verify_post_is_ignored ( payload , " repository_vulnerability_alert " )
2020-09-01 13:28:33 +02:00
def test_ignored_events ( self ) - > None :
# The payload for these events never gets looked at in the
# webhook itself; it only needs to be valid JSON.
payload = " {} "
ignored_events = [
" check_suite " ,
" label " ,
" meta " ,
" milestone " ,
" organization " ,
" project_card " ,
" repository_vulnerability_alert " ,
]
for event in ignored_events :
self . verify_post_is_ignored ( payload , event )