feat(npm): check if branch already updated (#13185)

This commit is contained in:
Rhys Arkins 2021-12-18 17:09:49 +01:00 committed by GitHub
parent b84ce2ad55
commit 1f1c86a4f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 17 deletions

View file

@ -109,6 +109,13 @@ describe('manager/npm/update/locked-dependency/index', () => {
const packageLock = JSON.parse(res.files['package-lock.json']); const packageLock = JSON.parse(res.files['package-lock.json']);
expect(packageLock.dependencies.express.version).toBe('4.1.0'); expect(packageLock.dependencies.express.version).toBe('4.1.0');
}); });
it('returns if already remediated', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.10';
config.newVersion = '1.2.11';
const res = await updateLockedDependency(config);
expect(res.status).toBe('already-updated');
});
it('remediates mime', async () => { it('remediates mime', async () => {
config.depName = 'mime'; config.depName = 'mime';
config.currentVersion = '1.2.11'; config.currentVersion = '1.2.11';

View file

@ -47,18 +47,32 @@ export async function updateLockedDependency(
currentVersion currentVersion
); );
if (!lockedDeps.length) { if (!lockedDeps.length) {
logger.debug( const newLockedDeps = getLockedDependencies(
`${depName}@${currentVersion} not found in ${lockFile} - no work to do` packageLockJson,
depName,
newVersion
); );
let status: 'update-failed' | 'already-updated';
if (newLockedDeps.length) {
logger.debug(
`${depName}@${currentVersion} not found in ${lockFile} but ${depName}@${newVersion} was - looks like it's already updated`
);
status = 'already-updated';
} else {
logger.debug(
`${depName}@${currentVersion} not found in ${lockFile} - cannot update`
);
status = 'update-failed';
}
// Don't return {} if we're a parent update or else the whole update will fail // Don't return {} if we're a parent update or else the whole update will fail
// istanbul ignore if: too hard to replicate // istanbul ignore if: too hard to replicate
if (isParentUpdate) { if (isParentUpdate) {
const res: UpdateLockedResult = { status: 'update-failed', files: {} }; const res: UpdateLockedResult = { status, files: {} };
res.files[packageFile] = packageFileContent; res.files[packageFile] = packageFileContent;
res.files[lockFile] = lockFileContent; res.files[lockFile] = lockFileContent;
return res; return res;
} }
return { status: 'update-failed' }; return { status };
} }
logger.debug( logger.debug(
`Found matching dependencies with length ${lockedDeps.length}` `Found matching dependencies with length ${lockedDeps.length}`

View file

@ -231,7 +231,7 @@ export interface UpdateLockedConfig {
} }
export interface UpdateLockedResult { export interface UpdateLockedResult {
status: 'updated' | 'update-failed'; status: 'updated' | 'already-updated' | 'update-failed';
files?: Record<string, string>; files?: Record<string, string>;
} }

View file

@ -74,23 +74,22 @@ export async function getUpdatedPackageFiles(
}); });
} }
const updateLockedDependency = get(manager, 'updateLockedDependency'); const updateLockedDependency = get(manager, 'updateLockedDependency');
const { files } = await updateLockedDependency({ const { status, files } = await updateLockedDependency({
...upgrade, ...upgrade,
packageFileContent, packageFileContent,
lockFileContent, lockFileContent,
}); });
if (reuseExistingBranch && status !== 'already-updated') {
logger.debug(
{ lockFile, depName, status },
'Need to retry branch as it is not already up-to-date'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
if (files) { if (files) {
if (reuseExistingBranch) {
// This ensure it's always 1 commit from the bot
logger.debug(
{ lockFile, depName },
'Need to update file(s) so will rebase first'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
updatedFileContents = { ...updatedFileContents, ...files }; updatedFileContents = { ...updatedFileContents, ...files };
} }
} else { } else {