-
Notifications
You must be signed in to change notification settings - Fork 119
/
jenkins-status.js
84 lines (66 loc) · 2.21 KB
/
jenkins-status.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'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 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: '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')
}
if (!isJenkinsIpWhitelisted(req)) {
return res.status(401).end('Invalid Jenkins IP')
}
pushJenkinsUpdate.pushEnded({
owner: 'nodejs',
repo,
logger: req.log
}, req.body, (err) => {
const statusCode = err !== null ? 500 : 201
res.status(statusCode).end()
})
})
}