From c8f417787276dcb50315264501ec184fe3205752 Mon Sep 17 00:00:00 2001 From: "Hemanth V. Alluri" Date: Thu, 14 May 2020 17:50:05 +0530 Subject: [PATCH] webhooks/github: Add a support for team/edited events. Signed-off-by: Hemanth V. Alluri --- .../fixtures/team__edited_description.json | 57 +++++++++++++++++++ .../github/fixtures/team__edited_name.json | 56 ++++++++++++++++++ .../fixtures/team__edited_privacy_closed.json | 56 ++++++++++++++++++ .../fixtures/team__edited_privacy_secret.json | 56 ++++++++++++++++++ zerver/webhooks/github/tests.py | 19 +++++++ zerver/webhooks/github/view.py | 21 ++++++- 6 files changed, 264 insertions(+), 1 deletion(-) create mode 100755 zerver/webhooks/github/fixtures/team__edited_description.json create mode 100755 zerver/webhooks/github/fixtures/team__edited_name.json create mode 100755 zerver/webhooks/github/fixtures/team__edited_privacy_closed.json create mode 100755 zerver/webhooks/github/fixtures/team__edited_privacy_secret.json diff --git a/zerver/webhooks/github/fixtures/team__edited_description.json b/zerver/webhooks/github/fixtures/team__edited_description.json new file mode 100755 index 0000000000..e5f9f227c1 --- /dev/null +++ b/zerver/webhooks/github/fixtures/team__edited_description.json @@ -0,0 +1,57 @@ +{ + "changes": { + "description": { + "from": "A temporary team so that I can get some webhook fixtures." + } + }, + "action": "edited", + "team": { + "name": "Testing", + "id": 3835900, + "node_id": "MDQ6VGVhbTM4MzU5MDA=", + "slug": "testing", + "description": "A temporary team so that I can get some webhook fixtures!", + "privacy": "closed", + "url": "https://api.github.com/organizations/14038814/team/3835900", + "html_url": "https://github.com/orgs/dvm-bitspilani/teams/testing", + "members_url": "https://api.github.com/organizations/14038814/team/3835900/members{/member}", + "repositories_url": "https://api.github.com/organizations/14038814/team/3835900/repos", + "permission": "pull", + "parent": null + }, + "organization": { + "login": "dvm-bitspilani", + "id": 14038814, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjE0MDM4ODE0", + "url": "https://api.github.com/orgs/dvm-bitspilani", + "repos_url": "https://api.github.com/orgs/dvm-bitspilani/repos", + "events_url": "https://api.github.com/orgs/dvm-bitspilani/events", + "hooks_url": "https://api.github.com/orgs/dvm-bitspilani/hooks", + "issues_url": "https://api.github.com/orgs/dvm-bitspilani/issues", + "members_url": "https://api.github.com/orgs/dvm-bitspilani/members{/member}", + "public_members_url": "https://api.github.com/orgs/dvm-bitspilani/public_members{/member}", + "avatar_url": "https://avatars3.githubusercontent.com/u/14038814?v=4", + "description": "" + }, + "sender": { + "login": "Hypro999", + "id": 29123352, + "node_id": "MDQ6VXNlcjI5MTIzMzUy", + "avatar_url": "https://avatars3.githubusercontent.com/u/29123352?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Hypro999", + "html_url": "https://github.com/Hypro999", + "followers_url": "https://api.github.com/users/Hypro999/followers", + "following_url": "https://api.github.com/users/Hypro999/following{/other_user}", + "gists_url": "https://api.github.com/users/Hypro999/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Hypro999/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Hypro999/subscriptions", + "organizations_url": "https://api.github.com/users/Hypro999/orgs", + "repos_url": "https://api.github.com/users/Hypro999/repos", + "events_url": "https://api.github.com/users/Hypro999/events{/privacy}", + "received_events_url": "https://api.github.com/users/Hypro999/received_events", + "type": "User", + "site_admin": false + } +} + diff --git a/zerver/webhooks/github/fixtures/team__edited_name.json b/zerver/webhooks/github/fixtures/team__edited_name.json new file mode 100755 index 0000000000..dcd4214eaf --- /dev/null +++ b/zerver/webhooks/github/fixtures/team__edited_name.json @@ -0,0 +1,56 @@ +{ + "changes": { + "name": { + "from": "Testing" + } + }, + "action": "edited", + "team": { + "name": "Testing Team", + "id": 3835900, + "node_id": "MDQ6VGVhbTM4MzU5MDA=", + "slug": "testing-team", + "description": "A temporary team so that I can get some webhook fixtures!", + "privacy": "closed", + "url": "https://api.github.com/organizations/14038814/team/3835900", + "html_url": "https://github.com/orgs/dvm-bitspilani/teams/testing-team", + "members_url": "https://api.github.com/organizations/14038814/team/3835900/members{/member}", + "repositories_url": "https://api.github.com/organizations/14038814/team/3835900/repos", + "permission": "pull", + "parent": null + }, + "organization": { + "login": "dvm-bitspilani", + "id": 14038814, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjE0MDM4ODE0", + "url": "https://api.github.com/orgs/dvm-bitspilani", + "repos_url": "https://api.github.com/orgs/dvm-bitspilani/repos", + "events_url": "https://api.github.com/orgs/dvm-bitspilani/events", + "hooks_url": "https://api.github.com/orgs/dvm-bitspilani/hooks", + "issues_url": "https://api.github.com/orgs/dvm-bitspilani/issues", + "members_url": "https://api.github.com/orgs/dvm-bitspilani/members{/member}", + "public_members_url": "https://api.github.com/orgs/dvm-bitspilani/public_members{/member}", + "avatar_url": "https://avatars3.githubusercontent.com/u/14038814?v=4", + "description": "" + }, + "sender": { + "login": "Hypro999", + "id": 29123352, + "node_id": "MDQ6VXNlcjI5MTIzMzUy", + "avatar_url": "https://avatars3.githubusercontent.com/u/29123352?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Hypro999", + "html_url": "https://github.com/Hypro999", + "followers_url": "https://api.github.com/users/Hypro999/followers", + "following_url": "https://api.github.com/users/Hypro999/following{/other_user}", + "gists_url": "https://api.github.com/users/Hypro999/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Hypro999/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Hypro999/subscriptions", + "organizations_url": "https://api.github.com/users/Hypro999/orgs", + "repos_url": "https://api.github.com/users/Hypro999/repos", + "events_url": "https://api.github.com/users/Hypro999/events{/privacy}", + "received_events_url": "https://api.github.com/users/Hypro999/received_events", + "type": "User", + "site_admin": false + } +} diff --git a/zerver/webhooks/github/fixtures/team__edited_privacy_closed.json b/zerver/webhooks/github/fixtures/team__edited_privacy_closed.json new file mode 100755 index 0000000000..e72e4b6025 --- /dev/null +++ b/zerver/webhooks/github/fixtures/team__edited_privacy_closed.json @@ -0,0 +1,56 @@ +{ + "changes": { + "privacy": { + "from": "secret" + } + }, + "action": "edited", + "team": { + "name": "Testing", + "id": 3835900, + "node_id": "MDQ6VGVhbTM4MzU5MDA=", + "slug": "testing", + "description": "A temporary team so that I can get some webhook fixtures!", + "privacy": "closed", + "url": "https://api.github.com/organizations/14038814/team/3835900", + "html_url": "https://github.com/orgs/dvm-bitspilani/teams/testing", + "members_url": "https://api.github.com/organizations/14038814/team/3835900/members{/member}", + "repositories_url": "https://api.github.com/organizations/14038814/team/3835900/repos", + "permission": "pull", + "parent": null + }, + "organization": { + "login": "dvm-bitspilani", + "id": 14038814, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjE0MDM4ODE0", + "url": "https://api.github.com/orgs/dvm-bitspilani", + "repos_url": "https://api.github.com/orgs/dvm-bitspilani/repos", + "events_url": "https://api.github.com/orgs/dvm-bitspilani/events", + "hooks_url": "https://api.github.com/orgs/dvm-bitspilani/hooks", + "issues_url": "https://api.github.com/orgs/dvm-bitspilani/issues", + "members_url": "https://api.github.com/orgs/dvm-bitspilani/members{/member}", + "public_members_url": "https://api.github.com/orgs/dvm-bitspilani/public_members{/member}", + "avatar_url": "https://avatars3.githubusercontent.com/u/14038814?v=4", + "description": "" + }, + "sender": { + "login": "Hypro999", + "id": 29123352, + "node_id": "MDQ6VXNlcjI5MTIzMzUy", + "avatar_url": "https://avatars3.githubusercontent.com/u/29123352?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Hypro999", + "html_url": "https://github.com/Hypro999", + "followers_url": "https://api.github.com/users/Hypro999/followers", + "following_url": "https://api.github.com/users/Hypro999/following{/other_user}", + "gists_url": "https://api.github.com/users/Hypro999/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Hypro999/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Hypro999/subscriptions", + "organizations_url": "https://api.github.com/users/Hypro999/orgs", + "repos_url": "https://api.github.com/users/Hypro999/repos", + "events_url": "https://api.github.com/users/Hypro999/events{/privacy}", + "received_events_url": "https://api.github.com/users/Hypro999/received_events", + "type": "User", + "site_admin": false + } +} diff --git a/zerver/webhooks/github/fixtures/team__edited_privacy_secret.json b/zerver/webhooks/github/fixtures/team__edited_privacy_secret.json new file mode 100755 index 0000000000..e5542ab249 --- /dev/null +++ b/zerver/webhooks/github/fixtures/team__edited_privacy_secret.json @@ -0,0 +1,56 @@ +{ + "changes": { + "privacy": { + "from": "closed" + } + }, + "action": "edited", + "team": { + "name": "Testing Team", + "id": 3835900, + "node_id": "MDQ6VGVhbTM4MzU5MDA=", + "slug": "testing-team", + "description": "A temporary team so that I can get some webhook fixtures!", + "privacy": "secret", + "url": "https://api.github.com/organizations/14038814/team/3835900", + "html_url": "https://github.com/orgs/dvm-bitspilani/teams/testing-team", + "members_url": "https://api.github.com/organizations/14038814/team/3835900/members{/member}", + "repositories_url": "https://api.github.com/organizations/14038814/team/3835900/repos", + "permission": "pull", + "parent": null + }, + "organization": { + "login": "dvm-bitspilani", + "id": 14038814, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjE0MDM4ODE0", + "url": "https://api.github.com/orgs/dvm-bitspilani", + "repos_url": "https://api.github.com/orgs/dvm-bitspilani/repos", + "events_url": "https://api.github.com/orgs/dvm-bitspilani/events", + "hooks_url": "https://api.github.com/orgs/dvm-bitspilani/hooks", + "issues_url": "https://api.github.com/orgs/dvm-bitspilani/issues", + "members_url": "https://api.github.com/orgs/dvm-bitspilani/members{/member}", + "public_members_url": "https://api.github.com/orgs/dvm-bitspilani/public_members{/member}", + "avatar_url": "https://avatars3.githubusercontent.com/u/14038814?v=4", + "description": "" + }, + "sender": { + "login": "Hypro999", + "id": 29123352, + "node_id": "MDQ6VXNlcjI5MTIzMzUy", + "avatar_url": "https://avatars3.githubusercontent.com/u/29123352?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Hypro999", + "html_url": "https://github.com/Hypro999", + "followers_url": "https://api.github.com/users/Hypro999/followers", + "following_url": "https://api.github.com/users/Hypro999/following{/other_user}", + "gists_url": "https://api.github.com/users/Hypro999/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Hypro999/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Hypro999/subscriptions", + "organizations_url": "https://api.github.com/users/Hypro999/orgs", + "repos_url": "https://api.github.com/users/Hypro999/repos", + "events_url": "https://api.github.com/users/Hypro999/events{/privacy}", + "received_events_url": "https://api.github.com/users/Hypro999/received_events", + "type": "User", + "site_admin": false + } +} diff --git a/zerver/webhooks/github/tests.py b/zerver/webhooks/github/tests.py index 8ccbda5e7d..12dd20c6c9 100644 --- a/zerver/webhooks/github/tests.py +++ b/zerver/webhooks/github/tests.py @@ -302,6 +302,25 @@ Check [randscape](http://github.com/github/hello-world/runs/4) completed (succes """.strip() self.send_and_test_stream_message('check_run__completed', expected_topic, expected_message) + 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! +```""" + self.send_and_test_stream_message('team__edited_description', expected_topic, expected_message) + + def test_team_edited_name(self) -> None: + expected_topic = "team Testing Team" + expected_message = """Team `Testing` was renamed to `Testing Team`.""" + self.send_and_test_stream_message('team__edited_name', expected_topic, expected_message) + + def test_team_edited_privacy(self) -> None: + expected_topic = "team Testing Team" + expected_message = """Team visibility changed to `secret`""" + self.send_and_test_stream_message('team__edited_privacy_secret', expected_topic, expected_message) + @patch('zerver.webhooks.github.view.check_send_webhook_message') def test_check_run_in_progress_ignore( self, check_send_webhook_message_mock: MagicMock) -> None: diff --git a/zerver/webhooks/github/view.py b/zerver/webhooks/github/view.py index 77bb1a3580..09efd8b7f6 100644 --- a/zerver/webhooks/github/view.py +++ b/zerver/webhooks/github/view.py @@ -235,6 +235,22 @@ def get_add_team_body(payload: Dict[str, Any]) -> str: payload['team']['name'] ) +def get_team_body(payload: Dict[str, Any]) -> str: + changes = payload["changes"] + if "description" in changes: + actor = payload["sender"]["login"] + new_description = payload["team"]["description"] + return "**{}** changed the team description to:\n```quote\n{}\n```".format(actor, new_description) + if "name" in changes: + original_name = changes["name"]["from"] + new_name = payload["team"]["name"] + return "Team `{}` was renamed to `{}`.".format(original_name, new_name) + if "privacy" in changes: + new_visibility = payload["team"]["privacy"] + return "Team visibility changed to `{}`".format(new_visibility) + else: # nocoverage + raise UnexpectedWebhookEventType("GitHub", "Team Edited: {}".format(changes.keys())) + def get_release_body(payload: Dict[str, Any]) -> str: data = { 'user_name': get_sender_name(payload), @@ -433,6 +449,8 @@ def get_subject_based_on_type(payload: Dict[str, Any], event: str) -> str: ) elif event == 'membership': return "{} organization".format(payload['organization']['login']) + elif event == 'team': + return "team {}".format(payload['team']['name']) elif event == 'push_commits': return TOPIC_WITH_BRANCH_TEMPLATE.format( repo=get_repository_name(payload), @@ -452,7 +470,6 @@ def get_subject_based_on_type(payload: Dict[str, Any], event: str) -> str: return get_repository_name(payload) EVENT_FUNCTION_MAPPER = { - 'team_add': get_add_team_body, 'commit_comment': get_commit_comment_body, 'closed_pull_request': get_closed_pull_request_body, 'create': partial(get_create_or_delete_body, action='created'), @@ -481,6 +498,8 @@ EVENT_FUNCTION_MAPPER = { 'repository': get_repository_body, 'star': get_star_body, 'status': get_status_body, + 'team': get_team_body, + 'team_add': get_add_team_body, 'watch': get_watch_body, }