diff --git a/lib/workers/repository/finalise/index.ts b/lib/workers/repository/finalise/index.ts index 0c8f93dcf72191bd7eb0a132fff716a426d39498..73871295832a7261c4ca1c1917115a3d8a356979 100644 --- a/lib/workers/repository/finalise/index.ts +++ b/lib/workers/repository/finalise/index.ts @@ -6,7 +6,10 @@ import { clearRenovateRefs } from '../../../util/git'; import { configMigration } from '../config-migration'; import { PackageFiles } from '../package-files'; import { pruneStaleBranches } from './prune'; -import { runRenovateRepoStats } from './repository-statistics'; +import { + runBranchSummary, + runRenovateRepoStats, +} from './repository-statistics'; // istanbul ignore next export async function finaliseRepo( @@ -33,5 +36,6 @@ export async function finaliseRepo( logger.debug('Repo is activated'); config.repoIsActivated = true; } + runBranchSummary(); runRenovateRepoStats(config, prList); } diff --git a/lib/workers/repository/finalise/repository-statistics.spec.ts b/lib/workers/repository/finalise/repository-statistics.spec.ts index d15cecbf893733b6030458a8bf80dbeb7434e2fb..acf400b75f5d91e1f3a44b1d65a4e18bff8f77b9 100644 --- a/lib/workers/repository/finalise/repository-statistics.spec.ts +++ b/lib/workers/repository/finalise/repository-statistics.spec.ts @@ -3,10 +3,20 @@ import { RenovateConfig, getConfig, mockedFunction, + partial, } from '../../../../test/util'; import { logger } from '../../../logger'; 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('../../../util/http/github'); @@ -21,7 +31,6 @@ describe('workers/repository/finalise/repository-statistics', () => { describe('runRenovateRepoStats', () => { beforeEach(() => { - jest.resetAllMocks(); config = getConfig(); mockedFunction(platform.getPrList).mockReturnValue(prJson); 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` + ); + }); + }); }); diff --git a/lib/workers/repository/finalise/repository-statistics.ts b/lib/workers/repository/finalise/repository-statistics.ts index 90277b5e52936909c22d73a6207da7b580d12c70..61fe51ed1605a78ef0ba887e3a090c286d0ac010 100644 --- a/lib/workers/repository/finalise/repository-statistics.ts +++ b/lib/workers/repository/finalise/repository-statistics.ts @@ -2,6 +2,13 @@ import type { RenovateConfig } from '../../../config/types'; import { logger } from '../../../logger'; import type { Pr } from '../../../modules/platform'; 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( config: RenovateConfig, @@ -33,3 +40,50 @@ export function runRenovateRepoStats( } 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'); +} diff --git a/lib/workers/repository/package-files.ts b/lib/workers/repository/package-files.ts index 0dc26a8c7db3d62c1ad70231f3e98f9f4af1cc28..59743a85771666e7376d15ced1e55a368de7466b 100644 --- a/lib/workers/repository/package-files.ts +++ b/lib/workers/repository/package-files.ts @@ -12,7 +12,7 @@ export class PackageFiles { ): void { logger.debug( { baseBranch }, - `PackageFiles.add() - Package file saved for branch` + `PackageFiles.add() - Package file saved for base branch` ); this.data.set(baseBranch, packageFiles); } diff --git a/lib/workers/types.ts b/lib/workers/types.ts index afc1d72b11d39df6b62dac9cae32f02d078bffa9..5d79ebf2eaa8966f2fee1479a7d7476753e056bd 100644 --- a/lib/workers/types.ts +++ b/lib/workers/types.ts @@ -130,6 +130,27 @@ export interface BranchConfig 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 { manager: string; fileList: string[];