Make the functionality to move a PR to test a scheduled task, instead of an event handler

Sidesteps the problem that a build will still be running when we get the event, and therefore we'll never be in a position where all checks have passed
This commit is contained in:
Pedro Pombeiro 2018-01-23 19:38:25 +01:00
parent 33d639ad89
commit 038944f361
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
7 changed files with 1087 additions and 442 deletions

View File

@ -12,7 +12,9 @@ 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 {
await slackClient.sendMessage(message, channel.id) if (!process.env.DRY_RUN) {
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`)
} }

1126
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,7 @@
"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",
"probot-scheduler": "^1.0.3",
"probot-slack-status": "^0.2.2", "probot-slack-status": "^0.2.2",
"unfurl": "github:probot/unfurl", "unfurl": "github:probot/unfurl",
"wip-bot": "github:gr2m/wip-bot" "wip-bot": "github:gr2m/wip-bot"

View File

@ -4,7 +4,8 @@
// //
// Dependencies: // Dependencies:
// github: "^13.1.0" // github: "^13.1.0"
// probot-config "^0.1.0" // probot-config: "^0.1.0"
// probot-scheduler: "^1.0.3"
// probot-slack-status: "^0.2.2" // probot-slack-status: "^0.2.2"
// //
// Author: // Author:
@ -12,6 +13,7 @@
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 Slack = require('probot-slack-status') const Slack = require('probot-slack-status')
let slackClient = null let slackClient = null
@ -23,26 +25,84 @@ module.exports = function(robot) {
slackClient = slack slackClient = slack
}) })
robot.on('pull_request_review.submitted', context => assignPullRequestToTest(context, robot)) createScheduler(robot, { interval: 10 * 60 * 1000 })
robot.on('pull_request_review.edited', context => assignPullRequestToTest(context, robot)) robot.on('schedule.repository', context => checkOpenPullRequests(robot, context))
} }
async function getReviewApprovalState(github, payload) { async function getReviewApprovalState(github, robot, repo, pullRequest) {
const ownerName = payload.repository.owner.login const ownerName = repo.owner.login
const repoName = payload.repository.name const repoName = repo.name
const prNumber = payload.pull_request.number const prNumber = pullRequest.number
const threshold = 2
const ghreviews = await github.pullRequests.getReviews({owner: ownerName, repo: repoName, number: prNumber}) var finalReviewsMap = new Map()
const approvedReviews = ghreviews.data.filter(review => review.state === 'APPROVED') const ghreviews = await github.paginate(
if (approvedReviews.length >= 2) { github.pullRequests.getReviews({owner: ownerName, repo: repoName, number: prNumber}),
const reviewsWithChangesRequested = ghreviews.data.filter(review => review.state === 'CHANGES_REQUESTED') res => res.data)
for (var review of ghreviews) {
finalReviewsMap.set(review.user.id, review.state)
}
var finalReviews = Array.from(finalReviewsMap.values())
if (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) {
return 'approved' var isGreen = await isStatusGreen(github, robot, repo, pullRequest)
if (isGreen) {
return 'approved'
}
} }
} }
return 'pending' return 'pending'
} }
async function isStatusGreen(github, robot, repo, pullRequest) {
const ownerName = repo.owner.login
const repoName = repo.name
// Accumulate all the statuses by chronological order and check if we have a green light
var finalStatesMap = new Map()
const ghstatuses = await github.paginate(
github.repos.getStatuses({owner: ownerName, repo: repoName, ref: pullRequest.head.sha}),
res => res.data)
var sortedStatuses = Array.from(ghstatuses).sort((a, b) => {
if (a.updated_at < b.updated_at) {
return -1
}
if (a.updated_at > b.updated_at) {
return 1
}
// dates must be equal
return 0
})
for (var status of sortedStatuses) {
finalStatesMap.set(status.context, status.state)
}
var finalStates = Array.from(finalStatesMap.values())
const statusStates = finalStates.filter(state => state === 'success')
if (process.env.DRY_RUN_PR_TO_TEST) {
robot.log.debug(finalStates)
}
return statusStates.length == finalStates.length
}
async function getProjectFromName(github, ownerName, repoName, projectBoardName) {
ghprojects = await github.projects.getRepoProjects({
owner: ownerName,
repo: repoName,
state: "open"
})
return ghprojects.data.find(p => p.name === projectBoardName)
}
async function getProjectCardForPullRequest(github, robot, columnId, pullRequestUrl) { async function getProjectCardForPullRequest(github, robot, columnId, pullRequestUrl) {
const ghcards = await github.projects.getProjectCards({column_id: columnId}) const ghcards = await github.projects.getProjectCards({column_id: columnId})
@ -51,51 +111,31 @@ async function getProjectCardForPullRequest(github, robot, columnId, pullRequest
return ghcard return ghcard
} }
async function assignPullRequestToTest(context, robot) { async function checkOpenPullRequests(robot, context) {
// Make sure we don't listen to our own messages
if (context.isBot) { return }
const payload = context.payload
const github = context.github 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 config = defaultConfig(robot, '.github/github-bot.yml')
const ownerName = payload.repository.owner.login const projectBoardConfig = config['project-board']
const repoName = payload.repository.name
const prNumber = payload.pull_request.number
if (!config['project-board']) { if (!projectBoardConfig) {
return; return
} }
robot.log(`assignPullRequestToTest - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`) const reviewColumnName = projectBoardConfig['review-column-name']
const testColumnName = projectBoardConfig['test-column-name']
state = getReviewApprovalState(github, payload)
const reviewColumnName = config['project-board']['review-column-name']
const testColumnName = config['project-board']['test-column-name']
if (state === 'approved') {
srcColumnName = reviewColumnName
dstColumnName = testColumnName
} else {
srcColumnName = testColumnName
dstColumnName = reviewColumnName
}
// 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
try { try {
ghprojects = await github.projects.getRepoProjects({
owner: ownerName,
repo: repoName,
state: "open"
})
// Find "Pipeline for QA" project // Find "Pipeline for QA" project
const projectBoardName = config['project-board'].name project = await getProjectFromName(github, ownerName, repoName, projectBoardConfig.name)
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 ${projectBoardConfig.name} in repo ${ownerName}/${repoName}`)
return return
} }
@ -104,69 +144,125 @@ async function assignPullRequestToTest(context, robot) {
// Fetch column IDs // Fetch column IDs
try { try {
ghcolumns = await github.projects.getProjectColumns({ project_id: project.id }) ghcolumns = await github.projects.getProjectColumns({ project_id: project.id })
const srcColumn = ghcolumns.data.find(c => c.name === srcColumnName)
if (!srcColumn) {
robot.log.error(`Couldn't find ${srcColumnName} column in project ${project.name}`)
return
}
const dstColumn = ghcolumns.data.find(c => c.name === dstColumnName)
if (!dstColumn) {
robot.log.error(`Couldn't find ${dstColumnName} column in project ${project.name}`)
return
}
robot.log.debug(`Fetched ${srcColumn.name} (${srcColumn.id}), ${dstColumn.name} (${dstColumn.id}) columns`)
// Move PR card to the destination column
let ghcard = null
try {
ghcard = await getProjectCardForPullRequest(github, robot, srcColumn.id, payload.pull_request.issue_url)
} catch (err) {
robot.log.error(`Failed to retrieve project card for the PR, aborting: ${err}`, srcColumn.id, payload.pull_request.issue_url)
return
}
// Send message to Slack
const slackHelper = require('../lib/slack')
if (ghcard) {
try {
robot.log.trace(`Found card in source column ${ghcard.id}`, srcColumn.id)
// 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.debug(`Moved card: ${ghcard.url}`, ghcard.id)
} catch (err) {
robot.log.error(`Couldn't move project card for the PR: ${err}`, srcColumn.id, dstColumn.id, payload.pull_request.id)
slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, `I couldn't move the PR to ${dstColumnName} in ${projectBoardName} project :confused:\n${payload.pull_request.html_url}`)
return
}
} else {
try {
robot.log.debug(`Didn't find card in source column`, srcColumn.id)
// It wasn't in source column, 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: payload.pull_request.id
})
robot.log.debug(`Created card: ${ghcard.data.url}`, ghcard.data.id)
} catch (err) {
robot.log.error(`Couldn't create project card for the PR: ${err}`, dstColumn.id, payload.pull_request.id)
return
}
}
slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, `Assigned PR to ${dstColumnName} in ${projectBoardName} project\n${payload.pull_request.html_url}`)
} catch (err) { } 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)
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)
} }
} catch (err) { } 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)
} }
} }
async function assignPullRequestToCorrectColumn(github, robot, repo, pullRequest, reviewColumn, testColumn, room) {
const ownerName = repo.owner.login
const repoName = repo.name
const prNumber = pullRequest.number
try {
state = await getReviewApprovalState(github, robot, repo, pullRequest)
} catch (err) {
robot.log.error(`Couldn't calculate the PR approval state: ${err}`, ownerName, repoName, prNumber)
}
if (state === 'approved') {
srcColumn = reviewColumn
dstColumn = testColumn
} else {
srcColumn = testColumn
dstColumn = reviewColumn
}
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 {
ghcard = await getProjectCardForPullRequest(github, robot, srcColumn.id, pullRequest.issue_url)
} catch (err) {
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) {
// 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}`)
}
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, robot, dstColumn.id, pullRequest.issue_url)
if (ghcard) {
robot.log.trace(`Found card in target column, ignoring`, ghcard.id, dstColumn.id)
return
}
} catch (err) {
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) {
// 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) {
// We normally arrive here because there is already a card for the PR in another column
robot.log.debug(`Couldn't create project card for the PR: ${err}`, dstColumn.id, pullRequest.id)
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}`)
}
}

View File

@ -4,7 +4,7 @@
// //
// Dependencies: // Dependencies:
// github: "^13.1.0" // github: "^13.1.0"
// probot-config "^0.1.0" // probot-config: "^0.1.0"
// probot-slack-status: "^0.2.2" // probot-slack-status: "^0.2.2"
// //
// Author: // Author:
@ -79,27 +79,31 @@ async function assignPullRequestToReview(context, robot) {
} }
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)
// 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}`)
} catch (err) {
robot.log.error(`Couldn't create project card for the PR: ${err}`, column.id, payload.pull_request.id)
}
} catch (err) { } 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)
return
} }
} catch (err) { } 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)
return
}
// Create project card for the PR in the REVIEW column
try {
if (!process.env.DRY_RUN) {
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')
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)
} }
} }

