2018-01-22 13:48:57 +00:00
// Description:
// Script that listens to new GitHub pull requests
// and assigns them to the REVIEW column on the "Pipeline for QA" project
//
// Dependencies:
// github: "^13.1.0"
// probot-config "^0.1.0"
// probot-slack-status: "^0.2.2"
//
// Author:
// PombeirP
2018-01-23 14:27:25 +00:00
const getConfig = require ( 'probot-config' )
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
module . exports = function ( robot ) {
// robot.on('slack.connected', ({ slack }) => {
Slack ( robot , ( slack ) => {
2018-01-23 14:27:25 +00:00
robot . log . trace ( "Connected, assigned slackClient" )
slackClient = slack
} )
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-22 13:48:57 +00:00
// A new PR was opened
2018-01-23 14:27:25 +00:00
await assignPullRequestToReview ( context , robot )
} )
}
2018-01-22 13:48:57 +00:00
async function assignPullRequestToReview ( context , robot ) {
2018-01-23 14:27:25 +00:00
const payload = context . payload
const github = context . github
//const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
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
robot . log ( ` assignPullRequestToReview - Handling Pull Request # ${ prNumber } on repo ${ ownerName } / ${ repoName } ` )
2018-01-22 13:48:57 +00:00
// Fetch repo projects
// TODO: The repo project and project column info should be cached
// in order to improve performance and reduce roundtrips
try {
ghprojects = await github . projects . getRepoProjects ( {
owner : ownerName ,
repo : repoName ,
state : "open"
2018-01-23 14:27:25 +00:00
} )
2018-01-22 13:48:57 +00:00
// Find "Pipeline for QA" project
2018-01-23 14:27:25 +00:00
const projectBoardName = config [ 'project-board' ] . name
const project = ghprojects . data . find ( p => p . name === projectBoardName )
2018-01-22 13:48:57 +00:00
if ( ! project ) {
2018-01-23 14:27:25 +00:00
robot . log . error ( ` Couldn't find project ${ projectBoardName } in repo ${ ownerName } / ${ repoName } ` )
return
2018-01-22 13:48:57 +00:00
}
2018-01-23 14:27:25 +00:00
robot . log . debug ( ` Fetched ${ project . name } project ( ${ project . id } ) ` )
2018-01-22 13:48:57 +00:00
// Fetch REVIEW column ID
try {
2018-01-23 14:27:25 +00:00
ghcolumns = await github . projects . getProjectColumns ( { project _id : project . id } )
const reviewColumnName = config [ 'project-board' ] [ 'review-column-name' ]
const column = ghcolumns . data . find ( c => c . name === reviewColumnName )
2018-01-22 13:48:57 +00:00
if ( ! column ) {
2018-01-23 14:27:25 +00:00
robot . log . error ( ` Couldn't find ${ reviewColumnName } column in project ${ project . name } ` )
return
2018-01-22 13:48:57 +00:00
}
2018-01-23 14:27:25 +00:00
robot . log . debug ( ` Fetched ${ column . name } column ( ${ column . id } ) ` )
2018-01-22 13:48:57 +00:00
// Create project card for the PR in the REVIEW column
try {
ghcard = await github . projects . createProjectCard ( {
column _id : column . id ,
content _type : 'PullRequest' ,
content _id : payload . pull _request . id
2018-01-23 14:27:25 +00:00
} )
robot . log . debug ( ` Created card: ${ ghcard . data . url } ` , ghcard . data . id )
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 , ` Assigned PR to ${ reviewColumnName } in ${ projectBoardName } project \n ${ payload . pull _request . html _url } ` )
2018-01-22 13:48:57 +00:00
} catch ( err ) {
2018-01-23 14:27:25 +00:00
robot . log . error ( ` Couldn't create project card for the PR: ${ err } ` , column . id , payload . pull _request . id )
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 github columns for project: ${ err } ` , ownerName , repoName , project . id )
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 github projects for repo: ${ err } ` , ownerName , repoName )
2018-01-22 13:48:57 +00:00
}
2018-01-23 14:27:25 +00:00
}