2018-01-18 14:34:32 +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"
// hubot-github-webhook-listener: "^0.9.1"
// hubot-slack: "^4.4.0"
//
// Notes:
// The hard-coded names for the project board and review column are just below.
// These could be read from a config file (e.g. YAML)
//
// Author:
// PombeirP
module . exports = function ( robot ) {
2018-01-18 17:54:54 +00:00
const gitHubContext = require ( './github-context.js' ) ;
2018-01-18 14:34:32 +00: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 17:54:54 +00:00
if ( gitHubContext . equalsRobotName ( robot , githubPayload . pull _request . user . login ) ) { return ; }
2018-01-18 14:34:32 +00:00
var { action } = githubPayload ;
if ( action === "opened" ) {
// A new PR was opened
2018-01-18 17:54:54 +00:00
return assignPullRequestToReview ( gitHubContext , githubPayload , robot ) ;
2018-01-18 14:34:32 +00:00
}
break ;
}
} ) ;
} ;
2018-01-18 17:54:54 +00:00
async function assignPullRequestToReview ( gitHubContext , githubPayload , robot ) {
const github = gitHubContext . api ( ) ;
const githubConfig = gitHubContext . config ( ) ;
2018-01-18 14:34:32 +00:00
const ownerName = githubPayload . repository . owner . login ;
const repoName = githubPayload . repository . name ;
const prNumber = githubPayload . pull _request . number ;
robot . logger . info ( ` assignPullRequestToReview - Handling Pull Request # ${ prNumber } on repo ${ ownerName } / ${ repoName } ` ) ;
// 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"
} ) ;
// Find "Pipeline for QA" project
2018-01-18 17:54:10 +00:00
const projectBoardName = githubConfig [ 'pull-requests' ] [ 'project-board' ] . name ;
2018-01-18 14:34:32 +00:00
const project = ghprojects . data . find ( function ( p ) { return p . name === projectBoardName } ) ;
if ( ! project ) {
robot . logger . warn ( ` Couldn't find project ${ projectBoardName } in repo ${ ownerName } / ${ repoName } ` ) ;
return ;
}
robot . logger . debug ( ` Fetched ${ project . name } project ( ${ project . id } ) ` ) ;
// Fetch REVIEW column ID
try {
ghcolumns = await github . projects . getProjectColumns ( { project _id : project . id } ) ;
2018-01-18 17:54:10 +00:00
const reviewColumnName = githubConfig [ 'pull-requests' ] [ 'project-board' ] [ 'review-column-name' ] ;
2018-01-18 14:34:32 +00:00
const column = ghcolumns . data . find ( function ( c ) { return c . name === reviewColumnName } ) ;
if ( ! column ) {
robot . logger . warn ( ` Couldn't find ${ projectBoardName } column in project ${ project . name } ` ) ;
return ;
}
robot . logger . debug ( ` Fetched ${ column . name } column ( ${ column . id } ) ` ) ;
// 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 : githubPayload . pull _request . id
} ) ;
robot . logger . debug ( ` Created card: ${ ghcard . data . url } ` , ghcard . data . id ) ;
// Send message to Slack
2018-01-18 17:54:10 +00:00
robot . messageRoom ( githubConfig . slack . notification . room , ` Moved PR ${ githubPayload . pull _request . number } to ${ reviewColumnName } in ${ projectBoardName } project ` ) ;
2018-01-18 14:34:32 +00:00
} catch ( err ) {
robot . logger . error ( ` Couldn't create project card for the PR: ${ err } ` , column . id , githubPayload . pull _request . id ) ;
}
} catch ( err ) {
robot . logger . error ( ` Couldn't fetch the github columns for project: ${ err } ` , ownerName , repoName , project . id ) ;
}
} catch ( err ) {
robot . logger . error ( ` Couldn't fetch the github projects for repo: ${ err } ` , ownerName , repoName ) ;
}
} ;