Fix linting issues. Add `eslint` package

This commit is contained in:
Pedro Pombeiro 2018-01-28 09:24:30 +01:00
parent 4d4a9c75cf
commit bf14c32d9d
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
9 changed files with 789 additions and 458 deletions

3
.eslintrc.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "standard"
}

View File

@ -12,13 +12,15 @@ module.exports.sendMessage = async (robot, slackClient, room, message) => {
if (slackClient != null) { if (slackClient != null) {
const channel = slackClient.dataStore.getChannelByName(room) const channel = slackClient.dataStore.getChannelByName(room)
try { try {
if (!process.env.DRY_RUN) { if (process.env.DRY_RUN) {
robot.log.debug(`Would have sent '${message}' to '${room}' channel`)
} else {
await slackClient.sendMessage(message, channel.id) await slackClient.sendMessage(message, channel.id)
} }
} catch (err) { } catch (err) {
robot.log.error(`Failed to send Slack message to ${room} channel`) robot.log.error(`Failed to send Slack message to '${room}' channel`)
} }
} else { } else {
robot.log.debug("Slack client not available") robot.log.debug('Slack client not available')
} }
} }

910
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@
"test": "jest && standard" "test": "jest && standard"
}, },
"dependencies": { "dependencies": {
"eslint": "^4.16.0",
"probot": "^5.0.0", "probot": "^5.0.0",
"probot-config": "^0.1.0", "probot-config": "^0.1.0",
"probot-gpg-status": "^0.5.4", "probot-gpg-status": "^0.5.4",
@ -19,6 +20,11 @@
"wip-bot": "github:gr2m/wip-bot" "wip-bot": "github:gr2m/wip-bot"
}, },
"devDependencies": { "devDependencies": {
"eslint-config-standard": "^11.0.0-beta.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"jest": "^22.1.4", "jest": "^22.1.4",
"smee-client": "^1.0.1", "smee-client": "^1.0.1",
"standard": "^10.0.3" "standard": "^10.0.3"

View File

@ -1,6 +1,6 @@
// Description: // Description:
// Script that listens to GitHub pull reviews // Script that listens to GitHub pull reviews
// and assigns the PR to TO TEST column on the "Pipeline for QA" project // and assigns the PR to TO TEST column on the 'Pipeline for QA' project
// //
// Dependencies: // Dependencies:
// github: "^13.1.0" // github: "^13.1.0"
@ -11,17 +11,17 @@
// Author: // Author:
// PombeirP // PombeirP
const getConfig = require('probot-config') // const getConfig = require('probot-config')
const defaultConfig = require('../lib/config') const defaultConfig = require('../lib/config')
const createScheduler = require('probot-scheduler') const createScheduler = require('probot-scheduler')
const Slack = require('probot-slack-status') const Slack = require('probot-slack-status')
let slackClient = null let slackClient = null
module.exports = function(robot) { module.exports = robot => {
// robot.on('slack.connected', ({ slack }) => { // robot.on('slack.connected', ({ slack }) => {
Slack(robot, (slack) => { Slack(robot, (slack) => {
robot.log.trace("Connected, assigned slackClient") robot.log.trace('Connected, assigned slackClient')
slackClient = slack slackClient = slack
}) })
@ -33,19 +33,19 @@ async function getReviewApprovalState(github, robot, repo, pullRequest) {
const threshold = 2 // Minimum number of approvers const threshold = 2 // Minimum number of approvers
var finalReviews = await getPullRequestReviewStates(github, repo, pullRequest) var finalReviews = await getPullRequestReviewStates(github, repo, pullRequest)
if (process.env.DRY_RUN_PR_TO_TEST) { if (process.env.DRY_RUN || process.env.DRY_RUN_PR_TO_TEST) {
robot.log.debug(finalReviews) robot.log.debug(finalReviews)
} }
const approvedReviews = finalReviews.filter(reviewState => reviewState === 'APPROVED') const approvedReviews = finalReviews.filter(reviewState => reviewState === 'APPROVED')
if (approvedReviews.length >= threshold) { if (approvedReviews.length >= threshold) {
const reviewsWithChangesRequested = finalReviews.filter(reviewState => reviewState === 'CHANGES_REQUESTED') const reviewsWithChangesRequested = finalReviews.filter(reviewState => reviewState === 'CHANGES_REQUESTED')
if (reviewsWithChangesRequested.length == 0) { if (reviewsWithChangesRequested.length === 0) {
// Get detailed pull request // Get detailed pull request
const fullPullRequest = await github.pullRequests.get({owner: repo.owner.login, repo: repo.name, number: pullRequest.number}) const fullPullRequest = await github.pullRequests.get({owner: repo.owner.login, repo: repo.name, number: pullRequest.number})
pullRequest = fullPullRequest.data pullRequest = fullPullRequest.data
if (pullRequest.mergeable !== null && pullRequest.mergeable !== undefined && !pullRequest.mergeable) { if (pullRequest.mergeable !== null && pullRequest.mergeable !== undefined && !pullRequest.mergeable) {
if (process.env.DRY_RUN_PR_TO_TEST) { if (process.env.DRY_RUN || process.env.DRY_RUN_PR_TO_TEST) {
robot.log.debug(`pullRequest.mergeable is ${pullRequest.mergeable}, considering as failed`) robot.log.debug(`pullRequest.mergeable is ${pullRequest.mergeable}, considering as failed`)
} }
return 'failed' return 'failed'
@ -60,7 +60,7 @@ async function getReviewApprovalState(github, robot, repo, pullRequest) {
state = 'failed' state = 'failed'
break break
} }
if (process.env.DRY_RUN_PR_TO_TEST) { if (process.env.DRY_RUN || process.env.DRY_RUN_PR_TO_TEST) {
robot.log.debug(`pullRequest.mergeable_state is ${pullRequest.mergeable_state}, considering state as ${state}`) robot.log.debug(`pullRequest.mergeable_state is ${pullRequest.mergeable_state}, considering state as ${state}`)
} }
@ -90,10 +90,10 @@ async function getPullRequestReviewStates(github, repo, pullRequest) {
} }
async function getProjectFromName (github, ownerName, repoName, projectBoardName) { async function getProjectFromName (github, ownerName, repoName, projectBoardName) {
ghprojects = await github.projects.getRepoProjects({ let ghprojects = await github.projects.getRepoProjects({
owner: ownerName, owner: ownerName,
repo: repoName, repo: repoName,
state: "open" state: 'open'
}) })
return ghprojects.data.find(p => p.name === projectBoardName) return ghprojects.data.find(p => p.name === projectBoardName)
@ -101,7 +101,7 @@ async function getProjectFromName(github, ownerName, repoName, projectBoardName)
async function getProjectCardForPullRequest (github, columnId, pullRequestUrl) { async function getProjectCardForPullRequest (github, columnId, pullRequestUrl) {
const ghcards = await github.projects.getProjectCards({column_id: columnId}) const ghcards = await github.projects.getProjectCards({column_id: columnId})
ghcard = ghcards.data.find(c => c.content_url === pullRequestUrl) let ghcard = ghcards.data.find(c => c.content_url === pullRequestUrl)
return ghcard return ghcard
} }
@ -127,8 +127,8 @@ async function checkOpenPullRequests(robot, context) {
// TODO: The repo project and project column info should be cached // TODO: The repo project and project column info should be cached
// in order to improve performance and reduce roundtrips // in order to improve performance and reduce roundtrips
try { try {
// Find "Pipeline for QA" project // Find 'Pipeline for QA' project
project = await getProjectFromName(github, ownerName, repoName, projectBoardConfig.name) let project = await getProjectFromName(github, ownerName, repoName, projectBoardConfig.name)
if (!project) { if (!project) {
robot.log.error(`Couldn't find project ${projectBoardConfig.name} in repo ${ownerName}/${repoName}`) robot.log.error(`Couldn't find project ${projectBoardConfig.name} in repo ${ownerName}/${repoName}`)
return return
@ -137,6 +137,7 @@ async function checkOpenPullRequests(robot, context) {
robot.log.debug(`Fetched ${project.name} project (${project.id})`) robot.log.debug(`Fetched ${project.name} project (${project.id})`)
// Fetch column IDs // Fetch column IDs
let ghcolumns = null
try { try {
ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }) ghcolumns = await github.projects.getProjectColumns({ project_id: project.id })
} catch (err) { } catch (err) {
@ -178,23 +179,25 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
const repoName = repo.name const repoName = repo.name
const prNumber = pullRequest.number const prNumber = pullRequest.number
let state = null
try { try {
state = await getReviewApprovalState(github, robot, repo, pullRequest) state = await getReviewApprovalState(github, robot, repo, pullRequest)
} catch (err) { } catch (err) {
robot.log.error(`Couldn't calculate the PR approval state: ${err}`, ownerName, repoName, prNumber) robot.log.error(`Couldn't calculate the PR approval state: ${err}`, ownerName, repoName, prNumber)
} }
let srcColumn, dstColumn
switch (state) { switch (state) {
case 'approved': case 'approved':
srcColumn = reviewColumn srcColumn = reviewColumn
dstColumn = testColumn dstColumn = testColumn
break; break
case 'failed': case 'failed':
srcColumn = testColumn srcColumn = testColumn
dstColumn = reviewColumn dstColumn = reviewColumn
break; break
default: default:
return; return
} }
robot.log.debug(`assignPullRequestToTest - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}. PR should be in ${dstColumn.name} column`) robot.log.debug(`assignPullRequestToTest - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}. PR should be in ${dstColumn.name} column`)
@ -213,7 +216,9 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
try { try {
robot.log.trace(`Found card in source column`, ghcard.id, srcColumn.id) robot.log.trace(`Found card in source column`, ghcard.id, srcColumn.id)
if (!process.env.DRY_RUN && !process.env.DRY_RUN_PR_TO_TEST) { if (process.env.DRY_RUN || process.env.DRY_RUN_PR_TO_TEST) {
robot.log.info(`Would have moved card ${ghcard.id} to ${dstColumn.name} for PR #${prNumber}`)
} else {
// Found in the source column, let's move it to the destination column // Found in the source column, let's move it to the destination column
await github.projects.moveProjectCard({id: ghcard.id, position: 'bottom', column_id: dstColumn.id}) await github.projects.moveProjectCard({id: ghcard.id, position: 'bottom', column_id: dstColumn.id})
} }
@ -223,9 +228,7 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
const slackHelper = require('../lib/slack') const slackHelper = require('../lib/slack')
robot.log.error(`Couldn't move project card for the PR: ${err}`, srcColumn.id, dstColumn.id, pullRequest.id) robot.log.error(`Couldn't move project card for the PR: ${err}`, srcColumn.id, dstColumn.id, pullRequest.id)
if (!process.env.DRY_RUN_PR_TO_TEST) { slackHelper.sendMessage(robot, slackClient, room, `I couldn't move the PR to ${dstColumn.name} column :confused:\n${pullRequest.html_url}`)
slackHelper.sendMessage(robot, slackClient, room, `I couldn't move the PR to ${dstColumnName} column :confused:\n${pullRequest.html_url}`)
}
return return
} }
} else { } else {
@ -244,7 +247,9 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
return return
} }
if (!process.env.DRY_RUN && !process.env.DRY_RUN_PR_TO_TEST) { if (process.env.DRY_RUN || process.env.DRY_RUN_PR_TO_TEST) {
robot.log.info(`Would have created card ${ghcard.data.id} in ${dstColumn.name} for PR #${prNumber}`)
} else {
// It wasn't in either the source nor the destination columns, let's create a new card for it in the destination column // It wasn't in either the source nor the destination columns, let's create a new card for it in the destination column
ghcard = await github.projects.createProjectCard({ ghcard = await github.projects.createProjectCard({
column_id: dstColumn.id, column_id: dstColumn.id,
@ -261,8 +266,6 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
} }
} }
if (!process.env.DRY_RUN_PR_TO_TEST) {
const slackHelper = require('../lib/slack') const slackHelper = require('../lib/slack')
slackHelper.sendMessage(robot, slackClient, room, `Assigned PR to ${dstColumn.name} column\n${pullRequest.html_url}`) slackHelper.sendMessage(robot, slackClient, room, `Assigned PR to ${dstColumn.name} column\n${pullRequest.html_url}`)
} }
}

View File

@ -1,6 +1,6 @@
// Description: // Description:
// Script that listens to new GitHub pull requests // Script that listens to new GitHub pull requests
// and assigns them to the REVIEW column on the "Pipeline for QA" project // and assigns them to the REVIEW column on the 'Pipeline for QA' project
// //
// Dependencies: // Dependencies:
// github: "^13.1.0" // github: "^13.1.0"
@ -10,16 +10,16 @@
// Author: // Author:
// PombeirP // PombeirP
const getConfig = require('probot-config') // const getConfig = require('probot-config')
const defaultConfig = require('../lib/config') const defaultConfig = require('../lib/config')
const Slack = require('probot-slack-status') const Slack = require('probot-slack-status')
let slackClient = null let slackClient = null
module.exports = function(robot) { module.exports = (robot) => {
// robot.on('slack.connected', ({ slack }) => { // robot.on('slack.connected', ({ slack }) => {
Slack(robot, (slack) => { Slack(robot, (slack) => {
robot.log.trace("Connected, assigned slackClient") robot.log.trace('Connected, assigned slackClient')
slackClient = slack slackClient = slack
}) })
@ -41,8 +41,9 @@ async function assignPullRequestToReview(context, robot) {
const repoName = payload.repository.name const repoName = payload.repository.name
const prNumber = payload.pull_request.number const prNumber = payload.pull_request.number
if (!config['project-board']) { const projectBoardConfig = config['project-board']
return; if (!projectBoardConfig) {
return
} }
robot.log(`assignPullRequestToReview - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`) robot.log(`assignPullRequestToReview - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`)
@ -50,15 +51,17 @@ async function assignPullRequestToReview(context, robot) {
// Fetch repo projects // Fetch repo projects
// TODO: The repo project and project column info should be cached // TODO: The repo project and project column info should be cached
// in order to improve performance and reduce roundtrips // in order to improve performance and reduce roundtrips
let column = null
const projectBoardName = projectBoardConfig.name
const reviewColumnName = projectBoardConfig['review-column-name']
try { try {
ghprojects = await github.projects.getRepoProjects({ let ghprojects = await github.projects.getRepoProjects({
owner: ownerName, owner: ownerName,
repo: repoName, repo: repoName,
state: "open" state: 'open'
}) })
// Find "Pipeline for QA" project // Find 'Pipeline for QA' project
const projectBoardName = config['project-board'].name
const project = ghprojects.data.find(p => p.name === projectBoardName) const project = ghprojects.data.find(p => p.name === projectBoardName)
if (!project) { if (!project) {
robot.log.error(`Couldn't find project ${projectBoardName} in repo ${ownerName}/${repoName}`) robot.log.error(`Couldn't find project ${projectBoardName} in repo ${ownerName}/${repoName}`)
@ -69,10 +72,9 @@ async function assignPullRequestToReview(context, robot) {
// Fetch REVIEW column ID // Fetch REVIEW column ID
try { try {
ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }) let ghcolumns = await github.projects.getProjectColumns({ project_id: project.id })
const reviewColumnName = config['project-board']['review-column-name'] column = ghcolumns.data.find(c => c.name === reviewColumnName)
const column = ghcolumns.data.find(c => c.name === reviewColumnName)
if (!column) { if (!column) {
robot.log.error(`Couldn't find ${reviewColumnName} column in project ${project.name}`) robot.log.error(`Couldn't find ${reviewColumnName} column in project ${project.name}`)
return return
@ -90,15 +92,17 @@ async function assignPullRequestToReview(context, robot) {
// Create project card for the PR in the REVIEW column // Create project card for the PR in the REVIEW column
try { try {
if (!process.env.DRY_RUN) { if (process.env.DRY_RUN) {
ghcard = await github.projects.createProjectCard({ robot.log.debug('Would have created card', column.id, payload.pull_request.id)
} else {
let ghcard = await github.projects.createProjectCard({
column_id: column.id, column_id: column.id,
content_type: 'PullRequest', content_type: 'PullRequest',
content_id: payload.pull_request.id 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 // Send message to Slack
const slackHelper = require('../lib/slack') const slackHelper = require('../lib/slack')

View File

@ -1,6 +1,6 @@
// Description: // Description:
// Script that listens to new labels on GitHub issues // 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 // and assigns the issues to the bounty-awaiting-approval column on the 'Status SOB Swarm' project
// //
// Dependencies: // Dependencies:
// github: "^13.1.0" // github: "^13.1.0"
@ -10,16 +10,16 @@
// Author: // Author:
// PombeirP // PombeirP
const getConfig = require('probot-config') // const getConfig = require('probot-config')
const defaultConfig = require('../lib/config') const defaultConfig = require('../lib/config')
const Slack = require('probot-slack-status') const Slack = require('probot-slack-status')
let slackClient = null let slackClient = null
module.exports = function(robot) { module.exports = (robot) => {
// robot.on('slack.connected', ({ slack }) => { // robot.on('slack.connected', ({ slack }) => {
Slack(robot, (slack) => { Slack(robot, (slack) => {
robot.log.trace("Connected, assigned slackClient") robot.log.trace('Connected, assigned slackClient')
slackClient = slack slackClient = slack
}) })
@ -48,7 +48,7 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
const config = defaultConfig(robot, '.github/github-bot.yml') const config = defaultConfig(robot, '.github/github-bot.yml')
if (!config['bounty-project-board']) { if (!config['bounty-project-board']) {
return; return
} }
const watchedLabelName = config['bounty-project-board']['label-name'] const watchedLabelName = config['bounty-project-board']['label-name']
@ -66,17 +66,19 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
// Fetch org projects // Fetch org projects
// TODO: The org project and project column info should be cached // TODO: The org project and project column info should be cached
// in order to improve performance and reduce roundtrips // in order to improve performance and reduce roundtrips
let column = null
const projectBoardName = config['bounty-project-board'].name
const approvalColumnName = config['bounty-project-board']['awaiting-approval-column-name']
try { try {
const orgName = ownerName const orgName = ownerName
ghprojects = await github.projects.getOrgProjects({ let ghprojects = await github.projects.getOrgProjects({
org: orgName, org: orgName,
state: "open" state: 'open'
}) })
// Find "Status SOB Swarm" project // Find 'Status SOB Swarm' project
const projectBoardName = config['bounty-project-board'].name const project = ghprojects.data.find(p => p.name === projectBoardName)
const project = ghprojects.data.find(function(p) { return p.name === projectBoardName })
if (!project) { if (!project) {
robot.log.error(`Couldn't find project ${projectBoardName} in ${orgName} org`) robot.log.error(`Couldn't find project ${projectBoardName} in ${orgName} org`)
return return
@ -86,10 +88,9 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
// Fetch bounty-awaiting-approval column ID // Fetch bounty-awaiting-approval column ID
try { try {
ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }) let ghcolumns = await github.projects.getProjectColumns({ project_id: project.id })
const approvalColumnName = config['bounty-project-board']['awaiting-approval-column-name'] column = ghcolumns.data.find(c => c.name === approvalColumnName)
const column = ghcolumns.data.find(c => c.name === approvalColumnName)
if (!column) { if (!column) {
robot.log.error(`Couldn't find ${approvalColumnName} column in project ${project.name}`) robot.log.error(`Couldn't find ${approvalColumnName} column in project ${project.name}`)
return return
@ -105,7 +106,14 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
return return
} }
if (!process.env.DRY_RUN) { let ghcard = null
if (process.env.DRY_RUN) {
if (assign) {
robot.log.info(`Would have created card for issue`, column.id, payload.issue.id)
} else {
robot.log.info(`Would have deleted card for issue`, column.id, payload.issue.id)
}
} else {
if (assign) { if (assign) {
try { try {
// Create project card for the issue in the bounty-awaiting-approval column // Create project card for the issue in the bounty-awaiting-approval column
@ -115,6 +123,8 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
content_id: payload.issue.id content_id: payload.issue.id
}) })
ghcard = ghcard.data ghcard = ghcard.data
robot.log(`Created card: ${ghcard.url}`, ghcard.id)
} catch (err) { } 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)
} }
@ -122,27 +132,25 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
try { try {
ghcard = await getProjectCardForIssue(github, column.id, payload.issue.url) ghcard = await getProjectCardForIssue(github, column.id, payload.issue.url)
await github.projects.deleteProjectCard({id: ghcard.id}) await github.projects.deleteProjectCard({id: ghcard.id})
robot.log(`Deleted card: ${ghcard.url}`, ghcard.id)
} catch (err) { } catch (err) {
robot.log.error(`Couldn't delete project card for the issue: ${err}`, column.id, payload.issue.id) robot.log.error(`Couldn't delete project card for the issue: ${err}`, column.id, payload.issue.id)
} }
} }
} }
// Send message to Slack
const slackHelper = require('../lib/slack') const slackHelper = require('../lib/slack')
if (assign) { if (assign) {
robot.log(`Created card: ${ghcard.url}`, ghcard.id)
// Send message to Slack
slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, `Assigned issue to ${approvalColumnName} in ${projectBoardName} project\n${payload.issue.html_url}`) slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, `Assigned issue to ${approvalColumnName} in ${projectBoardName} project\n${payload.issue.html_url}`)
} else { } else {
robot.log(`Deleted card: ${ghcard.url}`, ghcard.id)
// Send message to Slack
slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, `Unassigned issue from ${approvalColumnName} in ${projectBoardName} project\n${payload.issue.html_url}`) slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, `Unassigned issue from ${approvalColumnName} in ${projectBoardName} project\n${payload.issue.html_url}`)
} }
} }
async function getProjectCardForIssue (github, columnId, issueUrl) { async function getProjectCardForIssue (github, columnId, issueUrl) {
const ghcards = await github.projects.getProjectCards({column_id: columnId}) const ghcards = await github.projects.getProjectCards({column_id: columnId})
ghcard = ghcards.data.find(c => c.content_url === issueUrl) let ghcard = ghcards.data.find(c => c.content_url === issueUrl)
return ghcard return ghcard
} }

View File

@ -10,16 +10,16 @@
// Author: // Author:
// PombeirP // PombeirP
const getConfig = require('probot-config') // const getConfig = require('probot-config')
const defaultConfig = require('../lib/config') const defaultConfig = require('../lib/config')
const Slack = require('probot-slack-status') const Slack = require('probot-slack-status')
let slackClient = null let slackClient = null
module.exports = function(robot) { module.exports = (robot) => {
// robot.on('slack.connected', ({ slack }) => { // robot.on('slack.connected', ({ slack }) => {
Slack(robot, (slack) => { Slack(robot, (slack) => {
robot.log.trace("Connected, assigned slackClient") robot.log.trace('Connected, assigned slackClient')
slackClient = slack slackClient = slack
}) })
@ -41,14 +41,15 @@ async function greetNewContributor(context, robot) {
const repoName = payload.repository.name const repoName = payload.repository.name
const prNumber = payload.pull_request.number const prNumber = payload.pull_request.number
if (!config['welcome-bot']) { const welcomeBotConfig = config['welcome-bot']
return; if (!welcomeBotConfig) {
return
} }
robot.log(`greetNewContributor - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`) robot.log(`greetNewContributor - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`)
try { try {
ghissues = await github.issues.getForRepo({ let ghissues = await github.issues.getForRepo({
owner: ownerName, owner: ownerName,
repo: repoName, repo: repoName,
state: 'all', state: 'all',
@ -58,8 +59,10 @@ async function greetNewContributor(context, robot) {
const userPullRequests = ghissues.data.filter(issue => issue.pull_request) const userPullRequests = ghissues.data.filter(issue => issue.pull_request)
if (userPullRequests.length === 1) { if (userPullRequests.length === 1) {
try { try {
const welcomeMessage = config['welcome-bot'].message const welcomeMessage = welcomeBotConfig.message
if (!process.env.DRY_RUN) { if (process.env.DRY_RUN) {
robot.log('Would have created comment in GHI', ownerName, repoName, prNumber, welcomeMessage)
} else {
await github.issues.createComment({ await github.issues.createComment({
owner: ownerName, owner: ownerName,
repo: repoName, repo: repoName,
@ -77,7 +80,7 @@ async function greetNewContributor(context, robot) {
} }
} }
} else { } 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) { } 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)