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) {
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`)
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,17 +11,17 @@
// 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
})
@ -33,19 +33,19 @@ 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,7 +60,7 @@ 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}`)
}
@ -90,10 +90,10 @@ async function getPullRequestReviewStates(github, repo, pullRequest) {
}
async function getProjectFromName (github, ownerName, repoName, projectBoardName) {
ghprojects = await github.projects.getRepoProjects({
let ghprojects = await github.projects.getRepoProjects({
owner: ownerName,
repo: repoName,
state: "open"
state: 'open'
})
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) {
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
}
@ -127,8 +127,8 @@ async function checkOpenPullRequests(robot, context) {
// 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
@ -137,6 +137,7 @@ async function checkOpenPullRequests(robot, context) {
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 })
} catch (err) {
@ -178,23 +179,25 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
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`)
@ -213,7 +216,9 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
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})
}
@ -223,9 +228,7 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
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 {
@ -244,7 +247,9 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
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,
@ -261,8 +266,6 @@ async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest
}
}
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}`)
}
}

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,16 +10,16 @@
// 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
})
@ -41,8 +41,9 @@ async function assignPullRequestToReview(context, robot) {
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}`)
@ -50,15 +51,17 @@ async function assignPullRequestToReview(context, robot) {
// 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}`)
@ -69,10 +72,9 @@ async function assignPullRequestToReview(context, robot) {
// Fetch REVIEW column ID
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']
const column = ghcolumns.data.find(c => c.name === reviewColumnName)
column = ghcolumns.data.find(c => c.name === reviewColumnName)
if (!column) {
robot.log.error(`Couldn't find ${reviewColumnName} column in project ${project.name}`)
return
@ -90,15 +92,17 @@ async function assignPullRequestToReview(context, robot) {
// 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)
}
// Send message to Slack
const slackHelper = require('../lib/slack')

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,16 +10,16 @@
// 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
})
@ -48,7 +48,7 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
const config = defaultConfig(robot, '.github/github-bot.yml')
if (!config['bounty-project-board']) {
return;
return
}
const watchedLabelName = config['bounty-project-board']['label-name']
@ -66,17 +66,19 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
// 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
@ -86,10 +88,9 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
// Fetch bounty-awaiting-approval column ID
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']
const column = ghcolumns.data.find(c => c.name === approvalColumnName)
column = ghcolumns.data.find(c => c.name === approvalColumnName)
if (!column) {
robot.log.error(`Couldn't find ${approvalColumnName} column in project ${project.name}`)
return
@ -105,7 +106,14 @@ async function assignIssueToBountyAwaitingForApproval(context, robot, assign) {
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) {
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,16 +10,16 @@
// 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
})
@ -41,14 +41,15 @@ async function greetNewContributor(context, robot) {
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',
@ -58,8 +59,10 @@ async function greetNewContributor(context, robot) {
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,
@ -77,7 +80,7 @@ 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)