@@ -3,16 +3,21 @@ import * as pathLib from 'path';
3
3
4
4
import {
5
5
EntityToFix ,
6
+ FixChangesSummary ,
6
7
FixOptions ,
7
- WithFixChangesApplied ,
8
+ RemediationChanges ,
9
+ Workspace ,
8
10
} from '../../../../types' ;
9
11
import { PluginFixResponse } from '../../../types' ;
10
12
import { updateDependencies } from './update-dependencies' ;
11
13
import { MissingRemediationDataError } from '../../../../lib/errors/missing-remediation-data' ;
12
14
import { MissingFileNameError } from '../../../../lib/errors/missing-file-name' ;
13
15
import { partitionByFixable } from './is-supported' ;
14
16
import { 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' ;
16
21
17
22
const debug = debugLib ( 'snyk-fix:python:requirements.txt' ) ;
18
23
@@ -32,38 +37,60 @@ export async function pipRequirementsTxt(
32
37
33
38
for ( const entity of fixable ) {
34
39
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 } ) ;
37
52
} catch ( e ) {
38
53
handlerResult . failed . push ( { original : entity , error : e } ) ;
39
54
}
40
55
}
41
56
return handlerResult ;
42
57
}
43
58
44
- // TODO: optionally verify the deps install
45
- export async function fixIndividualRequirementsTxt (
59
+ export function getRequiredData (
46
60
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 ) {
52
68
throw new MissingRemediationDataError ( ) ;
53
69
}
54
- if ( ! fileName ) {
70
+ const { targetFile } = entity . scanResult . identity ;
71
+ if ( ! targetFile ) {
55
72
throw new MissingFileNameError ( ) ;
56
73
}
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 [ ] > {
63
90
// TODO: allow handlers per fix type (later also strategies or combine with strategies)
64
91
const { updatedManifest, changes } = updateDependencies (
65
- versionProvenance [ base ] ,
66
- remediationData . pin ,
92
+ provenance [ fileName ] ,
93
+ remediation . pin ,
67
94
) ;
68
95
69
96
if ( ! changes . length ) {
@@ -72,13 +99,10 @@ export async function fixIndividualRequirementsTxt(
72
99
}
73
100
if ( ! options . dryRun ) {
74
101
debug ( 'Writing changes to file' ) ;
75
- await entity . workspace . writeFile ( fileName , updatedManifest ) ;
102
+ await workspace . writeFile ( pathLib . join ( dir , fileName ) , updatedManifest ) ;
76
103
} else {
77
104
debug ( 'Skipping writing changes to file in --dry-run mode' ) ;
78
105
}
79
106
80
- return {
81
- original : entity ,
82
- changes,
83
- } ;
107
+ return changes ;
84
108
}
0 commit comments