diff --git a/lib/workers/repository/update/branch/__snapshots__/get-updated.spec.ts.snap b/lib/workers/repository/update/branch/__snapshots__/get-updated.spec.ts.snap index 96c1cbea0768ab1de6e813e1186c88838a1f9dcb..eecb58e2d990cf2d90a07d8e87c7a9b9b5e104b3 100644 --- a/lib/workers/repository/update/branch/__snapshots__/get-updated.spec.ts.snap +++ b/lib/workers/repository/update/branch/__snapshots__/get-updated.spec.ts.snap @@ -142,7 +142,7 @@ exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() h "updatedPackageFiles": [ { "contents": "some new content", - "path": "undefined", + "path": "composer.json", "type": "addition", }, ], diff --git a/lib/workers/repository/update/branch/get-updated.spec.ts b/lib/workers/repository/update/branch/get-updated.spec.ts index 64a614e517d08660321f402741014c017711b0bf..cdb22fd082f4065380df8017f1805c15cdb4688f 100644 --- a/lib/workers/repository/update/branch/get-updated.spec.ts +++ b/lib/workers/repository/update/branch/get-updated.spec.ts @@ -463,6 +463,7 @@ describe('workers/repository/update/branch/get-updated', () => { config.reuseExistingBranch = true; config.upgrades.push({ manager: 'composer', + packageFile: 'composer.json', branchName: '', }); autoReplace.doAutoReplace.mockResolvedValueOnce('some new content'); diff --git a/lib/workers/repository/update/branch/get-updated.ts b/lib/workers/repository/update/branch/get-updated.ts index d0f6a57f3e5717b1ecff90bea79d2b216fa6e573..0b226224fd9ce0861ab0e04e4e73e6a31973db43 100644 --- a/lib/workers/repository/update/branch/get-updated.ts +++ b/lib/workers/repository/update/branch/get-updated.ts @@ -40,6 +40,38 @@ async function getFileContent( return fileContent; } +function hasAny(set: Set<string>, targets: Iterable<string>): boolean { + for (const target of targets) { + if (set.has(target)) { + return true; + } + } + return false; +} + +type FilePath = Pick<FileChange, 'path'>; + +function getManagersForPackageFiles<T extends FilePath>( + packageFiles: T[], + managerPackageFiles: Record<string, Set<string>>, +): Set<string> { + const packageFileNames = packageFiles.map((packageFile) => packageFile.path); + return new Set( + Object.keys(managerPackageFiles).filter((manager) => + hasAny(managerPackageFiles[manager], packageFileNames), + ), + ); +} + +function getPackageFilesForManager<T extends FilePath>( + packageFiles: T[], + managerPackageFiles: Set<string>, +): T[] { + return packageFiles.filter((packageFile) => + managerPackageFiles.has(packageFile.path), + ); +} + export async function getUpdatedPackageFiles( config: BranchConfig, ): Promise<PackageFilesResult> { @@ -50,9 +82,9 @@ export async function getUpdatedPackageFiles( ); let updatedFileContents: Record<string, string> = {}; const nonUpdatedFileContents: Record<string, string> = {}; - const packageFileManagers: Record<string, Set<string>> = {}; + const managerPackageFiles: Record<string, Set<string>> = {}; const packageFileUpdatedDeps: Record<string, PackageDependency[]> = {}; - const lockFileMaintenanceFiles = []; + const lockFileMaintenanceFiles: string[] = []; let firstUpdate = true; for (const upgrade of config.upgrades) { const manager = upgrade.manager!; @@ -62,8 +94,8 @@ export async function getUpdatedPackageFiles( const newVersion = upgrade.newVersion!; const currentVersion = upgrade.currentVersion!; const updateLockedDependency = get(manager, 'updateLockedDependency')!; - packageFileManagers[packageFile] ??= new Set<string>(); - packageFileManagers[packageFile].add(manager); + managerPackageFiles[manager] ??= new Set<string>(); + managerPackageFiles[manager].add(packageFile); packageFileUpdatedDeps[packageFile] ??= []; packageFileUpdatedDeps[packageFile].push({ ...upgrade }); const packageFileContent = await getFileContent( @@ -291,15 +323,19 @@ export async function getUpdatedPackageFiles( const updatedArtifacts: FileChange[] = []; const artifactErrors: ArtifactError[] = []; const artifactNotices: ArtifactNotice[] = []; - // istanbul ignore if if (is.nonEmptyArray(updatedPackageFiles)) { logger.debug('updateArtifacts for updatedPackageFiles'); - } - for (const packageFile of updatedPackageFiles) { - const updatedDeps = packageFileUpdatedDeps[packageFile.path]; - const managers = packageFileManagers[packageFile.path]; - if (is.nonEmptySet(managers)) { - for (const manager of managers) { + const updatedPackageFileManagers = getManagersForPackageFiles( + updatedPackageFiles, + managerPackageFiles, + ); + for (const manager of updatedPackageFileManagers) { + const packageFilesForManager = getPackageFilesForManager( + updatedPackageFiles, + managerPackageFiles[manager], + ); + for (const packageFile of packageFilesForManager) { + const updatedDeps = packageFileUpdatedDeps[packageFile.path]; const results = await managerUpdateArtifacts(manager, { packageFileName: packageFile.path, updatedDeps, @@ -327,15 +363,19 @@ export async function getUpdatedPackageFiles( path: name, contents: nonUpdatedFileContents[name], })); - // istanbul ignore if if (is.nonEmptyArray(nonUpdatedPackageFiles)) { logger.debug('updateArtifacts for nonUpdatedPackageFiles'); - } - for (const packageFile of nonUpdatedPackageFiles) { - const updatedDeps = packageFileUpdatedDeps[packageFile.path]; - const managers = packageFileManagers[packageFile.path]; - if (is.nonEmptySet(managers)) { - for (const manager of managers) { + const nonUpdatedPackageFileManagers = getManagersForPackageFiles( + nonUpdatedPackageFiles, + managerPackageFiles, + ); + for (const manager of nonUpdatedPackageFileManagers) { + const packageFilesForManager = getPackageFilesForManager( + nonUpdatedPackageFiles, + managerPackageFiles[manager], + ); + for (const packageFile of packageFilesForManager) { + const updatedDeps = packageFileUpdatedDeps[packageFile.path]; const results = await managerUpdateArtifacts(manager, { packageFileName: packageFile.path, updatedDeps, @@ -360,26 +400,34 @@ export async function getUpdatedPackageFiles( } } if (!reuseExistingBranch) { + const lockFileMaintenancePackageFiles: FilePath[] = + lockFileMaintenanceFiles.map((name) => ({ + path: name, + })); // Only perform lock file maintenance if it's a fresh commit - // istanbul ignore if if (is.nonEmptyArray(lockFileMaintenanceFiles)) { logger.debug('updateArtifacts for lockFileMaintenanceFiles'); - } - for (const packageFileName of lockFileMaintenanceFiles) { - const managers = packageFileManagers[packageFileName]; - if (is.nonEmptySet(managers)) { - for (const manager of managers) { + const lockFileMaintenanceManagers = getManagersForPackageFiles( + lockFileMaintenancePackageFiles, + managerPackageFiles, + ); + for (const manager of lockFileMaintenanceManagers) { + const packageFilesForManager = getPackageFilesForManager( + lockFileMaintenancePackageFiles, + managerPackageFiles[manager], + ); + for (const packageFile of packageFilesForManager) { const contents = - updatedFileContents[packageFileName] || - (await getFile(packageFileName, config.baseBranch)); + updatedFileContents[packageFile.path] || + (await getFile(packageFile.path, config.baseBranch)); const results = await managerUpdateArtifacts(manager, { - packageFileName, + packageFileName: packageFile.path, updatedDeps: [], newPackageFileContent: contents!, config: patchConfigForArtifactsUpdate( config, manager, - packageFileName, + packageFile.path, ), }); processUpdateArtifactResults(