Skip to content

Commit b658554

Browse files
committed
jenkins: finding commit SHA and new endpoints
This introduces the bot finding the correct commit SHAs to use for core PRs, rather than trusting Jenkins to provide SHAs, mainly due to rebasing we often do when running PR jobs. Also refactored the endpoint Jenkins pushes updates from one single endpoint to /node/jenkins/{start,end}
1 parent fc935f0 commit b658554

8 files changed

Lines changed: 196 additions & 24 deletions

‎lib/push-jenkins-update.js‎

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,81 @@
22

33
const githubClient = require('./github-client')
44

5-
function push (options, build) {
5+
function pushStarted (options, build) {
6+
const pr = findPrInRef(build.ref)
7+
68
// create unique logger which is easily traceable for every subsequent log statement
7-
const traceFields = { commit: build.commit, job: build.identifier, status: build.status }
9+
const traceFields = { pr, job: build.identifier, status: build.status }
810
const logger = options.logger.child(traceFields, true)
911

10-
const statusOpts = Object.assign({
11-
sha: build.commit,
12-
url: build.url,
13-
context: build.identifier,
14-
state: build.status,
15-
message: build.message
16-
}, options)
12+
const optsWithPr = Object.assign({ pr }, options)
13+
14+
findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
15+
if (err) {
16+
return logger.error(err, 'Got error when retrieving GitHub commits for PR')
17+
}
18+
19+
const statusOpts = Object.assign({
20+
sha: latestCommit.sha,
21+
url: build.url,
22+
context: build.identifier,
23+
state: build.status,
24+
message: build.message || 'running tests'
25+
}, options)
26+
27+
createGhStatus(statusOpts, logger)
28+
})
29+
}
30+
31+
function pushEnded (options, build) {
32+
const pr = findPrInRef(build.ref)
33+
34+
// create unique logger which is easily traceable for every subsequent log statement
35+
const traceFields = { pr, job: build.identifier, status: build.status }
36+
const logger = options.logger.child(traceFields, true)
37+
38+
const optsWithPr = Object.assign({ pr }, options)
39+
40+
findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
41+
if (err) {
42+
return logger.error(err, 'Got error when retrieving GitHub commits for PR')
43+
}
44+
45+
const statusOpts = Object.assign({
46+
sha: latestCommit.sha,
47+
url: build.url,
48+
context: build.identifier,
49+
state: build.status,
50+
message: build.message || 'all tests passed'
51+
}, options)
52+
53+
createGhStatus(statusOpts, logger)
54+
})
55+
}
56+
57+
function findPrInRef (gitRef) {
58+
// refs/pull/12345/head
59+
return gitRef.split('/')[2]
60+
}
1761

18-
createGhStatus(statusOpts, logger)
62+
function findLatestCommitInPr (options, cb) {
63+
githubClient.pullRequests.getCommits({
64+
user: options.owner,
65+
repo: options.repo,
66+
number: options.pr
67+
}, (err, commitMetas) => {
68+
if (err) {
69+
return cb(err)
70+
}
71+
72+
const lastCommitMeta = commitMetas.pop()
73+
const lastCommit = {
74+
sha: lastCommitMeta.sha,
75+
date: lastCommitMeta.commit.committer.date
76+
}
77+
78+
cb(null, lastCommit)
79+
})
1980
}
2081

