Skip to content

Commit 370927f

Browse files
feat(instrumentation/report): add problems to reports (#28042)
Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com>
1 parent b6d6695 commit 370927f

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed

lib/instrumentation/reporting.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import {
99
addBranchStats,
1010
addExtractionStats,
1111
exportStats,
12+
finalizeReport,
1213
getReport,
1314
} from './reporting';
1415

1516
jest.mock('../util/fs', () => mockDeep());
1617
jest.mock('../util/s3', () => mockDeep());
18+
jest.mock('../logger', () => mockDeep());
1719

1820
describe('instrumentation/reporting', () => {
1921
const branchInformation: Partial<BranchCache>[] = [
@@ -52,8 +54,10 @@ describe('instrumentation/reporting', () => {
5254
};
5355

5456
const expectedReport = {
57+
problems: [],
5558
repositories: {
5659
'myOrg/myRepo': {
60+
problems: [],
5761
branches: branchInformation,
5862
packageFiles,
5963
},
@@ -70,6 +74,7 @@ describe('instrumentation/reporting', () => {
7074
});
7175

7276
expect(getReport()).toEqual({
77+
problems: [],
7378
repositories: {},
7479
});
7580
});
@@ -174,4 +179,49 @@ describe('instrumentation/reporting', () => {
174179
fs.writeSystemFile.mockRejectedValue(null);
175180
await expect(exportStats(config)).toResolve();
176181
});
182+
183+
it('should add problems to report', () => {
184+
const config: RenovateConfig = {
185+
repository: 'myOrg/myRepo',
186+
reportType: 'logging',
187+
};
188+
const expectedReport = {
189+
problems: [
190+
{
191+
level: 30,
192+
msg: 'a root problem',
193+
},
194+
],
195+
repositories: {
196+
'myOrg/myRepo': {
197+
problems: [
198+
{
199+
level: 30,
200+
msg: 'a repo problem',
201+
},
202+
],
203+
branches: branchInformation,
204+
packageFiles,
205+
},
206+
},
207+
};
208+
209+
addBranchStats(config, branchInformation);
210+
addExtractionStats(config, { branchList: [], branches: [], packageFiles });
211+
212+
logger.getProblems.mockReturnValue([
213+
{
214+
repository: 'myOrg/myRepo',
215+
level: 30,
216+
msg: 'a repo problem',
217+
},
218+
{
219+
level: 30,
220+
msg: 'a root problem',
221+
},
222+
]);
223+
finalizeReport();
224+
225+
expect(getReport()).toEqual(expectedReport);
226+
});
177227
});

lib/instrumentation/reporting.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { PutObjectCommand, PutObjectCommandInput } from '@aws-sdk/client-s3';
22
import is from '@sindresorhus/is';
33
import type { RenovateConfig } from '../config/types';
4-
import { logger } from '../logger';
4+
import { getProblems, logger } from '../logger';
55
import type { BranchCache } from '../util/cache/repository/types';
66
import { writeSystemFile } from '../util/fs';
77
import { getS3Client, parseS3Url } from '../util/s3';
88
import type { ExtractResult } from '../workers/repository/process/extract-update';
99
import type { Report } from './types';
1010

1111
const report: Report = {
12+
problems: [],
1213
repositories: {},
1314
};
1415

@@ -37,6 +38,22 @@ export function addExtractionStats(
3738
extractResult.packageFiles;
3839
}
3940

41+
export function finalizeReport(): void {
42+
const allProblems = structuredClone(getProblems());
43+
for (const problem of allProblems) {
44+
const repository = problem.repository;
45+
delete problem.repository;
46+
47+
// if the problem can be connected to a repository add it their else add to the root list
48+
if (repository) {
49+
coerceRepo(repository);
50+
report.repositories[repository].problems.push(problem);
51+
} else {
52+
report.problems.push(problem);
53+
}
54+
}
55+
}
56+
4057
export async function exportStats(config: RenovateConfig): Promise<void> {
4158
try {
4259
if (is.nullOrUndefined(config.reportType)) {
@@ -91,6 +108,7 @@ function coerceRepo(repository: string): void {
91108
}
92109

93110
report.repositories[repository] = {
111+
problems: [],
94112
branches: [],
95113
packageFiles: {},
96114
};

lib/instrumentation/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Attributes, SpanKind } from '@opentelemetry/api';
2+
import type { BunyanRecord } from '../logger/types';
23
import type { PackageFile } from '../modules/manager/types';
34
import type { BranchCache } from '../util/cache/repository/types';
45

@@ -28,10 +29,12 @@ export interface SpanParameters {
2829
}
2930

3031
export interface Report {
32+
problems: BunyanRecord[];
3133
repositories: Record<string, RepoReport>;
3234
}
3335

3436
interface RepoReport {
37+
problems: BunyanRecord[];
3538
branches: Partial<BranchCache>[];
3639
packageFiles: Record<string, PackageFile[]>;
3740
}

lib/workers/global/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const initPlatform = jest.spyOn(platform, 'initPlatform');
4141

4242
describe('workers/global/index', () => {
4343
beforeEach(() => {
44-
logger.getProblems.mockImplementationOnce(() => []);
44+
logger.getProblems.mockImplementation(() => []);
4545
initPlatform.mockImplementation((input) => Promise.resolve(input));
4646
delete process.env.AWS_SECRET_ACCESS_KEY;
4747
delete process.env.AWS_SESSION_TOKEN;
@@ -178,7 +178,7 @@ describe('workers/global/index', () => {
178178
repositories: [],
179179
});
180180
logger.getProblems.mockReset();
181-
logger.getProblems.mockImplementationOnce(() => [
181+
logger.getProblems.mockImplementation(() => [
182182
{
183183
level: ERROR,
184184
msg: 'meh',
@@ -195,7 +195,7 @@ describe('workers/global/index', () => {
195195
repositories: [],
196196
});
197197
logger.getProblems.mockReset();
198-
logger.getProblems.mockImplementationOnce(() => [
198+
logger.getProblems.mockImplementation(() => [
199199
{
200200
level: WARN,
201201
msg: 'meh',

lib/workers/global/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
import { CONFIG_PRESETS_INVALID } from '../../constants/error-messages';
1717
import { pkg } from '../../expose.cjs';
1818
import { instrument } from '../../instrumentation';
19-
import { exportStats } from '../../instrumentation/reporting';
19+
import { exportStats, finalizeReport } from '../../instrumentation/reporting';
2020
import { getProblems, logger, setMeta } from '../../logger';
2121
import { setGlobalLogLevelRemaps } from '../../logger/remap';
2222
import * as hostRules from '../../util/host-rules';
@@ -216,6 +216,7 @@ export async function start(): Promise<number> {
216216
);
217217
}
218218

219+
finalizeReport();
219220
await exportStats(config);
220221
} catch (err) /* istanbul ignore next */ {
221222
if (err.message.startsWith('Init: ')) {

0 commit comments

Comments
 (0)