2018-01-22 13:48:57 +00:00
// Description:
// Script that listens to new GitHub pull requests
2018-01-28 08:24:30 +00:00
// and assigns them to the REVIEW column on the 'Pipeline for QA' project
2018-01-22 13:48:57 +00:00
//
// Dependencies:
// github: "^13.1.0"
2019-01-31 15:07:54 +00:00
// probot-config: "^1.0.0"
2018-01-22 13:48:57 +00:00
//
// Author:
// PombeirP
2018-01-23 14:27:25 +00:00
const defaultConfig = require ( '../lib/config' )
2018-10-10 09:02:26 +00:00
// const slackHelper = require('../lib/slack')
2018-02-22 15:37:53 +00:00
const gitHubHelpers = require ( '../lib/github-helpers' )
2018-02-08 12:09:59 +00:00
const getConfig = require ( 'probot-config' )
2018-01-22 13:48:57 +00:00
2018-02-13 14:51:00 +00:00
const botName = 'assign-new-pr-to-review'
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 ( '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 assignPullRequestToReview ( context , robot )
} )
}
2018-01-22 13:48:57 +00:00
2018-01-28 08:24:30 +00:00
async function assignPullRequestToReview ( context , robot ) {
2018-02-07 16:04:59 +00:00
const { github , payload } = context
2018-02-08 12:09:59 +00:00
const config = await getConfig ( context , 'github-bot.yml' , defaultConfig ( robot , '.github/github-bot.yml' ) )
2019-01-22 11:26:05 +00:00
const repoInfo = context . repo ( )
2018-01-23 14:27:25 +00:00
const prNumber = payload . pull _request . number
2018-01-28 08:24:30 +00:00
2018-02-08 12:09:59 +00:00
const projectBoardConfig = config ? config [ 'project-board' ] : null
2018-01-28 08:24:30 +00:00
if ( ! projectBoardConfig ) {
return
2018-01-23 14:30:24 +00:00
}
2018-01-28 08:24:30 +00:00
2018-02-16 20:35:49 +00:00
robot . log ( ` ${ botName } - Handling Pull Request # ${ prNumber } on repo ${ repoInfo . owner } / ${ repoInfo . repo } ` )
2018-01-28 08:24:30 +00:00
const projectBoardName = projectBoardConfig . name
const reviewColumnName = projectBoardConfig [ 'review-column-name' ]
2018-02-22 15:37:53 +00:00
// Find 'Pipeline for QA' project
const project = await gitHubHelpers . getRepoProjectByName ( github , robot , repoInfo , projectBoardName , botName )
// Fetch REVIEW column ID
const column = await gitHubHelpers . getProjectColumnByName ( github , robot , project , reviewColumnName , botName )
if ( ! column ) {
2018-01-23 18:38:25 +00:00
return
}
2018-01-28 08:24:30 +00:00
2018-01-23 18:38:25 +00:00
// Create project card for the PR in the REVIEW column
try {
2018-01-28 08:24:30 +00:00
if ( process . env . DRY _RUN ) {
2018-02-13 14:51:00 +00:00
robot . log . debug ( ` ${ botName } - Would have created card ` , column . id , payload . pull _request . id )
2018-01-28 08:24:30 +00:00
} else {
2019-01-31 14:50:23 +00:00
const ghcardPayload = await github . projects . createCard ( {
2018-01-23 18:38:25 +00:00
column _id : column . id ,
content _type : 'PullRequest' ,
content _id : payload . pull _request . id
} )
2018-01-28 08:24:30 +00:00
2018-02-13 14:51:00 +00:00
robot . log . debug ( ` ${ botName } - Created card: ${ ghcardPayload . data . url } ` , ghcardPayload . data . id )
2018-01-23 18:38:25 +00:00
}
2018-01-28 08:24:30 +00:00
2018-01-23 18:38:25 +00:00
// Send message to Slack
2018-10-10 09:02:26 +00:00
// slackHelper.sendMessage(robot, config.slack.notification.room, `Assigned PR to ${reviewColumnName} in ${projectBoardName} project\n${payload.pull_request.html_url}`)
2018-01-23 18:38:25 +00:00
} catch ( err ) {
2018-02-13 14:51:00 +00:00
robot . log . error ( ` ${ botName } - Couldn't create project card for the PR: ${ err } ` , column . id , payload . pull _request . id )
2018-01-22 13:48:57 +00:00
}
2018-01-23 14:27:25 +00:00
}