From cd6aeb0f6fd8084f5c3469cb89acd18d99e886da Mon Sep 17 00:00:00 2001 From: Neil Kalman Date: Thu, 29 Apr 2021 15:00:50 +0000 Subject: [PATCH] feat(engine): add pr closed handling for now, it deletes the pr. look at added TODO to see the wanted behavior. but for now, this is good enough related to #177 --- server/src/abstracts/engine.abstract.ts | 5 +- .../webhook-event-manager.service.ts | 10 + .../pull-request-closed.event.ts | 487 ++++++++++++++++++ server/src/dev-tools/index.ts | 1 + server/src/engines/github.engine.ts | 27 + .../github-events.e2e-spec.ts.snap | 29 ++ server/test/github-events.e2e-spec.ts | 23 +- 7 files changed, 579 insertions(+), 3 deletions(-) create mode 100644 server/src/dev-tools/captured-events/pull-request-closed.event.ts diff --git a/server/src/abstracts/engine.abstract.ts b/server/src/abstracts/engine.abstract.ts index 3d62ce35..a5033e09 100644 --- a/server/src/abstracts/engine.abstract.ts +++ b/server/src/abstracts/engine.abstract.ts @@ -13,7 +13,8 @@ export enum AchievibitEventNames { PullRequestReviewCommentRemoved = 'PullRequestReviewCommentRemoved', PullRequestReviewCommentEdited = 'PullRequestReviewCommentEdited', PullRequestReviewSubmitted = 'PullRequestReviewSubmitted', - PullRequestMerged = 'PullRequestMerged' + PullRequestMerged = 'PullRequestMerged', + PullRequestClosed = 'PullRequestClosed' } export abstract class Engine { @@ -175,4 +176,6 @@ export abstract class Engine { * @param eventData the event payload */ abstract handlePullRequestMerged(eventData: IEventPayload): Promise; + + abstract handlePullRequestClosed(eventData: IEventPayload): Promise; } diff --git a/server/src/api/webhook-event-manager/webhook-event-manager.service.ts b/server/src/api/webhook-event-manager/webhook-event-manager.service.ts index e761bdbf..65ca02e9 100644 --- a/server/src/api/webhook-event-manager/webhook-event-manager.service.ts +++ b/server/src/api/webhook-event-manager/webhook-event-manager.service.ts @@ -91,6 +91,10 @@ export class WebhookEventManagerService { this.logger.debug('PullRequestMerged'); await this.githubEngine.handlePullRequestMerged(eventData); return eventName; + case AchievibitEventNames.PullRequestClosed: + this.logger.debug('PullRequestClosed'); + await this.githubEngine.handlePullRequestClosed(eventData); + return eventName; } } @@ -179,6 +183,12 @@ export class WebhookEventManagerService { return AchievibitEventNames.PullRequestMerged; } + if (isEqual(eventName, 'pull_request') && + isEqual(eventData.action, 'closed') && + !eventData.pull_request.merged) { + return AchievibitEventNames.PullRequestClosed; + } + return; } } diff --git a/server/src/dev-tools/captured-events/pull-request-closed.event.ts b/server/src/dev-tools/captured-events/pull-request-closed.event.ts new file mode 100644 index 00000000..3c15b27b --- /dev/null +++ b/server/src/dev-tools/captured-events/pull-request-closed.event.ts @@ -0,0 +1,487 @@ +export const pullRequestClosedEvent = { + 'event': 'pull_request', + 'payload': { + 'action': 'closed', + 'number': 1, + 'pull_request': { + 'url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/3', + 'id': 356096871, + 'node_id': 'MDExOlB1bGxSZXF1ZXN0MzU2MDk2ODcx', + 'html_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events/pull/3', + 'diff_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events/pull/3.diff', + 'patch_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events/pull/3.patch', + 'issue_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/3', + 'number': 1, + 'state': 'closed', + 'locked': false, + 'title': 'Update README.md updated the title', + 'user': { + 'login': 'Thatkookooguy', + 'id': 10427304, + 'node_id': 'MDQ6VXNlcjEwNDI3MzA0', + 'avatar_url': 'https://avatars.githubusercontent.com/u/10427304?v=4', + 'gravatar_id': '', + 'url': 'https://api.github.com/users/Thatkookooguy', + 'html_url': 'https://github.com/Thatkookooguy', + 'followers_url': 'https://api.github.com/users/Thatkookooguy/followers', + 'following_url': 'https://api.github.com/users/Thatkookooguy/following{/other_user}', + 'gists_url': 'https://api.github.com/users/Thatkookooguy/gists{/gist_id}', + 'starred_url': 'https://api.github.com/users/Thatkookooguy/starred{/owner}{/repo}', + 'subscriptions_url': 'https://api.github.com/users/Thatkookooguy/subscriptions', + 'organizations_url': 'https://api.github.com/users/Thatkookooguy/orgs', + 'repos_url': 'https://api.github.com/users/Thatkookooguy/repos', + 'events_url': 'https://api.github.com/users/Thatkookooguy/events{/privacy}', + 'received_events_url': 'https://api.github.com/users/Thatkookooguy/received_events', + 'type': 'User', + 'site_admin': false + }, + 'body': 'edit description', + 'created_at': '2019-12-22T15:39:59Z', + 'updated_at': '2021-04-29T14:18:19Z', + 'closed_at': '2021-04-29T14:18:19Z', + 'merged_at': null, + 'merge_commit_sha': 'a60b153551aff574c7bc716c34e401cc07143558', + 'assignee': null, + 'assignees': [], + 'requested_reviewers': [], + 'requested_teams': [], + 'labels': [ + { + 'id': 1730326713, + 'node_id': 'MDU6TGFiZWwxNzMwMzI2NzEz', + 'url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/labels/bug', + 'name': 'bug', + 'color': 'd73a4a', + 'default': true, + 'description': 'Something isn\'t working' + }, + { + 'id': 1730326714, + 'node_id': 'MDU6TGFiZWwxNzMwMzI2NzE0', + 'url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/labels/documentation', + 'name': 'documentation', + 'color': '0075ca', + 'default': true, + 'description': 'Improvements or additions to documentation' + }, + { + 'id': 1730326716, + 'node_id': 'MDU6TGFiZWwxNzMwMzI2NzE2', + 'url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/labels/duplicate', + 'name': 'duplicate', + 'color': 'cfd3d7', + 'default': true, + 'description': 'This issue or pull request already exists' + } + ], + 'milestone': null, + 'draft': false, + 'commits_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/3/commits', + 'review_comments_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/3/comments', + 'review_comment_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/comments{/number}', + 'comments_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/3/comments', + 'statuses_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/statuses/e2e7b8e3ae0e3a2dc9706f8cd63a10030c525023', + 'head': { + 'label': 'Thatkookooguy:Thatkookooguy-patch-3', + 'ref': 'Thatkookooguy-patch-3', + 'sha': 'e2e7b8e3ae0e3a2dc9706f8cd63a10030c525023', + 'user': { + 'login': 'Thatkookooguy', + 'id': 10427304, + 'node_id': 'MDQ6VXNlcjEwNDI3MzA0', + 'avatar_url': 'https://avatars.githubusercontent.com/u/10427304?v=4', + 'gravatar_id': '', + 'url': 'https://api.github.com/users/Thatkookooguy', + 'html_url': 'https://github.com/Thatkookooguy', + 'followers_url': 'https://api.github.com/users/Thatkookooguy/followers', + 'following_url': 'https://api.github.com/users/Thatkookooguy/following{/other_user}', + 'gists_url': 'https://api.github.com/users/Thatkookooguy/gists{/gist_id}', + 'starred_url': 'https://api.github.com/users/Thatkookooguy/starred{/owner}{/repo}', + 'subscriptions_url': 'https://api.github.com/users/Thatkookooguy/subscriptions', + 'organizations_url': 'https://api.github.com/users/Thatkookooguy/orgs', + 'repos_url': 'https://api.github.com/users/Thatkookooguy/repos', + 'events_url': 'https://api.github.com/users/Thatkookooguy/events{/privacy}', + 'received_events_url': 'https://api.github.com/users/Thatkookooguy/received_events', + 'type': 'User', + 'site_admin': false + }, + 'repo': { + 'id': 227616181, + 'node_id': 'MDEwOlJlcG9zaXRvcnkyMjc2MTYxODE=', + 'name': 'test-new-achievibit-events', + 'full_name': 'Thatkookooguy/test-new-achievibit-events', + 'private': false, + 'owner': { + 'login': 'Thatkookooguy', + 'id': 10427304, + 'node_id': 'MDQ6VXNlcjEwNDI3MzA0', + 'avatar_url': 'https://avatars.githubusercontent.com/u/10427304?v=4', + 'gravatar_id': '', + 'url': 'https://api.github.com/users/Thatkookooguy', + 'html_url': 'https://github.com/Thatkookooguy', + 'followers_url': 'https://api.github.com/users/Thatkookooguy/followers', + 'following_url': 'https://api.github.com/users/Thatkookooguy/following{/other_user}', + 'gists_url': 'https://api.github.com/users/Thatkookooguy/gists{/gist_id}', + 'starred_url': 'https://api.github.com/users/Thatkookooguy/starred{/owner}{/repo}', + 'subscriptions_url': 'https://api.github.com/users/Thatkookooguy/subscriptions', + 'organizations_url': 'https://api.github.com/users/Thatkookooguy/orgs', + 'repos_url': 'https://api.github.com/users/Thatkookooguy/repos', + 'events_url': 'https://api.github.com/users/Thatkookooguy/events{/privacy}', + 'received_events_url': 'https://api.github.com/users/Thatkookooguy/received_events', + 'type': 'User', + 'site_admin': false + }, + 'html_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events', + 'description': null, + 'fork': false, + 'url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events', + 'forks_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/forks', + 'keys_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/keys{/key_id}', + 'collaborators_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/collaborators{/collaborator}', + 'teams_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/teams', + 'hooks_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/hooks', + 'issue_events_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/events{/number}', + 'events_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/events', + 'assignees_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/assignees{/user}', + 'branches_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/branches{/branch}', + 'tags_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/tags', + 'blobs_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/blobs{/sha}', + 'git_tags_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/tags{/sha}', + 'git_refs_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/refs{/sha}', + 'trees_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/trees{/sha}', + 'statuses_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/statuses/{sha}', + 'languages_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/languages', + 'stargazers_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/stargazers', + 'contributors_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/contributors', + 'subscribers_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/subscribers', + 'subscription_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/subscription', + 'commits_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/commits{/sha}', + 'git_commits_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/commits{/sha}', + 'comments_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/comments{/number}', + 'issue_comment_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/comments{/number}', + 'contents_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/contents/{+path}', + 'compare_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/compare/{base}...{head}', + 'merges_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/merges', + 'archive_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/{archive_format}{/ref}', + 'downloads_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/downloads', + 'issues_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues{/number}', + 'pulls_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls{/number}', + 'milestones_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/milestones{/number}', + 'notifications_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/notifications{?since,all,participating}', + 'labels_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/labels{/name}', + 'releases_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/releases{/id}', + 'deployments_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/deployments', + 'created_at': '2019-12-12T13:41:55Z', + 'updated_at': '2019-12-26T14:00:47Z', + 'pushed_at': '2019-12-26T14:00:45Z', + 'git_url': 'git://github.com/Thatkookooguy/test-new-achievibit-events.git', + 'ssh_url': 'git@github.com:Thatkookooguy/test-new-achievibit-events.git', + 'clone_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events.git', + 'svn_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events', + 'homepage': null, + 'size': 65, + 'stargazers_count': 0, + 'watchers_count': 0, + 'language': 'HTML', + 'has_issues': true, + 'has_projects': true, + 'has_downloads': true, + 'has_wiki': true, + 'has_pages': false, + 'forks_count': 0, + 'mirror_url': null, + 'archived': false, + 'disabled': false, + 'open_issues_count': 1, + 'license': null, + 'forks': 0, + 'open_issues': 1, + 'watchers': 0, + 'default_branch': 'master', + 'allow_squash_merge': true, + 'allow_merge_commit': true, + 'allow_rebase_merge': true, + 'delete_branch_on_merge': false + } + }, + 'base': { + 'label': 'Thatkookooguy:master', + 'ref': 'master', + 'sha': '811f7789eb40784e8269cd9278fa2f5652db9d83', + 'user': { + 'login': 'Thatkookooguy', + 'id': 10427304, + 'node_id': 'MDQ6VXNlcjEwNDI3MzA0', + 'avatar_url': 'https://avatars.githubusercontent.com/u/10427304?v=4', + 'gravatar_id': '', + 'url': 'https://api.github.com/users/Thatkookooguy', + 'html_url': 'https://github.com/Thatkookooguy', + 'followers_url': 'https://api.github.com/users/Thatkookooguy/followers', + 'following_url': 'https://api.github.com/users/Thatkookooguy/following{/other_user}', + 'gists_url': 'https://api.github.com/users/Thatkookooguy/gists{/gist_id}', + 'starred_url': 'https://api.github.com/users/Thatkookooguy/starred{/owner}{/repo}', + 'subscriptions_url': 'https://api.github.com/users/Thatkookooguy/subscriptions', + 'organizations_url': 'https://api.github.com/users/Thatkookooguy/orgs', + 'repos_url': 'https://api.github.com/users/Thatkookooguy/repos', + 'events_url': 'https://api.github.com/users/Thatkookooguy/events{/privacy}', + 'received_events_url': 'https://api.github.com/users/Thatkookooguy/received_events', + 'type': 'User', + 'site_admin': false + }, + 'repo': { + 'id': 227616181, + 'node_id': 'MDEwOlJlcG9zaXRvcnkyMjc2MTYxODE=', + 'name': 'test-new-achievibit-events', + 'full_name': 'Thatkookooguy/test-new-achievibit-events', + 'private': false, + 'owner': { + 'login': 'Thatkookooguy', + 'id': 10427304, + 'node_id': 'MDQ6VXNlcjEwNDI3MzA0', + 'avatar_url': 'https://avatars.githubusercontent.com/u/10427304?v=4', + 'gravatar_id': '', + 'url': 'https://api.github.com/users/Thatkookooguy', + 'html_url': 'https://github.com/Thatkookooguy', + 'followers_url': 'https://api.github.com/users/Thatkookooguy/followers', + 'following_url': 'https://api.github.com/users/Thatkookooguy/following{/other_user}', + 'gists_url': 'https://api.github.com/users/Thatkookooguy/gists{/gist_id}', + 'starred_url': 'https://api.github.com/users/Thatkookooguy/starred{/owner}{/repo}', + 'subscriptions_url': 'https://api.github.com/users/Thatkookooguy/subscriptions', + 'organizations_url': 'https://api.github.com/users/Thatkookooguy/orgs', + 'repos_url': 'https://api.github.com/users/Thatkookooguy/repos', + 'events_url': 'https://api.github.com/users/Thatkookooguy/events{/privacy}', + 'received_events_url': 'https://api.github.com/users/Thatkookooguy/received_events', + 'type': 'User', + 'site_admin': false + }, + 'html_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events', + 'description': null, + 'fork': false, + 'url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events', + 'forks_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/forks', + 'keys_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/keys{/key_id}', + 'collaborators_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/collaborators{/collaborator}', + 'teams_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/teams', + 'hooks_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/hooks', + 'issue_events_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/events{/number}', + 'events_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/events', + 'assignees_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/assignees{/user}', + 'branches_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/branches{/branch}', + 'tags_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/tags', + 'blobs_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/blobs{/sha}', + 'git_tags_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/tags{/sha}', + 'git_refs_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/refs{/sha}', + 'trees_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/trees{/sha}', + 'statuses_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/statuses/{sha}', + 'languages_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/languages', + 'stargazers_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/stargazers', + 'contributors_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/contributors', + 'subscribers_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/subscribers', + 'subscription_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/subscription', + 'commits_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/commits{/sha}', + 'git_commits_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/commits{/sha}', + 'comments_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/comments{/number}', + 'issue_comment_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/comments{/number}', + 'contents_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/contents/{+path}', + 'compare_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/compare/{base}...{head}', + 'merges_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/merges', + 'archive_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/{archive_format}{/ref}', + 'downloads_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/downloads', + 'issues_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues{/number}', + 'pulls_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls{/number}', + 'milestones_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/milestones{/number}', + 'notifications_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/notifications{?since,all,participating}', + 'labels_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/labels{/name}', + 'releases_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/releases{/id}', + 'deployments_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/deployments', + 'created_at': '2019-12-12T13:41:55Z', + 'updated_at': '2019-12-26T14:00:47Z', + 'pushed_at': '2019-12-26T14:00:45Z', + 'git_url': 'git://github.com/Thatkookooguy/test-new-achievibit-events.git', + 'ssh_url': 'git@github.com:Thatkookooguy/test-new-achievibit-events.git', + 'clone_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events.git', + 'svn_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events', + 'homepage': null, + 'size': 65, + 'stargazers_count': 0, + 'watchers_count': 0, + 'language': 'HTML', + 'has_issues': true, + 'has_projects': true, + 'has_downloads': true, + 'has_wiki': true, + 'has_pages': false, + 'forks_count': 0, + 'mirror_url': null, + 'archived': false, + 'disabled': false, + 'open_issues_count': 1, + 'license': null, + 'forks': 0, + 'open_issues': 1, + 'watchers': 0, + 'default_branch': 'master', + 'allow_squash_merge': true, + 'allow_merge_commit': true, + 'allow_rebase_merge': true, + 'delete_branch_on_merge': false + } + }, + '_links': { + 'self': { + 'href': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/3' + }, + 'html': { + 'href': 'https://github.com/Thatkookooguy/test-new-achievibit-events/pull/3' + }, + 'issue': { + 'href': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/3' + }, + 'comments': { + 'href': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/3/comments' + }, + 'review_comments': { + 'href': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/3/comments' + }, + 'review_comment': { + 'href': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/comments{/number}' + }, + 'commits': { + 'href': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/3/commits' + }, + 'statuses': { + 'href': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/statuses/e2e7b8e3ae0e3a2dc9706f8cd63a10030c525023' + } + }, + 'author_association': 'OWNER', + 'auto_merge': null, + 'active_lock_reason': null, + 'merged': false, + 'mergeable': true, + 'rebaseable': false, + 'mergeable_state': 'clean', + 'merged_by': null, + 'comments': 0, + 'review_comments': 0, + 'maintainer_can_modify': false, + 'commits': 1, + 'additions': 2, + 'deletions': 0, + 'changed_files': 1 + }, + 'repository': { + 'id': 227616181, + 'node_id': 'MDEwOlJlcG9zaXRvcnkyMjc2MTYxODE=', + 'name': 'test-new-achievibit-events', + 'full_name': 'Thatkookooguy/test-new-achievibit-events', + 'private': false, + 'owner': { + 'login': 'Thatkookooguy', + 'id': 10427304, + 'node_id': 'MDQ6VXNlcjEwNDI3MzA0', + 'avatar_url': 'https://avatars.githubusercontent.com/u/10427304?v=4', + 'gravatar_id': '', + 'url': 'https://api.github.com/users/Thatkookooguy', + 'html_url': 'https://github.com/Thatkookooguy', + 'followers_url': 'https://api.github.com/users/Thatkookooguy/followers', + 'following_url': 'https://api.github.com/users/Thatkookooguy/following{/other_user}', + 'gists_url': 'https://api.github.com/users/Thatkookooguy/gists{/gist_id}', + 'starred_url': 'https://api.github.com/users/Thatkookooguy/starred{/owner}{/repo}', + 'subscriptions_url': 'https://api.github.com/users/Thatkookooguy/subscriptions', + 'organizations_url': 'https://api.github.com/users/Thatkookooguy/orgs', + 'repos_url': 'https://api.github.com/users/Thatkookooguy/repos', + 'events_url': 'https://api.github.com/users/Thatkookooguy/events{/privacy}', + 'received_events_url': 'https://api.github.com/users/Thatkookooguy/received_events', + 'type': 'User', + 'site_admin': false + }, + 'html_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events', + 'description': null, + 'fork': false, + 'url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events', + 'forks_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/forks', + 'keys_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/keys{/key_id}', + 'collaborators_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/collaborators{/collaborator}', + 'teams_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/teams', + 'hooks_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/hooks', + 'issue_events_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/events{/number}', + 'events_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/events', + 'assignees_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/assignees{/user}', + 'branches_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/branches{/branch}', + 'tags_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/tags', + 'blobs_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/blobs{/sha}', + 'git_tags_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/tags{/sha}', + 'git_refs_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/refs{/sha}', + 'trees_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/trees{/sha}', + 'statuses_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/statuses/{sha}', + 'languages_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/languages', + 'stargazers_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/stargazers', + 'contributors_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/contributors', + 'subscribers_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/subscribers', + 'subscription_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/subscription', + 'commits_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/commits{/sha}', + 'git_commits_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/git/commits{/sha}', + 'comments_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/comments{/number}', + 'issue_comment_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/comments{/number}', + 'contents_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/contents/{+path}', + 'compare_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/compare/{base}...{head}', + 'merges_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/merges', + 'archive_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/{archive_format}{/ref}', + 'downloads_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/downloads', + 'issues_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues{/number}', + 'pulls_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls{/number}', + 'milestones_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/milestones{/number}', + 'notifications_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/notifications{?since,all,participating}', + 'labels_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/labels{/name}', + 'releases_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/releases{/id}', + 'deployments_url': 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/deployments', + 'created_at': '2019-12-12T13:41:55Z', + 'updated_at': '2019-12-26T14:00:47Z', + 'pushed_at': '2019-12-26T14:00:45Z', + 'git_url': 'git://github.com/Thatkookooguy/test-new-achievibit-events.git', + 'ssh_url': 'git@github.com:Thatkookooguy/test-new-achievibit-events.git', + 'clone_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events.git', + 'svn_url': 'https://github.com/Thatkookooguy/test-new-achievibit-events', + 'homepage': null, + 'size': 65, + 'stargazers_count': 0, + 'watchers_count': 0, + 'language': 'HTML', + 'has_issues': true, + 'has_projects': true, + 'has_downloads': true, + 'has_wiki': true, + 'has_pages': false, + 'forks_count': 0, + 'mirror_url': null, + 'archived': false, + 'disabled': false, + 'open_issues_count': 1, + 'license': null, + 'forks': 0, + 'open_issues': 1, + 'watchers': 0, + 'default_branch': 'master' + }, + 'sender': { + 'login': 'Thatkookooguy', + 'id': 10427304, + 'node_id': 'MDQ6VXNlcjEwNDI3MzA0', + 'avatar_url': 'https://avatars.githubusercontent.com/u/10427304?v=4', + 'gravatar_id': '', + 'url': 'https://api.github.com/users/Thatkookooguy', + 'html_url': 'https://github.com/Thatkookooguy', + 'followers_url': 'https://api.github.com/users/Thatkookooguy/followers', + 'following_url': 'https://api.github.com/users/Thatkookooguy/following{/other_user}', + 'gists_url': 'https://api.github.com/users/Thatkookooguy/gists{/gist_id}', + 'starred_url': 'https://api.github.com/users/Thatkookooguy/starred{/owner}{/repo}', + 'subscriptions_url': 'https://api.github.com/users/Thatkookooguy/subscriptions', + 'organizations_url': 'https://api.github.com/users/Thatkookooguy/orgs', + 'repos_url': 'https://api.github.com/users/Thatkookooguy/repos', + 'events_url': 'https://api.github.com/users/Thatkookooguy/events{/privacy}', + 'received_events_url': 'https://api.github.com/users/Thatkookooguy/received_events', + 'type': 'User', + 'site_admin': false + } + } +} diff --git a/server/src/dev-tools/index.ts b/server/src/dev-tools/index.ts index 174022cd..224c55a0 100644 --- a/server/src/dev-tools/index.ts +++ b/server/src/dev-tools/index.ts @@ -7,6 +7,7 @@ export * from './dto.mock-generator'; export * from './in-memory-database.module'; export * from './captured-events/pull-request-assignee-added.event'; export * from './captured-events/pull-request-assignee-removed.event'; +export * from './captured-events/pull-request-closed.event'; export * from './captured-events/pull-request-created-organization.event'; export * from './captured-events/pull-request-created.event'; export * from './captured-events/pull-request-edited.event'; diff --git a/server/src/engines/github.engine.ts b/server/src/engines/github.engine.ts index 671c760d..c7d1c758 100644 --- a/server/src/engines/github.engine.ts +++ b/server/src/engines/github.engine.ts @@ -301,6 +301,33 @@ export class GithubEngine extends Engine { throw new Error('Method not implemented.'); } + async handlePullRequestClosed( + eventData: IGithubPullRequestEvent + ): Promise { + const { + githubCreator, + githubOwner, + githubPR + } = this.extractGithubEntities(eventData); + const pr = this.extractPullRequest( + githubPR, + this.extractUser(githubCreator), + this.extractRepo(eventData.repository), + this.extractUser(githubOwner) + ); + + /** + * TODO@Thatkookooguy: #340 Combine PR deletion to a single task + * This should later just flag the PR as closed\merged and should + * be dealt with in the task that runs at the start of the week. + * This way, we can just make the task also delete closed or merged prs + * but give achievements for some actions we want to allow before deleting + * all the data. Something like commiting to a branch with a closed pr + * or re-opening a pr. + */ + await this.pullRequestsService.deleteAsync({ prid: pr.prid }); + } + private extractGithubEntities(eventData: IGithubPullRequestEvent) { return { githubPR: eventData.pull_request, diff --git a/server/test/__snapshots__/github-events.e2e-spec.ts.snap b/server/test/__snapshots__/github-events.e2e-spec.ts.snap index b1fb529c..611788c2 100644 --- a/server/test/__snapshots__/github-events.e2e-spec.ts.snap +++ b/server/test/__snapshots__/github-events.e2e-spec.ts.snap @@ -114,6 +114,35 @@ Array [ ] `; +exports[`AppController (e2e) pr events / (POST) github pull request closed 1`] = `"PullRequestClosed"`; + +exports[`AppController (e2e) pr events / (POST) github pull request closed 2`] = ` +Array [ + Object { + "achievements": Array [], + "avatar": "https://avatars3.githubusercontent.com/u/10427304?v=4", + "organization": false, + "organizations": Array [], + "repos": Array [], + "url": "https://github.com/Thatkookooguy", + "username": "Thatkookooguy", + "users": Array [], + }, +] +`; + +exports[`AppController (e2e) pr events / (POST) github pull request closed 3`] = ` +Array [ + Object { + "fullname": "Thatkookooguy/test-new-achievibit-events", + "name": "test-new-achievibit-events", + "url": "https://github.com/Thatkookooguy/test-new-achievibit-events", + }, +] +`; + +exports[`AppController (e2e) pr events / (POST) github pull request closed 4`] = `Array []`; + exports[`AppController (e2e) pr events / (POST) github pull request created event should create user 1`] = `"PullRequestOpened"`; exports[`AppController (e2e) pr events / (POST) github pull request created event should create user 2`] = ` diff --git a/server/test/github-events.e2e-spec.ts b/server/test/github-events.e2e-spec.ts index f0e1e9d1..1d58aa77 100644 --- a/server/test/github-events.e2e-spec.ts +++ b/server/test/github-events.e2e-spec.ts @@ -9,6 +9,7 @@ import { ConfigService } from '@kb-config'; import { pullRequestAssigneeAddedEvent, pullRequestAssigneeRemovedEvent, + pullRequestClosedEvent, pullRequestCreatedEvent, pullRequestEditedEvent, pullRequestLabelAddedEvent, @@ -372,8 +373,26 @@ describe('AppController (e2e)', () => { await confirmPrDataCreated(); }); - test.todo('review comment added'); - test.todo('review comment removed'); + test('/ (POST) github pull request closed', async () => { + const server = app.getHttpServer(); + await request(server) + .post('/api/webhook-event-manager') + .set('Accept', 'application/json') + .set('x-github-event', pullRequestCreatedEvent.event) + .send(pullRequestCreatedEvent.payload) + .expect(201); + const sendWebhookResponse = await request(server) + .post('/api/webhook-event-manager') + .set('Accept', 'application/json') + .set('x-github-event', + pullRequestClosedEvent.event) + .send(pullRequestClosedEvent.payload) + .expect(201); + + expect(sendWebhookResponse.text).toMatchSnapshot(); + + await confirmPrDataCreated(); + }); async function confirmPrDataCreated() { const server = app.getHttpServer();