Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.

Commit a3d58ec

Browse files
committed
feat: DM for pull request review
1 parent f214bc4 commit a3d58ec

File tree

8 files changed

+152
-9
lines changed

8 files changed

+152
-9
lines changed

src/listener.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import * as Promise from 'bluebird';
44
import { SlackMessage } from 'botkit';
55
import * as http from 'http';
66
import { assign, forEach, get, has, includes, isEmpty, some } from 'lodash';
7-
import { Commit, Issue, MergeableState, StatusState, StatusWebhook } from './models/github';
7+
import {
8+
Commit,
9+
Issue,
10+
IssueState,
11+
MergeableState,
12+
PullRequestReviewWebhook,
13+
StatusState,
14+
StatusWebhook
15+
} from './models/github';
816
import { SlackAttachmentColor } from './models/slack';
917

1018
if (!process.env.GITHUB_TOKEN) {
@@ -67,6 +75,9 @@ export const messenger = controller => {
6775
break;
6876
}
6977
break;
78+
case 'pull_request_review':
79+
notifyPullRequestReview(payload);
80+
break;
7081
case 'status':
7182
checkStatus(payload);
7283
break;
@@ -209,6 +220,48 @@ export const messenger = controller => {
209220
});
210221
}
211222

223+
function notifyPullRequestReview(payload: PullRequestReviewWebhook) {
224+
controller.storage.users.all((err, all_user_data) => {
225+
if (err) {
226+
console.error(err);
227+
return;
228+
}
229+
230+
forEach(all_user_data, user => {
231+
if (payload.pull_request.user.login !== user.github_user) {
232+
// send message only to the PR owner
233+
return;
234+
}
235+
236+
// direct message to user
237+
bot.startPrivateConversation(
238+
{
239+
user: user.id
240+
},
241+
(err, convo) => {
242+
if (err) {
243+
console.error('failed to start private conversation', err);
244+
} else {
245+
convo.say({
246+
text: `Review ${payload.action} for *${payload.pull_request.base.repo.name}* by *${
247+
payload.sender.login
248+
}*:`,
249+
attachments: [
250+
{
251+
title: `#${payload.pull_request.number}: ${payload.pull_request.title}`,
252+
title_link: payload.pull_request.html_url,
253+
text: payload.review.body,
254+
mrkdwn_in: ['text']
255+
}
256+
]
257+
});
258+
}
259+
}
260+
);
261+
});
262+
});
263+
}
264+
212265
function checkStatus(payload: StatusWebhook) {
213266
if (payload.state === StatusState.PENDING) {
214267
// ignore non-final statuses
@@ -222,7 +275,7 @@ export const messenger = controller => {
222275
Promise.resolve(github.search.issues({ q: payload.sha }))
223276
.then(res => res.data.items as Issue[])
224277
// verify that the issue is not closed
225-
.filter((issue: Issue) => issue.state !== 'closed')
278+
.filter((issue: Issue) => issue.state !== IssueState.CLOSED)
226279
// verify that the commit is the latest, ignoring those for which it isn't
227280
.filter((issue: Issue) =>
228281
github.pullRequests

src/models/github/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
export * from './commit';
22
export * from './issue';
3+
export * from './issue-state';
4+
export * from './link';
35
export * from './mergeable-state';
46
export * from './organization';
7+
export * from './pull-request-review-webhook';
58
export * from './repository';
69
export * from './status';
7-
export * from './statusWebhook';
10+
export * from './status-webhook';
811
export * from './userish';

src/models/github/issue-state.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum IssueState {
2+
OPEN = 'open',
3+
CLOSED = 'closed'
4+
}

src/models/github/issue.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
import { IssueState } from './issue-state';
2+
import { Link } from './link';
13
import { MergeableState } from './mergeable-state';
24
import { Repository } from './repository';
35
import { Userish } from './userish';
46

5-
interface Link {
6-
href: string;
7-
}
8-
97
interface Label {
108
id: number;
119
node_id: string;
@@ -24,7 +22,7 @@ export interface Issue {
2422
patch_url: string;
2523
issue_url: string;
2624
number: number;
27-
state: 'open' | 'closed';
25+
state: IssueState;
2826
locked: boolean;
2927
title: string;
3028
user: Userish;

src/models/github/link.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Link {
2+
href: string;
3+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { IssueState } from './issue-state';
2+
import { Link } from './link';
3+
import { Repository } from './repository';
4+
import { Userish } from './userish';
5+
6+
export interface PullRequestReviewWebhook {
7+
action: 'submitted' | 'edited' | 'dismissed';
8+
review: {
9+
id: number;
10+
node_id: string;
11+
user: Userish;
12+
body: string;
13+
commit_id: string;
14+
submitted_at: string;
15+
state: 'commented' | string;
16+
html_url: string;
17+
pull_request_url: string;
18+
author_association: string;
19+
_links: {
20+
html: Link;
21+
pull_request: Link;
22+
};
23+
};
24+
pull_request: {
25+
url: string;
26+
id: number;
27+
node_id: string;
28+
html_url: string;
29+
diff_url: string;
30+
patch_url: string;
31+
issue_url: string;
32+
number: number;
33+
state: IssueState;
34+
locked: boolean;
35+
title: string;
36+
user: Userish;
37+
body: string;
38+
created_at: string;
39+
updated_at: string;
40+
closed_at: string;
41+
merged_at: string;
42+
merge_commit_sha: string;
43+
assignee: Userish;
44+
assignees: Userish[];
45+
requested_reviewers: Userish[];
46+
requested_teams: any[];
47+
labels: any[];
48+
milestone: string;
49+
commits_url: string;
50+
review_comments_url: string;
51+
review_comment_url: string;
52+
comments_url: string;
53+
statuses_url: string;
54+
head: {
55+
label: string;
56+
ref: string;
57+
sha: string;
58+
user: Userish;
59+
repo: Repository;
60+
};
61+
base: {
62+
label: string;
63+
ref: string;
64+
sha: string;
65+
user: Userish;
66+
repo: Repository;
67+
};
68+
_links: {
69+
self: Link;
70+
html: Link;
71+
issue: Link;
72+
comments: Link;
73+
review_comments: Link;
74+
review_comment: Link;
75+
commits: Link;
76+
statuses: Link;
77+
};
78+
author_association: string;
79+
};
80+
repository: Repository;
81+
sender: Userish;
82+
}

src/models/github/status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StatusState } from './statusWebhook';
1+
import { StatusState } from './status-webhook';
22
import { Userish } from './userish';
33

44
export interface Status {

0 commit comments

Comments
 (0)