Skip to content

Commit

Permalink
feat(instrumentation/report): add problems to reports (#28042)
Browse files Browse the repository at this point in the history
Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com>
  • Loading branch information
secustor and HonkingGoose authored Mar 20, 2024
1 parent b6d6695 commit 370927f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
50 changes: 50 additions & 0 deletions lib/instrumentation/reporting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import {
addBranchStats,
addExtractionStats,
exportStats,
finalizeReport,
getReport,
} from './reporting';

jest.mock('../util/fs', () => mockDeep());
jest.mock('../util/s3', () => mockDeep());
jest.mock('../logger', () => mockDeep());

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

const expectedReport = {
problems: [],
repositories: {
'myOrg/myRepo': {
problems: [],
branches: branchInformation,
packageFiles,
},
Expand All @@ -70,6 +74,7 @@ describe('instrumentation/reporting', () => {
});

expect(getReport()).toEqual({
problems: [],
repositories: {},
});
});
Expand Down Expand Up @@ -174,4 +179,49 @@ describe('instrumentation/reporting', () => {
fs.writeSystemFile.mockRejectedValue(null);
await expect(exportStats(config)).toResolve();
});

it('should add problems to report', () => {
const config: RenovateConfig = {
repository: 'myOrg/myRepo',
reportType: 'logging',
};
const expectedReport = {
problems: [
{
level: 30,
msg: 'a root problem',
},
],
repositories: {
'myOrg/myRepo': {
problems: [
{
level: 30,
msg: 'a repo problem',
},
],
branches: branchInformation,
packageFiles,
},
},
};

addBranchStats(config, branchInformation);
addExtractionStats(config, { branchList: [], branches: [], packageFiles });

logger.getProblems.mockReturnValue([
{
repository: 'myOrg/myRepo',
level: 30,
msg: 'a repo problem',
},
{
level: 30,
msg: 'a root problem',
},
]);
finalizeReport();

expect(getReport()).toEqual(expectedReport);
});
});
20 changes: 19 additions & 1 deletion lib/instrumentation/reporting.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { PutObjectCommand, PutObjectCommandInput } from '@aws-sdk/client-s3';
import is from '@sindresorhus/is';
import type { RenovateConfig } from '../config/types';
import { logger } from '../logger';
import { getProblems, logger } from '../logger';
import type { BranchCache } from '../util/cache/repository/types';
import { writeSystemFile } from '../util/fs';
import { getS3Client, parseS3Url } from '../util/s3';
import type { ExtractResult } from '../workers/repository/process/extract-update';
import type { Report } from './types';

const report: Report = {
problems: [],
repositories: {},
};

Expand Down Expand Up @@ -37,6 +38,22 @@ export function addExtractionStats(
extractResult.packageFiles;
}

export function finalizeReport(): void {
const allProblems = structuredClone(getProblems());
for (const problem of allProblems) {
const repository = problem.repository;
delete problem.repository;

// if the problem can be connected to a repository add it their else add to the root list
if (repository) {
coerceRepo(repository);
report.repositories[repository].problems.push(problem);
} else {
report.problems.push(problem);
}
}
}

export async function exportStats(config: RenovateConfig): Promise<void> {
try {
if (is.nullOrUndefined(config.reportType)) {
Expand Down Expand Up @@ -91,6 +108,7 @@ function coerceRepo(repository: string): void {
}

report.repositories[repository] = {
problems: [],
branches: [],
packageFiles: {},
};
Expand Down
3 changes: 3 additions & 0 deletions lib/instrumentation/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Attributes, SpanKind } from '@opentelemetry/api';
import type { BunyanRecord } from '../logger/types';
import type { PackageFile } from '../modules/manager/types';
import type { BranchCache } from '../util/cache/repository/types';

Expand Down Expand Up @@ -28,10 +29,12 @@ export interface SpanParameters {
}

export interface Report {
problems: BunyanRecord[];
repositories: Record<string, RepoReport>;
}

interface RepoReport {
problems: BunyanRecord[];
branches: Partial<BranchCache>[];
packageFiles: Record<string, PackageFile[]>;
}
6 changes: 3 additions & 3 deletions lib/workers/global/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const initPlatform = jest.spyOn(platform, 'initPlatform');

describe('workers/global/index', () => {
beforeEach(() => {
logger.getProblems.mockImplementationOnce(() => []);
logger.getProblems.mockImplementation(() => []);
initPlatform.mockImplementation((input) => Promise.resolve(input));
delete process.env.AWS_SECRET_ACCESS_KEY;
delete process.env.AWS_SESSION_TOKEN;
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('workers/global/index', () => {
repositories: [],
});
logger.getProblems.mockReset();
logger.getProblems.mockImplementationOnce(() => [
logger.getProblems.mockImplementation(() => [
{
level: ERROR,
msg: 'meh',
Expand All @@ -195,7 +195,7 @@ describe('workers/global/index', () => {
repositories: [],
});
logger.getProblems.mockReset();
logger.getProblems.mockImplementationOnce(() => [
logger.getProblems.mockImplementation(() => [
{
level: WARN,
msg: 'meh',
Expand Down
3 changes: 2 additions & 1 deletion lib/workers/global/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
import { CONFIG_PRESETS_INVALID } from '../../constants/error-messages';
import { pkg } from '../../expose.cjs';
import { instrument } from '../../instrumentation';
import { exportStats } from '../../instrumentation/reporting';
import { exportStats, finalizeReport } from '../../instrumentation/reporting';
import { getProblems, logger, setMeta } from '../../logger';
import { setGlobalLogLevelRemaps } from '../../logger/remap';
import * as hostRules from '../../util/host-rules';
Expand Down Expand Up @@ -216,6 +216,7 @@ export async function start(): Promise<number> {
);
}

finalizeReport();
await exportStats(config);
} catch (err) /* istanbul ignore next */ {
if (err.message.startsWith('Init: ')) {
Expand Down

0 comments on commit 370927f

Please sign in to comment.