-
Notifications
You must be signed in to change notification settings - Fork 652
/
Copy pathupload.js
590 lines (534 loc) · 19.4 KB
/
upload.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const URL = require('url').URL;
const fetch = require('isomorphic-fetch');
const _ = require('@lhci/utils/src/lodash.js');
const ApiClient = require('@lhci/utils/src/api-client.js');
const {writeUrlMapToFile} = require('@lhci/utils/src/saved-reports.js');
const {computeRepresentativeRuns} = require('@lhci/utils/src/representative-runs.js');
const {
loadSavedLHRs,
loadAssertionResults,
replaceUrlPatterns,
getHTMLReportForLHR,
} = require('@lhci/utils/src/saved-reports.js');
const {
getCurrentHash,
getCommitTime,
getCurrentBranch,
getExternalBuildUrl,
getCommitMessage,
getAuthor,
getAvatarUrl,
getAncestorHashForBase,
getAncestorHashForBranch,
getGitHubRepoSlug,
} = require('@lhci/utils/src/build-context.js');
/** @param {string} message */
const print = message => {
process.stdout.write(message);
};
const DEFAULT_GITHUB_API_HOST = 'https://api.github.com';
const DIFF_VIEWER_URL = 'https://googlechrome.github.io/lighthouse-ci/viewer/';
const TEMPORARY_PUBLIC_STORAGE_URL =
'https://us-central1-lighthouse-infrastructure.cloudfunctions.net/saveHtmlReport';
const GET_URL_MAP_URL =
'https://us-central1-lighthouse-infrastructure.cloudfunctions.net/getUrlMap';
const SAVE_URL_MAP_URL =
'https://us-central1-lighthouse-infrastructure.cloudfunctions.net/saveUrlMap';
const GITHUB_APP_STATUS_CHECK_URL =
'https://us-central1-lighthouse-infrastructure.cloudfunctions.net/githubAppPostStatusCheck';
/**
* @param {import('yargs').Argv} yargs
*/
function buildCommand(yargs) {
return yargs.options({
target: {
type: 'string',
default: 'lhci',
choices: ['lhci', 'temporary-public-storage', 'filesystem'],
description:
'The type of target to upload the data to. Some options will only apply to particular targets',
},
token: {
type: 'string',
description: '[lhci only] The Lighthouse CI server token for the project.',
},
ignoreDuplicateBuildFailure: {
type: 'boolean',
description:
'[lhci only] Whether to ignore failures (still exit with code 0) caused by uploads of a duplicate build.',
},
githubToken: {
type: 'string',
description: 'The GitHub token to use to apply a status check.',
},
githubApiHost: {
type: 'string',
default: DEFAULT_GITHUB_API_HOST,
description:
'The GitHub host to use for the status check API request. Modify this when using on a GitHub Enterprise server.',
},
githubAppToken: {
type: 'string',
description: 'The LHCI GitHub App token to use to apply a status check.',
},
githubStatusContextSuffix: {
type: 'string',
description: 'The suffix of the GitHub status check context label.',
},
extraHeaders: {
description: '[lhci only] Extra headers to use when making API requests to the LHCI server.',
},
'basicAuth.username': {
type: 'string',
description:
'[lhci only] The username to use on a server protected with HTTP Basic Authentication.',
},
'basicAuth.password': {
type: 'string',
description:
'[lhci only] The password to use on a server protected with HTTP Basic Authentication.',
},
serverBaseUrl: {
description: '[lhci only] The base URL of the LHCI server where results will be saved.',
default: 'http://localhost:9001/',
},
uploadUrlMap: {
type: 'boolean',
description:
'[temporary-public-storage only] Whether to post links to historical base results to storage or not. Defaults to true only on master branch.',
default: getCurrentBranch() === 'master',
},
urlReplacementPatterns: {
type: 'array',
description:
'[lhci only] sed-like replacement patterns to mask non-deterministic URL substrings.',
default: [
's#:[0-9]{3,5}/#:PORT/#', // replace ports
's/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/UUID/ig', // replace UUIDs
],
},
outputDir: {
type: 'string',
description: '[filesystem only] The directory in which to dump Lighthouse results.',
},
reportFilenamePattern: {
type: 'string',
description: '[filesystem only] The pattern to use for naming Lighthouse reports.',
default: '%%HOSTNAME%%-%%PATHNAME%%-%%DATETIME%%.report.%%EXTENSION%%',
},
});
}
/**
* @param {{slug: string, hash: string, state: 'failure'|'success', targetUrl: string, description: string, context: string, githubToken?: string, githubAppToken?: string, githubApiHost?: string}} options
*/
async function postStatusToGitHub(options) {
const {
slug,
hash,
state,
targetUrl,
context,
description,
githubToken,
githubAppToken,
githubApiHost = DEFAULT_GITHUB_API_HOST,
} = options;
let response;
if (githubAppToken) {
const url = GITHUB_APP_STATUS_CHECK_URL;
const payload = {...options, token: githubAppToken};
response = await fetch(url, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(payload),
});
} else {
const url = `${githubApiHost}/repos/${slug}/statuses/${hash}`;
const payload = {state, context, description, target_url: targetUrl};
response = await fetch(url, {
method: 'POST',
headers: {'Content-Type': 'application/json', Authorization: `token ${githubToken}`},
body: JSON.stringify(payload),
});
}
if (response.status === 201) {
print(`GitHub accepted "${state}" status for "${context}".\n`);
} else {
print(`GitHub responded with ${response.status}\n${await response.text()}\n\n`);
}
}
/**
*
* @param {string} rawUrl
* @param {LHCI.UploadCommand.Options} options
*/
function getUrlLabelForGithub(rawUrl, options) {
try {
const url = new URL(rawUrl);
return replaceUrlPatterns(url.pathname, options.urlReplacementPatterns);
} catch (_) {
return replaceUrlPatterns(rawUrl, options.urlReplacementPatterns);
}
}
/**
*
* @param {string} rawUrl
* @param {LHCI.UploadCommand.Options} options
*/
function getUrlForLhciTarget(rawUrl, options) {
let url = replaceUrlPatterns(rawUrl, options.urlReplacementPatterns);
if (url.length > 256) {
process.stderr.write('WARNING: audited URL exceeds character limits, truncation possible.');
url = url.slice(0, 256);
}
return url;
}
/**
* @param {string} urlLabel
* @param {LHCI.UploadCommand.Options} options
*/
function getGitHubContext(urlLabel, options) {
const prefix = options.githubStatusContextSuffix
? `lhci${options.githubStatusContextSuffix}`
: 'lhci';
return `${prefix}/url${urlLabel}`;
}
/**
* @param {LHCI.UploadCommand.Options} options
* @param {Map<string, string>} targetUrlMap
* @return {Promise<void>}
*/
async function runGithubStatusCheck(options, targetUrlMap) {
const {githubToken, githubAppToken, githubApiHost} = options;
const hash = getCurrentHash();
const slug = getGitHubRepoSlug(githubApiHost);
if (!githubToken && !githubAppToken) {
return print('No GitHub token set, skipping GitHub status check.\n');
}
print('GitHub token found, attempting to set status...\n');
if (!slug) return print(`No GitHub remote found, skipping.\n`);
if (!slug.includes('/')) return print(`Invalid repo slug "${slug}", skipping.\n`);
if (!hash) return print(`Invalid hash "${hash}"\n, skipping.`);
const assertionResults = loadAssertionResults();
const groupedResults = _.groupBy(assertionResults, result => result.url).sort(
(a, b) => a[0].url.length - b[0].url.length
);
if (groupedResults.length) {
for (const group of groupedResults) {
const rawUrl = group[0].url;
const urlLabel = getUrlLabelForGithub(rawUrl, options);
const failedResults = group.filter(result => result.level === 'error');
const warnResults = group.filter(result => result.level === 'warn');
const state = failedResults.length ? 'failure' : 'success';
const context = getGitHubContext(urlLabel, options);
const warningsLabel = warnResults.length ? ` with ${warnResults.length} warning(s)` : '';
const description = failedResults.length
? `Failed ${failedResults.length} assertion(s)`
: `Passed${warningsLabel}`;
const targetUrl = targetUrlMap.get(rawUrl) || rawUrl;
await postStatusToGitHub({
slug,
hash,
state,
context,
description,
targetUrl,
githubToken,
githubAppToken,
githubApiHost,
});
}
} else {
/** @type {Array<LH.Result>} */
const lhrs = loadSavedLHRs().map(lhr => JSON.parse(lhr));
/** @type {Array<Array<[LH.Result, LH.Result]>>} */
const lhrsByUrl = _.groupBy(lhrs, lhr => lhr.finalUrl).map(lhrs => lhrs.map(lhr => [lhr, lhr]));
const representativeLhrs = computeRepresentativeRuns(lhrsByUrl);
if (!representativeLhrs.length) return print('No LHRs for status check, skipping.\n');
for (const lhr of representativeLhrs) {
const rawUrl = lhr.finalUrl;
const urlLabel = getUrlLabelForGithub(rawUrl, options);
const state = 'success';
const context = getGitHubContext(urlLabel, options);
const categoriesDescription = Object.values(lhr.categories)
.map(category => `${category.title}: ${Math.round(category.score * 100)}`)
.join(', ');
const description = `${categoriesDescription}`;
const targetUrl = targetUrlMap.get(rawUrl) || rawUrl;
await postStatusToGitHub({
slug,
hash,
state,
context,
description,
targetUrl,
githubToken,
githubAppToken,
githubApiHost,
});
}
}
}
/**
* Fetches the last public URL mapping from master if it exists.
*
* @param {LHCI.UploadCommand.Options} options
* @return {Promise<Map<string, string>>}
*/
async function getPreviousUrlMap(options) {
const slug = getGitHubRepoSlug();
if (!slug) return new Map();
try {
const fetchUrl = new URL(GET_URL_MAP_URL);
fetchUrl.searchParams.set('slug', slug);
const apiResponse = await fetch(fetchUrl.href);
const {success, url} = await apiResponse.json();
if (!success) return new Map();
const mapResponse = await fetch(url);
if (mapResponse.status !== 200) return new Map();
const entries = Object.entries(await mapResponse.json());
return new Map(
entries.map(([k, v]) => [replaceUrlPatterns(k, options.urlReplacementPatterns), v])
);
} catch (err) {
print(`Error while fetching previous urlMap: ${err.message}`);
return new Map();
}
}
/**
* Saves the provided URL map to temporary public storage.
*
* @param {Map<string, string>} urlMap
* @return {Promise<void>}
*/
async function writeUrlMapToApi(urlMap) {
const slug = getGitHubRepoSlug();
if (!slug) return;
try {
/** @type {Record<string, string>} */
const payload = {slug};
Array.from(urlMap.entries()).forEach(([k, v]) => (payload[k] = v));
await fetch(SAVE_URL_MAP_URL, {
method: 'POST',
body: JSON.stringify(payload),
headers: {'content-type': 'application/json'},
});
} catch (err) {
print(`Failed to save urlMap: ${err.message}`);
}
}
/**
* @param {string} compareUrl
* @param {string} urlAudited
* @param {Map<string, string>} previousUrlMap
* @return {string}
*/
function buildTemporaryStorageLink(compareUrl, urlAudited, previousUrlMap) {
const baseUrl = previousUrlMap.get(urlAudited);
if (!baseUrl) return compareUrl;
const linkUrl = new URL(DIFF_VIEWER_URL);
linkUrl.searchParams.set('baseReport', baseUrl);
linkUrl.searchParams.set('compareReport', compareUrl);
return linkUrl.href;
}
/**
* @param {LHCI.UploadCommand.Options} options
* @return {Promise<void>}
*/
async function runLHCITarget(options) {
if (!options.token) throw new Error('Must provide token for LHCI target');
const api = new ApiClient({...options, rootURL: options.serverBaseUrl});
api.setBuildToken(options.token);
const project = await api.findProjectByToken(options.token);
if (!project) {
throw new Error('Could not find active project with provided token');
}
const baseBranch = project.baseBranch || 'master';
const hash = getCurrentHash();
const branch = getCurrentBranch();
const ancestorHash =
branch === baseBranch ? getAncestorHashForBase() : getAncestorHashForBranch('HEAD', baseBranch);
const build = await api.createBuild({
projectId: project.id,
lifecycle: 'unsealed',
hash,
branch,
ancestorHash,
commitMessage: getCommitMessage(hash),
author: getAuthor(hash),
avatarUrl: getAvatarUrl(hash),
externalBuildUrl: getExternalBuildUrl(),
runAt: new Date().toISOString(),
committedAt: getCommitTime(hash),
ancestorCommittedAt: ancestorHash ? getCommitTime(ancestorHash) : undefined,
});
print(`Saving CI project ${project.name} (${project.id})\n`);
print(`Saving CI build (${build.id})\n`);
const lhrs = loadSavedLHRs();
const targetUrlMap = new Map();
const buildViewUrl = new URL(
`/app/projects/${project.slug}/compare/${build.id}`,
options.serverBaseUrl
);
for (const lhr of lhrs) {
const parsedLHR = JSON.parse(lhr);
const url = getUrlForLhciTarget(parsedLHR.finalUrl, options);
const run = await api.createRun({
projectId: project.id,
buildId: build.id,
representative: false,
url,
lhr,
});
buildViewUrl.searchParams.set('compareUrl', url);
targetUrlMap.set(parsedLHR.finalUrl, buildViewUrl.href);
print(`Saved LHR to ${options.serverBaseUrl} (${run.id})\n`);
}
buildViewUrl.searchParams.delete('compareUrl');
await api.sealBuild(build.projectId, build.id);
print(`Done saving build results to Lighthouse CI\n`);
print(`View build diff at ${buildViewUrl.href}\n`);
writeUrlMapToFile(targetUrlMap);
await runGithubStatusCheck(options, targetUrlMap);
}
/**
* @param {LHCI.UploadCommand.Options} options
* @return {Promise<void>}
*/
async function runTemporaryPublicStorageTarget(options) {
/** @type {Array<LH.Result>} */
const lhrs = loadSavedLHRs().map(lhr => JSON.parse(lhr));
/** @type {Array<Array<[LH.Result, LH.Result]>>} */
const lhrsByUrl = _.groupBy(lhrs, lhr => lhr.finalUrl).map(lhrs => lhrs.map(lhr => [lhr, lhr]));
const representativeLhrs = computeRepresentativeRuns(lhrsByUrl);
const targetUrlMap = new Map();
const previousUrlMap = await getPreviousUrlMap(options);
for (const lhr of representativeLhrs) {
print(`Uploading median LHR of ${lhr.finalUrl}...`);
try {
const response = await fetch(TEMPORARY_PUBLIC_STORAGE_URL, {
method: 'POST',
headers: {'content-type': 'text/html'},
body: getHTMLReportForLHR(lhr),
});
const {success, url} = await response.json();
if (success && url) {
const urlReplaced = replaceUrlPatterns(lhr.finalUrl, options.urlReplacementPatterns);
const urlToLinkTo = buildTemporaryStorageLink(url, urlReplaced, previousUrlMap);
print(`success!\nOpen the report at ${urlToLinkTo}\n`);
targetUrlMap.set(lhr.finalUrl, urlToLinkTo);
} else {
print(`failed!\n`);
}
} catch (err) {
print(`failed!\n`);
process.stderr.write(err.stack + '\n');
}
}
writeUrlMapToFile(targetUrlMap);
if (options.uploadUrlMap) await writeUrlMapToApi(targetUrlMap);
await runGithubStatusCheck(options, targetUrlMap);
}
/**
*
* @param {string} pattern
* @param {Record<string, string>} context
*/
function getFileOutputPath(pattern, context) {
let filename = pattern;
const matches = pattern.match(/%%([a-z]+)%%/gi) || [];
for (const match of matches) {
const name = match.slice(2, -2).toLowerCase();
const value = context[name] || 'unknown';
const sanitizedValue = value.replace(/[^a-z0-9]+/gi, '_');
filename = filename.replace(match, sanitizedValue);
}
return filename;
}
/**
* @param {LHCI.UploadCommand.Options} options
* @return {Promise<void>}
*/
async function runFilesystemTarget(options) {
/** @type {Array<LH.Result>} */
const lhrs = loadSavedLHRs().map(lhr => JSON.parse(lhr));
/** @type {Array<Array<[LH.Result, LH.Result]>>} */
const lhrsByUrl = _.groupBy(lhrs, lhr => lhr.finalUrl).map(lhrs => lhrs.map(lhr => [lhr, lhr]));
const representativeLhrs = computeRepresentativeRuns(lhrsByUrl);
const targetDir = path.resolve(process.cwd(), options.outputDir || '');
if (!fs.existsSync(targetDir)) fs.mkdirSync(targetDir, {recursive: true});
print(`Dumping ${lhrs.length} reports to disk at ${targetDir}...\n`);
/** @type {Array<LHCI.UploadCommand.ManifestEntry>} */
const manifest = [];
// Process the median LHRs last so duplicate filenames will be overwritten by the median run
for (const lhr of _.sortBy(lhrs, lhr => (representativeLhrs.includes(lhr) ? 10 : 1))) {
const url = new URL(lhr.finalUrl);
const fetchTimeDate = new Date(new Date(lhr.fetchTime).getTime() || Date.now());
const context = {
hostname: url.hostname,
pathname: url.pathname,
date: fetchTimeDate.toISOString().replace(/T.*/, ''),
datetime: fetchTimeDate
.toISOString()
.replace(/\.\d{3}Z/, '')
.replace('T', ' '),
};
const filePattern = options.reportFilenamePattern;
const htmlPath = getFileOutputPath(filePattern, {...context, extension: 'html'});
const jsonPath = getFileOutputPath(filePattern, {...context, extension: 'json'});
/** @type {LHCI.UploadCommand.ManifestEntry} */
const entry = {
url: lhr.finalUrl,
isRepresentativeRun: representativeLhrs.includes(lhr),
htmlPath: path.join(targetDir, htmlPath),
jsonPath: path.join(targetDir, jsonPath),
summary: Object.keys(lhr.categories).reduce(
(summary, key) => {
summary[key] = lhr.categories[key].score;
return summary;
},
/** @type {Record<string, number>} */ ({})
),
};
fs.writeFileSync(entry.htmlPath, getHTMLReportForLHR(lhr));
fs.writeFileSync(entry.jsonPath, JSON.stringify(lhr));
manifest.push(entry);
}
const manifestPath = path.join(targetDir, 'manifest.json');
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
print('Done writing reports to disk.\n');
}
/**
* @param {LHCI.UploadCommand.Options} options
* @return {Promise<void>}
*/
async function runCommand(options) {
options.urlReplacementPatterns = options.urlReplacementPatterns.filter(Boolean);
switch (options.target) {
case 'lhci':
try {
return await runLHCITarget(options);
} catch (err) {
if (options.ignoreDuplicateBuildFailure && /Build already exists/.test(err.message)) {
print('Build already exists but ignore requested via options, skipping upload...');
return;
}
throw err;
}
case 'temporary-public-storage':
return runTemporaryPublicStorageTarget(options);
case 'filesystem':
return runFilesystemTarget(options);
default:
throw new Error(`Unrecognized target "${options.target}"`);
}
}
module.exports = {buildCommand, runCommand};