2182
function createGhStatus (options, logger) {
@@ -41,4 +102,5 @@ function validate (payload) {
41102
}
42103

43104
exports.validate = validate
44-
exports.push = push
105+
exports.pushStarted = pushStarted
106+
exports.pushEnded = pushEnded

‎scripts/node-jenkins-status.js‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,30 @@
33
const pushJenkinsUpdate = require('../lib/push-jenkins-update')
44

55
module.exports = function (app) {
6-
app.post('/node/jenkins', (req, res) => {
6+
app.post('/node/jenkins/start', (req, res) => {
77
const isValid = pushJenkinsUpdate.validate(req.body)
88

99
if (!isValid) {
1010
return res.status(400).end('Invalid payload')
1111
}
1212

13-
pushJenkinsUpdate.push({
13+
pushJenkinsUpdate.pushStarted({
14+
owner: 'nodejs',
15+
repo: 'node',
16+
logger: req.log
17+
}, req.body)
18+
19+
res.status(201).end()
20+
})
21+
22+
app.post('/node/jenkins/end', (req, res) => {
23+
const isValid = pushJenkinsUpdate.validate(req.body)
24+
25+
if (!isValid) {
26+
return res.status(400).end('Invalid payload')
27+
}
28+
29+
pushJenkinsUpdate.pushEnded({
1430
owner: 'nodejs',
1531
repo: 'node',
1632
logger: req.log

‎test/_fixtures/error-payload.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"status": "failure",
44
"message": "tests failed",
55
"commit": "8a5fec2a6bade91e544a30314d7cf21f8a200de1",
6-
"url": "https://ci.nodejs.org/job/node-test-commit-arm/3087/"
6+
"url": "https://ci.nodejs.org/job/node-test-commit-arm/3087/",
7+
"ref": "refs/pull/12345/head"
78
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"identifier": "test/osx",
33
"status": "pending",
4-
"url": "https://ci.nodejs.org/job/node-test-commit-osx/3157/"
4+
"url": "https://ci.nodejs.org/job/node-test-commit-osx/3157/",
5+
"ref": "refs/pull/12345/head"
56
}

‎test/_fixtures/pending-payload.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"status": "pending",
44
"message": "checking for errors",
55
"commit": "8a5fec2a6bade91e544a30314d7cf21f8a200de1",
6-
"url": "https://ci.nodejs.org/job/node-test-commit-linux/3176/"
6+
"url": "https://ci.nodejs.org/job/node-test-commit-linux/3176/",
7+
"ref": "refs/pull/12345/head"
78
}

‎test/_fixtures/pr-commits.json‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[
2+
{
3+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/8a5fec2a6bade91e544a30314d7cf21f8a200de1",
4+
"sha": "8a5fec2a6bade91e544a30314d7cf21f8a200de1",
5+
"html_url": "https://github.com/octocat/Hello-World/commit/8a5fec2a6bade91e544a30314d7cf21f8a200de1",
6+
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/8a5fec2a6bade91e544a30314d7cf21f8a200de1/comments",
7+
"commit": {
8+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/8a5fec2a6bade91e544a30314d7cf21f8a200de1",
9+
"author": {
10+
"name": "Monalisa Octocat",
11+
"email": "support@github.com",
12+
"date": "2011-04-14T16:00:49Z"
13+
},
14+
"committer": {
15+
"name": "Monalisa Octocat",
16+
"email": "support@github.com",
17+
"date": "2011-04-14T16:00:49Z"
18+
},
19+
"message": "Fix all the bugs",
20+
"tree": {
21+
"url": "https://api.github.com/repos/octocat/Hello-World/tree/8a5fec2a6bade91e544a30314d7cf21f8a200de1",
22+
"sha": "8a5fec2a6bade91e544a30314d7cf21f8a200de1"
23+
},
24+
"comment_count": 0,
25+
"verification": {
26+
"verified": true,
27+
"reason": "valid",
28+
"signature": "-----BEGIN PGP MESSAGE-----\n...\n-----END PGP MESSAGE-----",
29+
"payload": "tree 8a5fec2a6bade91e544a30314d7cf21f8a200de1\n..."
30+
}
31+
},
32+
"author": {
33+
"login": "octocat",
34+
"id": 1,
35+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
36+
"gravatar_id": "",
37+
"url": "https://api.github.com/users/octocat",
38+
"html_url": "https://github.com/octocat",
39+
"followers_url": "https://api.github.com/users/octocat/followers",
40+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
41+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
42+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
43+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
44+
"organizations_url": "https://api.github.com/users/octocat/orgs",
45+
"repos_url": "https://api.github.com/users/octocat/repos",
46+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
47+
"received_events_url": "https://api.github.com/users/octocat/received_events",
48+
"type": "User",
49+
"site_admin": false
50+
},
51+
"committer": {
52+
"login": "octocat",
53+
"id": 1,
54+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
55+
"gravatar_id": "",
56+
"url": "https://api.github.com/users/octocat",
57+
"html_url": "https://github.com/octocat",
58+
"followers_url": "https://api.github.com/users/octocat/followers",
59+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
60+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
61+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
62+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
63+
"organizations_url": "https://api.github.com/users/octocat/orgs",
64+
"repos_url": "https://api.github.com/users/octocat/repos",
65+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
66+
"received_events_url": "https://api.github.com/users/octocat/received_events",
67+
"type": "User",
68+
"site_admin": false
69+
},
70+
"parents": [
71+
{
72+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/8a5fec2a6bade91e544a30314d7cf21f8a200de1",
73+
"sha": "8a5fec2a6bade91e544a30314d7cf21f8a200de1"
74+
}
75+
]
76+
}
77+
]

‎test/_fixtures/success-payload.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"status": "success",
44
"message": "tests passed",
55
"commit": "8a5fec2a6bade91e544a30314d7cf21f8a200de1",
6-
"url": "https://ci.nodejs.org/job/node-test-commit-osx/3157/"
6+
"url": "https://ci.nodejs.org/job/node-test-commit-osx/3157/",
7+
"ref": "refs/pull/12345/head"
78
}

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ const supertest = require('supertest')
1010
const app = require('../app')
1111

1212
tap.test('Sends POST requests to https://api.github.com/repos/nodejs/node/statuses/<SHA>', (t) => {
13-
const fixture = readFixture('success-payload.json')
13+
const jenkinsPayload = readFixture('success-payload.json')
14+
15+
const prCommitsScope = setupGetCommitsMock()
1416
const scope = nock('https://api.github.com')
1517
.filteringPath(ignoreQueryParams)
1618
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1')
1719
.reply(201)
1820

1921
t.plan(1)
20-
t.tearDown(() => scope.done())
22+
t.tearDown(() => prCommitsScope.done() && scope.done())
2123

2224
supertest(app)
23-
.post('/node/jenkins')
24-
.send(fixture)
25+
.post('/node/jenkins/start')
26+
.send(jenkinsPayload)
2527
.expect(201)
2628
.end((err, res) => {
2729
t.equal(err, null)
@@ -30,6 +32,8 @@ tap.test('Sends POST requests to https://api.github.com/repos/nodejs/node/status
3032

3133
tap.test('Forwards payload provided in incoming POST to GitHub status API', (t) => {
3234
const fixture = readFixture('success-payload.json')
35+
36+
const prCommitsScope = setupGetCommitsMock()
3337
const scope = nock('https://api.github.com')
3438
.filteringPath(ignoreQueryParams)
3539
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1', {
@@ -41,10 +45,10 @@ tap.test('Forwards payload provided in incoming POST to GitHub status API', (t)
4145
.reply(201)
4246

4347
t.plan(1)
44-
t.tearDown(() => scope.done())
48+
t.tearDown(() => prCommitsScope.done() && scope.done())
4549

4650
supertest(app)
47-
.post('/node/jenkins')
51+
.post('/node/jenkins/start')
4852
.send(fixture)
4953
.expect(201)
5054
.end((err, res) => {
@@ -61,14 +65,23 @@ tap.test('Responds with 400 / "Bad request" when incoming request has invalid pa
6165
t.plan(1)
6266

6367
supertest(app)
64-
.post('/node/jenkins')
68+
.post('/node/jenkins/start')
6569
.send(fixture)
6670
.expect(400, 'Invalid payload')
6771
.end((err, res) => {
6872
t.equal(err, null)
6973
})
7074
})
7175

76+
function setupGetCommitsMock () {
77+
const commitsResponse = readFixture('pr-commits.json')
78+
79+
return nock('https://api.github.com')
80+
.filteringPath(ignoreQueryParams)
81+
.get('/repos/nodejs/node/pulls/12345/commits')
82+
.reply(200, commitsResponse)
83+
}
84+
7285
function ignoreQueryParams (pathAndQuery) {
7386
return url.parse(pathAndQuery, true).pathname
7487
}

0 commit comments

Comments
 (0)