Betterer: Expose results as JSON (#81352)

* Expose betterer results as JSON

* Make prettier and add command

* Add aggregation

* Add json generation to lefthook

* Use relative path

* Add grafanabot as codeowner

* Fix parameter type

* Include changes to results

* Run betterer:json
This commit is contained in:
Tobias Skarhed
2024-02-14 16:01:22 +02:00
committed by GitHub
parent 4cbc7dfb5b
commit 6ce286246b
5 changed files with 8317 additions and 1 deletions
+42
View File
@@ -0,0 +1,42 @@
import { BettererFileIssue, betterer } from '@betterer/betterer';
import { writeFile } from 'fs/promises';
interface Issue {
message: string;
count: string;
}
type ResultMap = Record<string, Record<string, Issue[]>>;
/**
* Produces a JSON file for consumption directly in Grafana
*/
async function main() {
const results = await betterer.results();
const resultMap: ResultMap = {};
for (const suite of results.resultSummaries) {
resultMap[suite.name] = {};
// Aggregate issues for each file in the suite
for (const [file, details] of Object.entries(suite.details)) {
const fileIssues: Issue[] = [];
for (const issue of details) {
const issueExists = fileIssues.find((i) => i.message === issue.message)!!;
if (issueExists) {
continue;
}
fileIssues.push({
message: issue.message,
count: details.filter((i: BettererFileIssue) => i.message === issue.message).length,
});
}
const relativePath = file.replace(process.cwd(), '');
resultMap[suite.name][relativePath] = fileIssues;
}
}
await writeFile('.betterer.results.json', JSON.stringify(resultMap, undefined, 2));
}
main().catch(console.error);