Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(npm): optimize remediation to detect already updated branches #14084

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/manager/npm/update/locked-dependency/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,37 @@ 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 () => {
it('returns already-updated if already remediated exactly', 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('returns already-updated if already remediated higher', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.9';
config.newVersion = '1.2.10';
config.allowHigherOrRemoved = true;
const res = await updateLockedDependency(config);
expect(res.status).toBe('already-updated');
});
it('returns already-updated if not found', async () => {
config.depName = 'notfound';
config.currentVersion = '1.2.9';
config.newVersion = '1.2.10';
config.allowHigherOrRemoved = true;
const res = await updateLockedDependency(config);
expect(res.status).toBe('already-updated');
});
it('returns update-failed if other, lower version found', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.5';
config.newVersion = '1.2.15';
config.allowHigherOrRemoved = true;
const res = await updateLockedDependency(config);
expect(res.status).toBe('update-failed');
});
it('remediates mime', async () => {
config.depName = 'mime';
config.currentVersion = '1.2.11';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe('manager/npm/update/locked-dependency/package-lock/get-locked', () => {
},
]);
});
it('finds any version', () => {
expect(getLockedDependencies(packageLockJson, 'send', null)).toHaveLength(
2
);
});
it('finds bundled dependency', () => {
expect(
getLockedDependencies(bundledPackageLockJson, 'ansi-regex', '3.0.0')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { PackageLockDependency, PackageLockOrEntry } from './types';
export function getLockedDependencies(
entry: PackageLockOrEntry,
depName: string,
currentVersion: string,
currentVersion: string | null,
bundled = false
): PackageLockDependency[] {
let res: PackageLockDependency[] = [];
Expand All @@ -15,7 +15,7 @@ export function getLockedDependencies(
return [];
}
const dep = dependencies[depName];
if (dep?.version === currentVersion) {
if (dep && (currentVersion === null || dep?.version === currentVersion)) {
if (bundled || entry.bundled) {
dep.bundled = true;
}
Expand Down
41 changes: 37 additions & 4 deletions lib/manager/npm/update/locked-dependency/package-lock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function updateLockedDependency(
lockFile,
lockFileContent,
allowParentUpdates = true,
allowHigherOrRemoved = false,
} = config;
logger.debug(
`npm.updateLockedDependency: ${depName}@${currentVersion} -> ${newVersion} [${lockFile}]`
Expand Down Expand Up @@ -66,10 +67,42 @@ export async function updateLockedDependency(
);
status = 'already-updated';
} else {
logger.debug(
`${depName}@${currentVersion} not found in ${lockFile} - cannot update`
);
status = 'update-failed';
if (allowHigherOrRemoved) {
// it's acceptable if the package is no longer present
const anyVersionLocked = getLockedDependencies(
packageLockJson,
depName,
null
);
if (anyVersionLocked.length) {
if (
anyVersionLocked.every((dep) =>
semver.isGreaterThan(dep.version, newVersion)
)
) {
logger.debug(
`${depName} found in ${lockFile} with higher version - looks like it's already updated`
);
status = 'already-updated';
} else {
logger.debug(
{ anyVersionLocked },
`Found alternative versions of qs`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qs ?

);
status = 'update-failed';
}
} else {
logger.debug(
`${depName} not found in ${lockFile} - looks like it's already removed`
);
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
Expand Down
1 change: 1 addition & 0 deletions lib/manager/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export interface UpdateLockedConfig {
currentVersion?: string;
newVersion?: string;
allowParentUpdates?: boolean;
allowHigherOrRemoved?: boolean;
}

export interface UpdateLockedResult {
Expand Down
1 change: 1 addition & 0 deletions lib/workers/branch/get-updated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export async function getUpdatedPackageFiles(
packageFileContent,
lockFileContent,
allowParentUpdates: true,
allowHigherOrRemoved: true,
});
if (reuseExistingBranch && status !== 'already-updated') {
logger.debug(
Expand Down