This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
codechecks.js
95 lines (83 loc) · 2.97 KB
/
codechecks.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
const { join } = require("path");
const fs = require("fs");
const { codechecks } = require("@codechecks/client");
const CodeChecksReport = require("eth-gas-reporter/lib/codechecksReport");
/**
* Consumed by codecheck command when user's .yml lists
* `eth-gas-reporter/codechecks`. The reporter dumps collected
* data to the project root whenever `process.env.CI` is true. This
* file processes it and runs the relevant codechecks routines.
* >
* > Source: krzkaczor/truffle-codechecks.
* >
*/
module.exports.default = async function gasReporter(options = {}) {
let output;
let file = "gasReporterOutput.json";
// Load gas reporter output
try {
output = JSON.parse(fs.readFileSync(file, "utf-8"));
} catch (error) {
const message =
`Error: Couldn't load data from "${file}".\n` +
`If you're using codechecks locally make sure you set ` +
`the environment variable "CI" to "true" before running ` +
`your tests. ( ex: CI=true npm test )`;
console.log(message);
return;
}
// Lets monorepo subcomponents individuate themselves
output.namespace = options.name
? `${output.namespace}:${options.name}`
: output.namespace;
let report = new CodeChecksReport(output.config);
report.generate(output.info);
try {
await codechecks.saveValue(output.namespace, report.newData);
console.log(`Successful save: output.namespace was: ${output.namespace}`);
} catch (err) {
console.log(
`If you have a chance, report this incident to the eth-gas-reporter github issues.`
);
console.log(`Codechecks errored running 'saveValue'...\n${err}\n`);
console.log(`output.namespace was: ${output.namespace}`);
console.log(`Saved gas-reporter data was: ${report.newData}`);
}
// Exit early on merge commit / push build
if (!codechecks.isPr()) {
return;
}
// Get historical data for each pr commit
try {
output.config.previousData = await codechecks.getValue(output.namespace);
} catch (err) {
console.log(
`If you have a chance, report this incident to the eth-gas-reporter github issues.`
);
console.log(`Codechecks errored running 'getValue'...\n${err}\n`);
return;
}
report = new CodeChecksReport(output.config);
const table = report.generate(output.info);
const shortDescription = report.getShortDescription();
// Support multiple reports
const checkName = options.name ? `Gas Usage: ${options.name}` : `Gas Usage`;
// Submit report
try {
const payload = {
name: checkName,
shortDescription: shortDescription,
longDescription: table
};
report.success
? await codechecks.success(payload)
: await codechecks.failure(payload);
} catch (err) {
console.log(
`If you have a chance, report this incident to the eth-gas-reporter github issues.`
);
console.log(`Codechecks errored running .success or .failure\n${err}\n`);
console.log(`Short description was: ${shortDescription}`);
console.log(`Table was: ${table}`);
}
};