status-github-bot/bot_scripts/assign-new-pr-to-review.js

113 lines
4.0 KiB
JavaScript
Raw Normal View History

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
2018-01-22 13:48:57 +00:00
//
// Dependencies:
// github: "^13.1.0"
// probot-config: "^0.1.0"
2018-01-22 13:48:57 +00:00
// probot-slack-status: "^0.2.2"
//
// Author:
// PombeirP
2018-01-23 14:27:25 +00:00
const defaultConfig = require('../lib/config')
const getConfig = require('probot-config')
2018-01-23 14:27:25 +00:00
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 = (robot) => {
2018-01-22 13:48:57 +00:00
// robot.on('slack.connected', ({ slack }) => {
Slack(robot, (slack) => {
2018-02-09 09:17:50 +00:00
robot.log.trace('assign-new-pr-to-review - Connected, assigned slackClient')
2018-01-23 14:27:25 +00:00
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-02-07 16:04:59 +00:00
const { github, payload } = context
const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
2018-01-23 14:27:25 +00:00
const ownerName = payload.repository.owner.login
const repoName = payload.repository.name
const prNumber = payload.pull_request.number
const projectBoardConfig = config ? config['project-board'] : null
if (!projectBoardConfig) {
return
}
2018-02-09 09:17:50 +00:00
robot.log(`assign-new-pr-to-review - 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
let column = null
const projectBoardName = projectBoardConfig.name
const reviewColumnName = projectBoardConfig['review-column-name']
2018-01-22 13:48:57 +00:00
try {
const ghprojectsPayload = await github.projects.getRepoProjects({
2018-01-22 13:48:57 +00:00
owner: ownerName,
repo: repoName,
state: 'open'
2018-01-23 14:27:25 +00:00
})
// Find 'Pipeline for QA' project
const project = ghprojectsPayload.data.find(p => p.name === projectBoardName)
2018-01-22 13:48:57 +00:00
if (!project) {
2018-02-09 09:17:50 +00:00
robot.log.error(`assign-new-pr-to-review - Couldn't find project ${projectBoardName} in repo ${ownerName}/${repoName}`)
2018-01-23 14:27:25 +00:00
return
2018-01-22 13:48:57 +00:00
}
2018-02-09 09:17:50 +00:00
robot.log.debug(`assign-new-pr-to-review - Fetched ${project.name} project (${project.id})`)
2018-01-22 13:48:57 +00:00
// Fetch REVIEW column ID
try {
const ghcolumnsPayload = await github.projects.getProjectColumns({ project_id: project.id })
column = ghcolumnsPayload.data.find(c => c.name === reviewColumnName)
2018-01-22 13:48:57 +00:00
if (!column) {
2018-02-09 09:17:50 +00:00
robot.log.error(`assign-new-pr-to-review - Couldn't find ${reviewColumnName} column in project ${project.name}`)
2018-01-23 14:27:25 +00:00
return
2018-01-22 13:48:57 +00:00
}
2018-02-09 09:17:50 +00:00
robot.log.debug(`assign-new-pr-to-review - Fetched ${column.name} column (${column.id})`)
2018-01-22 13:48:57 +00:00
} catch (err) {
2018-02-09 09:17:50 +00:00
robot.log.error(`assign-new-pr-to-review - Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id)
return
2018-01-22 13:48:57 +00:00
}
} catch (err) {
2018-02-09 09:17:50 +00:00
robot.log.error(`assign-new-pr-to-review - Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName)
return
}
// Create project card for the PR in the REVIEW column
try {
if (process.env.DRY_RUN) {
2018-02-09 09:17:50 +00:00
robot.log.debug('assign-new-pr-to-review - Would have created card', column.id, payload.pull_request.id)
} else {
const ghcardPayload = await github.projects.createProjectCard({
column_id: column.id,
content_type: 'PullRequest',
content_id: payload.pull_request.id
})
2018-02-09 09:17:50 +00:00
robot.log.debug(`assign-new-pr-to-review - Created card: ${ghcardPayload.data.url}`, ghcardPayload.data.id)
}
// Send message to Slack
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}`)
} catch (err) {
2018-02-09 09:17:50 +00:00
robot.log.error(`assign-new-pr-to-review - 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
}