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']);
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 () => {
config.depName = 'mime';
config.currentVersion = '1.2.11';

View file

@ -47,18 +47,32 @@ export async function updateLockedDependency(
currentVersion
);
if (!lockedDeps.length) {
logger.debug(
`${depName}@${currentVersion} not found in ${lockFile} - no work to do`
const newLockedDeps = getLockedDependencies(
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
// istanbul ignore if: too hard to replicate
if (isParentUpdate) {
const res: UpdateLockedResult = { status: 'update-failed', files: {} };
const res: UpdateLockedResult = { status, files: {} };
res.files[packageFile] = packageFileContent;
res.files[lockFile] = lockFileContent;
return res;
}
return { status: 'update-failed' };
return { status };
}
logger.debug(
`Found matching dependencies with length ${lockedDeps.length}`

View file

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

View file

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