2018-01-18 15:34:32 +01:00
// Description:
// Script that listens to new GitHub pull requests
// and greets the user if it is their first PR on the repo
//
// Dependencies:
// github: "^13.1.0"
// hubot-github-webhook-listener: "^0.9.1"
//
// Author:
// PombeirP
module . exports = function ( robot ) {
2018-01-19 22:59:32 +01:00
const gitHubContext = require ( './github-context.js' ) ( ) ;
2018-01-18 15:34:32 +01:00
return robot . on ( "github-repo-event" , function ( repo _event ) {
const githubPayload = repo _event . payload ;
switch ( repo _event . eventType ) {
case "pull_request" :
// Make sure we don't listen to our own messages
2018-01-18 18:54:54 +01:00
if ( gitHubContext . equalsRobotName ( robot , githubPayload . pull _request . user . login ) ) { return ; }
2018-01-18 15:34:32 +01:00
var { action } = githubPayload ;
if ( action === "opened" ) {
// A new PR was opened
2018-01-18 18:54:54 +01:00
return greetNewContributor ( gitHubContext , githubPayload , robot ) ;
2018-01-18 15:34:32 +01:00
}
break ;
}
} ) ;
} ;
2018-01-18 18:54:54 +01:00
async function greetNewContributor ( gitHubContext , githubPayload , robot ) {
2018-01-18 15:34:32 +01:00
// TODO: Read the welcome message from a (per-repo?) file (e.g. status-react.welcome-msg.md)
2018-01-18 18:54:54 +01:00
const github = gitHubContext . api ( ) ;
2018-01-20 00:46:51 +01:00
const welcomeMessage = gitHubContext . config ( ) [ 'new-pull-requests' ] [ 'welcome-bot' ] . message ;
2018-01-18 15:34:32 +01:00
const ownerName = githubPayload . repository . owner . login ;
const repoName = githubPayload . repository . name ;
const prNumber = githubPayload . pull _request . number ;
robot . logger . info ( ` greetNewContributor - Handling Pull Request # ${ prNumber } on repo ${ ownerName } / ${ repoName } ` ) ;
try {
ghissues = await github . issues . getForRepo ( {
owner : ownerName ,
repo : repoName ,
state : 'all' ,
creator : githubPayload . pull _request . user . login
} )
const userPullRequests = ghissues . data . filter ( issue => issue . pull _request ) ;
if ( userPullRequests . length === 1 ) {
try {
await github . issues . createComment ( {
owner : ownerName ,
repo : repoName ,
number : prNumber ,
body : welcomeMessage
} )
2018-01-20 01:03:22 +01:00
// Send message to Slack
robot . messageRoom ( githubConfig . slack . notification . room , ` Greeted ${ githubPayload . pull _request . user . login } on his first PR ( ${ githubPayload . pull _request . title } ) in ${ repoName } \n ${ githubPayload . pull _request . html _url } ` ) ;
2018-01-18 15:34:32 +01:00
} catch ( err ) {
if ( err . code !== 404 ) {
robot . logger . error ( ` Couldn't create comment on PR: ${ err } ` , ownerName , repoName ) ;
}
}
} else {
robot . logger . debug ( "This is not the user's first PR on the repo, ignoring" , ownerName , repoName , githubPayload . pull _request . user . login ) ;
}
} catch ( err ) {
robot . logger . error ( ` Couldn't fetch the user's github issues for repo: ${ err } ` , ownerName , repoName ) ;
}
} ;