-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(jenkins): refactor jenkins-status using events
- Loading branch information
Showing
4 changed files
with
146 additions
and
98 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
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,79 @@ | ||
'use strict' | ||
|
||
const pushJenkinsUpdate = require('../lib/push-jenkins-update') | ||
|
||
const debug = require('debug')('jenkins-events') | ||
const enabledRepos = ['citgm', 'http-parser', 'node', 'node-auto-test'] | ||
|
||
const listOfKnownJenkinsIps = process.env.JENKINS_WORKER_IPS ? process.env.JENKINS_WORKER_IPS.split(',') : [] | ||
|
||
function isKnownJenkinsIp (req) { | ||
const ip = req.connection.remoteAddress.split(':').pop() | ||
|
||
if (listOfKnownJenkinsIps.length && !listOfKnownJenkinsIps.includes(ip)) { | ||
req.log.warn({ ip }, 'Ignoring, not allowed to push Jenkins updates') | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
function isRelatedToPullRequest (gitRef) { | ||
// refs/pull/12345/head vs refs/heads/v8.x-staging/head | ||
return gitRef.includes('/pull/') | ||
} | ||
|
||
module.exports = (app, events) => { | ||
app.post('/:repo/jenkins/:event', async (req, res) => { | ||
const isValid = pushJenkinsUpdate.validate(req.body) | ||
const repo = req.params.repo | ||
const event = req.params.event | ||
const owner = req.body.owner || process.env.JENKINS_DEFAULT_GH_OWNER || 'nodejs' | ||
|
||
if (!isValid) { | ||
return res.status(400).end('Invalid payload') | ||
} | ||
|
||
if (!isRelatedToPullRequest(req.body.ref)) { | ||
return res.status(400).end('Will only push builds related to pull requests') | ||
} | ||
|
||
if (!enabledRepos.includes(repo)) { | ||
return res.status(400).end('Invalid repository') | ||
} | ||
|
||
if (!isKnownJenkinsIp(req)) { | ||
return res.status(401).end('Invalid Jenkins IP') | ||
} | ||
|
||
const data = { | ||
...req.body, | ||
owner, | ||
repo, | ||
event | ||
} | ||
|
||
try { | ||
await app.emitJenkinsEvent(event, data, req.log) | ||
res.status(200) | ||
} catch (err) { | ||
req.log.error(err, 'Error while emitting Jenkins event') | ||
res.status(500) | ||
} | ||
|
||
res.end() | ||
}) | ||
|
||
app.emitJenkinsEvent = function emitJenkinsEvent (event, data, logger) { | ||
const { identifier } = data | ||
|
||
// create unique logger which is easily traceable throughout the entire app | ||
// by having e.g. "nodejs/nodejs.org/#1337" part of every subsequent log statement | ||
data.logger = logger.child({ identifier, event }, true) | ||
|
||
data.logger.info('Emitting Jenkins event') | ||
debug(data) | ||
|
||
return events.emit(`jenkins.${event}`, data) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,84 +1,36 @@ | ||
'use strict' | ||
|
||
const pushJenkinsUpdate = require('../lib/push-jenkins-update') | ||
const enabledRepos = ['citgm', 'http-parser', 'node'] | ||
|
||
const jenkinsIpWhitelist = process.env.JENKINS_WORKER_IPS ? process.env.JENKINS_WORKER_IPS.split(',') : [] | ||
function handleJenkinsStart (event) { | ||
const { repo, owner } = event | ||
|
||
function isJenkinsIpWhitelisted (req) { | ||
const ip = req.connection.remoteAddress.split(':').pop() | ||
|
||
if (jenkinsIpWhitelist.length && !jenkinsIpWhitelist.includes(ip)) { | ||
req.log.warn({ ip }, 'Ignoring, not allowed to push Jenkins updates') | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
function isRelatedToPullRequest (gitRef) { | ||
// refs/pull/12345/head vs refs/heads/v8.x-staging/head | ||
return gitRef.includes('/pull/') | ||
} | ||
|
||
module.exports = function (app) { | ||
app.post('/:repo/jenkins/start', (req, res) => { | ||
const isValid = pushJenkinsUpdate.validate(req.body) | ||
const repo = req.params.repo | ||
|
||
if (!isValid) { | ||
return res.status(400).end('Invalid payload') | ||
} | ||
|
||
if (!isRelatedToPullRequest(req.body.ref)) { | ||
return res.status(400).end('Will only push builds related to pull requests') | ||
} | ||
|
||
if (!enabledRepos.includes(repo)) { | ||
return res.status(400).end('Invalid repository') | ||
} | ||
|
||
if (!isJenkinsIpWhitelisted(req)) { | ||
return res.status(401).end('Invalid Jenkins IP') | ||
pushJenkinsUpdate.pushStarted({ | ||
owner, | ||
repo, | ||
logger: event.logger | ||
}, event, (err) => { | ||
if (err) { | ||
event.logger.error(err, 'Error while handling Jenkins start event') | ||
} | ||
|
||
pushJenkinsUpdate.pushStarted({ | ||
owner: 'nodejs', | ||
repo, | ||
logger: req.log | ||
}, req.body, (err) => { | ||
const statusCode = err !== null ? 500 : 201 | ||
res.status(statusCode).end() | ||
}) | ||
}) | ||
} | ||
|
||
app.post('/:repo/jenkins/end', (req, res) => { | ||
const isValid = pushJenkinsUpdate.validate(req.body) | ||
const repo = req.params.repo | ||
|
||
if (!isValid) { | ||
return res.status(400).end('Invalid payload') | ||
} | ||
|
||
if (!isRelatedToPullRequest(req.body.ref)) { | ||
return res.status(400).end('Will only push builds related to pull requests') | ||
} | ||
|
||
if (!enabledRepos.includes(repo)) { | ||
return res.status(400).end('Invalid repository') | ||
} | ||
function handleJenkinsStop (event) { | ||
const { repo, owner } = event | ||
|
||
if (!isJenkinsIpWhitelisted(req)) { | ||
return res.status(401).end('Invalid Jenkins IP') | ||
pushJenkinsUpdate.pushEnded({ | ||
owner, | ||
repo, | ||
logger: event.logger | ||
}, event, (err) => { | ||
if (err) { | ||
event.logger.error(err, 'Error while handling Jenkins end event') | ||
} | ||
|
||
pushJenkinsUpdate.pushEnded({ | ||
owner: 'nodejs', | ||
repo, | ||
logger: req.log | ||
}, req.body, (err) => { | ||
const statusCode = err !== null ? 500 : 201 | ||
res.status(statusCode).end() | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = function (_, event) { | ||
event.on('jenkins.start', handleJenkinsStart) | ||
event.on('jenkins.end', handleJenkinsStop) | ||
} |
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