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
22 changes: 11 additions & 11 deletions scripts/trigger-jenkins-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ 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/blue/rest/organizations/jenkins/pipelines/node-test-pull-request-lite-pipeline/runs/
const jobUrl = process.env[`JENKINS_JOB_URL_${repo.toUpperCase()}`] || ''
return jobUrl
// Name for the Jenkins job should be triggered for a given repository
function getJobNameForRepo (repo) {
// e.g. JENKINS_JOB_CITGM = node-test-pull-request-lite-pipeline
return process.env[`JENKINS_JOB_${repo.toUpperCase()}`] || ''
}

// Authentication token configured per Jenkins job needed when triggering a build,
Expand Down Expand Up @@ -56,11 +55,11 @@ function triggerBuild (options, cb) {
const base64Credentials = new Buffer(jenkinsApiCredentials).toString('base64')
const authorization = `Basic ${base64Credentials}`

const uri = buildUrlForRepo(repo)
const jobName = getJobNameForRepo(repo)
const buildAuthToken = buildTokenForRepo(repo)

if (!uri) {
return cb(new TypeError(`Will not trigger Jenkins build because $JENKINS_JOB_URL_${repo.toUpperCase()} is not set`))
if (!jobName) {
return cb(new TypeError(`Will not trigger Jenkins build because $JENKINS_JOB_${repo.toUpperCase()} is not set`))
}

if (!buildAuthToken) {
Expand All @@ -70,7 +69,7 @@ function triggerBuild (options, cb) {
options.logger.debug('Triggering Jenkins build')

request.post({
uri,
uri: `https://ci.nodejs.org/blue/rest/organizations/jenkins/pipelines/${jobName}/runs/`,
headers: { authorization },
qs: { token: buildAuthToken },
json: { parameters: buildParametersForRepo(options, repo) }
Expand All @@ -81,7 +80,8 @@ function triggerBuild (options, cb) {
return cb(new Error(`Expected 200 from Jenkins, got ${response.statusCode}`))
}

cb(null, response.body._links.self.href)
cb(null,
`https://ci.nodejs.org/blue/organizations/jenkins/${jobName}/detail/${jobName}/${response.body.id}/pipeline`)
})
}

Expand Down Expand Up @@ -154,7 +154,7 @@ module.exports = (app) => {
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}`)
createPrComment(options, `@${pullRequestAuthor} build started: ${buildUrl}`)
logger.info({ buildUrl }, 'Jenkins build started')
}

Expand Down
8 changes: 4 additions & 4 deletions test/integration/trigger-jenkins-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ 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/blue/rest/organizations/jenkins/pipelines/node-test-pull-request-lite-pipeline/runs/'
process.env.JENKINS_JOB_NODE = 'node-test-pull-request-lite-pipeline'
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 pipelineUrl = 'https://ci.nodejs.org/blue/organizations/jenkins/node-test-pull-request-lite-pipeline/detail/node-test-pull-request-lite-pipeline/1/pipeline'

const collaboratorsScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
Expand All @@ -36,11 +36,11 @@ tap.test('Sends POST request to https://ci.nodejs.org', (t) => {
const ciJobScope = nock('https://ci.nodejs.org')
.filteringPath(ignoreQueryParams)
.post('/blue/rest/organizations/jenkins/pipelines/node-test-pull-request-lite-pipeline/runs/')
.reply(200, { _links: { self: { href: pipelineUrl } } }, {})
.reply(200, { id: 1 }, {})

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}` })
.post('/repos/nodejs/node/issues/19/comments', { body: `@phillipj build started: ${pipelineUrl}` })
.reply(200)

t.plan(1)
Expand Down