"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/packages/snyk-fix/src/plugins/python/handlers/poetry/update-dependencies/index.ts" (20 Feb 2024, 4186 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import * as debugLib from 'debug';
    2 
    3 import { PluginFixResponse } from '../../../../types';
    4 import {
    5   EntityToFix,
    6   FixChangesSummary,
    7   FixOptions,
    8 } from '../../../../../types';
    9 import { generateUpgrades } from './generate-upgrades';
   10 import { poetryAdd } from './poetry-add';
   11 import { NoFixesCouldBeAppliedError } from '../../../../../lib/errors/no-fixes-applied';
   12 import { isSuccessfulChange } from '../../attempted-changes-summary';
   13 
   14 const debug = debugLib('snyk-fix:python:Poetry');
   15 
   16 function chooseFixStrategy(options: FixOptions) {
   17   return options.sequentialFix ? fixSequentially : fixAll;
   18 }
   19 
   20 export async function updateDependencies(
   21   entity: EntityToFix,
   22   options: FixOptions,
   23 ): Promise<PluginFixResponse> {
   24   const handlerResult = await chooseFixStrategy(options)(entity, options);
   25   return handlerResult;
   26 }
   27 
   28 async function fixAll(
   29   entity: EntityToFix,
   30   options: FixOptions,
   31 ): Promise<PluginFixResponse> {
   32   const handlerResult: PluginFixResponse = {
   33     succeeded: [],
   34     failed: [],
   35     skipped: [],
   36   };
   37   const { upgrades, devUpgrades } = await generateUpgrades(entity);
   38 
   39   // TODO: for better support we need to:
   40   // 1. parse the manifest and extract original requirements, version spec etc
   41   // 2. swap out only the version and retain original spec
   42   // 3. re-lock the lockfile
   43   const changes: FixChangesSummary[] = [];
   44   try {
   45     if (![...upgrades, ...devUpgrades].length) {
   46       throw new NoFixesCouldBeAppliedError(
   47         'Failed to calculate package updates to apply',
   48       );
   49     }
   50     // update prod dependencies first
   51     if (upgrades.length) {
   52       changes.push(...(await poetryAdd(entity, options, upgrades)));
   53     }
   54 
   55     // update dev dependencies second
   56     if (devUpgrades.length) {
   57       const installDev = true;
   58       changes.push(
   59         ...(await poetryAdd(entity, options, devUpgrades, installDev)),
   60       );
   61     }
   62 
   63     if (!changes.length) {
   64       throw new NoFixesCouldBeAppliedError();
   65     }
   66     if (!changes.some((c) => isSuccessfulChange(c))) {
   67       handlerResult.failed.push({
   68         original: entity,
   69         changes,
   70       });
   71     } else {
   72       handlerResult.succeeded.push({
   73         original: entity,
   74         changes,
   75       });
   76     }
   77   } catch (error) {
   78     debug(
   79       `Failed to fix ${entity.scanResult.identity.targetFile}.\nERROR: ${error}`,
   80     );
   81     handlerResult.failed.push({
   82       original: entity,
   83       tip: error.tip,
   84       error,
   85     });
   86   }
   87   return handlerResult;
   88 }
   89 
   90 async function fixSequentially(
   91   entity: EntityToFix,
   92   options: FixOptions,
   93 ): Promise<PluginFixResponse> {
   94   const handlerResult: PluginFixResponse = {
   95     succeeded: [],
   96     failed: [],
   97     skipped: [],
   98   };
   99   const { upgrades, devUpgrades } = await generateUpgrades(entity);
  100   // TODO: for better support we need to:
  101   // 1. parse the manifest and extract original requirements, version spec etc
  102   // 2. swap out only the version and retain original spec
  103   // 3. re-lock the lockfile
  104   const changes: FixChangesSummary[] = [];
  105   try {
  106     if (![...upgrades, ...devUpgrades].length) {
  107       throw new NoFixesCouldBeAppliedError(
  108         'Failed to calculate package updates to apply',
  109       );
  110     }
  111     // update prod dependencies first
  112     if (upgrades.length) {
  113       for (const upgrade of upgrades) {
  114         changes.push(...(await poetryAdd(entity, options, [upgrade])));
  115       }
  116     }
  117 
  118     // update dev dependencies second
  119     if (devUpgrades.length) {
  120       for (const upgrade of devUpgrades) {
  121         const installDev = true;
  122         changes.push(
  123           ...(await poetryAdd(entity, options, [upgrade], installDev)),
  124         );
  125       }
  126     }
  127     if (!changes.length) {
  128       throw new NoFixesCouldBeAppliedError();
  129     }
  130     if (!changes.some((c) => isSuccessfulChange(c))) {
  131       handlerResult.failed.push({
  132         original: entity,
  133         changes,
  134       });
  135     } else {
  136       handlerResult.succeeded.push({
  137         original: entity,
  138         changes,
  139       });
  140     }
  141   } catch (error) {
  142     debug(
  143       `Failed to fix ${entity.scanResult.identity.targetFile}.\nERROR: ${error}`,
  144     );
  145     handlerResult.failed.push({
  146       original: entity,
  147       tip: error.tip,
  148       error,
  149     });
  150   }
  151   return handlerResult;
  152 }