Skip to content

Commit 2a95d13

Browse files
committed
jenkins: post CI comment in PR for node-test-pull-request builds
These changes posts CI style comments whenever Jenkins reports that node-test-pull-request builds has started. Refs #212
1 parent afc6d4b commit 2a95d13

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

lib/github-comment.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const githubClient = require('./github-client')
4+
5+
exports.createPrComment = function createPrComment ({ owner, repo, number, logger }, body) {
6+
githubClient.issues.createComment({
7+
owner,
8+
repo,
9+
number,
10+
body
11+
}, (err) => {
12+
if (err) {
13+
logger.error(err, 'Error while creating comment on GitHub')
14+
}
15+
})
16+
}

lib/push-jenkins-update.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const url = require('url')
44

55
const githubClient = require('./github-client')
6+
const { createPrComment } = require('./github-comment')
67

78
function pushStarted (options, build, cb) {
89
const pr = findPrInRef(build.ref)
@@ -13,6 +14,10 @@ function pushStarted (options, build, cb) {
1314

1415
const optsWithPr = Object.assign({ pr }, options)
1516

17+
if (build.identifier === 'node-test-pull-request') {
18+
createPrComment(Object.assign({ number: pr }, options), `CI: ${build.url}`)
19+
}
20+
1621
findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
1722
if (err) {
1823
logger.error(err, 'Got error when retrieving GitHub commits for PR')

scripts/trigger-jenkins-build.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const request = require('request')
44

55
const githubClient = require('../lib/github-client')
66
const botUsername = require('../lib/bot-username')
7+
const { createPrComment } = require('../lib/github-comment')
78

89
const jenkinsApiCredentials = process.env.JENKINS_API_CREDENTIALS || ''
910

@@ -85,19 +86,6 @@ function triggerBuild (options, cb) {
8586
})
8687
}
8788

88-
function createPrComment ({ owner, repo, number, logger }, body) {
89-
githubClient.issues.createComment({
90-
owner,
91-
repo,
92-
number,
93-
body
94-
}, (err) => {
95-
if (err) {
96-
logger.error(err, 'Error while creating comment to reply on CI run comment')
97-
}
98-
})
99-
}
100-
10189
function triggerBuildIfValid (options) {
10290
const { owner, repo, author, logger } = options
10391

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"identifier": "node-test-pull-request",
3+
"status": "success",
4+
"message": "tests passed",
5+
"commit": "8a5fec2a6bade91e544a30314d7cf21f8a200de1",
6+
"url": "https://ci.nodejs.org/job/node-test-pull-request/21633/",
7+
"ref": "refs/pull/12345/head"
8+
}

test/integration/push-jenkins-update.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ tap.test('Forwards payload provided in incoming POST to GitHub status API', (t)
110110
})
111111
})
112112

113+
tap.test('Posts a CI comment in the related PR when Jenkins build is named node-test-pull-request', (t) => {
114+
const fixture = readFixture('jenkins-test-pull-request-success-payload.json')
115+
const commentScope = nock('https://api.github.com')
116+
.filteringPath(ignoreQueryParams)
117+
.post('/repos/nodejs/node/issues/12345/comments', { body: 'CI: https://ci.nodejs.org/job/node-test-pull-request/21633/' })
118+
.reply(200)
119+
120+
// we don't care about asserting the scopes below, just want to stop the requests from actually being sent
121+
setupGetCommitsMock('node')
122+
nock('https://api.github.com')
123+
.filteringPath(ignoreQueryParams)
124+
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1')
125+
.reply(201)
126+
127+
t.plan(1)
128+
t.tearDown(() => commentScope.done())
129+
130+
supertest(app)
131+
.post('/node/jenkins/start')
132+
.send(fixture)
133+
.expect(201)
134+
.end((err, res) => {
135+
t.equal(err, null)
136+
})
137+
})
138+
113139
tap.test('Responds with 400 / "Bad request" when incoming request has invalid payload', (t) => {
114140
const fixture = readFixture('invalid-payload.json')
115141

0 commit comments

Comments
 (0)