View File

@ -4,7 +4,7 @@
// //
// Dependencies: // Dependencies:
// github: "^13.1.0" // github: "^13.1.0"
// probot-config "^0.1.0" // probot-config: "^0.1.0"
// probot-slack-status: "^0.2.2" // probot-slack-status: "^0.2.2"
// //
// Author: // Author:
@ -80,27 +80,31 @@ async function assignIssueToBountyAwaitingForApproval(context, robot) {
} }
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 {
ghcard = await github.projects.createProjectCard({
column_id: column.id,
content_type: 'Issue',
content_id: payload.issue.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 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)
}
} catch (err) { } 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)
return
} }
} catch (err) { } 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)
return
}
// Create project card for the issue in the bounty-awaiting-approval column
try {
if (!process.env.DRY_RUN) {
ghcard = await github.projects.createProjectCard({
column_id: column.id,
content_type: 'Issue',
content_id: payload.issue.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 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)
} }
} }

View File

@ -4,7 +4,7 @@
// //
// Dependencies: // Dependencies:
// github: "^13.1.0" // github: "^13.1.0"
// probot-config "^0.1.0" // probot-config: "^0.1.0"
// probot-slack-status: "^0.2.2" // probot-slack-status: "^0.2.2"
// //
// Author: // Author:
@ -59,12 +59,14 @@ async function greetNewContributor(context, robot) {
if (userPullRequests.length === 1) { if (userPullRequests.length === 1) {
try { try {
const welcomeMessage = config['welcome-bot'].message const welcomeMessage = config['welcome-bot'].message
await github.issues.createComment({ if (!process.env.DRY_RUN) {
owner: ownerName, await github.issues.createComment({
repo: repoName, owner: ownerName,
number: prNumber, repo: repoName,
body: welcomeMessage number: prNumber,
}) body: welcomeMessage
})
}
// Send message to Slack // Send message to Slack
const slackHelper = require('../lib/slack') const slackHelper = require('../lib/slack')