From 9f837f226d5718a5fd7445d0367c1d3e30488c03 Mon Sep 17 00:00:00 2001 From: Gabriel-Ladzaretti <97394622+Gabriel-Ladzaretti@users.noreply.github.com> Date: Fri, 12 Aug 2022 11:19:31 +0300 Subject: [PATCH] feat(lib/logger): add base branch metadata for multi-base repositories (#17027) --- lib/logger/pretty-stdout.ts | 1 + .../repository/process/extract-update.ts | 5 +++- lib/workers/repository/process/index.spec.ts | 5 ++++ lib/workers/repository/process/index.ts | 5 +++- lib/workers/repository/process/write.spec.ts | 26 ++++++++++++++++--- lib/workers/repository/process/write.ts | 9 +++++-- 6 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/logger/pretty-stdout.ts b/lib/logger/pretty-stdout.ts index 2259b88017..6cb1282494 100644 --- a/lib/logger/pretty-stdout.ts +++ b/lib/logger/pretty-stdout.ts @@ -19,6 +19,7 @@ const bunyanFields = [ ]; const metaFields = [ 'repository', + 'baseBranch', 'packageFile', 'depType', 'dependency', diff --git a/lib/workers/repository/process/extract-update.ts b/lib/workers/repository/process/extract-update.ts index 98eefd5de0..e151e5b7ae 100644 --- a/lib/workers/repository/process/extract-update.ts +++ b/lib/workers/repository/process/extract-update.ts @@ -129,7 +129,10 @@ export async function lookup( config, packageFiles ); - logger.debug({ config: packageFiles }, 'packageFiles with updates'); + logger.debug( + { baseBranch: config.baseBranch, config: packageFiles }, + 'packageFiles with updates' + ); sortBranches(branches); return { branches, branchList, packageFiles }; } diff --git a/lib/workers/repository/process/index.spec.ts b/lib/workers/repository/process/index.spec.ts index 143837f27f..0e45a6058e 100644 --- a/lib/workers/repository/process/index.spec.ts +++ b/lib/workers/repository/process/index.spec.ts @@ -7,6 +7,7 @@ import { } from '../../../../test/util'; import { GlobalConfig } from '../../../config/global'; import { CONFIG_VALIDATION } from '../../../constants/error-messages'; +import { addMeta } from '../../../logger'; import { getCache } from '../../../util/cache/repository'; import * as _extractUpdate from './extract-update'; import { lookup } from './extract-update'; @@ -83,6 +84,8 @@ describe('workers/repository/process/index', () => { undefined, 'dev' ); + expect(addMeta).toHaveBeenNthCalledWith(1, { baseBranch: 'master' }); + expect(addMeta).toHaveBeenNthCalledWith(2, { baseBranch: 'dev' }); }); it('handles config name mismatch between baseBranches if useBaseBranchConfig specified', async () => { @@ -101,6 +104,8 @@ describe('workers/repository/process/index', () => { await expect(extractDependencies(config)).rejects.toThrow( CONFIG_VALIDATION ); + expect(addMeta).toHaveBeenNthCalledWith(1, { baseBranch: 'master' }); + expect(addMeta).toHaveBeenNthCalledWith(2, { baseBranch: 'dev' }); }); it('processes baseBranches dryRun extract', async () => { diff --git a/lib/workers/repository/process/index.ts b/lib/workers/repository/process/index.ts index 407f41014c..02c9266e60 100644 --- a/lib/workers/repository/process/index.ts +++ b/lib/workers/repository/process/index.ts @@ -3,7 +3,7 @@ import { mergeChildConfig } from '../../../config'; import { GlobalConfig } from '../../../config/global'; import type { RenovateConfig } from '../../../config/types'; import { CONFIG_VALIDATION } from '../../../constants/error-messages'; -import { logger } from '../../../logger'; +import { addMeta, logger, removeMeta } from '../../../logger'; import type { PackageFile } from '../../../modules/manager/types'; import { platform } from '../../../modules/platform'; import { getCache } from '../../../util/cache/repository'; @@ -84,6 +84,7 @@ export async function extractDependencies( logger.debug({ baseBranches: config.baseBranches }, 'baseBranches'); const extracted: Record<string, Record<string, PackageFile[]>> = {}; for (const baseBranch of config.baseBranches) { + addMeta({ baseBranch }); if (branchExists(baseBranch)) { const baseBranchConfig = await getBaseBranchConfig(baseBranch, config); extracted[baseBranch] = await extract(baseBranchConfig); @@ -94,6 +95,7 @@ export async function extractDependencies( addSplit('extract'); for (const baseBranch of config.baseBranches) { if (branchExists(baseBranch)) { + addMeta({ baseBranch }); const baseBranchConfig = await getBaseBranchConfig(baseBranch, config); const packageFiles = extracted[baseBranch]; const baseBranchRes = await lookup(baseBranchConfig, packageFiles); @@ -102,6 +104,7 @@ export async function extractDependencies( res.packageFiles = res.packageFiles || baseBranchRes?.packageFiles; // Use the first branch } } + removeMeta(['baseBranch']); } else { logger.debug('No baseBranches'); const packageFiles = await extract(config); diff --git a/lib/workers/repository/process/write.spec.ts b/lib/workers/repository/process/write.spec.ts index bf02f04a41..b94cb734ba 100644 --- a/lib/workers/repository/process/write.spec.ts +++ b/lib/workers/repository/process/write.spec.ts @@ -1,5 +1,12 @@ -import { RenovateConfig, getConfig, git, mocked } from '../../../../test/util'; +import { + RenovateConfig, + getConfig, + git, + mocked, + partial, +} from '../../../../test/util'; import { GlobalConfig } from '../../../config/global'; +import { addMeta } from '../../../logger'; import { Limit, isLimitReached } from '../../global/limits'; import { BranchConfig, BranchResult } from '../../types'; import * as _branchWorker from '../update/branch'; @@ -57,7 +64,11 @@ describe('workers/repository/process/write', () => { }); it('increments branch counter', async () => { - const branches: BranchConfig[] = [{}] as never; + const branchName = 'branchName'; + const branches: BranchConfig[] = [ + partial<BranchConfig>({ baseBranch: 'main', branchName }), + partial<BranchConfig>({ baseBranch: 'dev', branchName }), + ] as never; branchWorker.processBranch.mockResolvedValueOnce({ branchExists: true, result: BranchResult.PrCreated, @@ -67,8 +78,17 @@ describe('workers/repository/process/write', () => { limits.getBranchesRemaining.mockResolvedValueOnce(1); expect(isLimitReached(Limit.Branches)).toBeFalse(); GlobalConfig.set({ dryRun: 'full' }); - await writeUpdates({ config }, branches); + config.baseBranches = ['main', 'dev']; + await writeUpdates(config, branches); expect(isLimitReached(Limit.Branches)).toBeTrue(); + expect(addMeta).toHaveBeenCalledWith({ + baseBranch: 'main', + branch: branchName, + }); + expect(addMeta).toHaveBeenCalledWith({ + baseBranch: 'dev', + branch: branchName, + }); }); }); }); diff --git a/lib/workers/repository/process/write.ts b/lib/workers/repository/process/write.ts index 4f56afd0b8..dbd830c10a 100644 --- a/lib/workers/repository/process/write.ts +++ b/lib/workers/repository/process/write.ts @@ -33,7 +33,12 @@ export async function writeUpdates( setMaxLimit(Limit.Branches, branchesRemaining); for (const branch of branches) { - addMeta({ branch: branch.branchName }); + const { baseBranch, branchName } = branch; + const meta: Record<string, string> = { branch: branchName }; + if (config.baseBranches?.length && baseBranch) { + meta['baseBranch'] = baseBranch; + } + addMeta(meta); const branchExisted = branchExists(branch.branchName); const res = await processBranch(branch); branch.prBlockedBy = res?.prBlockedBy; @@ -50,6 +55,6 @@ export async function writeUpdates( incLimitedValue(Limit.Branches); } } - removeMeta(['branch']); + removeMeta(['branch', 'baseBranch']); return 'done'; } -- GitLab