diff --git a/lib/workers/repository/errors-warnings.spec.ts b/lib/workers/repository/errors-warnings.spec.ts index 6442bb847750250cac471745f7f44a29f51aa2a2..7398e09f3e09c57d9b02a1dab76da2490c9df5e8 100644 --- a/lib/workers/repository/errors-warnings.spec.ts +++ b/lib/workers/repository/errors-warnings.spec.ts @@ -1,6 +1,6 @@ import { RenovateConfig, getConfig } from '../../../test/util'; import type { PackageFile } from '../../modules/manager/types'; -import { getDepWarnings, getErrors, getWarnings } from './errors-warnings'; +import { getDepWarningsPR, getErrors, getWarnings } from './errors-warnings'; describe('workers/repository/errors-warnings', () => { describe('getWarnings()', () => { @@ -31,14 +31,20 @@ describe('workers/repository/errors-warnings', () => { " `); }); + + it('getWarning returns empty string', () => { + config.warnings = []; + const res = getWarnings(config); + expect(res).toBe(''); + }); }); - describe('getDepWarnings()', () => { + describe('getDepWarningsPR()', () => { beforeEach(() => { jest.resetAllMocks(); }); - it('returns warning text', () => { + it('returns pr warning text', () => { const packageFiles: Record<string, PackageFile[]> = { npm: [ { @@ -70,7 +76,8 @@ describe('workers/repository/errors-warnings', () => { }, ], }; - const res = getDepWarnings(packageFiles); + + const res = getDepWarningsPR(packageFiles); expect(res).toMatchInlineSnapshot(` " --- @@ -87,6 +94,12 @@ describe('workers/repository/errors-warnings', () => { " `); }); + + it('PR warning returns empty string', () => { + const packageFiles: Record<string, PackageFile[]> = {}; + const res = getDepWarningsPR(packageFiles); + expect(res).toBe(''); + }); }); describe('getErrors()', () => { @@ -117,5 +130,11 @@ describe('workers/repository/errors-warnings', () => { " `); }); + + it('getError returns empty string', () => { + config.errors = []; + const res = getErrors(config); + expect(res).toBe(''); + }); }); }); diff --git a/lib/workers/repository/errors-warnings.ts b/lib/workers/repository/errors-warnings.ts index 0c2fb0ed6aea09125c6cbf5a98a73ef8ad4c4dc8..ecd0099c16aeb8001cc803e7708774e6989798ce 100644 --- a/lib/workers/repository/errors-warnings.ts +++ b/lib/workers/repository/errors-warnings.ts @@ -3,81 +3,82 @@ import type { RenovateConfig } from '../../config/types'; import { logger } from '../../logger'; import type { PackageFile } from '../../modules/manager/types'; import { emojify } from '../../util/emoji'; +import type { DepWarnings } from '../types'; export function getWarnings(config: RenovateConfig): string { - const warnings = config.warnings!; - if (!warnings.length) { + if (!config.warnings?.length) { return ''; } - let warningText = `\n# Warnings (${warnings.length})\n\n`; + let warningText = `\n# Warnings (${config.warnings.length})\n\n`; warningText += `Please correct - or verify that you can safely ignore - these warnings before you merge this PR.\n\n`; - warnings.forEach((w) => { + for (const w of config.warnings) { warningText += `- \`${w.topic}\`: ${w.message}\n`; - }); + } warningText += '\n---\n'; return warningText; } export function getErrors(config: RenovateConfig): string { - let errorText = ''; - const errors = config.errors!; - if (!errors.length) { + if (!config.errors?.length) { return ''; } - errorText = `\n# Errors (${errors.length})\n\n`; + let errorText = `\n# Errors (${config.errors.length})\n\n`; errorText += `Renovate has found errors that you should fix (in this branch) before finishing this PR.\n\n`; - errors.forEach((e) => { + for (const e of config.errors) { errorText += `- \`${e.topic}\`: ${e.message}\n`; - }); + } errorText += '\n---\n'; return errorText; } -export function getDepWarnings( +function getDepWarnings( packageFiles: Record<string, PackageFile[]> -): string { - let warningText = ''; - try { - const warnings: string[] = []; - const warningFiles: string[] = []; - for (const files of Object.values(packageFiles || {})) { - for (const file of files || []) { - if (file.deps) { - for (const dep of file.deps || []) { - if (dep.warnings?.length) { - const message = dep.warnings[0].message; - if (!warnings.includes(message)) { - warnings.push(message); - } - if (!warningFiles.includes(file.packageFile!)) { - warningFiles.push(file.packageFile!); - } +): DepWarnings { + const warnings: string[] = []; + const warningFiles: string[] = []; + for (const files of Object.values(packageFiles ?? {})) { + for (const file of files ?? []) { + // TODO: remove condition when type is fixed (#7154) + if (file.packageFile) { + for (const dep of file.deps ?? []) { + for (const w of dep.warnings ?? []) { + const message = w.message; + if (!warnings.includes(message)) { + warnings.push(message); + } + if (!warningFiles.includes(file.packageFile)) { + warningFiles.push(file.packageFile); } } } } } - if (!warnings.length) { - return ''; - } - logger.debug( - { warnings, warningFiles }, - 'Found package lookup warnings in onboarding' - ); - warningText = emojify( - `\n---\n\n### :warning: Dependency Lookup Warnings :warning:\n\n` - ); - warningText += `Please correct - or verify that you can safely ignore - these lookup failures before you merge this PR.\n\n`; - warnings.forEach((w) => { - warningText += `- \`${w}\`\n`; - }); - warningText += - '\nFiles affected: ' + - warningFiles.map((f) => '`' + f + '`').join(', ') + - '\n\n'; - } catch (err) { - // istanbul ignore next - logger.error({ err }, 'Error generating dep warnings text'); } + return { warnings, warningFiles }; +} + +export function getDepWarningsPR( + packageFiles: Record<string, PackageFile[]> +): string { + const { warnings, warningFiles } = getDepWarnings(packageFiles); + let warningText = ''; + if (!warnings.length) { + return ''; + } + logger.debug( + { warnings, warningFiles }, + 'Found package lookup warnings in onboarding' + ); + warningText = emojify( + `\n---\n\n### :warning: Dependency Lookup Warnings :warning:\n\n` + ); + warningText += `Please correct - or verify that you can safely ignore - these lookup failures before you merge this PR.\n\n`; + for (const w of warnings) { + warningText += `- \`${w}\`\n`; + } + warningText += + '\nFiles affected: ' + + warningFiles.map((f) => '`' + f + '`').join(', ') + + '\n\n'; return warningText; } diff --git a/lib/workers/repository/onboarding/pr/index.ts b/lib/workers/repository/onboarding/pr/index.ts index d16fb2ab21b04231faa5cc507167a21dd26d3635..8d986dc25471daa42b0152b11b0266e93729c291 100644 --- a/lib/workers/repository/onboarding/pr/index.ts +++ b/lib/workers/repository/onboarding/pr/index.ts @@ -13,7 +13,11 @@ import { } from '../../../../util/git'; import * as template from '../../../../util/template'; import type { BranchConfig } from '../../../types'; -import { getDepWarnings, getErrors, getWarnings } from '../../errors-warnings'; +import { + getDepWarningsPR, + getErrors, + getWarnings, +} from '../../errors-warnings'; import { getPlatformPrOptions } from '../../update/pr'; import { prepareLabels } from '../../update/pr/labels'; import { addParticipants } from '../../update/pr/participants'; @@ -109,7 +113,7 @@ If you need any further assistance then you can also [request help here](${ prBody = prBody.replace('{{CONFIG}}\n', configDesc); prBody = prBody.replace( '{{WARNINGS}}\n', - getWarnings(config) + getDepWarnings(packageFiles!) + getWarnings(config) + getDepWarningsPR(packageFiles!) ); prBody = prBody.replace('{{ERRORS}}\n', getErrors(config)); prBody = prBody.replace('{{BASEBRANCH}}\n', getBaseBranchDesc(config)); diff --git a/lib/workers/types.ts b/lib/workers/types.ts index 1ad103efc033791a1b989ead242823c1cf546b14..b8a8222e502062b9a0dbf93094fab3aeb6f9bcbf 100644 --- a/lib/workers/types.ts +++ b/lib/workers/types.ts @@ -140,3 +140,8 @@ export interface WorkerExtractConfig enabledManagers?: string[]; enabled?: boolean; } + +export interface DepWarnings { + warnings: string[]; + warningFiles: string[]; +}