@@ -4,7 +4,15 @@ import * as Promise from 'bluebird';
44import { SlackMessage } from 'botkit' ;
55import * as http from 'http' ;
66import { 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' ;
816import { SlackAttachmentColor } from './models/slack' ;
917
1018if ( ! 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
0 commit comments