Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 18 additions & 31 deletions scripts/trigger-jenkins-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function ifBotWasMentionedInCiComment (commentBody, cb) {

// URL to the Jenkins job should be triggered for a given repository
function buildUrlForRepo (repo) {
// e.g. JENKINS_JOB_URL_CITGM = https://ci.nodejs.org/job/citgm-continuous-integration-pipeline
// e.g. JENKINS_JOB_URL_CITGM = https://ci.nodejs.org/blue/rest/organizations/jenkins/pipelines/node-test-pull-request-lite-pipeline/runs/
const jobUrl = process.env[`JENKINS_JOB_URL_${repo.toUpperCase()}`] || ''
return jobUrl ? `${jobUrl}/build` : ''
return jobUrl
}

// Authentication token configured per Jenkins job needed when triggering a build,
Expand All @@ -41,26 +41,12 @@ function buildParametersForRepo (options, repo) {
value: `refs/pull/${options.number}/head`
}]
} else {
return [{
name: 'CERTIFY_SAFE',
value: 'true'
},
{
name: 'TARGET_GITHUB_ORG',
value: 'nodejs'
},
{
name: 'TARGET_REPO_NAME',
value: 'node'
},
{
name: 'PR_ID',
value: options.number
},
{
name: 'REBASE_ONTO',
value: '<pr base branch>'
}
return [
{ name: 'CERTIFY_SAFE', value: 'true' },
{ name: 'GITHUB_ORG', value: 'nodejs' },
{ name: 'REPO_NAME', value: 'node' },
{ name: 'PR_ID', value: options.number },
{ name: 'REBASE_ONTO', value: '' }
]
}
}
Expand All @@ -70,7 +56,6 @@ function triggerBuild (options, cb) {
const base64Credentials = new Buffer(jenkinsApiCredentials).toString('base64')
const authorization = `Basic ${base64Credentials}`

const payload = JSON.stringify({ parameter: buildParametersForRepo(options, repo) })
const uri = buildUrlForRepo(repo)
const buildAuthToken = buildTokenForRepo(repo)

Expand All @@ -88,15 +73,15 @@ function triggerBuild (options, cb) {
uri,
headers: { authorization },
qs: { token: buildAuthToken },
form: { json: payload }
json: { parameters: buildParametersForRepo(options, repo) }
}, (err, response) => {
if (err) {
return cb(err)
} else if (response.statusCode !== 201) {
return cb(new Error(`Expected 201 from Jenkins, got ${response.statusCode}`))
} else if (response.statusCode !== 200) {
return cb(new Error(`Expected 200 from Jenkins, got ${response.statusCode}`))
}

cb(null, response.headers.location)
cb(null, response.body._links.self.href)
})
}

Expand Down Expand Up @@ -163,20 +148,22 @@ module.exports = (app) => {
logger
}

function logBuildStarted (err) {
function replyToCollabWithBuildStarted (err, buildUrl) {
if (err) {
logger.error(err, 'Error while triggering Jenkins build')
} else {
logger.info('Jenkins build started')
return createPrComment(options, `@${pullRequestAuthor} sadly an error occured when I tried to trigger a build :(`)
}

createPrComment(options, `@${pullRequestAuthor} build started: https://ci.nodejs.org${buildUrl}`)
logger.info({ buildUrl }, 'Jenkins build started')
}

function triggerBuildWhenCollaborator (err) {
if (err) {
return logger.debug(`Ignoring comment to me by @${pullRequestAuthor} because they are not a repo collaborator`)
}

triggerBuild(options, logBuildStarted)
triggerBuild(options, replyToCollabWithBuildStarted)
}

githubClient.repos.checkCollaborator({ owner, repo, username: pullRequestAuthor }, triggerBuildWhenCollaborator)
Expand Down
16 changes: 10 additions & 6 deletions test/integration/trigger-jenkins-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,28 @@ tap.test('Sends POST request to https://ci.nodejs.org', (t) => {

const originalJobUrlValue = process.env.JENKINS_JOB_URL_NODE
const originalTokenValue = process.env.JENKINS_BUILD_TOKEN_NODE
process.env.JENKINS_JOB_URL_NODE = 'https://ci.nodejs.org/job/node-test-pull-request'
process.env.JENKINS_JOB_URL_NODE = 'https://ci.nodejs.org/blue/rest/organizations/jenkins/pipelines/node-test-pull-request-lite-pipeline/runs/'
process.env.JENKINS_BUILD_TOKEN_NODE = 'myToken'

const webhookPayload = readFixture('pull-request-opened.json')
const pipelineUrl = '/blue/organizations/jenkins/node-test-pull-request-lite-pipeline/detail/node-test-pull-request-lite-pipeline/23/pipeline/'

const collaboratorsScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/collaborators/phillipj')
.reply(200, { permission: 'admin' })
const ciJobScope = nock('https://ci.nodejs.org')
.filteringPath(ignoreQueryParams)
.post('/job/node-test-pull-request/build')
.reply(201, '', {
'Location': 'https://ci.nodejs.org/job/node-test-pull-request/1'
})
.post('/blue/rest/organizations/jenkins/pipelines/node-test-pull-request-lite-pipeline/runs/')
.reply(200, { _links: { self: { href: pipelineUrl } } }, {})

const commentScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/issues/19/comments', { body: `@phillipj build started: https://ci.nodejs.org/${pipelineUrl}` })
.reply(200)

t.plan(1)
t.tearDown(() => collaboratorsScope.done() && ciJobScope.done() && clock.uninstall())
t.tearDown(() => collaboratorsScope.done() && ciJobScope.done() && commentScope.done() && clock.uninstall())

supertest(app)
.post('/hooks/github')
Expand Down