Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/push-jenkins-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

function pushJenkinsUpdate (options, build) {
function push (options, build) {
const statusOpts = extendWithBuildData(options, build)
const createGhStatus = createGhStatusFn(statusOpts)

Expand Down Expand Up @@ -43,4 +43,10 @@ function prInfoStr (options) {
return `${options.owner}/${options.repoName}/${shortSha}`
}

module.exports = pushJenkinsUpdate
function validate (payload) {
const isString = (param) => typeof (payload[param]) === 'string'
return ['identifier', 'status', 'message', 'commit', 'url'].every(isString)
}

exports.validate = validate
exports.push = push
8 changes: 7 additions & 1 deletion scripts/node-jenkins-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ const pushJenkinsUpdate = require('../lib/push-jenkins-update')

module.exports = function (app) {
app.post('/node/jenkins', (req, res) => {
pushJenkinsUpdate({
const isValid = pushJenkinsUpdate.validate(req.body)

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

pushJenkinsUpdate.push({
owner: 'nodejs',
repoName: 'node'
}, req.body)
Expand Down
5 changes: 5 additions & 0 deletions test/_fixtures/invalid-payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"identifier": "test/osx",
"status": "pending",
"url": "https://ci.nodejs.org/job/node-test-commit-osx/3157/"
}
17 changes: 17 additions & 0 deletions test/push-jenkins-update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ tap.test('Forwards payload provided in incoming POST to GitHub status API', (t)
})
})

tap.test('Responds with 400 / "Bad request" when incoming request has invalid payload', (t) => {
const fixture = readFixture('invalid-payload.json')

// don't care about the results, just want to prevent any HTTP request ever being made
nock('https://api.github.com')

t.plan(1)

supertest(app)
.post('/node/jenkins')
.send(fixture)
.expect(400, 'Invalid payload')
.end((err, res) => {
t.equal(err, null)
})
})

function ignoreQueryParams (pathAndQuery) {
return url.parse(pathAndQuery, true).pathname
}
Expand Down