2018-01-22 13:48:57 +00: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"
2018-01-23 18:38:25 +00:00
// probot-config: "^0.1.0"
2018-01-22 13:48:57 +00:00
// probot-slack-status: "^0.2.2"
//
// Author:
// PombeirP
2018-01-28 08:24:30 +00:00
// const getConfig = require('probot-config')
2018-01-23 14:27:25 +00:00
const defaultConfig = require ( '../lib/config' )
const Slack = require ( 'probot-slack-status' )
2018-01-22 13:48:57 +00:00
2018-01-23 14:27:25 +00:00
let slackClient = null
2018-01-22 13:48:57 +00:00
2018-01-28 08:24:30 +00:00
module . exports = ( robot ) => {
2018-01-22 13:48:57 +00:00
// robot.on('slack.connected', ({ slack }) => {
Slack ( robot , ( slack ) => {
2018-01-28 08:24:30 +00:00
robot . log . trace ( 'Connected, assigned slackClient' )
2018-01-23 14:27:25 +00:00
slackClient = slack
} )
2018-01-28 08:24:30 +00:00
2018-01-22 13:48:57 +00:00
robot . on ( 'pull_request.opened' , async context => {
// Make sure we don't listen to our own messages
2018-01-23 14:27:25 +00:00
if ( context . isBot ) { return }
2018-01-28 08:24:30 +00:00
2018-01-22 13:48:57 +00:00
// A new PR was opened
2018-01-23 14:27:25 +00:00
await greetNewContributor ( context , robot )
} )
}
2018-01-22 13:48:57 +00:00
2018-01-28 08:24:30 +00:00
async function greetNewContributor ( context , robot ) {
2018-02-07 16:04:59 +00:00
const { github , payload } = context
2018-01-28 08:24:30 +00:00
// const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
2018-01-23 14:27:25 +00:00
const config = defaultConfig ( robot , '.github/github-bot.yml' )
const ownerName = payload . repository . owner . login
const repoName = payload . repository . name
const prNumber = payload . pull _request . number
2018-01-28 08:24:30 +00:00
const welcomeBotConfig = config [ 'welcome-bot' ]
if ( ! welcomeBotConfig ) {
return
2018-01-23 14:30:24 +00:00
}
2018-01-28 08:24:30 +00:00
2018-01-23 14:27:25 +00:00
robot . log ( ` greetNewContributor - Handling Pull Request # ${ prNumber } on repo ${ ownerName } / ${ repoName } ` )
2018-01-28 08:24:30 +00:00
2018-01-22 13:48:57 +00:00
try {
2018-02-05 16:12:16 +00:00
const ghissuesPayload = await github . issues . getForRepo ( {
2018-01-22 13:48:57 +00:00
owner : ownerName ,
repo : repoName ,
state : 'all' ,
creator : payload . pull _request . user . login
} )
2018-01-28 08:24:30 +00:00
2018-02-05 16:12:16 +00:00
const userPullRequests = ghissuesPayload . data . filter ( issue => issue . pull _request )
2018-01-22 13:48:57 +00:00
if ( userPullRequests . length === 1 ) {
try {
2018-01-28 08:24:30 +00:00
const welcomeMessage = welcomeBotConfig . message
if ( process . env . DRY _RUN ) {
robot . log ( 'Would have created comment in GHI' , ownerName , repoName , prNumber , welcomeMessage )
} else {
2018-01-23 18:38:25 +00:00
await github . issues . createComment ( {
owner : ownerName ,
repo : repoName ,
number : prNumber ,
body : welcomeMessage
} )
}
2018-01-28 08:24:30 +00:00
2018-01-22 13:48:57 +00:00
// Send message to Slack
2018-01-23 14:27:25 +00:00
const slackHelper = require ( '../lib/slack' )
slackHelper . sendMessage ( robot , slackClient , config . slack . notification . room , ` Greeted ${ payload . pull _request . user . login } on his first PR in the ${ repoName } repo \n ${ payload . pull _request . html _url } ` )
2018-01-22 13:48:57 +00:00
} catch ( err ) {
if ( err . code !== 404 ) {
2018-01-23 14:27:25 +00:00
robot . log . error ( ` Couldn't create comment on PR: ${ err } ` , ownerName , repoName )
2018-01-22 13:48:57 +00:00
}
}
} else {
2018-01-28 08:24:30 +00:00
robot . log . debug ( 'This is not the user\'s first PR on the repo, ignoring' , ownerName , repoName , payload . pull _request . user . login )
2018-01-22 13:48:57 +00:00
}
} catch ( err ) {
2018-01-23 14:27:25 +00:00
robot . log . error ( ` Couldn't fetch the user's github issues for repo: ${ err } ` , ownerName , repoName )
2018-01-22 13:48:57 +00:00
}
2018-01-28 08:24:30 +00:00
}