Select Git revision
get-updated.ts 9.57 KiB
import is from '@sindresorhus/is';
import { WORKER_FILE_UPDATE_FAILED } from '../../constants/error-messages';
import { logger } from '../../logger';
import { get } from '../../manager';
import type { ArtifactError, PackageDependency } from '../../manager/types';
import { File, getFile } from '../../util/git';
import type { BranchConfig } from '../types';
import { doAutoReplace } from './auto-replace';
export interface PackageFilesResult {
artifactErrors: ArtifactError[];
reuseExistingBranch?: boolean;
updatedPackageFiles: File[];
updatedArtifacts: File[];
}
export async function getUpdatedPackageFiles(
config: BranchConfig
): Promise<PackageFilesResult> {
logger.trace({ config });
const { reuseExistingBranch } = config;
logger.debug(
`manager.getUpdatedPackageFiles() reuseExistinbranch=${reuseExistingBranch}`
);
let updatedFileContents: Record<string, string> = {};
const nonUpdatedFileContents: Record<string, string> = {};
const packageFileManagers: Record<string, string> = {};
const packageFileUpdatedDeps: Record<string, PackageDependency[]> = {};
const lockFileMaintenanceFiles = [];
for (const upgrade of config.upgrades) {
const { manager, packageFile, lockFile, depName } = upgrade;
packageFileManagers[packageFile] = manager;
packageFileUpdatedDeps[packageFile] =
packageFileUpdatedDeps[packageFile] || [];
packageFileUpdatedDeps[packageFile].push({ ...upgrade });
let packageFileContent = updatedFileContents[packageFile];
if (!packageFileContent) {
packageFileContent = await getFile(
packageFile,
reuseExistingBranch ? config.branchName : config.baseBranch
);
}
// istanbul ignore if
if (reuseExistingBranch && !packageFileContent) {
logger.debug(
{ packageFile, depName },
'Rebasing branch after file not found'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
if (upgrade.updateType === 'lockFileMaintenance') {
lockFileMaintenanceFiles.push(packageFile);
} else if (upgrade.isRemediation) {
let lockFileContent = updatedFileContents[lockFile];
if (!lockFileContent) {
lockFileContent = await getFile(
lockFile,
reuseExistingBranch ? config.branchName : config.baseBranch
);
}
// istanbul ignore if: to hard to test
if (reuseExistingBranch && !lockFileContent) {
logger.debug(
{ lockFile, depName },
'Rebasing branch after lock file not found'
);
return getUpdatedPackageFiles({