diff --git a/scripts/trigger-jenkins-build.js b/scripts/trigger-jenkins-build.js index 2c133beb..522125bf 100644 --- a/scripts/trigger-jenkins-build.js +++ b/scripts/trigger-jenkins-build.js @@ -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, @@ -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) { @@ -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) } @@ -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`) }) } @@ -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') } diff --git a/test/integration/trigger-jenkins-build.test.js b/test/integration/trigger-jenkins-build.test.js index abd7d109..b4051198 100644 --- a/test/integration/trigger-jenkins-build.test.js +++ b/test/integration/trigger-jenkins-build.test.js @@ -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) @@ -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)