status-github-bot/bot_scripts/bounty-awaiting-approval-sl...

122 lines
4.2 KiB
JavaScript
Raw Normal View History

// Description:
// Script that listens for issues with the label 'bounty-awaiting-approval'
// and notifies the collaborators on Slack.
//
// Dependencies:
// github: "^13.1.0"
// hashset: "0.0.6"
// probot-config: "^0.1.0"
//
// Author:
// Max Tyrrell (ImFeelingDucky/mac/yung_mac)
const getConfig = require('probot-config')
const HashSet = require('hashset')
const defaultConfig = require('../lib/config')
const slackHelper = require('../lib/slack')
2018-02-13 14:51:00 +00:00
const botName = 'bounty-awaiting-approval-slack-ping'
2018-02-14 23:57:46 +00:00
module.exports = (robot) => {
registerForNewBounties(robot)
}
2018-02-14 23:57:46 +00:00
function registerForNewBounties (robot) {
robot.on('issues.labeled', async context => {
// Make sure we don't listen to our own messages
if (context.isBot) return null
2018-02-14 23:57:46 +00:00
await notifyCollaborators(context, robot)
})
}
2018-02-14 23:57:46 +00:00
async function notifyCollaborators (context, robot) {
const { github, payload } = context
2018-02-16 20:35:49 +00:00
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 bountyProjectBoardConfig = config ? config['bounty-project-board'] : null
const gitHubTeamConfig = config ? config['github-team'] : null
if (!bountyProjectBoardConfig) {
2018-02-16 20:35:49 +00:00
robot.log.debug(`${botName} - Bounty project board not configured in repo ${repoInfo.owner}/${repoInfo.repo}, ignoring`)
return
}
if (!gitHubTeamConfig) {
2018-02-16 20:35:49 +00:00
robot.log.debug(`${botName} - GitHub team not configured in repo ${repoInfo.owner}/${repoInfo.repo}, ignoring`)
return
}
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`)
return null
}
2018-02-16 20:35:49 +00:00
robot.log(`${botName} - issue #${payload.issue.number} on ${repoInfo.owner}/${repoInfo.repo} was labeled as a bounty awaiting approval. Pinging slack...`)
2018-02-16 20:35:49 +00:00
const slackCollaborators = await getSlackCollaborators(repoInfo, github, robot, gitHubTeamConfig)
// Mention the project board owner as well, if configured
const bountyProjectBoardOwner = bountyProjectBoardConfig['owner']
if (bountyProjectBoardOwner) {
2018-02-14 23:57:46 +00:00
const slackUserMention = robot.gitHubIdMapper.getSlackMentionFromGitHubId(bountyProjectBoardOwner)
if (slackUserMention) {
slackCollaborators.push(slackUserMention)
}
}
// Send message to Slack
slackHelper.sendMessage(
robot,
config.slack.notification.room,
`New bounty awaiting approval: ${payload.issue.html_url}
/cc ${slackCollaborators.values().join(', ')}`
)
}
function randomInt (low, high) {
return Math.floor(Math.random() * (high - low) + low)
}
// Get the Slack IDs of the collaborators of this repo.
2018-02-16 20:35:49 +00:00
async function getSlackCollaborators (repoInfo, github, robot, gitHubTeamConfig) {
const teamSlug = gitHubTeamConfig['slug']
if (!teamSlug) {
2018-02-16 20:35:49 +00:00
robot.log.debug(`${botName} - GitHub team slug not configured in repo ${repoInfo.owner}/${repoInfo.repo}, ignoring`)
return
}
// Grab a list of collaborators to this repo, as an array of GitHub login usernames
2018-02-16 20:35:49 +00:00
const teams = await github.paginate(github.orgs.getTeams({ org: repoInfo.owner }), res => res.data)
const team = teams.find(t => t.slug === teamSlug)
if (!team) {
2018-02-13 14:51:00 +00:00
robot.log.debug(`${botName} - GitHub team with slug ${teamSlug} was not found. Ignoring`)
return
}
const teamMembers = await github.paginate(github.orgs.getTeamMembers({id: team.id, per_page: 100}), res => res.data)
// Create an array of Slack usernames from GitHub usernames
2018-02-14 23:57:46 +00:00
const gitHubUsers = teamMembers.map(u => u.login)
const slackUsers = new HashSet()
for (const gitHubUser of gitHubUsers) {
const id = await robot.gitHubIdMapper.getSlackMentionFromGitHubId(gitHubUser)
if (id) {
slackUsers.add(id)
}
}
// Select 2 random Slack team members
const randomTeamMemberLimit = 2
const selectedSlackUsers = new HashSet()
2018-02-14 23:57:46 +00:00
while (selectedSlackUsers.size() < randomTeamMemberLimit || selectedSlackUsers.size() < slackUsers.size()) {
const slackUser = slackUsers[randomInt(0, slackUsers.size())]
selectedSlackUsers.add(slackUser)
}
return selectedSlackUsers
}