import * as debugLib from 'debug' ; import { PluginFixResponse } from '../../../../types' ; import { EntityToFix , FixChangesSummary , FixOptions , } from '../../../../../types' ; import { generateUpgrades } from './generate-upgrades' ; import { poetryAdd } from './poetry-add' ; import { NoFixesCouldBeAppliedError } from '../../../../../lib/errors/no-fixes-applied' ; import { isSuccessfulChange } from '../../attempted-changes-summary' ; const debug = debugLib ( 'snyk-fix:python:Poetry' ) ; function chooseFixStrategy ( options ) { return options . sequentialFix ? fixSequentially : fixAll ; } export async function updateDependencies ( entity , options ) { const handlerResult = await chooseFixStrategy ( options ) ( entity , options ) ; return handlerResult ; } async function fixAll ( entity , options ) { const handlerResult = { succeeded : [ ] , failed : [ ] , skipped : [ ] , } ; const { upgrades , devUpgrades } = await generateUpgrades ( entity ) ; const changes = [ ] ; try { if ( ! [ ... upgrades , ... devUpgrades ] . length ) { throw new NoFixesCouldBeAppliedError ( 'Failed to calculate package updates to apply' ) ; } if ( upgrades . length ) { changes . push ( ... ( await poetryAdd ( entity , options , upgrades ) ) ) ; } if ( devUpgrades . length ) { const installDev = true ; changes . push ( ... ( await poetryAdd ( entity , options , devUpgrades , installDev ) ) ) ; } if ( ! changes . length ) { throw new NoFixesCouldBeAppliedError ( ) ; } if ( ! changes . some ( ( c ) => isSuccessfulChange ( c ) ) ) { handlerResult . failed . push ( { original : entity , changes , } ) ; } else { 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 ; } async function fixSequentially ( entity , options ) { const handlerResult = { succeeded : [ ] , failed : [ ] , skipped : [ ] , } ; const { upgrades , devUpgrades } = await generateUpgrades ( entity ) ; const changes = [ ] ; try { if ( ! [ ... upgrades , ... devUpgrades ] . length ) { throw new NoFixesCouldBeAppliedError ( 'Failed to calculate package updates to apply' ) ; } if ( upgrades . length ) { for ( const upgrade of upgrades ) { changes . push ( ... ( await poetryAdd ( entity , options , [ upgrade ] ) ) ) ; } } if ( devUpgrades . length ) { for ( const upgrade of devUpgrades ) { const installDev = true ; changes . push ( ... ( await poetryAdd ( entity , options , [ upgrade ] , installDev ) ) ) ; } } if ( ! changes . length ) { throw new NoFixesCouldBeAppliedError ( ) ; } if ( ! changes . some ( ( c ) => isSuccessfulChange ( c ) ) ) { handlerResult . failed . push ( { original : entity , changes , } ) ; } else { 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 ; }