ci: fix the payload-size checking scripts (#20683)

The scripts were accidentally broken in #20524. More specifically, when a limit
was exceeded the script would break while trying to log an error message due to
a missing `commit` variable.
This commit fixes it and also does some minor clean-up (improve docs, use more
descriptive variable names, remove dead code, etc).

PR Close #20683
This commit is contained in:
George Kalpakas
2017-11-29 12:47:04 +02:00
committed by Igor Minar
parent d34f0bf573
commit 7e7ff2e0aa
4 changed files with 67 additions and 45 deletions

View File

@ -1,30 +1,32 @@
'use strict';
// Imports
const fs = require('fs');
// Get branch and project name from command line arguments
const [, , limitFile, project, branch] = process.argv;
const limit = JSON.parse(fs.readFileSync(limitFile, 'utf8'));
const current = JSON.parse(fs.readFileSync('/tmp/current.log', 'utf8'));
// Get branch and project name from command line arguments.
const [, , limitFile, project, branch, commit] = process.argv;
// Load sizes.
const currentSizes = JSON.parse(fs.readFileSync('/tmp/current.log', 'utf8'));
const allLimitSizes = JSON.parse(fs.readFileSync(limitFile, 'utf8'));
const limitSizes = allLimitSizes[project][branch] || allLimitSizes[project]['master'];
const limitData = limit[project][branch] || limit[project]["master"];
if (!limitData) {
console.error(`No limit data found.`);
// If no payload limit file found, we don't need to check the size
exit(0);
}
// Check current sizes against limits.
let failed = false;
for (let compressionType in limitData) {
if (typeof limitData[compressionType] === 'object') {
for (let file in limitData[compressionType]) {
const name = `${compressionType}/${file}`;
const number = limitData[compressionType][file];
if (Math.abs(current[name] - number) > number / 100) {
console.log(`Commit ${commit} ${compressionType} ${file} changed from ${number} to ${current[name]}.
If this is a desired change, please update the payload size limits in file ${limitFile}`);
for (const compressionType in limitSizes) {
if (typeof limitSizes[compressionType] === 'object') {
const limitPerFile = limitSizes[compressionType];
for (const filename in limitPerFile) {
const expectedSize = limitPerFile[filename];
const actualSize = currentSizes[`${compressionType}/${filename}`];
if (Math.abs(actualSize - expectedSize) > expectedSize / 100) {
failed = true;
console.log(
`Commit ${commit} ${compressionType} ${filename} exceeded expected size by >1% ` +
`(expected: ${expectedSize}, actual: ${actualSize}).\n` +
`If this is a desired change, please update the size limits in file '${limitFile}'.`);
}
}
}
@ -33,5 +35,5 @@ for (let compressionType in limitData) {
if (failed) {
process.exit(1);
} else {
console.log(`Payload size 1% check okay`);
console.log('Payload size <1% change check passed.');
}