diff --git a/lib/github-comment.js b/lib/github-comment.js new file mode 100644 index 00000000..221bc195 --- /dev/null +++ b/lib/github-comment.js @@ -0,0 +1,16 @@ +'use strict' + +const githubClient = require('./github-client') + +exports.createPrComment = function createPrComment ({ owner, repo, number, logger }, body) { + githubClient.issues.createComment({ + owner, + repo, + number, + body + }, (err) => { + if (err) { + logger.error(err, 'Error while creating comment on GitHub') + } + }) +} diff --git a/lib/push-jenkins-update.js b/lib/push-jenkins-update.js index 3365ee79..25bd1965 100644 --- a/lib/push-jenkins-update.js +++ b/lib/push-jenkins-update.js @@ -3,6 +3,7 @@ const url = require('url') const githubClient = require('./github-client') +const { createPrComment } = require('./github-comment') function pushStarted (options, build, cb) { const pr = findPrInRef(build.ref) @@ -13,6 +14,10 @@ function pushStarted (options, build, cb) { const optsWithPr = Object.assign({ pr }, options) + if (build.identifier === 'node-test-pull-request' && build.status === 'pending') { + createPrComment(Object.assign({ number: pr }, options), `CI: ${build.url}`) + } + findLatestCommitInPr(optsWithPr, (err, latestCommit) => { if (err) { logger.error(err, 'Got error when retrieving GitHub commits for PR') diff --git a/scripts/trigger-jenkins-build.js b/scripts/trigger-jenkins-build.js index 81b150c2..dc201b02 100644 --- a/scripts/trigger-jenkins-build.js +++ b/scripts/trigger-jenkins-build.js @@ -4,6 +4,7 @@ const request = require('request') const githubClient = require('../lib/github-client') const botUsername = require('../lib/bot-username') +const { createPrComment } = require('../lib/github-comment') const jenkinsApiCredentials = process.env.JENKINS_API_CREDENTIALS || '' @@ -85,19 +86,6 @@ function triggerBuild (options, cb) { }) } -function createPrComment ({ owner, repo, number, logger }, body) { - githubClient.issues.createComment({ - owner, - repo, - number, - body - }, (err) => { - if (err) { - logger.error(err, 'Error while creating comment to reply on CI run comment') - } - }) -} - function triggerBuildIfValid (options) { const { owner, repo, author, logger } = options diff --git a/test/_fixtures/jenkins-test-pull-request-success-payload.json b/test/_fixtures/jenkins-test-pull-request-success-payload.json new file mode 100644 index 00000000..c0726ee6 --- /dev/null +++ b/test/_fixtures/jenkins-test-pull-request-success-payload.json @@ -0,0 +1,8 @@ +{ + "identifier": "node-test-pull-request", + "status": "pending", + "message": "running tests", + "commit": "8a5fec2a6bade91e544a30314d7cf21f8a200de1", + "url": "https://ci.nodejs.org/job/node-test-pull-request/21633/", + "ref": "refs/pull/12345/head" +} diff --git a/test/integration/push-jenkins-update.test.js b/test/integration/push-jenkins-update.test.js index 3188c1bc..7184d81a 100644 --- a/test/integration/push-jenkins-update.test.js +++ b/test/integration/push-jenkins-update.test.js @@ -102,6 +102,32 @@ tap.test('Forwards payload provided in incoming POST to GitHub status API', (t) }) }) +tap.test('Posts a CI comment in the related PR when Jenkins build is named node-test-pull-request', (t) => { + const fixture = readFixture('jenkins-test-pull-request-success-payload.json') + const commentScope = nock('https://api.github.com') + .filteringPath(ignoreQueryParams) + .post('/repos/nodejs/node/issues/12345/comments', { body: 'CI: https://ci.nodejs.org/job/node-test-pull-request/21633/' }) + .reply(200) + + // we don't care about asserting the scopes below, just want to stop the requests from actually being sent + setupGetCommitsMock('node') + nock('https://api.github.com') + .filteringPath(ignoreQueryParams) + .post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1') + .reply(201) + + t.plan(1) + + supertest(app) + .post('/node/jenkins/start') + .send(fixture) + .expect(201) + .end((err, res) => { + commentScope.done() + t.equal(err, null) + }) +}) + tap.test('Responds with 400 / "Bad request" when incoming request has invalid payload', (t) => { const fixture = readFixture('invalid-payload.json')