From a5f43547438fee422fe6ce860ff0fa6d3c9081a3 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Tue, 23 Jan 2018 15:27:25 +0100 Subject: [PATCH] Code cleanup --- index.js | 6 +- lib/config.js | 10 +- lib/slack.js | 22 ++++ scripts/assign-new-pr-to-review.js | 104 ++++++++---------- .../assign-to-bounty-awaiting-for-approval.js | 86 +++++++-------- scripts/greet-new-contributor.js | 62 +++++------ 6 files changed, 146 insertions(+), 144 deletions(-) create mode 100644 lib/slack.js diff --git a/index.js b/index.js index 1d67bcc..65d727c 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ module.exports = (robot) => { console.log('Yay, the app was loaded!') - require('./scripts/assign-new-pr-to-review.js')(robot); - require('./scripts/assign-to-bounty-awaiting-for-approval.js')(robot); - require('./scripts/greet-new-contributor.js')(robot); + require('./scripts/assign-new-pr-to-review')(robot) + require('./scripts/assign-to-bounty-awaiting-for-approval')(robot) + require('./scripts/greet-new-contributor')(robot) // For more information on building apps: // https://probot.github.io/docs/ diff --git a/lib/config.js b/lib/config.js index 47de498..69f1f23 100644 --- a/lib/config.js +++ b/lib/config.js @@ -10,13 +10,13 @@ module.exports = (robot, fileName) => { // Get document, or throw exception on error try { - const yaml = require('js-yaml'); - const fs = require('fs'); + const yaml = require('js-yaml') + const fs = require('fs') - return yaml.safeLoad(fs.readFileSync(fileName, 'utf8')); + return yaml.safeLoad(fs.readFileSync(fileName, 'utf8')) } catch (e) { - robot.log.error(e); + robot.log.error(e) } - return null; + return null } diff --git a/lib/slack.js b/lib/slack.js new file mode 100644 index 0000000..b0f8884 --- /dev/null +++ b/lib/slack.js @@ -0,0 +1,22 @@ +// Description: +// Configuration-related functionality +// +// Dependencies: +// probot-slack-status: "^0.2.2" +// +// Author: +// PombeirP + +module.exports.sendMessage = async (robot, slackClient, room, message) => { + // Send message to Slack + if (slackClient != null) { + const channel = slackClient.dataStore.getChannelByName(room) + try { + await slackClient.sendMessage(message, channel.id) + } catch(err) { + robot.log.error(`Failed to send Slack message to ${room} channel`) + } + } else { + robot.log.debug("Slack client not available") + } +} diff --git a/scripts/assign-new-pr-to-review.js b/scripts/assign-new-pr-to-review.js index b1eb0d7..0de435e 100644 --- a/scripts/assign-new-pr-to-review.js +++ b/scripts/assign-new-pr-to-review.js @@ -10,39 +10,39 @@ // Author: // PombeirP -const getConfig = require('probot-config'); -const defaultConfig = require('../lib/config.js'); -const Slack = require('probot-slack-status'); +const getConfig = require('probot-config') +const defaultConfig = require('../lib/config') +const Slack = require('probot-slack-status') -let slackClient = null; +let slackClient = null module.exports = function(robot) { // robot.on('slack.connected', ({ slack }) => { Slack(robot, (slack) => { - robot.log.trace("Connected, assigned slackClient"); - slackClient = slack; - }); + robot.log.trace("Connected, assigned slackClient") + slackClient = slack + }) robot.on('pull_request.opened', async context => { // Make sure we don't listen to our own messages - if (context.isBot) { return; } - + if (context.isBot) { return } + // A new PR was opened - await assignPullRequestToReview(context, robot); - }); -}; + await assignPullRequestToReview(context, robot) + }) +} async function assignPullRequestToReview(context, robot) { - const payload = context.payload; - const github = context.github; - //const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml')); - const config = defaultConfig(robot, '.github/github-bot.yml'); - const ownerName = payload.repository.owner.login; - const repoName = payload.repository.name; - const prNumber = payload.pull_request.number; - - robot.log(`assignPullRequestToReview - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`); - + const payload = context.payload + const github = context.github + //const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml')) + const config = defaultConfig(robot, '.github/github-bot.yml') + const ownerName = payload.repository.owner.login + const repoName = payload.repository.name + const prNumber = payload.pull_request.number + + robot.log(`assignPullRequestToReview - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`) + // Fetch repo projects // TODO: The repo project and project column info should be cached // in order to improve performance and reduce roundtrips @@ -51,59 +51,51 @@ async function assignPullRequestToReview(context, robot) { owner: ownerName, repo: repoName, state: "open" - }); - + }) + // Find "Pipeline for QA" project - const projectBoardName = config['project-board'].name; - const project = ghprojects.data.find(function(p) { return p.name === projectBoardName }); + const projectBoardName = config['project-board'].name + const project = ghprojects.data.find(p => p.name === projectBoardName) if (!project) { - robot.log.error(`Couldn't find project ${projectBoardName} in repo ${ownerName}/${repoName}`); - return; + robot.log.error(`Couldn't find project ${projectBoardName} in repo ${ownerName}/${repoName}`) + return } - robot.log.debug(`Fetched ${project.name} project (${project.id})`); - + robot.log.debug(`Fetched ${project.name} project (${project.id})`) + // Fetch REVIEW column ID try { - ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }); - - const reviewColumnName = config['project-board']['review-column-name']; - const column = ghcolumns.data.find(function(c) { return c.name === reviewColumnName }); + ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }) + + const reviewColumnName = config['project-board']['review-column-name'] + const column = ghcolumns.data.find(c => c.name === reviewColumnName) if (!column) { - robot.log.error(`Couldn't find ${reviewColumnName} column in project ${project.name}`); - return; + robot.log.error(`Couldn't find ${reviewColumnName} column in project ${project.name}`) + return } - robot.log.debug(`Fetched ${column.name} column (${column.id})`); - + robot.log.debug(`Fetched ${column.name} column (${column.id})`) + // Create project card for the PR in the REVIEW column try { ghcard = await github.projects.createProjectCard({ column_id: column.id, content_type: 'PullRequest', content_id: payload.pull_request.id - }); - - robot.log.debug(`Created card: ${ghcard.data.url}`, ghcard.data.id); - + }) + + robot.log.debug(`Created card: ${ghcard.data.url}`, ghcard.data.id) + // Send message to Slack - if (slackClient != null) { - const channel = slackClient.dataStore.getChannelByName(config.slack.notification.room); - try { - await slackClient.sendMessage(`Assigned PR to ${reviewColumnName} in ${projectBoardName} project\n${payload.pull_request.html_url}`, channel.id); - } catch(err) { - robot.log.error(`Failed to send Slack message to ${config.slack.notification.room} channel`); - } - } else { - robot.log.debug("Slack client not available"); - } + 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) { - robot.log.error(`Couldn't create project card for the PR: ${err}`, column.id, payload.pull_request.id); + robot.log.error(`Couldn't create project card for the PR: ${err}`, column.id, payload.pull_request.id) } } catch (err) { - robot.log.error(`Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id); + robot.log.error(`Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id) } } catch (err) { - robot.log.error(`Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName); + robot.log.error(`Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName) } -}; +} diff --git a/scripts/assign-to-bounty-awaiting-for-approval.js b/scripts/assign-to-bounty-awaiting-for-approval.js index e22e78f..66bc192 100644 --- a/scripts/assign-to-bounty-awaiting-for-approval.js +++ b/scripts/assign-to-bounty-awaiting-for-approval.js @@ -10,72 +10,72 @@ // Author: // PombeirP -const getConfig = require('probot-config'); -const defaultConfig = require('../lib/config.js'); -const Slack = require('probot-slack-status'); +const getConfig = require('probot-config') +const defaultConfig = require('../lib/config') +const Slack = require('probot-slack-status') -let slackClient = null; +let slackClient = null module.exports = function(robot) { // robot.on('slack.connected', ({ slack }) => { Slack(robot, (slack) => { - robot.log.trace("Connected, assigned slackClient"); - slackClient = slack; - }); + robot.log.trace("Connected, assigned slackClient") + slackClient = slack + }) robot.on('issues.labeled', async context => { // Make sure we don't listen to our own messages - if (context.isBot) { return; } + if (context.isBot) { return } // A new issue was labeled - await assignIssueToBountyAwaitingForApproval(context, robot); - }); -}; + await assignIssueToBountyAwaitingForApproval(context, robot) + }) +} async function assignIssueToBountyAwaitingForApproval(context, robot) { - const github = context.github; - const payload = context.payload; - //const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml')); - const config = defaultConfig(robot, '.github/github-bot.yml'); - const ownerName = payload.repository.owner.login; - const repoName = payload.repository.name; - const issueNumber = payload.issue.number; + const github = context.github + const payload = context.payload + //const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml')) + const config = defaultConfig(robot, '.github/github-bot.yml') + const ownerName = payload.repository.owner.login + const repoName = payload.repository.name + const issueNumber = payload.issue.number - robot.log(`assignIssueToBountyAwaitingForApproval - Handling Issue #${issueNumber} on repo ${ownerName}/${repoName}`); + robot.log(`assignIssueToBountyAwaitingForApproval - Handling Issue #${issueNumber} on repo ${ownerName}/${repoName}`) // Fetch org projects // TODO: The org project and project column info should be cached // in order to improve performance and reduce roundtrips try { - const orgName = 'status-im'; + const orgName = 'status-im' ghprojects = await github.projects.getOrgProjects({ org: orgName, state: "open" - }); + }) // Find "Status SOB Swarm" project - const projectBoardName = config['bounty-project-board'].name; - const project = ghprojects.data.find(function(p) { return p.name === projectBoardName }); + const projectBoardName = config['bounty-project-board'].name + const project = ghprojects.data.find(function(p) { return p.name === projectBoardName }) if (!project) { - robot.log.error(`Couldn't find project ${projectBoardName} in ${orgName} org`); - return; + robot.log.error(`Couldn't find project ${projectBoardName} in ${orgName} org`) + return } - robot.log.debug(`Fetched ${project.name} project (${project.id})`); + robot.log.debug(`Fetched ${project.name} project (${project.id})`) // Fetch bounty-awaiting-approval column ID try { - ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }); + ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }) - const approvalColumnName = config['bounty-project-board']['awaiting-approval-column-name']; - const column = ghcolumns.data.find(function(c) { return c.name === approvalColumnName }); + const approvalColumnName = config['bounty-project-board']['awaiting-approval-column-name'] + const column = ghcolumns.data.find(c => c.name === approvalColumnName) if (!column) { - robot.log.error(`Couldn't find ${approvalColumnName} column in project ${project.name}`); - return; + robot.log.error(`Couldn't find ${approvalColumnName} column in project ${project.name}`) + return } - robot.log.debug(`Fetched ${column.name} column (${column.id})`); + robot.log.debug(`Fetched ${column.name} column (${column.id})`) // Create project card for the issue in the bounty-awaiting-approval column try { @@ -83,26 +83,20 @@ async function assignIssueToBountyAwaitingForApproval(context, robot) { column_id: column.id, content_type: 'Issue', content_id: payload.issue.id - }); + }) - robot.log.debug(`Created card: ${ghcard.data.url}`, ghcard.data.id); + robot.log.debug(`Created card: ${ghcard.data.url}`, ghcard.data.id) // Send message to Slack - if (slackClient != null) { - const channel = slackClient.dataStore.getChannelByName(config.slack.notification.room); - try { - await slackClient.sendMessage(`Assigned issue to ${approvalColumnName} in ${projectBoardName} project\n${payload.issue.html_url}`, channel.id); - } catch(err) { - robot.log.error(`Failed to send Slack message to ${config.slack.notification.room} channel`); - } - } + const slackHelper = require('../lib/slack') + slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, `Assigned issue to ${approvalColumnName} in ${projectBoardName} project\n${payload.issue.html_url}`) } catch (err) { - robot.log.error(`Couldn't create project card for the issue: ${err}`, column.id, payload.issue.id); + robot.log.error(`Couldn't create project card for the issue: ${err}`, column.id, payload.issue.id) } } catch (err) { - robot.log.error(`Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id); + robot.log.error(`Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id) } } catch (err) { - robot.log.error(`Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName); + robot.log.error(`Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName) } -}; +} diff --git a/scripts/greet-new-contributor.js b/scripts/greet-new-contributor.js index e85b0f6..d4c1f82 100644 --- a/scripts/greet-new-contributor.js +++ b/scripts/greet-new-contributor.js @@ -10,39 +10,39 @@ // Author: // PombeirP -const getConfig = require('probot-config'); -const defaultConfig = require('../lib/config.js'); -const Slack = require('probot-slack-status'); +const getConfig = require('probot-config') +const defaultConfig = require('../lib/config') +const Slack = require('probot-slack-status') -let slackClient = null; +let slackClient = null module.exports = function(robot) { // robot.on('slack.connected', ({ slack }) => { Slack(robot, (slack) => { - robot.log.trace("Connected, assigned slackClient"); - slackClient = slack; - }); - + robot.log.trace("Connected, assigned slackClient") + slackClient = slack + }) + robot.on('pull_request.opened', async context => { // Make sure we don't listen to our own messages - if (context.isBot) { return; } + if (context.isBot) { return } // A new PR was opened - await greetNewContributor(context, robot); - }); -}; + await greetNewContributor(context, robot) + }) +} async function greetNewContributor(context, robot) { - const payload = context.payload; - const github = context.github; - //const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml')); - const config = defaultConfig(robot, '.github/github-bot.yml'); - const welcomeMessage = config['welcome-bot'].message; - const ownerName = payload.repository.owner.login; - const repoName = payload.repository.name; - const prNumber = payload.pull_request.number; + const payload = context.payload + const github = context.github + //const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml')) + const config = defaultConfig(robot, '.github/github-bot.yml') + const welcomeMessage = config['welcome-bot'].message + const ownerName = payload.repository.owner.login + const repoName = payload.repository.name + const prNumber = payload.pull_request.number - robot.log(`greetNewContributor - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`); + robot.log(`greetNewContributor - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`) try { ghissues = await github.issues.getForRepo({ @@ -52,7 +52,7 @@ async function greetNewContributor(context, robot) { creator: payload.pull_request.user.login }) - const userPullRequests = ghissues.data.filter(issue => issue.pull_request); + const userPullRequests = ghissues.data.filter(issue => issue.pull_request) if (userPullRequests.length === 1) { try { await github.issues.createComment({ @@ -63,23 +63,17 @@ async function greetNewContributor(context, robot) { }) // Send message to Slack - if (slackClient != null) { - const channel = slackClient.dataStore.getChannelByName(config.slack.notification.room); - try { - await slackClient.sendMessage(`Greeted ${payload.pull_request.user.login} on his first PR in the ${repoName} repo\n${payload.pull_request.html_url}`, channel.id); - } catch(err) { - robot.log.error(`Failed to send Slack message to ${config.slack.notification.room} channel`); - } - } + const slackHelper = require('../lib/slack') + slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, `Greeted ${payload.pull_request.user.login} on his first PR in the ${repoName} repo\n${payload.pull_request.html_url}`) } catch (err) { if (err.code !== 404) { - robot.log.error(`Couldn't create comment on PR: ${err}`, ownerName, repoName); + robot.log.error(`Couldn't create comment on PR: ${err}`, ownerName, repoName) } } } else { - robot.log.debug("This is not the user's first PR on the repo, ignoring", ownerName, repoName, payload.pull_request.user.login); + robot.log.debug("This is not the user's first PR on the repo, ignoring", ownerName, repoName, payload.pull_request.user.login) } } catch (err) { - robot.log.error(`Couldn't fetch the user's github issues for repo: ${err}`, ownerName, repoName); + robot.log.error(`Couldn't fetch the user's github issues for repo: ${err}`, ownerName, repoName) } -}; \ No newline at end of file +} \ No newline at end of file