feat(logs): log end of repository run branch summary (#17047)

This commit is contained in:
Gabriel-Ladzaretti 2022-10-27 20:29:01 +03:00 committed by GitHub
parent c981d2f8dd
commit d48ede78f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 184 additions and 4 deletions

View file

@ -6,7 +6,10 @@ import { clearRenovateRefs } from '../../../util/git';
import { configMigration } from '../config-migration'; import { configMigration } from '../config-migration';
import { PackageFiles } from '../package-files'; import { PackageFiles } from '../package-files';
import { pruneStaleBranches } from './prune'; import { pruneStaleBranches } from './prune';
import { runRenovateRepoStats } from './repository-statistics'; import {
runBranchSummary,
runRenovateRepoStats,
} from './repository-statistics';
// istanbul ignore next // istanbul ignore next
export async function finaliseRepo( export async function finaliseRepo(
@ -33,5 +36,6 @@ export async function finaliseRepo(
logger.debug('Repo is activated'); logger.debug('Repo is activated');
config.repoIsActivated = true; config.repoIsActivated = true;
} }
runBranchSummary();
runRenovateRepoStats(config, prList); runRenovateRepoStats(config, prList);
} }

View file

@ -3,10 +3,20 @@ import {
RenovateConfig, RenovateConfig,
getConfig, getConfig,
mockedFunction, mockedFunction,
partial,
} from '../../../../test/util'; } from '../../../../test/util';
import { logger } from '../../../logger'; import { logger } from '../../../logger';
import { platform } from '../../../modules/platform'; import { platform } from '../../../modules/platform';
import { runRenovateRepoStats } from './repository-statistics'; import * as cache from '../../../util/cache/repository';
import type {
BaseBranchCache,
BranchCache,
RepoCacheData,
} from '../../../util/cache/repository/types';
import {
runBranchSummary,
runRenovateRepoStats,
} from './repository-statistics';
jest.mock('../../../modules/platform/github/pr'); jest.mock('../../../modules/platform/github/pr');
jest.mock('../../../util/http/github'); jest.mock('../../../util/http/github');
@ -21,7 +31,6 @@ describe('workers/repository/finalise/repository-statistics', () => {
describe('runRenovateRepoStats', () => { describe('runRenovateRepoStats', () => {
beforeEach(() => { beforeEach(() => {
jest.resetAllMocks();
config = getConfig(); config = getConfig();
mockedFunction(platform.getPrList).mockReturnValue(prJson); mockedFunction(platform.getPrList).mockReturnValue(prJson);
config.repository = 'owner/repo'; config.repository = 'owner/repo';
@ -42,4 +51,96 @@ describe('workers/repository/finalise/repository-statistics', () => {
); );
}); });
}); });
describe('runBranchSummary', () => {
const getCacheSpy = jest.spyOn(cache, 'getCache');
const isCacheModifiedSpy = jest.spyOn(cache, 'isCacheModified');
it('processes cache with baseBranches only', () => {
const sha = '793221454914cdc422e1a8f0ca27b96fe39ff9ad';
const baseCache = partial<BaseBranchCache>({ sha });
const cache = partial<RepoCacheData>({
scan: { main: baseCache, dev: baseCache },
});
getCacheSpy.mockReturnValueOnce(cache);
isCacheModifiedSpy.mockReturnValueOnce(true);
runBranchSummary();
expect(logger.debug).toHaveBeenCalledWith(
{
cacheModified: true,
baseBranches: [
{
branchName: 'main',
sha,
},
{
branchName: 'dev',
sha,
},
],
branches: [],
inactiveBranches: [],
},
`Branch summary`
);
});
it('processes cache with baseBranches and branches', () => {
const sha = '793221454914cdc422e1a8f0ca27b96fe39ff9ad';
const parentSha = '793221454914cdc422e1a8f0ca27b96fe39ff9ad';
const baseBranch = 'base-branch';
const baseCache = partial<BaseBranchCache>({ sha });
const branchCache = partial<BranchCache>({
sha,
parentSha,
baseBranch,
isModified: false,
automerge: false,
});
const expectedMeta = {
automerge: branchCache.automerge,
isModified: branchCache.isModified,
baseBranch,
baseBranchSha: parentSha,
branchSha: sha,
};
const branches: BranchCache[] = [
{ ...branchCache, branchName: 'b1' },
{
...branchCache,
branchName: 'b2',
},
partial<BranchCache>({ branchName: 'b3' }),
];
const cache = partial<RepoCacheData>({
scan: { main: baseCache, dev: baseCache },
branches,
});
getCacheSpy.mockReturnValueOnce(cache);
isCacheModifiedSpy.mockReturnValueOnce(false);
runBranchSummary();
expect(logger.debug).toHaveBeenCalledWith(
{
cacheModified: false,
baseBranches: [
{
branchName: 'main',
sha,
},
{
branchName: 'dev',
sha,
},
],
branches: [
{ ...expectedMeta, branchName: 'b1' },
{ ...expectedMeta, branchName: 'b2' },
],
inactiveBranches: ['b3'],
},
`Branch summary`
);
});
});
}); });

