-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
458 lines (372 loc) · 18.4 KB
/
server.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
const https = require('https');
const fetch = require('node-fetch');
const constants = require('constants');
const cheerio = require('cheerio');
const path = require('path');
const Koa = require('koa');
const Router = require('@koa/router');
const hbs = require('koa-hbs');
const serve = require('koa-static');
const sass = require('node-sass-koa-middleware');
const semverRegex = require('semver-regex');
const semverDiff = require('semver-diff');
const NodeCache = require('node-cache');
const fetchResultCache = new NodeCache();
const bitbucketUsername = process.env.BITBUCKET_USERNAME;
const bitbucketPassword = process.env.BITBUCKET_PASSWORD;
const bitbucketHost = process.env.BITBUCKET_HOST;
const bitbucketPort = process.env.BITBUCKET_PORT;
const satisUsername = process.env.SATIS_USERNAME;
const satisPassword = process.env.SATIS_PASSWORD;
const satisHost = process.env.SATIS_HOST;
const baseDistributionName = process.env.BASE_DISTRIBUTION_NAME;
const homepageCacheTtl = parseInt(process.env.HOMEPAGE_CACHE_TTL) || 600;
const fetchCacheTtl = parseInt(process.env.FETCH_CACHE_TTL) || 600;
const httpsAgent = new https.Agent({
secureOptions: constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_TLSv1,
ecdhCurve: 'auto',
});
const fetchUrl = async (url, authUser, authPass, method = 'GET') => {
const cacheHit = fetchResultCache.get(url);
if (cacheHit === undefined) {
const headers = {};
if (authUser && authPass) headers['Authorization'] = `Basic ${Buffer.from(authUser + ":" + authPass).toString('base64')}`;
const request = await fetch(url, { method, headers, agent: httpsAgent });
if (request.status === 404) throw new Error(`404; file ${url} was not found`);
const requestBody = await request.text();
fetchResultCache.set(url, requestBody, fetchCacheTtl);
return requestBody;
} else {
return cacheHit;
}
};
const distributionFileUrl = (projectKey, file, branch) => {
return `https://${bitbucketHost}/projects/${projectKey}/repos/${baseDistributionName}/raw/${file}?at=refs%2Fheads%2F${branch}`;
};
const distributionFolderPath = (projectKey, path, branch) => {
return `https://${bitbucketHost}/projects/${projectKey}/repos/${baseDistributionName}/browse/${path}?at=refs%2Fheads%2F${branch}`;
};
const extractDistributionData = (distributionElement, $satisHomepage) => {
const $distribution = $satisHomepage(distributionElement);
const $gitUrl = $distribution.find('dt:contains(Releases)').next('dd').find('a:contains(dev-master)');
return {
"name": $distribution.find('.card-header').attr('id').replace('"', '').trim(),
"gitUrl": $gitUrl.attr('href')
};
};
const extractExtensionData = (extensionElement, $satisHomepage) => {
const $extension = $satisHomepage(extensionElement);
const $distributionNames = $extension.find('dt:contains(Required by)').next('dd').find('a');
const $allVersionLinks = $extension.find('dt:contains(Releases)').next('dd').find('a');
const allVersionLinks = $allVersionLinks.toArray();
const currentVersionLink = allVersionLinks.find(version => semverRegex().test(version.firstChild.nodeValue));
const currentVersion = currentVersionLink.firstChild.nodeValue;
let inDistributions = [];
$distributionNames.each((index, element) => inDistributions.push($satisHomepage(element).text()));
return {
"name": $extension.find('.card-header').attr('id').replace('"', '').trim(),
"inDistributions": inDistributions,
"currentVersion": currentVersion
};
};
const extractDataFromPanelFn = ($satisHomepage, distributions, extensions) => {
return (index, element) => {
let $element = $satisHomepage(element);
if ($element.find(`.card-header a:contains(${baseDistributionName})`).length) {
distributions.push(extractDistributionData(element, $satisHomepage));
} else {
extensions.push(extractExtensionData(element, $satisHomepage));
}
}
};
const filterComposerLockToRmPackages = async (composerLockResponse) => {
const composerLockJSON = JSON.parse(composerLockResponse);
const rmPackages = composerLockJSON['packages'].filter(package => package.name.indexOf('/rm-') > -1 && package.name.indexOf('ruhmesmeile') > -1);
const rmPackagesDev = composerLockJSON['packages-dev'].filter(package => package.name.indexOf('/rm-') > -1 && package.name.indexOf('ruhmesmeile') > -1);
const thirdPartyPackages = composerLockJSON['packages'].filter(package => package.name.indexOf('/rm-') < 0);
const thirdPartyPackagesDev = composerLockJSON['packages-dev'].filter(package => package.name.indexOf('/rm-') < 0);
const projectPackages = composerLockJSON['packages'].filter(package => package.name.indexOf('/rm-') > -1 && package.name.indexOf('ruhmesmeile') < 0);
const projectPackagesDev = composerLockJSON['packages-dev'].filter(package => package.name.indexOf('/rm-') > -1 && package.name.indexOf('ruhmesmeile') < 0);
// TODO this signifies a difference in build process (general cms-include, vs. specific core)
const typo3CoreExt = composerLockJSON['packages'].filter(package => (package.name === 'typo3/cms-core' || package.name === 'typo3/cms'));
return [
rmPackages,
rmPackagesDev,
thirdPartyPackages,
thirdPartyPackagesDev,
projectPackages,
projectPackagesDev,
typo3CoreExt[0].version.replace('v','')
];
};
const extractDistributionInfoFromComposerJson = async (composerJsonResponse) => {
const composerJsonJSON = JSON.parse(composerJsonResponse);
let rmBambooProjectKey = '/';
// TODO this signifies a difference in build process (where the bambooKey is stored)
if (composerJsonJSON.extra['ruhmesmeile/rm-configuration'] && composerJsonJSON.extra['ruhmesmeile/rm-configuration'].rmBambooProjectKey) {
rmBambooProjectKey = composerJsonJSON.extra['ruhmesmeile/rm-configuration'].rmBambooProjectKey;
} else if (composerJsonJSON.extra['ruhmesmeile/rm-configuration'] && composerJsonJSON.extra['ruhmesmeile/rm-configuration'].settings.rmBambooProjectKey) {
rmBambooProjectKey = composerJsonJSON.extra['ruhmesmeile/rm-configuration'].settings.rmBambooProjectKey;
}
return [
composerJsonJSON.version,
rmBambooProjectKey.substr(0,6)
];
};
const extractDistributionInfoFromWriteInstallFiles = async (writeInstallFilesResponse) => {
const newFrontendIntegration = writeInstallFilesResponse.indexOf('verdaccio') > 0;
return [
newFrontendIntegration,
];
};
const extractTypo3CMSVersionFromPackagist = async (typo3CmsPackagistResponse) => {
const $typo3CmsPackagist = cheerio.load(typo3CmsPackagistResponse);
const typo3CmsPackagistVersion = $typo3CmsPackagist('.version-details .version-number').text().replace('v','')
return [
typo3CmsPackagistVersion,
];
};
const extractNumberOfPatches = async (listingHtml) => {
const $page = cheerio.load(listingHtml);
const hasPatches = $page('#browse-table tbody tr').length > 1;
return [
hasPatches,
];
};
const extractDataForDistribution = (extensions) => {
return async (distribution) => {
const setRelationInSatis = (extension) => {
const satisExtension = extensions.find(ext => ext.name === extension.name);
extension['relationInSatis'] = satisExtension.inDistributions.includes(distribution.name);
return extension;
};
const setCurrentVersion = (extension) => {
const matchingExtension = extensions.find(ext => ext.name === extension.name);
extension['currentVersion'] = matchingExtension.currentVersion;
return extension;
};
const setVersionDiff = (extension) => {
const matchingExtension = extensions.find(ext => ext.name === extension.name);
extension['versionDiff'] = semverDiff(extension.version, matchingExtension.currentVersion);
return extension;
};
// gitUrl format: ssh://git@${bitbucketHost}:${bitbucketPort}/PROJECTKEY/${baseDistributionName}.git
const distributionProjectKey = distribution.gitUrl.substring(
distribution.gitUrl.indexOf(`${bitbucketPort}/`) + 5,
distribution.gitUrl.indexOf(`/${baseDistributionName}.git`)
);
const composerLock = await fetchUrl(distributionFileUrl(distributionProjectKey, 'composer.lock', 'master'), bitbucketUsername, bitbucketPassword)
.then(filterComposerLockToRmPackages).catch((err) => { if (err.message.indexOf('404') > -1) return [false]; });
const composerJson = await fetchUrl(distributionFileUrl(distributionProjectKey, 'composer.json', 'master'), bitbucketUsername, bitbucketPassword)
.then(extractDistributionInfoFromComposerJson).catch((err) => { if (err.message.indexOf('404') > -1) return [false]; });
const writeInstallFiles = await fetchUrl(distributionFileUrl(distributionProjectKey, 'bamboo-specs/src/main/resources/build/03-write-install-files.sh', 'master'), bitbucketUsername, bitbucketPassword)
.then(extractDistributionInfoFromWriteInstallFiles).catch((err) => { if (err.message.indexOf('404') > -1) return [false]; });
const backupTypo3 = await fetchUrl(distributionFileUrl(distributionProjectKey, 'bamboo-specs/src/main/resources/deploy/backup-typo3.util.sh', 'master'), bitbucketUsername, bitbucketPassword)
.then(() => [true]).catch((err) => { if (err.message.indexOf('404') > -1) return [false]; });
const frontendRc = await fetchUrl(distributionFileUrl(distributionProjectKey, '.rm-frontendrc.js', 'master'), bitbucketUsername, bitbucketPassword)
.then(() => [true]).catch((err) => { if (err.message.indexOf('404') > -1) return [false]; });
const composerPatches = await fetchUrl(distributionFolderPath(distributionProjectKey, 'src/Patches', 'master'), bitbucketUsername, bitbucketPassword)
.then(extractNumberOfPatches).catch((err) => { if (err.message.indexOf('404') > -1) return [false]; });
const frontendPatches = await fetchUrl(distributionFolderPath(distributionProjectKey, 'patches', 'master'), bitbucketUsername, bitbucketPassword)
.then(extractNumberOfPatches).catch((err) => { if (err.message.indexOf('404') > -1) return [false]; });
distribution['extensions'] = {
'master': {
'rm': composerLock[0],
'third': composerLock[2],
'project': composerLock[4]
}
};
distribution['typo3CoreVersion'] = composerLock[6];
distribution['rmDistVersion'] = composerJson[0];
distribution['rmBambooProjectKey'] = composerJson[1];
distribution['frontendIntegration'] = writeInstallFiles[0];
distribution['backupTypo3'] = backupTypo3[0];
distribution['newFrontend'] = frontendRc[0];
distribution['hasComposerPatches'] = composerPatches[0];
distribution['hasFrontendPatches'] = frontendPatches[0];
distribution.extensions.master.rm.forEach(setRelationInSatis);
distribution.extensions.master.rm.forEach(setCurrentVersion);
distribution.extensions.master.rm.forEach(setVersionDiff);
distribution.extensions.master.project.forEach(setRelationInSatis);
// These (devExtensions) are not used for anything yet, might want to copy some more
// forEach cases from above when starting to use these
distribution['devExtensions'] = {
'master': { 'rm': composerLock[1], 'third': composerLock[3], 'project': composerLock[5] }
};
distribution.devExtensions.master.rm.forEach(setRelationInSatis);
distribution.devExtensions.master.rm.forEach(setCurrentVersion);
distribution.devExtensions.master.rm.forEach(setVersionDiff);
distribution.devExtensions.master.project.forEach(setRelationInSatis);
return distribution;
};
};
const getMatrixData = async () => {
const distributions = [];
const extensions = [];
const $satisHomepage = cheerio.load(await fetchUrl(`https://${satisHost}/`, satisUsername, satisPassword));
$satisHomepage('#package-list .card').map(extractDataFromPanelFn($satisHomepage, distributions, extensions));
return Promise.all(distributions.map(extractDataForDistribution(extensions))).then(async (distributions) => {
const removeDuplicates = (array, property) => {
let obj = {};
return Object.keys(array.reduce((prev, next) => {
if (!obj[next[property]]) obj[next[property]] = next;
return obj;
}, obj)).map((i) => obj[i]);
}
const removeNameFromProjectExtension = function removeNameFromProjectExtension(extension) {
extension.name = extension.name.substring(
extension.name.indexOf('/') + 1,
extension.name.length
);
return extension;
};
const reduceToRmExtensions = function reduceToRmExtensions(accumulator, distribution) {
return accumulator.concat([...distribution.extensions.master.rm]);
};
const reduceToThirdPartyExtensions = function reduceToThirdPartyExtensions(accumulator, distribution) {
return accumulator.concat([...distribution.extensions.master.third]);
};
const reduceToProjectExtensions = function reduceToProjectExtensions(accumulator, distribution) {
return accumulator.concat([...distribution.extensions.master.project.map(removeNameFromProjectExtension)]);
};
const allRmExtensions = removeDuplicates(distributions.reduce(reduceToRmExtensions, []), 'name');
const allThirdPartyExtensions = removeDuplicates(distributions.reduce(reduceToThirdPartyExtensions, []), 'name');
const allProjectExtensions = removeDuplicates(distributions.reduce(reduceToProjectExtensions, []), 'name');
const currentTypo3CoreVersion = (await fetchUrl('https://packagist.org/packages/typo3/cms-core')
.then(extractTypo3CMSVersionFromPackagist))[0];
return {
currentTypo3CoreVersion,
distributions,
allRmExtensions,
allThirdPartyExtensions,
allProjectExtensions
}
});
}
const versionLink = function versionLink(extension) {
// extension.source.url format: "ssh://git@${bitbucketHost}:${bitbucketPort}/PROJECTKEY/REPONAME.git"
const repoName = extension.source.url.substring(
extension.source.url.lastIndexOf('/') + 1,
extension.source.url.indexOf('.git')
);
const projectKey = extension.source.url.substring(
extension.source.url.indexOf(`${bitbucketPort}/`) + 5,
extension.source.url.indexOf(repoName) - 1
);
return `https://${bitbucketHost}/projects/${projectKey}/repos/${repoName}/commits?until=refs%2Ftags%2F${extension.version}&merges=include`;
};
const versionDiffLink = function versionDiffLink(extension) {
// extension.source.url format: "ssh://git@${bitbucketHost}:${bitbucketPort}/PROJECTKEY/REPONAME.git"
const repoName = extension.source.url.substring(
extension.source.url.lastIndexOf('/') + 1,
extension.source.url.indexOf('.git')
);
const projectKey = extension.source.url.substring(
extension.source.url.indexOf(`${bitbucketPort}/`) + 5,
extension.source.url.indexOf(repoName) - 1
);
return `https://${bitbucketHost}/projects/${projectKey}/repos/${repoName}/compare/commits?targetBranch=refs%2Ftags%2F${extension.version}&sourceBranch=refs%2Ftags%2F${extension.currentVersion}`;
};
const app = new Koa();
const router = new Router();
const extension = (extensionFullName) => {
return extensionFullName.substring(
extensionFullName.indexOf('/') + 1,
extensionFullName.length
);
};
const version = (distribution, extensionName) => {
let masterExtension = distribution.extensions.master.rm.find(ext => ext.name === extensionName);
if (!masterExtension) {
masterExtension = distribution.extensions.master.project.find(ext => ext.name.indexOf(extensionName) > -1);
}
return masterExtension
? new hbs.SafeString(`
<span class="diff diff--master diff--${masterExtension.versionDiff}">
<a target="_blank" href="${versionLink(masterExtension)}">${masterExtension.version}</a>
<span class="currentVersion">
(<a target="_blank" href="${versionDiffLink(masterExtension)}">${masterExtension.currentVersion}</a>)
</span>
</span>
`)
: new hbs.SafeString('<span class="x">X</span>');
};
const typo3Version = (distributionCoreVersion, currentCoreVersion) => {
return new hbs.SafeString(`
<span class="diff diff--master diff--${semverDiff(distributionCoreVersion, currentCoreVersion)}">
${distributionCoreVersion}
<span class="currentVersion">
(${currentCoreVersion})
</span>
</span>
`);
};
const isDivider = (category, parentIndex, index, options) => {
if (category === 'rm' && ((parentIndex == 0) && (index === 0))) {
return options.fn(this);
} else if (category === 'all' && ((parentIndex == 0) && (index === 0))) {
return options.fn(this);
} else {
return options.inverse(this);
}
};
hbs.registerHelper('extension', extension);
hbs.registerHelper('version', version);
hbs.registerHelper('typo3Version', typo3Version);
hbs.registerHelper('isDivider', isDivider);
router.get('/', async (ctx) => {
let matrixData;
const cacheHit = fetchResultCache.get('matrixHomepage');
if (cacheHit === undefined) {
matrixData = await getMatrixData();
fetchResultCache.set('matrixHomepage', matrixData, homepageCacheTtl);
} else {
matrixData = cacheHit;
}
await ctx.render('feature-matrix', matrixData);
});
router.get('/cache/clear', async (ctx) => {
fetchResultCache.flushAll();
ctx.status = 200;
});
router.get('/cache/clear/:repository', async (ctx) => {
const distributionProjectKey = ctx.params.repository;
const keys = [
'matrixHomepage',
`https://${satisHost}/`,
distributionFileUrl(distributionProjectKey, 'composer.lock', 'master'),
distributionFileUrl(distributionProjectKey, 'composer.json', 'master'),
distributionFileUrl(distributionProjectKey, 'bamboo-specs/src/main/resources/build-distribution/03-write-install-files.sh', 'master'),
distributionFileUrl(distributionProjectKey, 'bamboo-specs/src/main/resources/distribution-deployment/util/backup-typo3.util.sh', 'master'),
distributionFileUrl(distributionProjectKey, '.rm-frontendrc.js', 'master'),
];
fetchResultCache.del(keys);
ctx.status = 200;
});
app
.use(hbs.middleware({
viewPath: path.join(__dirname, 'views'),
layoutsPath: path.join(__dirname, 'views', 'layouts'),
defaultLayout: 'main',
extname: '.handlebars'
}))
.use(router.routes())
.use(router.allowedMethods())
.use(sass({
src: path.join(__dirname, 'src', 'scss'),
dest: path.join(__dirname, 'public', 'css'),
debug: true,
outputStyle: 'compressed',
includePaths: 'node_modules/compass-mixins/lib',
prefix: '/css'
}))
.use(serve(path.join(__dirname, 'public')));
app.listen(3000);
/*
# TODO
* sort extensions by frequency (per category)
* add sensible links where applicable (typo3 systems, projects etc)
* move all assets to local hosting from CDNs
* add cache clear route for url / project / repository, integrate with docker-satis
* add better data table
*/