@@ -3,16 +3,21 @@ import * as pathLib from 'path';
33
44import  { 
55  EntityToFix , 
6+   FixChangesSummary , 
67  FixOptions , 
7-   WithFixChangesApplied , 
8+   RemediationChanges , 
9+   Workspace , 
810}  from  '../../../../types' ; 
911import  {  PluginFixResponse  }  from  '../../../types' ; 
1012import  {  updateDependencies  }  from  './update-dependencies' ; 
1113import  {  MissingRemediationDataError  }  from  '../../../../lib/errors/missing-remediation-data' ; 
1214import  {  MissingFileNameError  }  from  '../../../../lib/errors/missing-file-name' ; 
1315import  {  partitionByFixable  }  from  './is-supported' ; 
1416import  {  NoFixesCouldBeAppliedError  }  from  '../../../../lib/errors/no-fixes-applied' ; 
15- import  {  extractProvenance  }  from  './extract-version-provenance' ; 
17+ import  { 
18+   extractProvenance , 
19+   PythonProvenance , 
20+ }  from  './extract-version-provenance' ; 
1621
1722const  debug  =  debugLib ( 'snyk-fix:python:requirements.txt' ) ; 
1823
@@ -32,38 +37,60 @@ export async function pipRequirementsTxt(
3237
3338  for  ( const  entity  of  fixable )  { 
3439    try  { 
35-       const  fixedEntity  =  await  fixIndividualRequirementsTxt ( entity ,  options ) ; 
36-       handlerResult . succeeded . push ( fixedEntity ) ; 
40+       const  {  remediation,  targetFile,  workspace }  =  getRequiredData ( entity ) ; 
41+       const  {  dir,  base }  =  pathLib . parse ( targetFile ) ; 
42+       const  provenance  =  await  extractProvenance ( workspace ,  dir ,  base ) ; 
43+       const  changes  =  await  fixIndividualRequirementsTxt ( 
44+         workspace , 
45+         dir , 
46+         base , 
47+         remediation , 
48+         provenance , 
49+         options , 
50+       ) ; 
51+       handlerResult . succeeded . push ( {  original : entity ,  changes } ) ; 
3752    }  catch  ( e )  { 
3853      handlerResult . failed . push ( {  original : entity ,  error : e  } ) ; 
3954    } 
4055  } 
4156  return  handlerResult ; 
4257} 
4358
44- // TODO: optionally verify the deps install 
45- export  async  function  fixIndividualRequirementsTxt ( 
59+ export  function  getRequiredData ( 
4660  entity : EntityToFix , 
47-   options : FixOptions , 
48- ) : Promise < WithFixChangesApplied < EntityToFix > >  { 
49-   const  fileName  =  entity . scanResult . identity . targetFile ; 
50-   const  remediationData  =  entity . testResult . remediation ; 
51-   if  ( ! remediationData )  { 
61+ ) : { 
62+   remediation : RemediationChanges ; 
63+   targetFile : string ; 
64+   workspace : Workspace ; 
65+ }  { 
66+   const  {  remediation }  =  entity . testResult ; 
67+   if  ( ! remediation )  { 
5268    throw  new  MissingRemediationDataError ( ) ; 
5369  } 
54-   if  ( ! fileName )  { 
70+   const  {  targetFile }  =  entity . scanResult . identity ; 
71+   if  ( ! targetFile )  { 
5572    throw  new  MissingFileNameError ( ) ; 
5673  } 
57-   const  {  dir,  base }  =  pathLib . parse ( fileName ) ; 
58-   const  versionProvenance  =  await  extractProvenance ( 
59-     entity . workspace , 
60-     dir , 
61-     base , 
62-   ) ; 
74+   const  {  workspace }  =  entity ; 
75+   if  ( ! workspace )  { 
76+     throw  new  NoFixesCouldBeAppliedError ( ) ; 
77+   } 
78+   return  {  targetFile,  remediation,  workspace } ; 
79+ } 
80+ 
81+ // TODO: optionally verify the deps install 
82+ export  async  function  fixIndividualRequirementsTxt ( 
83+   workspace : Workspace , 
84+   dir : string , 
85+   fileName : string , 
86+   remediation : RemediationChanges , 
87+   provenance : PythonProvenance , 
88+   options : FixOptions , 
89+ ) : Promise < FixChangesSummary [ ] >  { 
6390  // TODO: allow handlers per fix type (later also strategies or combine with strategies) 
6491  const  {  updatedManifest,  changes }  =  updateDependencies ( 
65-     versionProvenance [ base ] , 
66-     remediationData . pin , 
92+     provenance [ fileName ] , 
93+     remediation . pin , 
6794  ) ; 
6895
6996  if  ( ! changes . length )  { 
@@ -72,13 +99,10 @@ export async function fixIndividualRequirementsTxt(
7299  } 
73100  if  ( ! options . dryRun )  { 
74101    debug ( 'Writing changes to file' ) ; 
75-     await  entity . workspace . writeFile ( fileName ,  updatedManifest ) ; 
102+     await  workspace . writeFile ( pathLib . join ( dir ,   fileName ) ,  updatedManifest ) ; 
76103  }  else  { 
77104    debug ( 'Skipping writing changes to file in --dry-run mode' ) ; 
78105  } 
79106
80-   return  { 
81-     original : entity , 
82-     changes, 
83-   } ; 
107+   return  changes ; 
84108} 
0 commit comments