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:
parent
33d639ad89
commit
038944f361
|
@ -12,7 +12,9 @@ module.exports.sendMessage = async (robot, slackClient, room, message) => {
|
|||
if (slackClient != null) {
|
||||
const channel = slackClient.dataStore.getChannelByName(room)
|
||||
try {
|
||||
if (!process.env.DRY_RUN) {
|
||||
await slackClient.sendMessage(message, channel.id)
|
||||
}
|
||||
} catch(err) {
|
||||
robot.log.error(`Failed to send Slack message to ${room} channel`)
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -13,6 +13,7 @@
|
|||
"probot": "^5.0.0",
|
||||
"probot-config": "^0.1.0",
|
||||
"probot-gpg-status": "^0.5.4",
|
||||
"probot-scheduler": "^1.0.3",
|
||||
"probot-slack-status": "^0.2.2",
|
||||
"unfurl": "github:probot/unfurl",
|
||||
"wip-bot": "github:gr2m/wip-bot"
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
//
|
||||
// Dependencies:
|
||||
// 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"
|
||||
//
|
||||
// Author:
|
||||
|
@ -12,6 +13,7 @@
|
|||
|
||||
const getConfig = require('probot-config')
|
||||
const defaultConfig = require('../lib/config')
|
||||
const createScheduler = require('probot-scheduler')
|
||||
const Slack = require('probot-slack-status')
|
||||
|
||||
let slackClient = null
|
||||
|
@ -23,27 +25,85 @@ module.exports = function(robot) {
|
|||
slackClient = slack
|
||||
})
|
||||
|
||||
robot.on('pull_request_review.submitted', context => assignPullRequestToTest(context, robot))
|
||||
robot.on('pull_request_review.edited', context => assignPullRequestToTest(context, robot))
|
||||
createScheduler(robot, { interval: 10 * 60 * 1000 })
|
||||
robot.on('schedule.repository', context => checkOpenPullRequests(robot, context))
|
||||
}
|
||||
|
||||
async function getReviewApprovalState(github, payload) {
|
||||
const ownerName = payload.repository.owner.login
|
||||
const repoName = payload.repository.name
|
||||
const prNumber = payload.pull_request.number
|
||||
async function getReviewApprovalState(github, robot, repo, pullRequest) {
|
||||
const ownerName = repo.owner.login
|
||||
const repoName = repo.name
|
||||
const prNumber = pullRequest.number
|
||||
const threshold = 2
|
||||
|
||||
const ghreviews = await github.pullRequests.getReviews({owner: ownerName, repo: repoName, number: prNumber})
|
||||
const approvedReviews = ghreviews.data.filter(review => review.state === 'APPROVED')
|
||||
if (approvedReviews.length >= 2) {
|
||||
const reviewsWithChangesRequested = ghreviews.data.filter(review => review.state === 'CHANGES_REQUESTED')
|
||||
var finalReviewsMap = new Map()
|
||||
const ghreviews = await github.paginate(
|
||||
github.pullRequests.getReviews({owner: ownerName, repo: repoName, number: prNumber}),
|
||||
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) {
|
||||
var isGreen = await isStatusGreen(github, robot, repo, pullRequest)
|
||||
if (isGreen) {
|
||||
return 'approved'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
const ghcards = await github.projects.getProjectCards({column_id: columnId})
|
||||
ghcard = ghcards.data.find(c => c.content_url === pullRequestUrl)
|
||||
|
@ -51,51 +111,31 @@ async function getProjectCardForPullRequest(github, robot, columnId, pullRequest
|
|||
return ghcard
|
||||
}
|
||||
|
||||
async function assignPullRequestToTest(context, robot) {
|
||||
// Make sure we don't listen to our own messages
|
||||
if (context.isBot) { return }
|
||||
|
||||
const payload = context.payload
|
||||
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 = defaultConfig(robot, '.github/github-bot.yml')
|
||||
const ownerName = payload.repository.owner.login
|
||||
const repoName = payload.repository.name
|
||||
const prNumber = payload.pull_request.number
|
||||
const projectBoardConfig = config['project-board']
|
||||
|
||||
if (!config['project-board']) {
|
||||
return;
|
||||
if (!projectBoardConfig) {
|
||||
return
|
||||
}
|
||||
|
||||
robot.log(`assignPullRequestToTest - Handling Pull Request #${prNumber} on repo ${ownerName}/${repoName}`)
|
||||
|
||||
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
|
||||
}
|
||||
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 {
|
||||
ghprojects = await github.projects.getRepoProjects({
|
||||
owner: ownerName,
|
||||
repo: repoName,
|
||||
state: "open"
|
||||
})
|
||||
|
||||
// Find "Pipeline for QA" project
|
||||
const projectBoardName = config['project-board'].name
|
||||
const project = ghprojects.data.find(p => p.name === projectBoardName)
|
||||
project = await getProjectFromName(github, ownerName, repoName, projectBoardConfig.name)
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -104,69 +144,125 @@ async function assignPullRequestToTest(context, robot) {
|
|||
// Fetch column IDs
|
||||
try {
|
||||
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}`)
|
||||
} catch (err) {
|
||||
robot.log.error(`Couldn't fetch the github columns for project: ${err}`, ownerName, repoName, project.id)
|
||||
return
|
||||
}
|
||||
|
||||
const dstColumn = ghcolumns.data.find(c => c.name === dstColumnName)
|
||||
if (!dstColumn) {
|
||||
robot.log.error(`Couldn't find ${dstColumnName} column in project ${project.name}`)
|
||||
const reviewColumn = ghcolumns.data.find(c => c.name === reviewColumnName)
|
||||
if (!reviewColumn) {
|
||||
robot.log.error(`Couldn't find ${reviewColumnName} column in project ${project.name}`)
|
||||
return
|
||||
}
|
||||
|
||||
robot.log.debug(`Fetched ${srcColumn.name} (${srcColumn.id}), ${dstColumn.name} (${dstColumn.id}) columns`)
|
||||
const testColumn = ghcolumns.data.find(c => c.name === testColumnName)
|
||||
if (!testColumn) {
|
||||
robot.log.error(`Couldn't find ${testColumnName} column in project ${project.name}`)
|
||||
return
|
||||
}
|
||||
|
||||
// Move PR card to the destination column
|
||||
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) {
|
||||
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, payload.pull_request.issue_url)
|
||||
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, payload.pull_request.issue_url)
|
||||
robot.log.error(`Failed to retrieve project card for the PR, aborting: ${err}`, srcColumn.id, pullRequest.issue_url)
|
||||
return
|
||||
}
|
||||
|
||||
// Send message to Slack
|
||||
const slackHelper = require('../lib/slack')
|
||||
|
||||
if (ghcard) {
|
||||
// Move PR card to the destination column
|
||||
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) {
|
||||
// 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)
|
||||
robot.log.info(`Moved card ${ghcard.id} to ${dstColumn.name} for PR #${prNumber}`)
|
||||
} 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}`)
|
||||
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)
|
||||
|
||||
// It wasn't in source column, let's create a new card for it in the destination column
|
||||
// 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: payload.pull_request.id
|
||||
content_id: pullRequest.id
|
||||
})
|
||||
|
||||
robot.log.debug(`Created card: ${ghcard.data.url}`, ghcard.data.id)
|
||||
robot.log.info(`Created card ${ghcard.data.id} in ${dstColumn.name} for PR #${prNumber}`)
|
||||
}
|
||||
} catch (err) {
|
||||
robot.log.error(`Couldn't create project card for the PR: ${err}`, dstColumn.id, payload.pull_request.id)
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
slackHelper.sendMessage(robot, slackClient, config.slack.notification.room, `Assigned PR to ${dstColumnName} in ${projectBoardName} project\n${payload.pull_request.html_url}`)
|
||||
} catch (err) {
|
||||
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)
|
||||
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}`)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//
|
||||
// Dependencies:
|
||||
// github: "^13.1.0"
|
||||
// probot-config "^0.1.0"
|
||||
// probot-config: "^0.1.0"
|
||||
// probot-slack-status: "^0.2.2"
|
||||
//
|
||||
// Author:
|
||||
|
@ -79,14 +79,24 @@ async function assignPullRequestToReview(context, robot) {
|
|||
}
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
} catch (err) {
|
||||
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)
|
||||
|
||||
|
@ -96,10 +106,4 @@ async function assignPullRequestToReview(context, robot) {
|
|||
} catch (err) {
|
||||
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)
|
||||
}
|
||||
} catch (err) {
|
||||
robot.log.error(`Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//
|
||||
// Dependencies:
|
||||
// github: "^13.1.0"
|
||||
// probot-config "^0.1.0"
|
||||
// probot-config: "^0.1.0"
|
||||
// probot-slack-status: "^0.2.2"
|
||||
//
|
||||
// Author:
|
||||
|
@ -80,14 +80,24 @@ async function assignIssueToBountyAwaitingForApproval(context, robot) {
|
|||
}
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
} catch (err) {
|
||||
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)
|
||||
|
||||
|
@ -97,10 +107,4 @@ async function assignIssueToBountyAwaitingForApproval(context, robot) {
|
|||
} catch (err) {
|
||||
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)
|
||||
}
|
||||
} catch (err) {
|
||||
robot.log.error(`Couldn't fetch the github projects for repo: ${err}`, ownerName, repoName)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//
|
||||
// Dependencies:
|
||||
// github: "^13.1.0"
|
||||
// probot-config "^0.1.0"
|
||||
// probot-config: "^0.1.0"
|
||||
// probot-slack-status: "^0.2.2"
|
||||
//
|
||||
// Author:
|
||||
|
@ -59,12 +59,14 @@ async function greetNewContributor(context, robot) {
|
|||
if (userPullRequests.length === 1) {
|
||||
try {
|
||||
const welcomeMessage = config['welcome-bot'].message
|
||||
if (!process.env.DRY_RUN) {
|
||||
await github.issues.createComment({
|
||||
owner: ownerName,
|
||||
repo: repoName,
|
||||
number: prNumber,
|
||||
body: welcomeMessage
|
||||
})
|
||||
}
|
||||
|
||||
// Send message to Slack
|
||||
const slackHelper = require('../lib/slack')
|
||||
|
|
Loading…
Reference in New Issue