2018-03-31 02:20:48 +02:00
|
|
|
#!/usr/bin/env node
|
2021-02-04 19:15:23 +01:00
|
|
|
const { promises: fs } = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const VERSION = require('../dist/chrome/manifest.json').version; // eslint-disable-line import/no-unresolved
|
2018-03-31 02:20:48 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
start().catch(console.error);
|
2018-04-03 21:51:18 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function capitalizeFirstLetter(string) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function start() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { GITHUB_COMMENT_TOKEN, CIRCLE_PULL_REQUEST } = process.env;
|
|
|
|
console.log('CIRCLE_PULL_REQUEST', CIRCLE_PULL_REQUEST);
|
|
|
|
const { CIRCLE_SHA1 } = process.env;
|
|
|
|
console.log('CIRCLE_SHA1', CIRCLE_SHA1);
|
|
|
|
const { CIRCLE_BUILD_NUM } = process.env;
|
|
|
|
console.log('CIRCLE_BUILD_NUM', CIRCLE_BUILD_NUM);
|
2018-04-03 21:51:18 +02:00
|
|
|
|
|
|
|
if (!CIRCLE_PULL_REQUEST) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.warn(`No pull request detected for commit "${CIRCLE_SHA1}"`);
|
|
|
|
return;
|
2018-04-03 21:51:18 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const CIRCLE_PR_NUMBER = CIRCLE_PULL_REQUEST.split('/').pop();
|
|
|
|
const SHORT_SHA1 = CIRCLE_SHA1.slice(0, 7);
|
|
|
|
const BUILD_LINK_BASE = `https://${CIRCLE_BUILD_NUM}-42009758-gh.circle-artifacts.com/0`;
|
2018-04-03 21:51:18 +02:00
|
|
|
|
2019-09-11 17:35:30 +02:00
|
|
|
// build the github comment content
|
|
|
|
|
|
|
|
// links to extension builds
|
2021-02-04 19:15:23 +01:00
|
|
|
const platforms = ['chrome', 'firefox', 'opera'];
|
2020-11-03 00:41:28 +01:00
|
|
|
const buildLinks = platforms
|
|
|
|
.map((platform) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const url = `${BUILD_LINK_BASE}/builds/metamask-${platform}-${VERSION}.zip`;
|
|
|
|
return `<a href="${url}">${platform}</a>`;
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2021-02-04 19:15:23 +01:00
|
|
|
.join(', ');
|
2019-09-11 17:35:30 +02:00
|
|
|
|
|
|
|
// links to bundle browser builds
|
2020-11-03 00:41:28 +01:00
|
|
|
const bundles = [
|
|
|
|
'background',
|
|
|
|
'ui',
|
|
|
|
'inpage',
|
|
|
|
'contentscript',
|
|
|
|
'ui-libs',
|
|
|
|
'bg-libs',
|
|
|
|
'phishing-detect',
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-11-03 00:41:28 +01:00
|
|
|
const bundleLinks = bundles
|
|
|
|
.map((bundle) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const url = `${BUILD_LINK_BASE}/build-artifacts/source-map-explorer/${bundle}.html`;
|
|
|
|
return `<a href="${url}">${bundle}</a>`;
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2021-02-04 19:15:23 +01:00
|
|
|
.join(', ');
|
2019-09-11 17:35:30 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const coverageUrl = `${BUILD_LINK_BASE}/coverage/index.html`;
|
|
|
|
const coverageLink = `<a href="${coverageUrl}">Report</a>`;
|
2020-12-11 20:51:00 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const storybookUrl = `${BUILD_LINK_BASE}/storybook/index.html`;
|
|
|
|
const storybookLink = `<a href="${storybookUrl}">Storybook</a>`;
|
2021-02-04 15:30:22 +01:00
|
|
|
|
2021-02-22 15:43:29 +01:00
|
|
|
// links to bundle browser builds
|
|
|
|
const depVizUrl = `${BUILD_LINK_BASE}/build-artifacts/build-viz/index.html`;
|
|
|
|
const depVizLink = `<a href="${depVizUrl}">Build System</a>`;
|
|
|
|
|
2019-09-16 06:19:15 +02:00
|
|
|
// link to artifacts
|
2021-02-04 19:15:23 +01:00
|
|
|
const allArtifactsUrl = `https://circleci.com/gh/MetaMask/metamask-extension/${CIRCLE_BUILD_NUM}#artifacts/containers/0`;
|
2019-09-16 06:19:15 +02:00
|
|
|
|
|
|
|
const contentRows = [
|
|
|
|
`builds: ${buildLinks}`,
|
|
|
|
`bundle viz: ${bundleLinks}`,
|
2021-02-22 15:43:29 +01:00
|
|
|
`build viz: ${depVizLink}`,
|
2020-12-11 20:51:00 +01:00
|
|
|
`code coverage: ${coverageLink}`,
|
2021-02-04 15:30:22 +01:00
|
|
|
`storybook: ${storybookLink}`,
|
2019-09-16 06:19:15 +02:00
|
|
|
`<a href="${allArtifactsUrl}">all artifacts</a>`,
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-11-03 00:41:28 +01:00
|
|
|
const hiddenContent = `<ul>${contentRows
|
|
|
|
.map((row) => `<li>${row}</li>`)
|
2021-02-04 19:15:23 +01:00
|
|
|
.join('\n')}</ul>`;
|
|
|
|
const exposedContent = `Builds ready [${SHORT_SHA1}]`;
|
|
|
|
const artifactsBody = `<details><summary>${exposedContent}</summary>${hiddenContent}</details>`;
|
2020-01-23 21:55:17 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const benchmarkResults = {};
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const platform of platforms) {
|
2020-11-03 00:41:28 +01:00
|
|
|
const benchmarkPath = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
path.join('test-artifacts', platform, 'benchmark', 'pageload.json'),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-01-23 21:55:17 +01:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const data = await fs.readFile(benchmarkPath, 'utf8');
|
|
|
|
const benchmark = JSON.parse(data);
|
|
|
|
benchmarkResults[platform] = benchmark;
|
2020-01-23 21:55:17 +01:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ENOENT') {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log(`No benchmark data found for ${platform}; skipping`);
|
2020-01-23 21:55:17 +01:00
|
|
|
} else {
|
2020-11-03 00:41:28 +01:00
|
|
|
console.error(
|
|
|
|
`Error encountered processing benchmark data for '${platform}': '${error}'`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const summaryPlatform = 'chrome';
|
|
|
|
const summaryPage = 'home';
|
|
|
|
let commentBody;
|
2020-08-14 13:47:43 +02:00
|
|
|
if (benchmarkResults[summaryPlatform]) {
|
2020-01-23 21:55:17 +01:00
|
|
|
try {
|
2020-11-03 00:41:28 +01:00
|
|
|
const summaryPageLoad = Math.round(
|
|
|
|
parseFloat(benchmarkResults[summaryPlatform][summaryPage].average.load),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
const summaryPageLoadMarginOfError = Math.round(
|
|
|
|
parseFloat(
|
|
|
|
benchmarkResults[summaryPlatform][summaryPage].marginOfError.load,
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const benchmarkSummary = `Page Load Metrics (${summaryPageLoad} ± ${summaryPageLoadMarginOfError} ms)`;
|
2020-01-23 21:55:17 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const allPlatforms = new Set();
|
|
|
|
const allPages = new Set();
|
|
|
|
const allMetrics = new Set();
|
|
|
|
const allMeasures = new Set();
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const platform of Object.keys(benchmarkResults)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
allPlatforms.add(platform);
|
|
|
|
const platformBenchmark = benchmarkResults[platform];
|
|
|
|
const pages = Object.keys(platformBenchmark);
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const page of pages) {
|
2021-02-04 19:15:23 +01:00
|
|
|
allPages.add(page);
|
|
|
|
const pageBenchmark = platformBenchmark[page];
|
|
|
|
const measures = Object.keys(pageBenchmark);
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const measure of measures) {
|
2021-02-04 19:15:23 +01:00
|
|
|
allMeasures.add(measure);
|
|
|
|
const measureBenchmark = pageBenchmark[measure];
|
|
|
|
const metrics = Object.keys(measureBenchmark);
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const metric of metrics) {
|
2021-02-04 19:15:23 +01:00
|
|
|
allMetrics.add(metric);
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const tableRows = [];
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const platform of allPlatforms) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const pageRows = [];
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const page of allPages) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metricRows = [];
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const metric of allMetrics) {
|
2021-02-04 19:15:23 +01:00
|
|
|
let metricData = `<td>${metric}</td>`;
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const measure of allMeasures) {
|
2020-11-03 00:41:28 +01:00
|
|
|
metricData += `<td align="right">${Math.round(
|
|
|
|
parseFloat(benchmarkResults[platform][page][measure][metric]),
|
2021-02-04 19:15:23 +01:00
|
|
|
)}</td>`;
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
metricRows.push(metricData);
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
metricRows[0] = `<td rowspan="${
|
|
|
|
allMetrics.size
|
2021-02-04 19:15:23 +01:00
|
|
|
}">${capitalizeFirstLetter(page)}</td>${metricRows[0]}`;
|
|
|
|
pageRows.push(...metricRows);
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
pageRows[0] = `<td rowspan="${
|
|
|
|
allPages.size * allMetrics.size
|
2021-02-04 19:15:23 +01:00
|
|
|
}">${capitalizeFirstLetter(platform)}</td>${pageRows[0]}`;
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const row of pageRows) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tableRows.push(`<tr>${row}</tr>`);
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const benchmarkTableHeaders = ['Platform', 'Page', 'Metric'];
|
2020-01-23 21:55:17 +01:00
|
|
|
for (const measure of allMeasures) {
|
2021-02-04 19:15:23 +01:00
|
|
|
benchmarkTableHeaders.push(`${capitalizeFirstLetter(measure)} (ms)`);
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
const benchmarkTableHeader = `<thead><tr>${benchmarkTableHeaders
|
|
|
|
.map((header) => `<th>${header}</th>`)
|
2021-02-04 19:15:23 +01:00
|
|
|
.join('')}</tr></thead>`;
|
|
|
|
const benchmarkTableBody = `<tbody>${tableRows.join('')}</tbody>`;
|
|
|
|
const benchmarkTable = `<table>${benchmarkTableHeader}${benchmarkTableBody}</table>`;
|
|
|
|
const benchmarkBody = `<details><summary>${benchmarkSummary}</summary>${benchmarkTable}</details>`;
|
|
|
|
commentBody = `${artifactsBody}${benchmarkBody}`;
|
2020-01-23 21:55:17 +01:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(`Error constructing benchmark results: '${error}'`);
|
|
|
|
commentBody = artifactsBody;
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
2020-08-14 13:47:43 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log(`No results for ${summaryPlatform} found; skipping benchmark`);
|
|
|
|
commentBody = artifactsBody;
|
2020-01-23 21:55:17 +01:00
|
|
|
}
|
2018-04-03 21:51:18 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const JSON_PAYLOAD = JSON.stringify({ body: commentBody });
|
|
|
|
const POST_COMMENT_URI = `https://api.github.com/repos/metamask/metamask-extension/issues/${CIRCLE_PR_NUMBER}/comments`;
|
|
|
|
console.log(`Announcement:\n${commentBody}`);
|
|
|
|
console.log(`Posting to: ${POST_COMMENT_URI}`);
|
2018-04-03 21:51:18 +02:00
|
|
|
|
2020-01-26 17:43:50 +01:00
|
|
|
const response = await fetch(POST_COMMENT_URI, {
|
2019-07-31 22:17:11 +02:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON_PAYLOAD,
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'metamaskbot',
|
2020-11-03 00:41:28 +01:00
|
|
|
Authorization: `token ${GITHUB_COMMENT_TOKEN}`,
|
2019-07-31 22:17:11 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-26 17:43:50 +01:00
|
|
|
if (!response.ok) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(`Post comment failed with status '${response.statusText}'`);
|
2020-01-26 17:43:50 +01:00
|
|
|
}
|
2018-04-03 21:51:18 +02:00
|
|
|
}
|