2018-02-07 16:59:55 +00:00
|
|
|
// Description:
|
|
|
|
// Script that listens for PRs moving into the 'TO TEST' column
|
|
|
|
// and triggers a Jenkins build.
|
|
|
|
//
|
|
|
|
// Dependencies:
|
|
|
|
// github: "^13.1.0"
|
|
|
|
// jenkins: "^0.20.1"
|
|
|
|
// probot-config: "^0.1.0"
|
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// PombeirP
|
|
|
|
|
2018-02-08 12:09:59 +00:00
|
|
|
const getConfig = require('probot-config')
|
2018-02-07 16:59:55 +00:00
|
|
|
const jenkins = require('jenkins')({ baseUrl: process.env.JENKINS_URL, crumbIssuer: true, promisify: true })
|
|
|
|
const HashMap = require('hashmap')
|
|
|
|
|
2018-02-08 12:09:59 +00:00
|
|
|
const defaultConfig = require('../lib/config')
|
|
|
|
const gitHubHelpers = require('../lib/github-helpers')
|
|
|
|
|
2018-02-13 14:51:00 +00:00
|
|
|
const botName = 'trigger-automation-test-build'
|
2018-02-07 16:59:55 +00:00
|
|
|
const pendingPullRequests = new HashMap()
|
|
|
|
|
|
|
|
module.exports = (robot) => {
|
|
|
|
if (!process.env.JENKINS_URL) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.info(`${botName} - Jenkins is not configured, not loading script`)
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-08 12:09:59 +00:00
|
|
|
setInterval(checkPendingPullRequests, 5 * 1000 * 60, robot)
|
|
|
|
registerForRelevantCardEvents(robot)
|
2018-02-07 16:59:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 12:09:59 +00:00
|
|
|
function registerForRelevantCardEvents (robot) {
|
2018-02-09 21:50:23 +00:00
|
|
|
robot.on(['project_card.created', 'project_card.moved'], context => processChangedProjectCard(robot, context))
|
2018-02-07 16:59:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 12:09:59 +00:00
|
|
|
async function processChangedProjectCard (robot, context) {
|
2018-02-09 21:50:23 +00:00
|
|
|
const { github, payload } = context
|
|
|
|
const repo = payload.repository
|
|
|
|
if (!repo) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.debug(`${botName} - Repository info is not present in payload, ignoring`)
|
2018-02-09 21:50:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-08 12:09:59 +00:00
|
|
|
const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
|
|
|
|
const projectBoardConfig = config ? config['project-board'] : null
|
|
|
|
const automatedTestsConfig = config ? config['automated-tests'] : null
|
|
|
|
if (!projectBoardConfig || !automatedTestsConfig) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-07 16:59:55 +00:00
|
|
|
if (payload.project_card.note) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.trace(`${botName} - Card is a note, ignoring`)
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const projectBoardName = projectBoardConfig['name']
|
|
|
|
const testColumnName = projectBoardConfig['test-column-name']
|
|
|
|
|
2018-02-08 12:09:59 +00:00
|
|
|
if (repo.full_name !== automatedTestsConfig['repo-full-name']) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.trace(`${botName} - Pull request project doesn't match watched repo, exiting`, repo.full_name, automatedTestsConfig['repo-full-name'])
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let inTestColumn
|
|
|
|
try {
|
|
|
|
const columnPayload = await github.projects.getProjectColumn({ id: payload.project_card.column_id })
|
|
|
|
|
|
|
|
if (columnPayload.data.name !== testColumnName) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.trace(`${botName} - Card column name doesn't match watched column name, exiting`, columnPayload.data.name, testColumnName)
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inTestColumn = columnPayload.data
|
|
|
|
} catch (error) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.warn(`${botName} - Error while fetching project column`, payload.project_card.column_id, error)
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const last = (a, index) => {
|
|
|
|
return a[a.length + index]
|
|
|
|
}
|
|
|
|
|
|
|
|
let project
|
|
|
|
try {
|
|
|
|
const projectId = last(inTestColumn.project_url.split('/'), -1)
|
|
|
|
const projectPayload = await github.projects.getProject({ id: projectId })
|
|
|
|
|
|
|
|
project = projectPayload.data
|
|
|
|
if (project.name !== projectBoardName) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.trace(`${botName} - Card column name doesn't match watched column name, exiting`, project.name, projectBoardName)
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.warn(`${botName} - Error while fetching project column`, payload.project_card.column_id, error)
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const prNumber = last(payload.project_card.content_url.split('/'), -1)
|
2018-02-08 12:09:59 +00:00
|
|
|
const fullJobName = automatedTestsConfig['job-full-name']
|
2018-02-07 16:59:55 +00:00
|
|
|
|
|
|
|
await processPullRequest(github, robot, repo.owner.login, repo.name, prNumber, fullJobName)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processPullRequest (github, robot, repoOwner, repoName, prNumber, fullJobName) {
|
|
|
|
// Remove the PR from the pending PR list, if it is there
|
|
|
|
pendingPullRequests.delete(prNumber)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const state = await gitHubHelpers.getReviewApprovalState(github, robot, repoOwner, repoName, prNumber)
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 'unstable':
|
|
|
|
case 'awaiting_reviewers':
|
|
|
|
case 'changes_requested':
|
|
|
|
pendingPullRequests.set(prNumber, { github: github, repoOwner: repoOwner, repoName: repoName, fullJobName: fullJobName })
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.debug(`${botName} - State is '${state}', adding to backlog to check periodically`, prNumber)
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
case 'failed':
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.debug(`${botName} - State is '${state}', exiting`, prNumber)
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
case 'approved':
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.debug(`${botName} - State is '${state}', proceeding`, prNumber)
|
2018-02-07 16:59:55 +00:00
|
|
|
break
|
|
|
|
default:
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.warn(`${botName} - State is '${state}', ignoring`, prNumber)
|
2018-02-07 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
robot.log.error(`Couldn't calculate the PR approval state: ${err}`, repoOwner, repoName, prNumber)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const args = { parameters: { pr_id: prNumber, apk: `--apk=${prNumber}.apk` } }
|
|
|
|
|
|
|
|
if (process.env.DRY_RUN) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log(`${botName} - Would start ${fullJobName} job in Jenkins`, prNumber, args)
|
2018-02-07 16:59:55 +00:00
|
|
|
} else {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log(`${botName} - Starting ${fullJobName} job in Jenkins`, prNumber, args)
|
2018-02-07 16:59:55 +00:00
|
|
|
const buildId = await jenkins.job.build(fullJobName, args)
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log(`${botName} - Started job in Jenkins`, prNumber, buildId)
|
2018-02-07 16:59:55 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.error(`${botName} - Error while triggering Jenkins build. Will retry later`, prNumber, error)
|
2018-02-07 16:59:55 +00:00
|
|
|
|
|
|
|
pendingPullRequests.set(prNumber, { github: github, repoOwner: repoOwner, repoName: repoName, fullJobName: fullJobName })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkPendingPullRequests (robot) {
|
|
|
|
const _pendingPullRequests = pendingPullRequests.clone()
|
|
|
|
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.trace(`${botName} - Processing ${_pendingPullRequests.size} pending PRs`)
|
2018-02-07 16:59:55 +00:00
|
|
|
|
|
|
|
for (const kvp of _pendingPullRequests.entries()) {
|
|
|
|
const prNumber = kvp[0]
|
|
|
|
const { github, repoOwner, repoName, fullJobName } = kvp[1]
|
|
|
|
|
|
|
|
await processPullRequest(github, robot, repoOwner, repoName, prNumber, fullJobName)
|
|
|
|
}
|
|
|
|
|
2018-02-13 14:51:00 +00:00
|
|
|
robot.log.trace(`${botName} - Finished processing ${_pendingPullRequests.size} pending PRs`)
|
2018-02-07 16:59:55 +00:00
|
|
|
}
|