/
webhook_interceptor.js
203 lines (169 loc) · 6.47 KB
/
webhook_interceptor.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const httpProxy = require('http-proxy');
const Queue = require('node-persistent-queue');
const config = require('./config.json')
const setupProfile = require('./profile');
const hooks = require('./hooks');
const axios = require('axios')
const useManualBuildRequestMiddleware = require('./manual-build-middleware');
const q = new Queue('db.sqlite', 1);
const jenkins = require('jenkins')({
baseUrl: 'http://'+config.jenkinsAuth+'@localhost:'+config.jenkinsPort,
crumbIssuer: true
});
const proxy = httpProxy.createProxyServer({});
const proxyApp = express();
proxyApp.use(bodyParser.json({ limit: "50mb" }));
const pis = {}
config.profiles.forEach((pc)=>{
const {
// do not forward these into the profile instance
profiles, jenkinsAuth, jenkinsPort, listenPort,
// forward the rest though
...fwGlobalConf
} = config;
let combinedConfig = { ...fwGlobalConf, ...pc }
let repoFullName = combinedConfig.repoOwner+'/'+combinedConfig.repoName;
pis[repoFullName] = setupProfile(combinedConfig, jenkins, q);
console.log("Defined profile:", repoFullName);
});
function getPiByFullName(repoFullName) {
return pis[repoFullName];
}
function getPi(repoOwner, repoName) {
return getPiByFullName(repoOwner+'/'+repoName);
}
const manualBuildRequestMiddleware = useManualBuildRequestMiddleware({ getPi, getPiByFullName })
const commitLengthCheckMiddleware = (req, res, next)=>{
if (req.body.commit.length !== 40) {
return res.send("commit should be 40 characters\n")
// If trying to build a branch, you maybe can use this to get the SHA1
// either way you need that because the checks API does not accept a branch name
// when we are ready to report the result.
//
// https://docs.github.com/en/rest/commits/commits#list-commits
//let commits = await octokit.request('GET /repos/{owner}/{repo}/commits', {
// owner: pi.config.repoOwner,
// repo: pi.config.repoName,
// sha:
//})
}
return next()
}
proxyApp.post('/build-all', manualBuildRequestMiddleware, commitLengthCheckMiddleware, async (req, res, next)=>{
await req.pi.queueBuild(req.body.commit)
res.send("queued\n")
})
proxyApp.post('/build-one', manualBuildRequestMiddleware, commitLengthCheckMiddleware, async (req, res, next)=>{
await req.pi.queueBuildForProject(req.body.project, req.body.commit)
res.send("queued\n")
})
proxyApp.post('/retry-one', manualBuildRequestMiddleware, async (req, res, next)=>{
jenkins.build.get(req.body.project, req.body.build, async (err, data)=>{
if (err) {
return next(err)
}
let paramsAction = data.actions.find(a=>a._class === 'hudson.model.ParametersAction')
let commit = paramsAction.parameters.find(a=>a.name="BRANCH_SPECIFIER").value
await req.pi.queueBuildForProject(req.body.project, commit)
res.send("queued\n")
})
})
proxyApp.get("/retry/:repoOrg/:repoName/:projectName/:buildNumber", (req, res, next)=>{
res.render("retry.html.ejs", req.params);
});
if (config.playwrightTraceLinkers) {
require('./playwright-trace-link')(proxyApp, proxy, config, jenkins);
}
proxyApp.use(async function(req, res){
let githubEvent = req.headers['x-github-event'];
if (!githubEvent) {
return proxy.web(req, res, {
target: 'http://localhost:'+config.jenkinsPort
}, function(err) {
console.log("proxy error", err.message);
});
}
const pi = getPiByFullName(req.body.repository.full_name);
if (!pi) {
res.status(404).end("no profile defined for this repository")
}
console.log("Received github event", githubEvent, "matched to", pi.config);
if (githubEvent == "pull_request" && ( req.body.action == "synchronize" || req.body.action == "opened" )) {
await pi.queueBuild(req.body.pull_request.head.sha, null, req.body);
return res.status(201).end("thanks for the PR, i will build it");
} else if (githubEvent == "push" && pi.config.refHooks && pi.config.refHooks[req.body.ref]) {
let hookDefinitions = pi.config.refHooks[req.body.ref];
if (hookDefinitions.buildBranch) {
await pi.queueBuild(req.body.head_commit.id, hookDefinitions.buildBranch, req.body, (projectList)=>{
if (hookDefinitions.extraJenkinsProjects) {
return [...projectList, ...hookDefinitions.extraJenkinsProjects]
} else {
return projectList
}
});
}
if (hookDefinitions.exec) {
console.log("Executing hook...");
let stdout = ""
let stderr = ""
try {
const out = await hooks.exec(hookDefinitions.exec.command);
if (out && out.stdout) {
stdout = out.stdout
}
if (out && out.stderr) {
stderr = out.stderr
}
} catch(err) {
console.error("error with exec", err.stack);
if (pi.config.slackAlertEndpoint)
axios.post(pi.config.slackAlertEndpoint, pi.slackNotifyHookError(req.body, err, stdout, stderr));
}
}
return res.status(201).end("thanks for the push. hooks have executed.");
} else if (githubEvent == "issue_comment" && req.body.action == "created" && req.body.issue.pull_request && req.body.comment.body.includes(pi.config.triggerPhrase)) {
let pull = await pi.octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
owner: pi.config.repoOwner,
repo: pi.config.repoName,
pull_number: req.body.issue.number
})
await pi.queueBuild(pull.data.head.sha, null, req.body);
return res.status(201).end("thanks for the issue comment, i will test it");
} else {
console.log("ignoring irrelevant webook delivery", req.headers['x-github-delivery']);
return res.status(202).end("irrelevant webhook, ignoring");
}
});
q.on('next',task => {
const { repoOwner, repoName } = task.job.config;
let pi = getPi(repoOwner, repoName);
pi.onNext(task);
});
q.on('stop',() => {
console.log('Stopping queue') ;
});
q.on('close',() => {
console.log('Closing SQLite DB') ;
});
q.on('open',() => {
console.log('Opening SQLite DB') ;
console.log('Queue contains '+q.getLength()+' job/s');
}) ;
q.on('add',task => {
console.log('Adding task ('+q.getLength()+' total) ');//+JSON.stringify(task));
}) ;
q.on('start',() => {
console.log('Started queue') ;
});
q.open().then(() => {
q.start();
}).catch(err => {
console.log('Error occurred:') ;
console.log(err.stack);
});
http.createServer(proxyApp).listen(config.listenPort, '0.0.0.0', () => {
console.log('Proxy server listening on '+config.listenPort);
});