View file

@ -2,6 +2,13 @@ import type { RenovateConfig } from '../../../config/types';
import { logger } from '../../../logger'; import { logger } from '../../../logger';
import type { Pr } from '../../../modules/platform'; import type { Pr } from '../../../modules/platform';
import { PrState } from '../../../types'; import { PrState } from '../../../types';
import { getCache, isCacheModified } from '../../../util/cache/repository';
import type { BranchCache } from '../../../util/cache/repository/types';
import type {
BaseBranchMetadata,
BranchMetadata,
BranchSummary,
} from '../../types';
export function runRenovateRepoStats( export function runRenovateRepoStats(
config: RenovateConfig, config: RenovateConfig,
@ -33,3 +40,50 @@ export function runRenovateRepoStats(
} }
logger.debug({ stats: prStats }, `Renovate repository PR statistics`); logger.debug({ stats: prStats }, `Renovate repository PR statistics`);
} }
function branchCacheToMetadata({
branchName,
sha: branchSha,
baseBranch,
parentSha: baseBranchSha,
automerge,
isModified,
}: BranchCache): BranchMetadata {
return {
branchName,
branchSha,
baseBranch,
baseBranchSha,
automerge,
isModified,
};
}
export function runBranchSummary(): void {
const { scan, branches } = getCache();
const baseMetadata: BaseBranchMetadata[] = [];
for (const [branchName, cached] of Object.entries(scan ?? {})) {
baseMetadata.push({ branchName, sha: cached.sha });
}
const branchMetadata: BranchMetadata[] = [];
const inactiveBranches: string[] = [];
for (const branch of branches ?? []) {
if (branch.sha) {
branchMetadata.push(branchCacheToMetadata(branch));
} else {
inactiveBranches.push(branch.branchName);
}
}
const res: BranchSummary = {
cacheModified: isCacheModified(),
baseBranches: baseMetadata,
branches: branchMetadata,
inactiveBranches,
};
logger.debug(res, 'Branch summary');
}

View file

@ -12,7 +12,7 @@ export class PackageFiles {
): void { ): void {
logger.debug( logger.debug(
{ baseBranch }, { baseBranch },
`PackageFiles.add() - Package file saved for branch` `PackageFiles.add() - Package file saved for base branch`
); );
this.data.set(baseBranch, packageFiles); this.data.set(baseBranch, packageFiles);
} }

View file

@ -130,6 +130,27 @@ export interface BranchConfig
skipBranchUpdate?: boolean; skipBranchUpdate?: boolean;
} }
export interface BranchMetadata {
branchName: string;
branchSha: string | null;
baseBranch: string | undefined;
baseBranchSha: string | null | undefined;
automerge: boolean;
isModified: boolean | undefined;
}
export interface BaseBranchMetadata {
branchName: string;
sha: string;
}
export interface BranchSummary {
cacheModified: boolean | undefined;
baseBranches: BaseBranchMetadata[];
branches: BranchMetadata[];
inactiveBranches: string[];
}
export interface WorkerExtractConfig extends ExtractConfig { export interface WorkerExtractConfig extends ExtractConfig {
manager: string; manager: string;
fileList: string[]; fileList: string[];