2018-01-22 13:48:57 +00:00
|
|
|
// Description:
|
|
|
|
// Script that listens to new labels on GitHub issues
|
2018-01-28 08:24:30 +00:00
|
|
|
// and assigns the issues to the bounty-awaiting-approval column on the 'Status SOB Swarm' project
|
2018-01-22 13:48:57 +00:00
|
|
|
//
|
|
|
|
// 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-30 14:57:16 +00:00
|
|
|
const slackHelper = require('../lib/slack')
|
2018-02-07 16:59:55 +00:00
|
|
|
const gitHubHelpers = require('../lib/github-helpers')
|
2018-01-23 14:27:25 +00:00
|
|
|
const defaultConfig = require('../lib/config')
|
2018-02-08 12:09:59 +00:00
|
|
|
|
|
|
|
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-02-13 14:51:00 +00:00
|
|
|
const botName = 'assign-to-bounty-awaiting-for-approval'
|
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-02-13 14:51:00 +00:00
|
|
|
robot.log.trace(`${botName} - 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('issues.labeled', 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 issue was labeled
|
2018-01-24 13:38:40 +00:00
|
|
|
await assignIssueToBountyAwaitingForApproval(context, robot, true)
|
|
|
|
})
|
|
|
|
robot.on('issues.unlabeled', async context => {
|
|
|
|
// Make sure we don't listen to our own messages
|
|
|
|
if (context.isBot) { return }
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-01-24 13:38:40 +00:00
|
|
|
// An issue was unlabeled
|
|
|
|
await assignIssueToBountyAwaitingForApproval(context, robot, false)
|
2018-01-23 14:27:25 +00:00
|
|
|
})
|
|
|
|
}
|
2018-01-22 13:48:57 +00:00
|
|
|
|
2018-01-28 08:24:30 +00:00
|
|
|
async function assignIssueToBountyAwaitingForApproval (context, robot, assign) {
|
2018-02-07 16:04:59 +00:00
|
|
|
const { github, payload } = context
|
2018-01-23 14:27:25 +00:00
|
|
|
const ownerName = payload.repository.owner.login
|
|
|
|
const repoName = payload.repository.name
|
2018-02-08 12:09:59 +00:00
|
|
|
const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
|
|
|
|
const projectBoardConfig = config ? config['bounty-project-board'] : null
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-02-08 09:11:11 +00:00
|
|
|
if (!projectBoardConfig) {
|
2018-01-28 08:24:30 +00:00
|
|
|
return
|
2018-01-23 14:30:24 +00:00
|
|
|
}
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-02-08 09:11:11 +00:00
|
|
|
const watchedLabelName = projectBoardConfig['awaiting-approval-label-name']
|
2018-01-24 13:38:40 +00:00
|
|
|
if (payload.label.name !== watchedLabelName) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.debug(`${botName} - ${payload.label.name} doesn't match watched ${watchedLabelName} label. Ignoring`)
|
2018-01-24 13:38:40 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-01-24 13:38:40 +00:00
|
|
|
if (assign) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log(`${botName} - Handling labeling of #${payload.issue.number} with ${payload.label.name} on repo ${ownerName}/${repoName}`)
|
2018-01-24 13:38:40 +00:00
|
|
|
} else {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log(`${botName} - Handling unlabeling of #${payload.issue.number} with ${payload.label.name} on repo ${ownerName}/${repoName}`)
|
2018-01-24 13:38:40 +00:00
|
|
|
}
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-01-22 13:48:57 +00:00
|
|
|
// Fetch org projects
|
|
|
|
// TODO: The org project and project column info should be cached
|
|
|
|
// in order to improve performance and reduce roundtrips
|
2018-01-28 08:24:30 +00:00
|
|
|
let column = null
|
2018-02-08 09:11:11 +00:00
|
|
|
const projectBoardName = projectBoardConfig.name
|
|
|
|
const approvalColumnName = projectBoardConfig['awaiting-approval-column-name']
|
2018-01-22 13:48:57 +00:00
|
|
|
try {
|
2018-01-24 13:38:40 +00:00
|
|
|
const orgName = ownerName
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-02-05 16:12:16 +00:00
|
|
|
const ghprojectsPayload = await github.projects.getOrgProjects({
|
2018-01-22 13:48:57 +00:00
|
|
|
org: orgName,
|
2018-01-28 08:24:30 +00:00
|
|
|
state: 'open'
|
2018-01-23 14:27:25 +00:00
|
|
|
})
|
2018-01-28 08:24:30 +00:00
|
|
|
|
|
|
|
// Find 'Status SOB Swarm' project
|
2018-02-05 16:12:16 +00:00
|
|
|
const project = ghprojectsPayload.data.find(p => p.name === projectBoardName)
|
2018-01-22 13:48:57 +00:00
|
|
|
if (!project) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.error(`${botName} - Couldn't find project ${projectBoardName} in ${orgName} org`)
|
2018-01-23 14:27:25 +00:00
|
|
|
return
|
2018-01-22 13:48:57 +00:00
|
|
|
}
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.debug(`${botName} - Fetched ${project.name} project (${project.id})`)
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-01-22 13:48:57 +00:00
|
|
|
// Fetch bounty-awaiting-approval column ID
|
|
|
|
try {
|
2018-02-05 16:12:16 +00:00
|
|
|
const ghcolumnsPayload = await github.projects.getProjectColumns({ project_id: project.id })
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-02-05 16:12:16 +00:00
|
|
|
column = ghcolumnsPayload.data.find(c => c.name === approvalColumnName)
|
2018-01-22 13:48:57 +00:00
|
|
|
if (!column) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.error(`${botName} - Couldn't find ${approvalColumnName} column in project ${project.name}`)
|
2018-01-23 14:27:25 +00:00
|
|
|
return
|
2018-01-22 13:48:57 +00:00
|
|
|
}
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.debug(`${botName} - Fetched ${column.name} column (${column.id})`)
|
2018-01-22 13:48:57 +00:00
|
|
|
} catch (err) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.error(`${botName} - Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id)
|
2018-01-23 18:38:25 +00:00
|
|
|
return
|
2018-01-22 13:48:57 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.error(`${botName} - Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName)
|
2018-01-23 18:38:25 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-02-08 09:11:11 +00:00
|
|
|
const bountyLabelName = projectBoardConfig['bounty-label-name']
|
|
|
|
const isOfficialBounty = !!payload.issue.labels.find(l => l.name === bountyLabelName)
|
|
|
|
|
2018-01-28 08:24:30 +00:00
|
|
|
if (process.env.DRY_RUN) {
|
|
|
|
if (assign) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.info(`${botName} - Would have created card for issue`, column.id, payload.issue.id)
|
2018-01-28 08:24:30 +00:00
|
|
|
} else {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.info(`${botName} - Would have deleted card for issue`, column.id, payload.issue.id)
|
2018-01-28 08:24:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-24 13:38:40 +00:00
|
|
|
if (assign) {
|
|
|
|
try {
|
|
|
|
// Create project card for the issue in the bounty-awaiting-approval column
|
2018-02-07 16:59:55 +00:00
|
|
|
const ghcardPayload = await github.projects.createProjectCard({
|
2018-01-24 13:38:40 +00:00
|
|
|
column_id: column.id,
|
|
|
|
content_type: 'Issue',
|
|
|
|
content_id: payload.issue.id
|
|
|
|
})
|
2018-02-05 16:12:16 +00:00
|
|
|
const ghcard = ghcardPayload.data
|
2018-01-28 08:24:30 +00:00
|
|
|
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log(`${botName} - Created card: ${ghcard.url}`, ghcard.id)
|
2018-01-24 13:38:40 +00:00
|
|
|
} catch (err) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.error(`${botName} - Couldn't create project card for the issue: ${err}`, column.id, payload.issue.id)
|
2018-01-24 13:38:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
2018-02-07 16:59:55 +00:00
|
|
|
const ghcard = await gitHubHelpers.getProjectCardForIssue(github, column.id, payload.issue.url)
|
2018-01-30 14:05:09 +00:00
|
|
|
if (ghcard) {
|
|
|
|
await github.projects.deleteProjectCard({id: ghcard.id})
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log(`${botName} - Deleted card: ${ghcard.url}`, ghcard.id)
|
2018-01-30 14:05:09 +00:00
|
|
|
}
|
2018-01-24 13:38:40 +00:00
|
|
|
} catch (err) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.error(`${botName} - Couldn't delete project card for the issue: ${err}`, column.id, payload.issue.id)
|
2018-01-24 13:38:40 +00:00
|
|
|
}
|
2018-01-23 18:38:25 +00:00
|
|
|
}
|
2018-01-24 13:38:40 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:11:11 +00:00
|
|
|
let message
|
|
|
|
// Send message to Slack
|
|
|
|
if (assign) {
|
|
|
|
message = `Assigned issue to ${approvalColumnName} in ${projectBoardName} project\n${payload.issue.html_url}`
|
|
|
|
} else {
|
|
|
|
if (isOfficialBounty) {
|
|
|
|
message = `${payload.issue.html_url} has been approved as an official bounty!`
|
2018-01-30 14:05:09 +00:00
|
|
|
} else {
|
2018-02-08 09:11:11 +00:00
|
|
|
message = `Unassigned issue from ${approvalColumnName} in ${projectBoardName} project\n${payload.issue.html_url}`
|
2018-01-30 14:05:09 +00:00
|
|
|
}
|
2018-01-22 13:48:57 +00:00
|
|
|
}
|
2018-02-08 09:11:11 +00:00
|
|
|
|
|
|
|
if (message && !process.env.DRY_RUN_BOUNTY_APPROVAL) {
|
|
|
|
// Send message to Slack
|
|
|
|
slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, message)
|
|
|
|
}
|
2018-01-23 14:27:25 +00:00
|
|
|
}
|