renovate/lib/workers/branch/get-updated.ts
Sourav Das 982d5e9d4d refactor(function): Update Artifact Function (#5139)
1. Create interface UpdateArtifact
2. Update function call
3. Update Test
2020-01-17 12:18:34 +01:00

146 lines
5.1 KiB
TypeScript

import is from '@sindresorhus/is';
import { FileData, platform } from '../../platform';
import { logger } from '../../logger';
import { get } from '../../manager';
import { RenovateConfig } from '../../config';
import { UpdateArtifactsConfig, ArtifactError } from '../../manager/common';
import { WORKER_FILE_UPDATE_FAILED } from '../../constants/error-messages';
import { DATASOURCE_GIT_SUBMODULES } from '../../constants/data-binary-source';
export interface PackageFilesResult {
artifactErrors: ArtifactError[];
parentBranch?: string;
updatedPackageFiles: FileData[];
updatedArtifacts: FileData[];
}
export async function getUpdatedPackageFiles(
config: RenovateConfig & UpdateArtifactsConfig
): Promise<PackageFilesResult> {
logger.debug('manager.getUpdatedPackageFiles()');
logger.trace({ config });
const updatedFileContents: Record<string, string> = {};
const packageFileManagers: Record<string, string> = {};
const packageFileUpdatedDeps: Record<string, string[]> = {};
const lockFileMaintenanceFiles = [];
for (const upgrade of config.upgrades) {
const { manager, packageFile, depName } = upgrade;
packageFileManagers[packageFile] = manager;
packageFileUpdatedDeps[packageFile] =
packageFileUpdatedDeps[packageFile] || [];
packageFileUpdatedDeps[packageFile].push(depName);
if (upgrade.updateType === 'lockFileMaintenance') {
lockFileMaintenanceFiles.push(packageFile);
} else {
const existingContent =
updatedFileContents[packageFile] ||
(await platform.getFile(packageFile, config.parentBranch));
// istanbul ignore if
if (config.parentBranch && !existingContent) {
logger.info('Rebasing branch after file not found');
return getUpdatedPackageFiles({
...config,
parentBranch: undefined,
});
}
let newContent = existingContent;
const updateDependency = get(manager, 'updateDependency');
newContent = await updateDependency(existingContent, upgrade);
if (!newContent) {
if (config.parentBranch) {
logger.info('Rebasing branch after error updating content');
return getUpdatedPackageFiles({
...config,
parentBranch: undefined,
});
}
logger.debug(
{ existingContent, config: upgrade },
'Error updating file'
);
throw new Error(WORKER_FILE_UPDATE_FAILED);
}
if (newContent !== existingContent) {
if (config.parentBranch) {
// This ensure it's always 1 commit from the bot
logger.info('Need to update package file so will rebase first');
return getUpdatedPackageFiles({
...config,
parentBranch: undefined,
});
}
logger.debug('Updating packageFile content');
updatedFileContents[packageFile] = newContent;
}
if (
newContent === existingContent &&
upgrade.datasource === DATASOURCE_GIT_SUBMODULES
) {
updatedFileContents[packageFile] = newContent;
}
}
}
const updatedPackageFiles = Object.keys(updatedFileContents).map(name => ({
name,
contents: updatedFileContents[name],
}));
const updatedArtifacts: FileData[] = [];
const artifactErrors: ArtifactError[] = [];
for (const packageFile of updatedPackageFiles) {
const manager = packageFileManagers[packageFile.name];
const updatedDeps = packageFileUpdatedDeps[packageFile.name];
const updateArtifacts = get(manager, 'updateArtifacts');
if (updateArtifacts) {
const results = await updateArtifacts({
packageFileName: packageFile.name,
updatedDeps,
newPackageFileContent: packageFile.contents,
config,
});
if (is.nonEmptyArray(results)) {
for (const res of results) {
const { file, artifactError } = res;
if (file) {
updatedArtifacts.push(file);
} else if (artifactError) {
artifactErrors.push(artifactError);
}
}
}
}
}
if (!config.parentBranch) {
// Only perform lock file maintenance if it's a fresh commit
for (const packageFile of lockFileMaintenanceFiles) {
const manager = packageFileManagers[packageFile];
const updateArtifacts = get(manager, 'updateArtifacts');
if (updateArtifacts) {
const packageFileContents =
updatedFileContents[packageFile] ||
(await platform.getFile(packageFile, config.parentBranch));
const results = await updateArtifacts({
packageFileName: packageFile,
updatedDeps: [],
newPackageFileContent: packageFileContents,
config,
});
if (is.nonEmptyArray(results)) {
for (const res of results) {
const { file, artifactError } = res;
if (file) {
updatedArtifacts.push(file);
} else if (artifactError) {
artifactErrors.push(artifactError);
}
}
}
}
}
}
return {
parentBranch: config.parentBranch, // Need to overwrite original config
updatedPackageFiles,
updatedArtifacts,
artifactErrors,
};
}