2018-01-30 14:57:16 +00:00
// Description:
// Script that listens for issues with the label 'bounty-awaiting-approval'
// and notifies the collaborators on Slack.
//
// Dependencies:
// github: "^13.1.0"
// probot-config: "^0.1.0"
// probot-slack-status: "^0.2.2"
//
// Author:
// Max Tyrrell (ImFeelingDucky/mac/yung_mac)
const Slack = require ( 'probot-slack-status' )
const defaultConfig = require ( '../lib/config' )
const slackHelper = require ( '../lib/slack' )
2018-02-06 11:17:58 +00:00
module . exports = ( robot , getSlackMentionFromGitHubId ) => {
2018-01-30 14:57:16 +00:00
robot . log ( 'Connected to bounty-awaiting-approval-slack-ping' )
Slack ( robot , ( slack ) => {
robot . log . trace ( 'Connected to Slack' )
2018-02-06 11:17:58 +00:00
registerForNewBounties ( robot , slack , getSlackMentionFromGitHubId )
2018-01-30 14:57:16 +00:00
} )
}
2018-02-06 11:17:58 +00:00
function registerForNewBounties ( robot , slackClient , getSlackMentionFromGitHubId ) {
2018-01-30 14:57:16 +00:00
robot . on ( 'issues.labeled' , async context => {
// Make sure we don't listen to our own messages
if ( context . isBot ) return null
2018-02-06 11:17:58 +00:00
await notifyCollaborators ( context , robot , slackClient , getSlackMentionFromGitHubId )
2018-01-30 14:57:16 +00:00
} )
}
2018-02-06 11:17:58 +00:00
async function notifyCollaborators ( context , robot , slackClient , getSlackMentionFromGitHubId ) {
2018-01-30 14:57:16 +00:00
const { github , payload } = context
const ownerName = payload . repository . owner . login
const repoName = payload . repository . name
const config = defaultConfig ( robot , '.github/github-bot.yml' )
const watchedLabelName = config [ 'bounty-project-board' ] [ 'label-name' ]
if ( payload . label . name !== watchedLabelName ) {
robot . log . debug ( ` bountyAwaitingApprovalSlackPing - ${ payload . label . name } doesn't match watched ${ watchedLabelName } label. Ignoring ` )
return null
}
robot . log ( ` bountyAwaitingApprovalSlackPing - issue # ${ payload . issue . number } on ${ ownerName } / ${ repoName } was labeled as a bounty awaiting approval. Pinging slack... ` )
2018-02-06 11:17:58 +00:00
const slackCollaborators = await getSlackCollaborators ( ownerName , repoName , github , robot , getSlackMentionFromGitHubId )
2018-01-30 14:57:16 +00:00
// Send message to Slack
slackHelper . sendMessage (
robot ,
slackClient ,
config . slack . notification . room ,
2018-02-07 10:58:50 +00:00
` New bounty awaiting approval: ${ payload . issue . html _url }
/ c c $ { s l a c k C o l l a b o r a t o r s . j o i n ( ' , ' ) } `
2018-01-30 14:57:16 +00:00
)
}
2018-02-06 11:17:58 +00:00
// Get the Slack IDs of the collaborators of this repo.
async function getSlackCollaborators ( owner , repo , github , robot , getSlackMentionFromGitHubId ) {
2018-01-30 14:57:16 +00:00
// Grab a list of collaborators to this repo, as an array of GitHub login usernames
let collaborators = await github . repos . getCollaborators ( { owner , repo } )
collaborators = collaborators . data . map ( collaboratorObject => collaboratorObject . login )
// Create an array of Slack usernames from GitHub usernames
2018-02-06 11:17:58 +00:00
return collaborators . map ( getSlackMentionFromGitHubId ) . filter ( id => id )
2018-01-30 14:57:16 +00:00
}