Skip to content

Commit 64c2b20

Browse files
authored
jenkins: start CI run on new pull requests
This will start a CI build on all new pull requests in nodejs/node, that are opened by project collaborators PR-URL: #173 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent bc73d7e commit 64c2b20

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

scripts/trigger-jenkins-build.js

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,39 @@ function buildTokenForRepo (repo) {
3434
return process.env[`JENKINS_BUILD_TOKEN_${repo.toUpperCase()}`] || ''
3535
}
3636

37+
function buildParametersForRepo (options, repo) {
38+
if (repo === 'citgm') {
39+
return [{
40+
name: 'GIT_REMOTE_REF',
41+
value: `refs/pull/${options.number}/head`
42+
}]
43+
} else {
44+
return [{
45+
name: 'CERTIFY_SAFE',
46+
value: 'true'
47+
},
48+
{
49+
name: 'TARGET_GITHUB_ORG',
50+
value: 'nodejs'
51+
},
52+
{
53+
name: 'TARGET_REPO_NAME',
54+
value: 'node'
55+
},
56+
{
57+
name: 'PR_ID',
58+
value: options.number
59+
}
60+
]
61+
}
62+
}
63+
3764
function triggerBuild (options, cb) {
3865
const { repo } = options
3966
const base64Credentials = new Buffer(jenkinsApiCredentials).toString('base64')
4067
const authorization = `Basic ${base64Credentials}`
41-
const buildParameters = [{
42-
name: 'GIT_REMOTE_REF',
43-
value: `refs/pull/${options.number}/head`
44-
}]
45-
const payload = JSON.stringify({ parameter: buildParameters })
68+
69+
const payload = JSON.stringify({ parameter: buildParametersForRepo(options, repo) })
4670
const uri = buildUrlForRepo(repo)
4771
const buildAuthToken = buildTokenForRepo(repo)
4872

@@ -124,4 +148,33 @@ module.exports = (app) => {
124148
githubClient.repos.checkCollaborator({ owner, repo, username: commentAuthor }, triggerBuildWhenCollaborator)
125149
})
126150
})
151+
152+
app.on('pull_request.opened', function handlePullCreated (event, owner, repo) {
153+
const { number, logger, pull_request } = event
154+
const pullRequestAuthor = pull_request.user.login
155+
const options = {
156+
owner,
157+
repo,
158+
number,
159+
logger
160+
}
161+
162+
function logBuildStarted (err) {
163+
if (err) {
164+
logger.error(err, 'Error while triggering Jenkins build')
165+
} else {
166+
logger.info('Jenkins build started')
167+
}
168+
}
169+
170+
function triggerBuildWhenCollaborator (err) {
171+
if (err) {
172+
return logger.debug(`Ignoring comment to me by @${pullRequestAuthor} because they are not a repo collaborator`)
173+
}
174+
175+
triggerBuild(options, replyToCollabWithBuildStarted)
176+
}
177+
178+
githubClient.repos.checkCollaborator({ owner, repo, username: pullRequestAuthor }, triggerBuildWhenCollaborator)
179+
})
127180
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict'
2+
3+
const tap = require('tap')
4+
const url = require('url')
5+
const nock = require('nock')
6+
const supertest = require('supertest')
7+
const proxyquire = require('proxyquire')
8+
const lolex = require('lolex')
9+
const readFixture = require('../read-fixture')
10+
11+
const app = proxyquire('../../app', {
12+
'./github-secret': {
13+
isValid: () => true,
14+
15+
// necessary to make makes proxyquire return this stub
16+
// whenever *any* module tries to require('./github-secret')
17+
'@global': true
18+
}
19+
})
20+
21+
tap.test('Sends POST request to https://ci.nodejs.org', (t) => {
22+
const clock = lolex.install()
23+
24+
const originalJobUrlValue = process.env.JENKINS_JOB_URL_NODE
25+
const originalTokenValue = process.env.JENKINS_BUILD_TOKEN_NODE
26+
process.env.JENKINS_JOB_URL_NODE = 'https://ci.nodejs.org/job/node-test-pull-request'
27+
process.env.JENKINS_BUILD_TOKEN_NODE = 'myToken'
28+
29+
const webhookPayload = readFixture('pull-request-opened.json')
30+
31+
const collaboratorsScope = nock('https://api.github.com')
32+
.filteringPath(ignoreQueryParams)
33+
.get('/repos/nodejs/node/collaborators/phillipj')
34+
.reply(200, { permission: 'admin' })
35+
const ciJobScope = nock('https://ci.nodejs.org')
36+
.filteringPath(ignoreQueryParams)
37+
.post('/job/node-test-pull-request/build')
38+
.reply(201, '', {
39+
'Location': 'https://ci.nodejs.org/job/node-test-pull-request/1'
40+
})
41+
42+
t.plan(1)
43+
t.tearDown(() => collaboratorsScope.done() && ciJobScope.done() && clock.uninstall())
44+
45+
supertest(app)
46+
.post('/hooks/github')
47+
.set('x-github-event', 'pull_request')
48+
.send(webhookPayload)
49+
.expect(200)
50+
.end((err, res) => {
51+
process.env.JENKINS_JOB_URL_NODE = originalJobUrlValue
52+
process.env.JENKINS_BUILD_TOKEN_NODE = originalTokenValue
53+
clock.runAll()
54+
t.equal(err, null)
55+
})
56+
})
57+
58+
function ignoreQueryParams (pathAndQuery) {
59+
return url.parse(pathAndQuery, true).pathname
60+
}

0 commit comments

Comments
 (0)