-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
398 lines (346 loc) · 14.3 KB
/
index.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
const Sentry = require('@sentry/node');
const express = require('express');
const crypto = require('crypto');
const { Octokit } = require("@octokit/rest");
const HttpStatus = require('http-status-codes');
const YAML = require('yaml');
const fs = require('fs');
const path = require('path');
const AWS = require('aws-sdk');
let suitePublishRuns = {};
let config = null;
try {
const configFileContents = fs.readFileSync('./config.yml', 'utf8');
config = YAML.parse(configFileContents);
} catch (e) {
console.error('Failed to load config.yml be sure exists otherwise copy config.example.yml and modify it to your needs.');
return;
}
const LISTENING_PORT = Number(config.listening_port || 3000);
const ALLOWED_REPOS = Object.keys(config.repositories);
const BUILD_UPLOAD_ALLOWED_LABEL = config.build_allowed_upload_label || "allow-build-upload";
const BUILD_FILE_HASHES_REGEX = "BuildFileHashes: (\\[(.+)\\])";
Sentry.init({ dsn: config.sentry_dsn });
const app = express();
const octokit = new Octokit({
auth: config.github_auth_token
});
app.use('/webhook', express.json());
app.use(async (request, response, next) => {
if (request.path === '/webhook') {
next();
return;
}
const {owner, repo, commit_hash, pull_number, job_name, file_sizes} = request.headers;
if (!owner || !repo || !commit_hash || !pull_number || !job_name || !file_sizes) {
response.status(HttpStatus.INTERNAL_SERVER_ERROR).send('One of the required headers is not set correctly.');
return;
}
if (!ALLOWED_REPOS.includes(`${owner}/${repo}`)) {
response.status(HttpStatus.INTERNAL_SERVER_ERROR).send('Selected repo is not permitted to use this service.');
return;
}
let pull_data = (await octokit.pulls.get({owner, repo, pull_number})).data;
if (!isPRBuildAllowedToBeUploaded(pull_data)) {
response.status(HttpStatus.INTERNAL_SERVER_ERROR)
.send(`Please label PR build next time with the ${BUILD_UPLOAD_ALLOWED_LABEL} label.`);
return;
}
const GITHUB_MAX_VISIBLE_COMMITS = 250;
let last_commit_page = pull_data.commits > GITHUB_MAX_VISIBLE_COMMITS ? GITHUB_MAX_VISIBLE_COMMITS : pull_data.commits;
let pull_commits = (await octokit.pulls.listCommits({
owner,
repo,
pull_number,
per_page: 1,
page: last_commit_page
})).data;
if (!pull_commits.some(commit => commit.sha === commit_hash)) {
response.status(HttpStatus.INTERNAL_SERVER_ERROR)
.send(`Pull request does not contains commit sha: ${commit_hash}.`);
return;
}
let file_sizes_list = file_sizes.split(',').map(file_size => parseInt(file_size));
let file_index = 0;
request.files = [];
let hash = crypto.createHash('sha256');
let fileBuffers = [];
let fileSeek = 0;
let fileSize = file_sizes_list[file_index];
request.on('data', (chunk) => {
let readData = chunk;
while (readData.length > 0) {
let dataLength = readData.length;
let bytesToGo = fileSize - fileSeek;
// only read part of this file
let readLength = Math.min(bytesToGo, dataLength);
fileSeek += readLength;
let readDataNow = readData.slice(0, readLength);
let remainingData = readData.slice(readLength, readData.length);
hash.update(readDataNow);
fileBuffers.push(readDataNow);
if (remainingData.length > 0 || fileSeek === fileSize) {
console.log('Reading of file completed');
request.files.push({
hash: hash.digest('hex'),
buffer: Buffer.concat(fileBuffers),
fileSize,
verified: false
});
if (file_index < (file_sizes_list.length - 1)) {
// Go to next file
file_index++;
fileSize = file_sizes_list[file_index];
fileSeek = 0;
fileBuffers = [];
hash = crypto.createHash('sha256');
}
}
readData = remainingData;
}
}).on('end', () => {
next();
});
});
function isPRBuildAllowedToBeUploaded(pull_request_data) {
return pull_request_data.author_association === "OWNER" ||
pull_request_data.author_association === "MEMBER" ||
pull_request_data.author_association === "COLLABORATOR" ||
pull_request_data.labels.some(label => label.name === BUILD_UPLOAD_ALLOWED_LABEL);
}
function getStoragePath(path, variables) {
return path.replace(/\[\:([a-zA-Z0-9_]+)\]/g, (match, variable_key) => {
if (variables[variable_key] === undefined) {
console.error(`Could not find ${variable_key} for path: ${path}.`);
return '';
}
return variables[variable_key];
});
}
async function publishRuns(check_suite_id) {
const requests = suitePublishRuns[check_suite_id];
delete suitePublishRuns[check_suite_id];
let publishUrls = {};
let areGlobalCheckSuiteVariablesSet = false;
let globalPullNumber = false;
let globalOwner = null;
let globalRepo = null;
for (let requestKey in requests) {
if (!requests.hasOwnProperty(requestKey)) {
continue;
}
let request = requests[requestKey];
const {owner, repo, commit_hash, pull_number, job_name, run_id} = request.headers;
if (!areGlobalCheckSuiteVariablesSet) {
globalPullNumber = pull_number;
globalOwner = owner;
globalRepo = repo;
areGlobalCheckSuiteVariablesSet = true;
}
const repositoryConfig = config.repositories[`${owner}/${repo}`];
let jobsForWorkflowRun = (await octokit.actions.listJobsForWorkflowRun({
owner,
repo,
run_id
}).catch((e) => {
console.log(e);
})).data;
console.log('workflow run jobs: ' + JSON.stringify(jobsForWorkflowRun));
let actualJobID = null;
let isJobCompleted = false;
if (!jobsForWorkflowRun.jobs.some((job) => {
if (job.name === job_name) {
actualJobID = job.id;
isJobCompleted = job.status === "completed";
return true;
}
return false;
})) {
console.log(`Failed to find matching job_name in running jobs.`);
return false;
}
let logs = (await octokit.actions.listWorkflowJobLogs({
owner,
repo,
job_id: actualJobID
}).catch((e) => {
console.log(e);
})).data;
let regExp = new RegExp(BUILD_FILE_HASHES_REGEX, 'gm');
let match = regExp.exec(logs);
if (!match) {
console.log(`check_run ${check_run.name} doesn't have build file hashes.`);
continue;
}
let buildFileHashes;
try {
buildFileHashes = JSON.parse(match[1]);
} catch (e) {
console.log(`failed to parse build hashes.`);
Sentry.captureException(e);
continue;
}
console.log('buildFileHashes: ' + JSON.stringify(buildFileHashes));
let hasHashError = false;
for (let i = 0; i < request.files.length; i++) {
let file = request.files[i];
console.log(`Looking for the filename of artifact with the hash ${file.hash}.`);
let buildFileHashPair = buildFileHashes.find((buildFileHash) => {
return buildFileHash.sha256_checksum === file.hash;
});
if (!buildFileHashPair) {
hasHashError = true;
console.log(`Failed to find build hash in JSON object. Bailing.`);
break;
}
file.name = buildFileHashPair.filename;
file.extname = path.extname(file.name);
file.basename = path.basename(file.name, file.extname);
console.log(`Found ${file.name} with selected hash ${file.hash}.`);
}
if (hasHashError) {
console.log(`Hash error found. Publishing for ${job_name} terminated`);
continue;
}
publishUrls[job_name] = [];
// Storage
const repositoryStorages = repositoryConfig.storages;
for (let repositoryStorageKey in repositoryStorages) {
if (!repositoryStorages.hasOwnProperty(repositoryStorageKey)) {
continue;
}
const repositoryStorage = repositoryStorages[repositoryStorageKey];
const storage = config.storages[repositoryStorage.storage];
for (let i = 0; i < request.files.length; i++) {
let file = request.files[i];
const storageParams = {
owner,
repo,
pull_number,
file_name: file.name,
file_basename: file.basename,
file_extname: file.extname,
file_hash: file.hash,
commit_short_hash: commit_hash.substring(0, 8)
};
const storagePath = getStoragePath(storage.path, storageParams);
if (storage.method === 'file') {
try {
let storageDirectory = path.dirname(storagePath);
if (!fs.existsSync(storageDirectory)) {
fs.mkdirSync(storageDirectory, {recursive: true});
}
await fs.writeFile(storagePath, file.buffer, function (err) {
if (err) {
throw err;
}
console.log('Saved ' + file.hash + '!');
});
if (repositoryStorage.publish_url) {
publishUrls[job_name].push(storagePath);
}
} catch (err) {
Sentry.captureException(err);
console.log(JSON.stringify(err));
continue;
}
} else if (storage.method === 'S3') {
// Create S3 service object
let s3 = new AWS.S3({
apiVersion: '2006-03-01',
region: storage.region,
credentials: new AWS.Credentials(storage.accessKeyId, storage.secretAccessKey)
});
let data = (await s3.upload({
Bucket: storage.bucket,
Key: storagePath,
Body: file.buffer,
ACL: storage.acl
}).promise().catch((err) => {
console.log("Error", err);
}));
console.log('s3 upload output = ' + JSON.stringify(data));
if (data !== undefined && repositoryStorage.publish_url) {
publishUrls[job_name].push(getStoragePath(storage.public_url, storageParams));
}
}
console.log(storagePath);
}
}
}
let urlCount = Object.values(publishUrls).reduce((a, b) => {
return a.concat(b);
}).length;
// Only comment on PR if public urls are available
if (urlCount > 0) {
let message = "";
for (let publishUrlJobName in publishUrls) {
if (!publishUrls.hasOwnProperty(publishUrlJobName)) {
continue;
}
let urls = publishUrls[publishUrlJobName];
if (urls.length > 0) {
message += `**${publishUrlJobName}**\n - ` + urls.join('\n - ') + '\n\n';
}
}
// Publish a message with build links
octokit.issues.createComment({
owner: globalOwner,
repo: globalRepo,
issue_number: globalPullNumber,
body: 'The following links are available: \n' + message
}).catch((e) => {
Sentry.captureException(e);
console.error('Caught error: ', JSON.stringify(e));
});
}
}
app.listen(LISTENING_PORT, () => console.log(`App listening on port ${LISTENING_PORT}!`));
app.put('/', async function (request, response) {
try {
const {owner, repo, commit_hash, pull_number, job_name, run_id} = request.headers;
let check_run_data = (await octokit.checks.listForRef({
owner,
repo,
ref: commit_hash,
check_name: job_name
})).data;
console.log(JSON.stringify(check_run_data));
const checksuite_id = check_run_data.check_runs[0].check_suite.id;
console.log('checkrun id = ' + check_run_data.check_runs[0].id);
console.log('checksuite id = ' + checksuite_id);
console.log('run_id = ' + run_id);
if (suitePublishRuns[checksuite_id] === undefined) {
suitePublishRuns[checksuite_id] = [];
}
suitePublishRuns[checksuite_id].push(request);
response.send({
success: true,
message: "Publishing procedure started, unfortunately the GHA will have to finish before this."
});
} catch (err) {
console.log("err = " + err);
Sentry.captureException(err);
response.status(HttpStatus.INTERNAL_SERVER_ERROR).send('Error happened please check server logs.');
}
});
function getSignatureForBody(body) {
return 'sha1='+ crypto.createHmac('sha1', config.repositories[body.repository.full_name].gh_notify_secret)
.update(JSON.stringify(body))
.digest('hex')
}
app.post('/webhook', async function(request, response) {
const signature = request.headers['x-hub-signature'];
const event = request.headers['x-github-event'];
const expectedSignature = getSignatureForBody(request.body);
if (signature !== expectedSignature) {
response.status(HttpStatus.UNAUTHORIZED).send('Webhook authentication failure.');
return;
}
if (event === 'check_suite' && request.body.action === 'completed') {
await publishRuns(request.body.check_suite.id);
response.status(HttpStatus.OK).send('Check suite completed request handled.');
return;
}
response.status(HttpStatus.OK).send('Request handled.');
});