From f5f110940484442b11cecd27219044c324298829 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Wed, 14 Mar 2018 14:24:11 +0100 Subject: [PATCH] Persuade probot to put any issues that are tagged by a reviewer with the label `bounty-bug` into that column --- .../assign-to-bounty-awaiting-for-approval.js | 7 +- bot_scripts/assign-to-bounty-bug-column.js | 110 ++++++++++++++++++ .../bounty-awaiting-approval-slack-ping.js | 2 +- bot_scripts/trigger-automation-test-build.js | 2 +- index.js | 1 + 5 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 bot_scripts/assign-to-bounty-bug-column.js diff --git a/bot_scripts/assign-to-bounty-awaiting-for-approval.js b/bot_scripts/assign-to-bounty-awaiting-for-approval.js index 3a2efb3..b01eb2e 100644 --- a/bot_scripts/assign-to-bounty-awaiting-for-approval.js +++ b/bot_scripts/assign-to-bounty-awaiting-for-approval.js @@ -1,6 +1,7 @@ // Description: -// Script that listens to new labels on GitHub issues -// and assigns the issues to the bounty-awaiting-approval column on the 'Status SOB Swarm' project +// Script that listens to labeling/unlabeling on GitHub issues with 'bounty-awaiting-approval' +// and respectively assigns the issues to the bounty-awaiting-approval column on the 'Status SOB Swarm' project +// or removes the card from project board // // Dependencies: // github: "^13.1.0" @@ -46,7 +47,7 @@ async function assignIssueToBountyAwaitingForApproval (context, robot, assign) { const watchedLabelName = projectBoardConfig['awaiting-approval-label-name'] if (payload.label.name !== watchedLabelName) { - robot.log.debug(`${botName} - ${payload.label.name} doesn't match watched ${watchedLabelName} label. Ignoring`) + robot.log.debug(`${botName} - '${payload.label.name}' doesn't match watched '${watchedLabelName}' label. Ignoring`) return } diff --git a/bot_scripts/assign-to-bounty-bug-column.js b/bot_scripts/assign-to-bounty-bug-column.js new file mode 100644 index 0000000..475ae0f --- /dev/null +++ b/bot_scripts/assign-to-bounty-bug-column.js @@ -0,0 +1,110 @@ +// Description: +// Script that listens to new bounty-bug labels on GitHub issues +// and adds/removes card for the issues to/from the bounty-bug column on the 'Status SOB Swarm' project +// +// Dependencies: +// github: "^13.1.0" +// probot-config: "^0.1.0" +// +// Author: +// PombeirP + +const slackHelper = require('../lib/slack') +const gitHubHelpers = require('../lib/github-helpers') +const defaultConfig = require('../lib/config') + +const getConfig = require('probot-config') + +const botName = 'assign-to-bounty-bug-column' + +module.exports = (robot) => { + robot.on('issues.labeled', async context => { + // Make sure we don't listen to our own messages + if (context.isBot) { return } + + // A new issue was labeled + await assignIssueToBountyBug(context, robot, true) + }) + robot.on('issues.unlabeled', async context => { + // Make sure we don't listen to our own messages + if (context.isBot) { return } + + // An issue was unlabeled + await assignIssueToBountyBug(context, robot, false) + }) +} + +async function assignIssueToBountyBug (context, robot, assign) { + const { github, payload } = context + const repoInfo = { owner: payload.repository.owner.login, repo: payload.repository.name } + const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml')) + const projectBoardConfig = config ? config['bounty-project-board'] : null + + if (!projectBoardConfig) { + return + } + + const watchedLabelName = projectBoardConfig['bounty-bug-label-name'] + if (payload.label.name !== watchedLabelName) { + robot.log.debug(`${botName} - '${payload.label.name}' doesn't match watched '${watchedLabelName}' label. Ignoring`) + return + } + + robot.log(`${botName} - Handling labeling of #${payload.issue.number} with ${payload.label.name} on repo ${repoInfo.owner}/${repoInfo.repo}`) + + // Fetch bounty-bug column in project board + const bountyBugColumnName = projectBoardConfig['bounty-bug-column-name'] + const project = await gitHubHelpers.getOrgProjectByName(github, robot, repoInfo.owner, projectBoardConfig.name, botName) + const column = await gitHubHelpers.getProjectColumnByName(github, robot, project, bountyBugColumnName, botName) + if (!column) { + return + } + + if (process.env.DRY_RUN) { + if (assign) { + robot.log.info(`${botName} - Would have created card for issue`, column.id, payload.issue.id) + } else { + robot.log.info(`${botName} - Would have deleted card for issue`, column.id, payload.issue.id) + } + } else { + if (assign) { + try { + // Create project card for the issue in the bounty-bug column + const ghcardPayload = await github.projects.createProjectCard({ + column_id: column.id, + content_type: 'Issue', + content_id: payload.issue.id + }) + const ghcard = ghcardPayload.data + + robot.log(`${botName} - Created card: ${ghcard.url}`, ghcard.id) + } catch (err) { + robot.log.error(`${botName} - Couldn't create project card for the issue: ${err}`, column.id, payload.issue.id) + } + } else { + try { + const ghcard = await gitHubHelpers.getProjectCardForIssue(github, column.id, payload.issue.url) + if (ghcard) { + await github.projects.deleteProjectCard({id: ghcard.id}) + robot.log(`${botName} - Deleted card: ${ghcard.url}`, ghcard.id) + } + } catch (err) { + robot.log.error(`${botName} - Couldn't delete project card for the issue: ${err}`, column.id, payload.issue.id) + } + } + } + + const slackMessage = getSlackMessage(projectBoardConfig.name, bountyBugColumnName, payload, assign) + if (slackMessage) { + // Send message to Slack + slackHelper.sendMessage(robot, config.slack.notification.room, slackMessage) + } +} + +function getSlackMessage (projectBoardName, bountyBugColumnName, payload, assign) { + if (assign) { + return `Assigned issue to ${bountyBugColumnName} in ${projectBoardName} project\n${payload.issue.html_url}` + } + + return `Unassigned issue from ${bountyBugColumnName} in ${projectBoardName} project\n${payload.issue.html_url}` +} diff --git a/bot_scripts/bounty-awaiting-approval-slack-ping.js b/bot_scripts/bounty-awaiting-approval-slack-ping.js index 4f0e6e8..74528fd 100644 --- a/bot_scripts/bounty-awaiting-approval-slack-ping.js +++ b/bot_scripts/bounty-awaiting-approval-slack-ping.js @@ -50,7 +50,7 @@ async function notifyCollaborators (context, robot) { const watchedLabelName = bountyProjectBoardConfig['awaiting-approval-label-name'] if (payload.label.name !== watchedLabelName) { - robot.log.debug(`${botName} - ${payload.label.name} doesn't match watched ${watchedLabelName} label. Ignoring`) + robot.log.debug(`${botName} - '${payload.label.name}' doesn't match watched '${watchedLabelName}' label. Ignoring`) return null } diff --git a/bot_scripts/trigger-automation-test-build.js b/bot_scripts/trigger-automation-test-build.js index 92e8779..ba8c24e 100644 --- a/bot_scripts/trigger-automation-test-build.js +++ b/bot_scripts/trigger-automation-test-build.js @@ -87,7 +87,7 @@ async function processChangedProjectCard (robot, context) { const projectPayload = await github.projects.getProject({ id: projectId }) const project = projectPayload.data if (project.name !== projectBoardName) { - robot.log.trace(`${botName} - Card column name doesn't match watched column name, exiting`, project.name, projectBoardName) + robot.log.trace(`${botName} - Project board name doesn't match watched project board, exiting`, project.name, projectBoardName) return } } catch (error) { diff --git a/index.js b/index.js index 53e83e2..b13bc65 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ module.exports = async (robot) => { require('./bot_scripts/assign-new-pr-to-review')(robot) require('./bot_scripts/assign-approved-pr-to-test')(robot) require('./bot_scripts/assign-to-bounty-awaiting-for-approval')(robot) + require('./bot_scripts/assign-to-bounty-bug-column')(robot) require('./bot_scripts/greet-new-contributor')(robot) require('./bot_scripts/trigger-automation-test-build')(robot) require('./bot_scripts/bounty-awaiting-approval-slack-ping')(robot)