Skip to content

Commit

Permalink
feat: fix sequentially support for Pipenv
Browse files Browse the repository at this point in the history
if asked fix a package 1 by 1, this is much slower
but can increase success % when some packages might be incompatible
  • Loading branch information
lili2311 committed Sep 14, 2021
1 parent d820026 commit a0b4bf7
Show file tree
Hide file tree
Showing 5 changed files with 560 additions and 36 deletions.
Expand Up @@ -6,19 +6,23 @@ import {
FixChangesSummary,
FixOptions,
} from '../../../../../types';
import { NoFixesCouldBeAppliedError } from '../../../../../lib/errors/no-fixes-applied';

import { ensureHasUpdates } from '../../ensure-has-updates';
import { pipenvAdd } from './pipenv-add';
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;
}

Expand Down Expand Up @@ -66,3 +70,51 @@ 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
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])));
}
}

ensureHasUpdates(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;
}
Expand Up @@ -6,10 +6,10 @@ import {
FixChangesSummary,
FixOptions,
} from '../../../../../types';
import { NoFixesCouldBeAppliedError } from '../../../../../lib/errors/no-fixes-applied';
import { generateUpgrades } from './generate-upgrades';
import { poetryAdd } from './poetry-add';
import { ensureHasUpdates } from '../../ensure-has-updates';
import { NoFixesCouldBeAppliedError } from '../../../../../lib/errors/no-fixes-applied';

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

Expand Down

0 comments on commit a0b4bf7

Please sign in to comment.