|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const request = require('request') |
| 4 | + |
| 5 | +const githubClient = require('../lib/github-client') |
| 6 | +const botUsername = require('../lib/bot-username') |
| 7 | + |
| 8 | +const jenkinsApiCredentials = process.env.JENKINS_API_CREDENTIALS || '' |
| 9 | + |
| 10 | +function ifBotWasMentionedInCiComment (commentBody, cb) { |
| 11 | + botUsername.resolve((err, username) => { |
| 12 | + if (err) { |
| 13 | + return cb(err) |
| 14 | + } |
| 15 | + |
| 16 | + const atBotName = new RegExp(`^@${username} run CI`, 'mi') |
| 17 | + const wasMentioned = commentBody.match(atBotName) !== null |
| 18 | + |
| 19 | + cb(null, wasMentioned) |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +// URL to the Jenkins job should be triggered for a given repository |
| 24 | +function buildUrlForRepo (repo) { |
| 25 | + // e.g. JENKINS_JOB_URL_CITGM = https://ci.nodejs.org/job/citgm-continuous-integration-pipeline |
| 26 | + const jobUrl = process.env[`JENKINS_JOB_URL_${repo.toUpperCase()}`] || '' |
| 27 | + return jobUrl ? `${jobUrl}/build` : '' |
| 28 | +} |
| 29 | + |
| 30 | +// Authentication token configured per Jenkins job needed when triggering a build, |
| 31 | +// this is set per job in Configure -> Build Triggers -> Trigger builds remotely |
| 32 | +function buildTokenForRepo (repo) { |
| 33 | + // e.g. JENKINS_BUILD_TOKEN_CITGM |
| 34 | + return process.env[`JENKINS_BUILD_TOKEN_${repo.toUpperCase()}`] || '' |
| 35 | +} |
| 36 | + |
| 37 | +function triggerBuild (options, cb) { |
| 38 | + const { repo } = options |
| 39 | + const base64Credentials = new Buffer(jenkinsApiCredentials).toString('base64') |
| 40 | + const authorization = `Basic ${base64Credentials}` |
| 41 | + const buildParameters = [{ |
| 42 | + name: 'GIT_REMOTE_REF', |
| 43 | + value: `refs/pull/${options.number}/head` |
| 44 | + }] |
| 45 | + const payload = JSON.stringify({ parameter: buildParameters }) |
| 46 | + const uri = buildUrlForRepo(repo) |
| 47 | + const buildAuthToken = buildTokenForRepo(repo) |
| 48 | + |
| 49 | + if (!uri) { |
| 50 | + return cb(new TypeError(`Will not trigger Jenkins build because $JENKINS_JOB_URL_${repo.toUpperCase()} is not set`)) |
| 51 | + } |
| 52 | + |
| 53 | + if (!buildAuthToken) { |
| 54 | + return cb(new TypeError(`Will not trigger Jenkins build because $JENKINS_BUILD_TOKEN_${repo.toUpperCase()} is not set`)) |
| 55 | + } |
| 56 | + |
| 57 | + options.logger.debug('Triggering Jenkins build') |
| 58 | + |
| 59 | + request.post({ |
| 60 | + uri, |
| 61 | + headers: { authorization }, |
| 62 | + qs: { token: buildAuthToken }, |
| 63 | + form: { json: payload } |
| 64 | + }, (err, response) => { |
| 65 | + if (err) { |
| 66 | + return cb(err) |
| 67 | + } else if (response.statusCode !== 201) { |
| 68 | + return cb(new Error(`Expected 201 from Jenkins, got ${response.statusCode}`)) |
| 69 | + } |
| 70 | + |
| 71 | + cb(null, response.headers.location) |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +function createPrComment ({ owner, repo, number, logger }, body) { |
| 76 | + githubClient.issues.createComment({ |
| 77 | + owner, |
| 78 | + repo, |
| 79 | + number, |
| 80 | + body |
| 81 | + }, (err) => { |
| 82 | + if (err) { |
| 83 | + logger.error(err, 'Error while creating comment to reply on CI run comment') |
| 84 | + } |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +module.exports = (app) => { |
| 89 | + app.on('issue_comment.created', function handleCommentCreated (event, owner, repo) { |
| 90 | + const { number, logger, comment } = event |
| 91 | + const commentAuthor = comment.user.login |
| 92 | + const options = { |
| 93 | + owner, |
| 94 | + repo, |
| 95 | + number, |
| 96 | + logger |
| 97 | + } |
| 98 | + |
| 99 | + function replyToCollabWithBuildStarted (err, buildUrl) { |
| 100 | + if (err) { |
| 101 | + logger.error(err, 'Error while triggering Jenkins build') |
| 102 | + return createPrComment(options, `@${commentAuthor} sadly an error occured when I tried to trigger a build :(`) |
| 103 | + } |
| 104 | + |
| 105 | + createPrComment(options, `@${commentAuthor} build started: ${buildUrl}`) |
| 106 | + logger.info({ buildUrl }, 'Jenkins build started') |
| 107 | + } |
| 108 | + |
| 109 | + function triggerBuildWhenCollaborator (err) { |
| 110 | + if (err) { |
| 111 | + return logger.debug(`Ignoring comment to me by @${commentAuthor} because they are not a repo collaborator`) |
| 112 | + } |
| 113 | + |
| 114 | + triggerBuild(options, replyToCollabWithBuildStarted) |
| 115 | + } |
| 116 | + |
| 117 | + ifBotWasMentionedInCiComment(comment.body, (err, wasMentioned) => { |
| 118 | + if (err) { |
| 119 | + return logger.error(err, 'Error while checking if the bot username was mentioned in a comment') |
| 120 | + } |
| 121 | + |
| 122 | + if (!wasMentioned) return |
| 123 | + |
| 124 | + githubClient.repos.checkCollaborator({ owner, repo, username: commentAuthor }, triggerBuildWhenCollaborator) |
| 125 | + }) |
| 126 | + }) |
| 127 | +} |
0 commit comments