-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proxy Jenkins build status to GitHub (#35)
This enables Jenkins jobs to push updates as POST requests to the bot, which proxies the information given to the GitHub status API, resulting in PRs getting inline build status like we're used to with Travis CI.
- Loading branch information
Showing
7 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const githubClient = require('./github-client') | ||
|
||
function pushJenkinsUpdate (options, build) { | ||
const statusOpts = extendWithBuildData(options, build) | ||
const createGhStatus = createGhStatusFn(statusOpts) | ||
|
||
createGhStatus(build.status, build.message) | ||
} | ||
|
||
function createGhStatusFn (options) { | ||
const prInfo = prInfoStr(options) | ||
|
||
return (state, message) => { | ||
githubClient.statuses.create({ | ||
user: options.owner, | ||
repo: options.repoName, | ||
sha: options.sha, | ||
target_url: options.url, | ||
context: options.context, | ||
state: state, | ||
description: message | ||
}, (err, res) => { | ||
if (err) { | ||
return console.error(`! ${prInfo} Error while updating Jenkins / GitHub PR status`, err) | ||
} | ||
console.log(`* ${prInfo} Jenkins / Github PR status updated to '${state}'`) | ||
}) | ||
} | ||
} | ||
|
||
function extendWithBuildData (options, build) { | ||
return Object.assign({ | ||
sha: build.commit, | ||
url: build.url, | ||
context: build.identifier | ||
}, options) | ||
} | ||
|
||
function prInfoStr (options) { | ||
const shortSha = options.sha.substr(0, 7) | ||
return `${options.owner}/${options.repoName}/${shortSha}` | ||
} | ||
|
||
module.exports = pushJenkinsUpdate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
const pushJenkinsUpdate = require('../lib/push-jenkins-update') | ||
|
||
module.exports = function (app) { | ||
app.post('/node/jenkins', (req, res) => { | ||
pushJenkinsUpdate({ | ||
owner: 'nodejs', | ||
repoName: 'node' | ||
}, req.body) | ||
|
||
res.status(201).end() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"identifier": "test/arm", | ||
"status": "failure", | ||
"message": "tests failed", | ||
"commit": "8a5fec2a6bade91e544a30314d7cf21f8a200de1", | ||
"url": "https://ci.nodejs.org/job/node-test-commit-arm/3087/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"identifier": "test/linux", | ||
"status": "pending", | ||
"message": "checking for errors", | ||
"commit": "8a5fec2a6bade91e544a30314d7cf21f8a200de1", | ||
"url": "https://ci.nodejs.org/job/node-test-commit-linux/3176/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"identifier": "test/osx", | ||
"status": "success", | ||
"message": "tests passed", | ||
"commit": "8a5fec2a6bade91e544a30314d7cf21f8a200de1", | ||
"url": "https://ci.nodejs.org/job/node-test-commit-osx/3157/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict' | ||
|
||
const tap = require('tap') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const url = require('url') | ||
const nock = require('nock') | ||
const supertest = require('supertest') | ||
|
||
const app = require('../app') | ||
|
||
tap.test('Sends POST requests to https://api.github.com/repos/nodejs/node/statuses/<SHA>', (t) => { | ||
const fixture = readFixture('success-payload.json') | ||
const scope = nock('https://api.github.com') | ||
.filteringPath(ignoreQueryParams) | ||
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1') | ||
.reply(201) | ||
|
||
t.plan(1) | ||
t.tearDown(() => scope.done()) | ||
|
||
supertest(app) | ||
.post('/node/jenkins') | ||
.send(fixture) | ||
.expect(201) | ||
.end((err, res) => { | ||
t.equal(err, null) | ||
}) | ||
}) | ||
|
||
tap.test('Forwards payload provided in incoming POST to GitHub status API', (t) => { | ||
const fixture = readFixture('success-payload.json') | ||
const scope = nock('https://api.github.com') | ||
.filteringPath(ignoreQueryParams) | ||
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1', { | ||
state: 'success', | ||
context: 'test/osx', | ||
description: 'tests passed', | ||
target_url: 'https://ci.nodejs.org/job/node-test-commit-osx/3157/' | ||
}) | ||
.reply(201) | ||
|
||
t.plan(1) | ||
t.tearDown(() => scope.done()) | ||
|
||
supertest(app) | ||
.post('/node/jenkins') | ||
.send(fixture) | ||
.expect(201) | ||
.end((err, res) => { | ||
t.equal(err, null) | ||
}) | ||
}) | ||
|
||
function ignoreQueryParams (pathAndQuery) { | ||
return url.parse(pathAndQuery, true).pathname | ||
} | ||
|
||
function readFixture (fixtureName) { | ||
const content = fs.readFileSync(path.join(__dirname, '_fixtures', fixtureName)).toString() | ||
return JSON.parse(content) | ||
} |