From 2352493d92f943996d6b12ac07720bd29d86a4d2 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 5 Dec 2016 21:28:23 -0600 Subject: [PATCH] Fix error on push event --- lib/context.js | 6 +- test/context.js | 34 ++++-- test/fixtures/webhook/push.json | 181 ++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/webhook/push.json diff --git a/lib/context.js b/lib/context.js index d015de43..b465f183 100644 --- a/lib/context.js +++ b/lib/context.js @@ -8,9 +8,11 @@ module.exports = class Context { } toRepo(object) { + const repo = this.payload.repository; + return Object.assign({ - owner: this.payload.repository.owner.login, - repo: this.payload.repository.name + owner: repo.owner.login || repo.owner.name, + repo: repo.name }, object); } diff --git a/test/context.js b/test/context.js index 6ce93533..cebd3260 100644 --- a/test/context.js +++ b/test/context.js @@ -3,16 +3,21 @@ const Context = require('../lib/context'); describe('Context', () => { const github = {}; - const event = { - payload: { - repository: { - owner: {login: 'bkeepers'}, - name: 'probot' - }, - issue: {number: 4} - } - }; - const context = new Context(github, event); + let event; + let context; + + beforeEach(() => { + event = { + payload: { + repository: { + owner: {login: 'bkeepers'}, + name: 'probot' + }, + issue: {number: 4} + } + }; + context = new Context(github, event); + }); describe('toRepo', () => { it('returns attributes from repository payload', () => { @@ -30,6 +35,15 @@ describe('Context', () => { owner: 'muahaha', repo:'probot' }); }); + + // The `repository` object on the push event has a different format than the other events + // https://developer.github.com/v3/activity/events/types/#pushevent + it('properly handles the push event', () => { + event.payload = require('./fixtures/webhook/push'); + + context = new Context(github, event); + expect(context.toRepo()).toEqual({owner: 'bkeepers-inc', repo:'test'}); + }); }); describe('toIssue', () => { diff --git a/test/fixtures/webhook/push.json b/test/fixtures/webhook/push.json new file mode 100644 index 00000000..1150f983 --- /dev/null +++ b/test/fixtures/webhook/push.json @@ -0,0 +1,181 @@ +{ + "ref": "refs/heads/bkeepers-patch-3", + "before": "f5cd85485562ec4b0855c77cf44ecbed40c8e689", + "after": "738a951dba49dfec487000ef85ae80291d7ab32a", + "created": false, + "deleted": false, + "forced": false, + "base_ref": null, + "compare": "https://github.com/bkeepers-inc/test/compare/f5cd85485562...738a951dba49", + "commits": [ + { + "id": "738a951dba49dfec487000ef85ae80291d7ab32a", + "tree_id": "e69ee96a51b638bcf92c9eba8e1aeefa28fa0513", + "distinct": true, + "message": "Update README.md", + "timestamp": "2016-12-05T00:18:54-06:00", + "url": "https://github.com/bkeepers-inc/test/commit/738a951dba49dfec487000ef85ae80291d7ab32a", + "author": { + "name": "Brandon Keepers", + "email": "bkeepers@github.com", + "username": "bkeepers" + }, + "committer": { + "name": "GitHub", + "email": "noreply@github.com", + "username": "web-flow" + }, + "added": [ + + ], + "removed": [ + + ], + "modified": [ + "README.md" + ] + } + ], + "head_commit": { + "id": "738a951dba49dfec487000ef85ae80291d7ab32a", + "tree_id": "e69ee96a51b638bcf92c9eba8e1aeefa28fa0513", + "distinct": true, + "message": "Update README.md", + "timestamp": "2016-12-05T00:18:54-06:00", + "url": "https://github.com/bkeepers-inc/test/commit/738a951dba49dfec487000ef85ae80291d7ab32a", + "author": { + "name": "Brandon Keepers", + "email": "bkeepers@github.com", + "username": "bkeepers" + }, + "committer": { + "name": "GitHub", + "email": "noreply@github.com", + "username": "web-flow" + }, + "added": [ + + ], + "removed": [ + + ], + "modified": [ + "README.md" + ] + }, + "repository": { + "id": 68474533, + "name": "test", + "full_name": "bkeepers-inc/test", + "owner": { + "name": "bkeepers-inc", + "email": null + }, + "private": true, + "html_url": "https://github.com/bkeepers-inc/test", + "description": "personal botland", + "fork": false, + "url": "https://github.com/bkeepers-inc/test", + "forks_url": "https://api.github.com/repos/bkeepers-inc/test/forks", + "keys_url": "https://api.github.com/repos/bkeepers-inc/test/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/bkeepers-inc/test/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/bkeepers-inc/test/teams", + "hooks_url": "https://api.github.com/repos/bkeepers-inc/test/hooks", + "issue_events_url": "https://api.github.com/repos/bkeepers-inc/test/issues/events{/number}", + "events_url": "https://api.github.com/repos/bkeepers-inc/test/events", + "assignees_url": "https://api.github.com/repos/bkeepers-inc/test/assignees{/user}", + "branches_url": "https://api.github.com/repos/bkeepers-inc/test/branches{/branch}", + "tags_url": "https://api.github.com/repos/bkeepers-inc/test/tags", + "blobs_url": "https://api.github.com/repos/bkeepers-inc/test/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/bkeepers-inc/test/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/bkeepers-inc/test/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/bkeepers-inc/test/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/bkeepers-inc/test/statuses/{sha}", + "languages_url": "https://api.github.com/repos/bkeepers-inc/test/languages", + "stargazers_url": "https://api.github.com/repos/bkeepers-inc/test/stargazers", + "contributors_url": "https://api.github.com/repos/bkeepers-inc/test/contributors", + "subscribers_url": "https://api.github.com/repos/bkeepers-inc/test/subscribers", + "subscription_url": "https://api.github.com/repos/bkeepers-inc/test/subscription", + "commits_url": "https://api.github.com/repos/bkeepers-inc/test/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/bkeepers-inc/test/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/bkeepers-inc/test/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/bkeepers-inc/test/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/bkeepers-inc/test/contents/{+path}", + "compare_url": "https://api.github.com/repos/bkeepers-inc/test/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/bkeepers-inc/test/merges", + "archive_url": "https://api.github.com/repos/bkeepers-inc/test/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/bkeepers-inc/test/downloads", + "issues_url": "https://api.github.com/repos/bkeepers-inc/test/issues{/number}", + "pulls_url": "https://api.github.com/repos/bkeepers-inc/test/pulls{/number}", + "milestones_url": "https://api.github.com/repos/bkeepers-inc/test/milestones{/number}", + "notifications_url": "https://api.github.com/repos/bkeepers-inc/test/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/bkeepers-inc/test/labels{/name}", + "releases_url": "https://api.github.com/repos/bkeepers-inc/test/releases{/id}", + "deployments_url": "https://api.github.com/repos/bkeepers-inc/test/deployments", + "created_at": 1474141446, + "updated_at": "2016-11-18T21:04:26Z", + "pushed_at": 1480918735, + "git_url": "git://github.com/bkeepers-inc/test.git", + "ssh_url": "git@github.com:bkeepers-inc/test.git", + "clone_url": "https://github.com/bkeepers-inc/test.git", + "svn_url": "https://github.com/bkeepers-inc/test", + "homepage": null, + "size": 18, + "stargazers_count": 0, + "watchers_count": 0, + "language": "JavaScript", + "has_issues": true, + "has_downloads": true, + "has_wiki": true, + "has_pages": false, + "forks_count": 0, + "mirror_url": null, + "open_issues_count": 16, + "forks": 0, + "open_issues": 16, + "watchers": 0, + "default_branch": "master", + "stargazers": 0, + "master_branch": "master", + "organization": "bkeepers-inc" + }, + "pusher": { + "name": "bkeepers", + "email": "bkeepers@github.com" + }, + "organization": { + "login": "bkeepers-inc", + "id": 11724939, + "url": "https://api.github.com/orgs/bkeepers-inc", + "repos_url": "https://api.github.com/orgs/bkeepers-inc/repos", + "events_url": "https://api.github.com/orgs/bkeepers-inc/events", + "hooks_url": "https://api.github.com/orgs/bkeepers-inc/hooks", + "issues_url": "https://api.github.com/orgs/bkeepers-inc/issues", + "members_url": "https://api.github.com/orgs/bkeepers-inc/members{/member}", + "public_members_url": "https://api.github.com/orgs/bkeepers-inc/public_members{/member}", + "avatar_url": "https://avatars.githubusercontent.com/u/11724939?v=3", + "description": null + }, + "sender": { + "login": "bkeepers", + "id": 173, + "avatar_url": "https://avatars.githubusercontent.com/u/173?v=3", + "gravatar_id": "", + "url": "https://api.github.com/users/bkeepers", + "html_url": "https://github.com/bkeepers", + "followers_url": "https://api.github.com/users/bkeepers/followers", + "following_url": "https://api.github.com/users/bkeepers/following{/other_user}", + "gists_url": "https://api.github.com/users/bkeepers/gists{/gist_id}", + "starred_url": "https://api.github.com/users/bkeepers/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/bkeepers/subscriptions", + "organizations_url": "https://api.github.com/users/bkeepers/orgs", + "repos_url": "https://api.github.com/users/bkeepers/repos", + "events_url": "https://api.github.com/users/bkeepers/events{/privacy}", + "received_events_url": "https://api.github.com/users/bkeepers/received_events", + "type": "User", + "site_admin": true + }, + "installation": { + "id": 1729 + } +}