Code cleanup

This commit is contained in:
Pedro Pombeiro 2018-01-23 15:27:25 +01:00
parent 50e8a236bc
commit a5f4354743
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
6 changed files with 146 additions and 144 deletions

View File

@ -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/

View File

@ -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
}

22
lib/slack.js Normal file
View File

@ -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")
}
}

View File

@ -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)
}
};
}

View File

@ -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)
}
};
}

View File

@ -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)
}
};
}