Skip to content

Commit 6bfdafa

Browse files
committed
First shot at pushing GH update from Jenkins
1 parent 55e7fcb commit 6bfdafa

File tree

7 files changed

+122
-125
lines changed

7 files changed

+122
-125
lines changed

lib/poll-jenkins.js

Lines changed: 0 additions & 114 deletions
This file was deleted.

lib/push-jenkins-update.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict'
2+
3+
const githubClient = require('./github-client')
4+
5+
const jenkinsToGhStatusMap = {
6+
'SUCCESS': {
7+
state: 'success',
8+
message: 'all tests passed'
9+
},
10+
'FAILURE': {
11+
state: 'failure',
12+
message: 'build failure'
13+
},
14+
'ABORTED': {
15+
state: 'error',
16+
message: 'build aborted'
17+
},
18+
'UNSTABLE': {
19+
state: 'error',
20+
message: 'build unstable'
21+
}
22+
}
23+
24+
const rxDisplayName = /node\-test\-commit\-(\w+)/
25+
26+
function pushJenkinsUpdate (options, build) {
27+
const statusOpts = extendWithBuildData(options, build)
28+
const createGhStatus = createGhStatusFn(statusOpts)
29+
const isPending = build.result === null
30+
const matchedGhStatus = jenkinsToGhStatusMap[build.result]
31+
32+
if (isPending) {
33+
createGhStatus('pending', 'build in progress')
34+
} else if (matchedGhStatus) {
35+
createGhStatus(matchedGhStatus.state, matchedGhStatus.message)
36+
} else {
37+
console.error(`! ${prInfoStr(options)} Unknown Jenkins build result '${build.result}'`)
38+
}
39+
}
40+
41+
function createGhStatusFn (options) {
42+
const prInfo = prInfoStr(options)
43+
44+
return (state, message) => {
45+
githubClient.statuses.create({
46+
user: options.owner,
47+
repo: options.repoName,
48+
sha: options.sha,
49+
target_url: options.url,
50+
context: options.context,
51+
state: state,
52+
description: message
53+
}, (err, res) => {
54+
if (err) {
55+
return console.error(`! ${prInfo} Error while updating Jenkins / GitHub PR status`, err)
56+
}
57+
console.log(`* ${prInfo} Jenkins / Github PR status updated to '${state}'`)
58+
})
59+
}
60+
}
61+
62+
function extendWithBuildData (options, build) {
63+
const lastChangeSet = build.changeSet.items[0]
64+
65+
return Object.assign({
66+
sha: lastChangeSet.commitId,
67+
url: build.url,
68+
context: jobNameToStatusCtx(build.fullDisplayName)
69+
}, options)
70+
}
71+
72+
function jobNameToStatusCtx (displayName) {
73+
return rxDisplayName.exec(displayName)[1]
74+
}
75+
76+
function prInfoStr (options) {
77+
return `${options.owner}/${options.repoName}`
78+
}
79+
80+
module.exports = pushJenkinsUpdate

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"express": "^4.13.4",
2020
"github": "^0.2.4",
2121
"glob": "^7.0.3",
22-
"request": "^2.72.0",
2322
"travis-ci": "^2.1.0"
2423
},
2524
"devDependencies": {

scripts/node-jenkins-status.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
'use strict'
22

3-
const pollJenkins = require('../lib/poll-jenkins')
3+
const pushJenkinsUpdate = require('../lib/push-jenkins-update')
44

55
module.exports = function (app) {
6-
// to trigger polling manually
7-
app.get('/jenkins/:prId', (req, res) => {
8-
const prId = req.params.prId
9-
10-
pollJenkins({
11-
owner: 'nodejs',
12-
repoName: 'node',
13-
prId: parseInt(prId, 10)
14-
})
6+
app.post('/node/jenkins', (req, res) => {
7+
pushJenkinsUpdate({
8+
owner: 'TestOrgPleaseIgnore',
9+
repoName: 'node'
10+
}, req.body)
1511

1612
res.end()
1713
})

test/_fixtures/error-payload.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"fullDisplayName": "node-test-commit-arm #3087",
3+
"result": "FAILURE",
4+
"changeSet": {
5+
"items": [
6+
{
7+
"commitId": "7190bd23810f0880d95605bf6f785224a345d477"
8+
}
9+
]
10+
},
11+
"url": "https://ci.nodejs.org/job/node-test-commit-arm/3087/"
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"fullDisplayName": "node-test-commit-linux #3176",
3+
"result": null,
4+
"changeSet": {
5+
"items": [
6+
{
7+
"commitId": "7190bd23810f0880d95605bf6f785224a345d477"
8+
}
9+
]
10+
},
11+
"url": "https://ci.nodejs.org/job/node-test-commit-linux/3176/"
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"fullDisplayName": "node-test-commit-osx #3157",
3+
"result": "SUCCESS",
4+
"changeSet": {
5+
"items": [
6+
{
7+
"commitId": "7190bd23810f0880d95605bf6f785224a345d477"
8+
}
9+
]
10+
},
11+
"url": "https://ci.nodejs.org/job/node-test-commit-osx/3157/"
12+
}

0 commit comments

Comments
 (0)