From 8ef18463d67d0ce8c59165fa30f7ac357a7e5da5 Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Sat, 29 Apr 2017 03:46:07 -0230 Subject: [PATCH] github_webhook: Use author's full name if username is not available. In cases where the webhook payload doesn't have the username for the author of a particular commit (this can happen if the author doesn't have a GitHub account or the author's email is not associated with their GitHub account), we now use the author's full name to format messages. --- ...ebhook_push_1_commit_without_username.json | 180 ++++++++++++++++++ zerver/webhooks/github_webhook/tests.py | 5 + zerver/webhooks/github_webhook/view.py | 3 +- 3 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 zerver/webhooks/github_webhook/fixtures/github_webhook_push_1_commit_without_username.json diff --git a/zerver/webhooks/github_webhook/fixtures/github_webhook_push_1_commit_without_username.json b/zerver/webhooks/github_webhook/fixtures/github_webhook_push_1_commit_without_username.json new file mode 100644 index 0000000000..e173c0108c --- /dev/null +++ b/zerver/webhooks/github_webhook/fixtures/github_webhook_push_1_commit_without_username.json @@ -0,0 +1,180 @@ +{ + "ref":"refs/heads/changes", + "before":"0383613da871608c384cef652df7b61669156cac", + "after":"2e8cf535fb38a3dab2476cdf856efda904ad4c94", + "created":false, + "deleted":false, + "forced":false, + "base_ref":null, + "compare":"https://github.com/eeshangarg/public-repo/compare/0383613da871...2e8cf535fb38", + "commits":[ + { + "id":"2e8cf535fb38a3dab2476cdf856efda904ad4c94", + "tree_id":"09303be712bd8e923f9b227c8522257fa32ca7dc", + "distinct":true, + "message":"Update the README", + "timestamp":"2017-04-29T14:31:31-02:30", + "url":"https://github.com/eeshangarg/public-repo/commit/2e8cf535fb38a3dab2476cdf856efda904ad4c94", + "author":{ + "name":"John Snow", + "email":"johnsnow@got.com" + }, + "committer":{ + "name":"Eeshan Garg", + "email":"jerryguitarist@gmail.com", + "username":"eeshangarg" + }, + "added":[ + + ], + "removed":[ + + ], + "modified":[ + "README.md" + ] + } + ], + "head_commit":{ + "id":"2e8cf535fb38a3dab2476cdf856efda904ad4c94", + "tree_id":"09303be712bd8e923f9b227c8522257fa32ca7dc", + "distinct":true, + "message":"Update the README", + "timestamp":"2017-04-29T14:31:31-02:30", + "url":"https://github.com/eeshangarg/public-repo/commit/2e8cf535fb38a3dab2476cdf856efda904ad4c94", + "author":{ + "name":"John Snow", + "email":"johnsnow@got.com" + }, + "committer":{ + "name":"Eeshan Garg", + "email":"jerryguitarist@gmail.com", + "username":"eeshangarg" + }, + "added":[ + + ], + "removed":[ + + ], + "modified":[ + "README.md" + ] + }, + "repository":{ + "id":89799239, + "name":"public-repo", + "full_name":"eeshangarg/public-repo", + "owner":{ + "name":"eeshangarg", + "email":"jerryguitarist@gmail.com", + "login":"eeshangarg", + "id":7251823, + "avatar_url":"https://avatars3.githubusercontent.com/u/7251823?v=3", + "gravatar_id":"", + "url":"https://api.github.com/users/eeshangarg", + "html_url":"https://github.com/eeshangarg", + "followers_url":"https://api.github.com/users/eeshangarg/followers", + "following_url":"https://api.github.com/users/eeshangarg/following{/other_user}", + "gists_url":"https://api.github.com/users/eeshangarg/gists{/gist_id}", + "starred_url":"https://api.github.com/users/eeshangarg/starred{/owner}{/repo}", + "subscriptions_url":"https://api.github.com/users/eeshangarg/subscriptions", + "organizations_url":"https://api.github.com/users/eeshangarg/orgs", + "repos_url":"https://api.github.com/users/eeshangarg/repos", + "events_url":"https://api.github.com/users/eeshangarg/events{/privacy}", + "received_events_url":"https://api.github.com/users/eeshangarg/received_events", + "type":"User", + "site_admin":false + }, + "private":false, + "html_url":"https://github.com/eeshangarg/public-repo", + "description":null, + "fork":false, + "url":"https://github.com/eeshangarg/public-repo", + "forks_url":"https://api.github.com/repos/eeshangarg/public-repo/forks", + "keys_url":"https://api.github.com/repos/eeshangarg/public-repo/keys{/key_id}", + "collaborators_url":"https://api.github.com/repos/eeshangarg/public-repo/collaborators{/collaborator}", + "teams_url":"https://api.github.com/repos/eeshangarg/public-repo/teams", + "hooks_url":"https://api.github.com/repos/eeshangarg/public-repo/hooks", + "issue_events_url":"https://api.github.com/repos/eeshangarg/public-repo/issues/events{/number}", + "events_url":"https://api.github.com/repos/eeshangarg/public-repo/events", + "assignees_url":"https://api.github.com/repos/eeshangarg/public-repo/assignees{/user}", + "branches_url":"https://api.github.com/repos/eeshangarg/public-repo/branches{/branch}", + "tags_url":"https://api.github.com/repos/eeshangarg/public-repo/tags", + "blobs_url":"https://api.github.com/repos/eeshangarg/public-repo/git/blobs{/sha}", + "git_tags_url":"https://api.github.com/repos/eeshangarg/public-repo/git/tags{/sha}", + "git_refs_url":"https://api.github.com/repos/eeshangarg/public-repo/git/refs{/sha}", + "trees_url":"https://api.github.com/repos/eeshangarg/public-repo/git/trees{/sha}", + "statuses_url":"https://api.github.com/repos/eeshangarg/public-repo/statuses/{sha}", + "languages_url":"https://api.github.com/repos/eeshangarg/public-repo/languages", + "stargazers_url":"https://api.github.com/repos/eeshangarg/public-repo/stargazers", + "contributors_url":"https://api.github.com/repos/eeshangarg/public-repo/contributors", + "subscribers_url":"https://api.github.com/repos/eeshangarg/public-repo/subscribers", + "subscription_url":"https://api.github.com/repos/eeshangarg/public-repo/subscription", + "commits_url":"https://api.github.com/repos/eeshangarg/public-repo/commits{/sha}", + "git_commits_url":"https://api.github.com/repos/eeshangarg/public-repo/git/commits{/sha}", + "comments_url":"https://api.github.com/repos/eeshangarg/public-repo/comments{/number}", + "issue_comment_url":"https://api.github.com/repos/eeshangarg/public-repo/issues/comments{/number}", + "contents_url":"https://api.github.com/repos/eeshangarg/public-repo/contents/{+path}", + "compare_url":"https://api.github.com/repos/eeshangarg/public-repo/compare/{base}...{head}", + "merges_url":"https://api.github.com/repos/eeshangarg/public-repo/merges", + "archive_url":"https://api.github.com/repos/eeshangarg/public-repo/{archive_format}{/ref}", + "downloads_url":"https://api.github.com/repos/eeshangarg/public-repo/downloads", + "issues_url":"https://api.github.com/repos/eeshangarg/public-repo/issues{/number}", + "pulls_url":"https://api.github.com/repos/eeshangarg/public-repo/pulls{/number}", + "milestones_url":"https://api.github.com/repos/eeshangarg/public-repo/milestones{/number}", + "notifications_url":"https://api.github.com/repos/eeshangarg/public-repo/notifications{?since,all,participating}", + "labels_url":"https://api.github.com/repos/eeshangarg/public-repo/labels{/name}", + "releases_url":"https://api.github.com/repos/eeshangarg/public-repo/releases{/id}", + "deployments_url":"https://api.github.com/repos/eeshangarg/public-repo/deployments", + "created_at":1493484776, + "updated_at":"2017-04-29T16:52:56Z", + "pushed_at":1493485308, + "git_url":"git://github.com/eeshangarg/public-repo.git", + "ssh_url":"git@github.com:eeshangarg/public-repo.git", + "clone_url":"https://github.com/eeshangarg/public-repo.git", + "svn_url":"https://github.com/eeshangarg/public-repo", + "homepage":null, + "size":0, + "stargazers_count":0, + "watchers_count":0, + "language":null, + "has_issues":true, + "has_projects":true, + "has_downloads":true, + "has_wiki":true, + "has_pages":false, + "forks_count":0, + "mirror_url":null, + "open_issues_count":0, + "forks":0, + "open_issues":0, + "watchers":0, + "default_branch":"changes", + "stargazers":0, + "master_branch":"changes" + }, + "pusher":{ + "name":"eeshangarg", + "email":"jerryguitarist@gmail.com" + }, + "sender":{ + "login":"eeshangarg", + "id":7251823, + "avatar_url":"https://avatars3.githubusercontent.com/u/7251823?v=3", + "gravatar_id":"", + "url":"https://api.github.com/users/eeshangarg", + "html_url":"https://github.com/eeshangarg", + "followers_url":"https://api.github.com/users/eeshangarg/followers", + "following_url":"https://api.github.com/users/eeshangarg/following{/other_user}", + "gists_url":"https://api.github.com/users/eeshangarg/gists{/gist_id}", + "starred_url":"https://api.github.com/users/eeshangarg/starred{/owner}{/repo}", + "subscriptions_url":"https://api.github.com/users/eeshangarg/subscriptions", + "organizations_url":"https://api.github.com/users/eeshangarg/orgs", + "repos_url":"https://api.github.com/users/eeshangarg/repos", + "events_url":"https://api.github.com/users/eeshangarg/events{/privacy}", + "received_events_url":"https://api.github.com/users/eeshangarg/received_events", + "type":"User", + "site_admin":false + } +} diff --git a/zerver/webhooks/github_webhook/tests.py b/zerver/webhooks/github_webhook/tests.py index 5d0198be36..91b455abf8 100644 --- a/zerver/webhooks/github_webhook/tests.py +++ b/zerver/webhooks/github_webhook/tests.py @@ -28,6 +28,11 @@ class GithubWebhookTest(WebhookTestCase): expected_message = u"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))" self.send_and_test_stream_message('push_1_commit', self.EXPECTED_SUBJECT_BRANCH_EVENTS, expected_message, HTTP_X_GITHUB_EVENT='push') + def test_push_1_commit_without_username(self): + # type: () -> None + expected_message = u"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))" + self.send_and_test_stream_message('push_1_commit_without_username', self.EXPECTED_SUBJECT_BRANCH_EVENTS, expected_message, HTTP_X_GITHUB_EVENT='push') + def test_push_1_commit_filtered_by_branches(self): # type: () -> None self.url = self.build_webhook_url('master,changes') diff --git a/zerver/webhooks/github_webhook/view.py b/zerver/webhooks/github_webhook/view.py index 47954ca2d0..09719a17db 100644 --- a/zerver/webhooks/github_webhook/view.py +++ b/zerver/webhooks/github_webhook/view.py @@ -180,7 +180,8 @@ def get_push_tags_body(payload): def get_push_commits_body(payload): # type: (Dict[str, Any]) -> Text commits_data = [{ - 'name': commit['author']['username'], + 'name': (commit.get('author').get('username') or + commit.get('author').get('name')), 'sha': commit['id'], 'url': commit['url'], 'message': commit['message']