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: sequential fix #2252

Merged
merged 7 commits into from
Sep 15, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isSuccessfulChange } from './attempted-changes-summary';

const debug = debugLib('snyk-fix:python:ensure-changes-applied');

export function ensureHasUpdates(changes: FixChangesSummary[]) {
export function failIfNoUpdatesApplied(changes: FixChangesSummary[]) {
if (!changes.length) {
throw new NoFixesCouldBeAppliedError();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { EntityToFix } from '../../../../../types';
import { standardizePackageName } from '../../../standardize-package-name';
import { validateRequiredData } from '../../validate-required-data';

export function generateUpgrades(entity: EntityToFix): { upgrades: string[] } {
const { remediation } = validateRequiredData(entity);
const { pin: pins } = remediation;

const upgrades: string[] = [];
for (const pkgAtVersion of Object.keys(pins)) {
const pin = pins[pkgAtVersion];
const newVersion = pin.upgradeTo.split('@')[1];
const [pkgName] = pkgAtVersion.split('@');
upgrades.push(`${standardizePackageName(pkgName)}==${newVersion}`);
}
return { upgrades };
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,26 @@ import {
FixChangesSummary,
FixOptions,
} from '../../../../../types';
import { NoFixesCouldBeAppliedError } from '../../../../../lib/errors/no-fixes-applied';
import { standardizePackageName } from '../../../standardize-package-name';
import { validateRequiredData } from '../../validate-required-data';

import { ensureHasUpdates } from '../../ensure-has-updates';
import { failIfNoUpdatesApplied } from '../../fail-if-no-updates-applied';
import { NoFixesCouldBeAppliedError } from '../../../../../lib/errors/no-fixes-applied';
import { generateUpgrades } from './generate-upgrades';
import { pipenvAdd } from './pipenv-add';

const debug = debugLib('snyk-fix:python:Pipfile');

function chooseFixStrategy(options: FixOptions) {
return options.sequentialFix ? fixSequentially : fixAll;
}

export async function updateDependencies(
entity: EntityToFix,
options: FixOptions,
): Promise<PluginFixResponse> {
const handlerResult = await fixAll(entity, options);
const handlerResult = await chooseFixStrategy(options)(entity, options);
return handlerResult;
}

export function generateUpgrades(entity: EntityToFix): { upgrades: string[] } {
const { remediation } = validateRequiredData(entity);
const { pin: pins } = remediation;

const upgrades: string[] = [];
for (const pkgAtVersion of Object.keys(pins)) {
const pin = pins[pkgAtVersion];
const newVersion = pin.upgradeTo.split('@')[1];
const [pkgName] = pkgAtVersion.split('@');
upgrades.push(`${standardizePackageName(pkgName)}==${newVersion}`);
}
return { upgrades };
}

async function fixAll(
entity: EntityToFix,
options: FixOptions,
Expand All @@ -46,14 +35,14 @@ async function fixAll(
failed: [],
skipped: [],
};
const { upgrades } = await generateUpgrades(entity);
if (!upgrades.length) {
throw new NoFixesCouldBeAppliedError(
'Failed to calculate package updates to apply',
);
}
const changes: FixChangesSummary[] = [];
try {
const { upgrades } = await generateUpgrades(entity);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

move back into try/catch to not prevent further fixes & to let the spinner finish

if (!upgrades.length) {
throw new NoFixesCouldBeAppliedError(
'Failed to calculate package updates to apply',
);
}
// TODO: for better support we need to:
// 1. parse the manifest and extract original requirements, version spec etc
// 2. swap out only the version and retain original spec
Expand All @@ -64,7 +53,7 @@ async function fixAll(
changes.push(...(await pipenvAdd(entity, options, upgrades)));
}

ensureHasUpdates(changes);
failIfNoUpdatesApplied(changes);
handlerResult.succeeded.push({
original: entity,
changes,
Expand All @@ -81,3 +70,53 @@ async function fixAll(
}
return handlerResult;
}

async function fixSequentially(
entity: EntityToFix,
options: FixOptions,
): Promise<PluginFixResponse> {
const handlerResult: PluginFixResponse = {
succeeded: [],
failed: [],
skipped: [],
};
const { upgrades } = await generateUpgrades(entity);
// TODO: for better support we need to:
// 1. parse the manifest and extract original requirements, version spec etc
// 2. swap out only the version and retain original spec
// 3. re-lock the lockfile
// at the moment we do not parse Pipfile and therefore can't tell the difference
// between prod & dev updates
const changes: FixChangesSummary[] = [];

try {
if (!upgrades.length) {
throw new NoFixesCouldBeAppliedError(
'Failed to calculate package updates to apply',
);
}
// update prod dependencies first
if (upgrades.length) {
for (const upgrade of upgrades) {
changes.push(...(await pipenvAdd(entity, options, [upgrade])));
}
}

failIfNoUpdatesApplied(changes);

handlerResult.succeeded.push({
original: entity,
changes,
});
} catch (error) {
debug(
`Failed to fix ${entity.scanResult.identity.targetFile}.\nERROR: ${error}`,
);
handlerResult.failed.push({
original: entity,
tip: error.tip,
error,
});
}
return handlerResult;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as pathLib from 'path';
import * as toml from 'toml';

import * as debugLib from 'debug';

import { EntityToFix } from '../../../../../types';

import { validateRequiredData } from '../../validate-required-data';
import { standardizePackageName } from '../../../standardize-package-name';

const debug = debugLib('snyk-fix:python:Poetry');

interface PyProjectToml {
tool: {
poetry: {
name: string;
version: string;
description: string;
authors: string[];
dependencies?: object;
'dev-dependencies'?: object;
};
};
}

export async function generateUpgrades(
entity: EntityToFix,
): Promise<{ upgrades: string[]; devUpgrades: string[] }> {
const { remediation, targetFile } = validateRequiredData(entity);
const pins = remediation.pin;

const targetFilePath = pathLib.resolve(entity.workspace.path, targetFile);
const { dir } = pathLib.parse(targetFilePath);
const pyProjectTomlRaw = await entity.workspace.readFile(
pathLib.resolve(dir, 'pyproject.toml'),
);
const pyProjectToml: PyProjectToml = toml.parse(pyProjectTomlRaw);

const prodTopLevelDeps = Object.keys(
pyProjectToml.tool.poetry.dependencies ?? {},
).map((dep) => standardizePackageName(dep));
const devTopLevelDeps = Object.keys(
pyProjectToml.tool.poetry['dev-dependencies'] ?? {},
).map((dep) => standardizePackageName(dep));

const upgrades: string[] = [];
const devUpgrades: string[] = [];
for (const pkgAtVersion of Object.keys(pins)) {
const pin = pins[pkgAtVersion];
const newVersion = pin.upgradeTo.split('@')[1];
const [pkgName] = pkgAtVersion.split('@');

const upgrade = `${standardizePackageName(pkgName)}==${newVersion}`;

if (pin.isTransitive || prodTopLevelDeps.includes(pkgName)) {
// transitive and it could have come from a dev or prod dep
// since we can't tell right now let be pinned into production deps
upgrades.push(upgrade);
} else if (prodTopLevelDeps.includes(pkgName)) {
upgrades.push(upgrade);
} else if (entity.options.dev && devTopLevelDeps.includes(pkgName)) {
devUpgrades.push(upgrade);
} else {
debug(
`Could not determine what type of upgrade ${upgrade} is. When choosing between: transitive upgrade, production or dev direct upgrade. `,
);
}
}
return { upgrades, devUpgrades };
}
Loading