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

@ -11,7 +11,7 @@ module.exports = (robot, fileName) => {
// Get document, or throw exception on error
try {
const yaml = require('js-yaml')
const fs = require('fs')
const fs = require('fs')
return yaml.safeLoad(fs.readFileSync(fileName, 'utf8'))
} catch (e) {

View File

@ -12,13 +12,15 @@ module.exports.sendMessage = async (robot, slackClient, room, message) => {
if (slackClient != null) {
const channel = slackClient.dataStore.getChannelByName(room)
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)
}
} catch(err) {
robot.log.error(`Failed to send Slack message to ${room} channel`)
} catch (err) {
robot.log.error(`Failed to send Slack message to '${room}' channel`)
}
} 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"
},
"dependencies": {
"eslint": "^4.16.0",
"probot": "^5.0.0",
"probot-config": "^0.1.0",
"probot-gpg-status": "^0.5.4",
@ -19,6 +20,11 @@
"wip-bot": "github:gr2m/wip-bot"
},
"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",
"smee-client": "^1.0.1",
"standard": "^10.0.3"

View File

@ -1,6 +1,6 @@
// Description:
// 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:
// github: "^13.1.0"
@ -11,41 +11,41 @@
// Author:
// PombeirP
const getConfig = require('probot-config')
// const getConfig = require('probot-config')
const defaultConfig = require('../lib/config')
const createScheduler = require('probot-scheduler')
const Slack = require('probot-slack-status')
let slackClient = null
module.exports = function(robot) {
module.exports = robot => {
// robot.on('slack.connected', ({ slack }) => {
Slack(robot, (slack) => {
robot.log.trace("Connected, assigned slackClient")
robot.log.trace('Connected, assigned slackClient')
slackClient = slack
})
createScheduler(robot, { interval: 10 * 60 * 1000 })
robot.on('schedule.repository', context => checkOpenPullRequests(robot, context))
}
async function getReviewApprovalState(github, robot, repo, pullRequest) {
async function getReviewApprovalState (github, robot, repo, pullRequest) {
const threshold = 2 // Minimum number of approvers
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)
}
const approvedReviews = finalReviews.filter(reviewState => reviewState === 'APPROVED')
if (approvedReviews.length >= threshold) {
const reviewsWithChangesRequested = finalReviews.filter(reviewState => reviewState === 'CHANGES_REQUESTED')
if (reviewsWithChangesRequested.length == 0) {
if (reviewsWithChangesRequested.length === 0) {
// Get detailed pull request
const fullPullRequest = await github.pullRequests.get({owner: repo.owner.login, repo: repo.name, number: pullRequest.number})
pullRequest = fullPullRequest.data
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`)
}
return 'failed'
@ -60,18 +60,18 @@ async function getReviewApprovalState(github, robot, repo, pullRequest) {
state = 'failed'
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}`)
}
return state
}
}
return 'pending'
}
async function getPullRequestReviewStates(github, repo, pullRequest) {
async function getPullRequestReviewStates (github, repo, pullRequest) {
var finalReviewsMap = new Map()
const ghreviews = await github.paginate(
github.pullRequests.getReviews({owner: repo.owner.login, repo: repo.name, number: pullRequest.number}),
@ -89,81 +89,82 @@ async function getPullRequestReviewStates(github, repo, pullRequest) {
return Array.from(finalReviewsMap.values())
}
async function getProjectFromName(github, ownerName, repoName, projectBoardName) {
ghprojects = await github.projects.getRepoProjects({
async function getProjectFromName (github, ownerName, repoName, projectBoardName) {
let ghprojects = await github.projects.getRepoProjects({
owner: ownerName,
repo: repoName,
state: "open"
state: 'open'
})
return ghprojects.data.find(p => p.name === projectBoardName)
}
async function getProjectCardForPullRequest(github, columnId, pullRequestUrl) {
async function getProjectCardForPullRequest (github, columnId, pullRequestUrl) {
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
}
async function checkOpenPullRequests(robot, context) {
async function checkOpenPullRequests (robot, context) {
const github = context.github
const repo = context.payload.repository
const ownerName = repo.owner.login
const repoName = repo.name
//const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
// const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
const config = defaultConfig(robot, '.github/github-bot.yml')
const projectBoardConfig = config['project-board']
if (!projectBoardConfig) {
return
}
const reviewColumnName = projectBoardConfig['review-column-name']
const testColumnName = projectBoardConfig['test-column-name']
// Fetch repo projects
// TODO: The repo project and project column info should be cached
// in order to improve performance and reduce roundtrips
try {
// Find "Pipeline for QA" project
project = await getProjectFromName(github, ownerName, repoName, projectBoardConfig.name)
// Find 'Pipeline for QA' project
let project = await getProjectFromName(github, ownerName, repoName, projectBoardConfig.name)
if (!project) {
robot.log.error(`Couldn't find project ${projectBoardConfig.name} in repo ${ownerName}/${repoName}`)
return
}
robot.log.debug(`Fetched ${project.name} project (${project.id})`)
// Fetch column IDs
let ghcolumns = null
try {
ghcolumns = await github.projects.getProjectColumns({ project_id: project.id })
ghcolumns = await github.projects.getProjectColumns({ project_id: project.id })
} catch (err) {
robot.log.error(`Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id)
return
}
const reviewColumn = ghcolumns.data.find(c => c.name === reviewColumnName)
if (!reviewColumn) {
robot.log.error(`Couldn't find ${reviewColumnName} column in project ${project.name}`)
return
}
const testColumn = ghcolumns.data.find(c => c.name === testColumnName)
if (!testColumn) {
robot.log.error(`Couldn't find ${testColumnName} column in project ${project.name}`)
return
}
robot.log.debug(`Fetched ${reviewColumn.name} (${reviewColumn.id}), ${testColumn.name} (${testColumn.id}) columns`)
// Gather all open PRs in this repo
const allPullRequests = await github.paginate(
github.pullRequests.getAll({owner: ownerName, repo: repoName}),
res => res.data
)
// And make sure they are assigned to the correct prject column
for (var pullRequest of allPullRequests) {
await assignPullRequestToCorrectColumn(github, robot, repo, pullRequest, reviewColumn, testColumn, config.slack.notification.room)
@ -173,32 +174,34 @@ async function checkOpenPullRequests(robot, context) {
}
}
async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest, reviewColumn, testColumn, room) {
async function assignPullRequestToCorrectColumn (github, robot, repo, pullRequest, reviewColumn, testColumn, room) {
const ownerName = repo.owner.login
const repoName = repo.name
const prNumber = pullRequest.number
let state = null
try {
state = await getReviewApprovalState(github, robot, repo, pullRequest)
} catch (err) {
robot.log.error(`Couldn't calculate the PR approval state: ${err}`, ownerName, repoName, prNumber)
}
let srcColumn, dstColumn
switch (state) {
case 'approved':
srcColumn = reviewColumn
dstColumn = testColumn
break;
break
case 'failed':
srcColumn = testColumn
dstColumn = reviewColumn
break;
break
default:
return;
return
}
robot.log.debug(`assignPullRequestToTest - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}. PR should be in ${dstColumn.name} column`)
// Look for PR card in source column
let ghcard = null
try {
@ -207,31 +210,31 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
robot.log.error(`Failed to retrieve project card for the PR, aborting: ${err}`, srcColumn.id, pullRequest.issue_url)
return
}
if (ghcard) {
// Move PR card to the destination column
try {
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
await github.projects.moveProjectCard({id: ghcard.id, position: 'bottom', column_id: dstColumn.id})
}
robot.log.info(`Moved card ${ghcard.id} to ${dstColumn.name} for PR #${prNumber}`)
} catch (err) {
const slackHelper = require('../lib/slack')
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 ${dstColumnName} column :confused:\n${pullRequest.html_url}`)
}
slackHelper.sendMessage(robot, slackClient, room, `I couldn't move the PR to ${dstColumn.name} column :confused:\n${pullRequest.html_url}`)
return
}
} else {
try {
robot.log.debug(`Didn't find card in source column`, srcColumn.id)
// Look for PR card in destination column
try {
ghcard = await getProjectCardForPullRequest(github, dstColumn.id, pullRequest.issue_url)
@ -243,15 +246,17 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
robot.log.error(`Failed to retrieve project card for the PR, aborting: ${err}`, dstColumn.id, pullRequest.issue_url)
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
ghcard = await github.projects.createProjectCard({
column_id: dstColumn.id,
content_type: 'PullRequest',
content_id: pullRequest.id
})
robot.log.info(`Created card ${ghcard.data.id} in ${dstColumn.name} for PR #${prNumber}`)
}
} catch (err) {
@ -260,9 +265,7 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
return
}
}
if (!process.env.DRY_RUN_PR_TO_TEST) {
const slackHelper = require('../lib/slack')
slackHelper.sendMessage(robot, slackClient, room, `Assigned PR to ${dstColumn.name} column\n${pullRequest.html_url}`)
}
const slackHelper = require('../lib/slack')
slackHelper.sendMessage(robot, slackClient, room, `Assigned PR to ${dstColumn.name} column\n${pullRequest.html_url}`)
}

View File

@ -1,6 +1,6 @@
// Description:
// 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:
// github: "^13.1.0"
@ -10,74 +10,76 @@
// Author:
// PombeirP
const getConfig = require('probot-config')
// const getConfig = require('probot-config')
const defaultConfig = require('../lib/config')
const Slack = require('probot-slack-status')
let slackClient = null
module.exports = function(robot) {
module.exports = (robot) => {
// robot.on('slack.connected', ({ slack }) => {
Slack(robot, (slack) => {
robot.log.trace("Connected, assigned slackClient")
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 }
// A new PR was opened
await assignPullRequestToReview(context, robot)
})
}
async function 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 = 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
if (!config['project-board']) {
return;
const projectBoardConfig = config['project-board']
if (!projectBoardConfig) {
return
}
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
let column = null
const projectBoardName = projectBoardConfig.name
const reviewColumnName = projectBoardConfig['review-column-name']
try {
ghprojects = await github.projects.getRepoProjects({
let ghprojects = await github.projects.getRepoProjects({
owner: ownerName,
repo: repoName,
state: "open"
state: 'open'
})
// Find "Pipeline for QA" project
const projectBoardName = config['project-board'].name
// Find 'Pipeline for QA' project
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.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(c => c.name === reviewColumnName)
let ghcolumns = await github.projects.getProjectColumns({ project_id: project.id })
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.debug(`Fetched ${column.name} column (${column.id})`)
} catch (err) {
robot.log.error(`Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id)
@ -87,19 +89,21 @@ async function assignPullRequestToReview(context, robot) {
robot.log.error(`Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName)
return
}
// Create project card for the PR in the REVIEW column
try {
if (!process.env.DRY_RUN) {
ghcard = await github.projects.createProjectCard({
if (process.env.DRY_RUN) {
robot.log.debug('Would have created card', column.id, payload.pull_request.id)
} else {
let 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
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}`)

View File

@ -1,6 +1,6 @@
// Description:
// 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:
// github: "^13.1.0"
@ -10,91 +10,92 @@
// Author:
// PombeirP
const getConfig = require('probot-config')
// const getConfig = require('probot-config')
const defaultConfig = require('../lib/config')
const Slack = require('probot-slack-status')
let slackClient = null
module.exports = function(robot) {
module.exports = (robot) => {
// robot.on('slack.connected', ({ slack }) => {
Slack(robot, (slack) => {
robot.log.trace("Connected, assigned slackClient")
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 }
// A new issue was labeled
await assignIssueToBountyAwaitingForApproval(context, robot, true)
})
robot.on('issues.unlabeled', async context => {
// Make sure we don't listen to our own messages
if (context.isBot) { return }
// An issue was unlabeled
await assignIssueToBountyAwaitingForApproval(context, robot, false)
})
}
async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
async function assignIssueToBountyAwaitingForApproval (context, robot, assign) {
const github = context.github
const payload = context.payload
const ownerName = payload.repository.owner.login
const repoName = payload.repository.name
//const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
// const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
const config = defaultConfig(robot, '.github/github-bot.yml')
if (!config['bounty-project-board']) {
return;
return
}
const watchedLabelName = config['bounty-project-board']['label-name']
if (payload.label.name !== watchedLabelName) {
robot.log.debug(`assignIssueToBountyAwaitingForApproval - ${payload.label.name} doesn't match watched ${watchedLabelName} label. Ignoring`)
return
}
if (assign) {
robot.log(`assignIssueToBountyAwaitingForApproval - Handling labeling of #${payload.issue.number} with ${payload.label.name} on repo ${ownerName}/${repoName}`)
} else {
robot.log(`assignIssueToBountyAwaitingForApproval - Handling unlabeling of #${payload.issue.number} with ${payload.label.name} 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
let column = null
const projectBoardName = config['bounty-project-board'].name
const approvalColumnName = config['bounty-project-board']['awaiting-approval-column-name']
try {
const orgName = ownerName
ghprojects = await github.projects.getOrgProjects({
let ghprojects = await github.projects.getOrgProjects({
org: orgName,
state: "open"
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 })
// Find 'Status SOB Swarm' project
const project = ghprojects.data.find(p => p.name === projectBoardName)
if (!project) {
robot.log.error(`Couldn't find project ${projectBoardName} in ${orgName} org`)
return
}
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 })
const approvalColumnName = config['bounty-project-board']['awaiting-approval-column-name']
const column = ghcolumns.data.find(c => c.name === approvalColumnName)
let ghcolumns = await github.projects.getProjectColumns({ project_id: project.id })
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.debug(`Fetched ${column.name} column (${column.id})`)
} catch (err) {
robot.log.error(`Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id)
@ -104,8 +105,15 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
robot.log.error(`Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName)
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) {
try {
// 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
})
ghcard = ghcard.data
robot.log(`Created card: ${ghcard.url}`, ghcard.id)
} catch (err) {
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 {
ghcard = await getProjectCardForIssue(github, column.id, payload.issue.url)
await github.projects.deleteProjectCard({id: ghcard.id})
robot.log(`Deleted card: ${ghcard.url}`, ghcard.id)
} catch (err) {
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')
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}`)
} 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}`)
}
}
async function getProjectCardForIssue(github, columnId, issueUrl) {
async function getProjectCardForIssue (github, columnId, issueUrl) {
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
}

View File

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