@trustify-da/trustify-da-javascript-client 0.3.0-ea.f2d5d72 → 0.3.0-ea.fa16f06

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +2 -2
  2. package/dist/package.json +26 -23
  3. package/dist/src/analysis.d.ts +14 -7
  4. package/dist/src/analysis.js +32 -15
  5. package/dist/src/cli.js +156 -3
  6. package/dist/src/cyclone_dx_sbom.d.ts +7 -1
  7. package/dist/src/cyclone_dx_sbom.js +29 -8
  8. package/dist/src/index.d.ts +84 -11
  9. package/dist/src/index.js +95 -8
  10. package/dist/src/license/index.d.ts +3 -3
  11. package/dist/src/license/index.js +4 -1
  12. package/dist/src/license/license_utils.js +4 -1
  13. package/dist/src/license/licenses_api.d.ts +2 -2
  14. package/dist/src/oci_image/images.d.ts +1 -1
  15. package/dist/src/oci_image/images.js +15 -1
  16. package/dist/src/oci_image/utils.d.ts +5 -5
  17. package/dist/src/oci_image/utils.js +11 -2
  18. package/dist/src/provider.d.ts +4 -3
  19. package/dist/src/provider.js +8 -2
  20. package/dist/src/providers/base_java.d.ts +14 -12
  21. package/dist/src/providers/base_java.js +18 -44
  22. package/dist/src/providers/base_javascript.d.ts +20 -9
  23. package/dist/src/providers/base_javascript.js +10 -3
  24. package/dist/src/providers/base_pyproject.d.ts +42 -31
  25. package/dist/src/providers/base_pyproject.js +121 -78
  26. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  27. package/dist/src/providers/containerfile_parser.js +20 -0
  28. package/dist/src/providers/golang_gomodules.d.ts +11 -1
  29. package/dist/src/providers/golang_gomodules.js +65 -8
  30. package/dist/src/providers/java_gradle.d.ts +19 -0
  31. package/dist/src/providers/java_gradle.js +116 -2
  32. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  33. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  34. package/dist/src/providers/java_maven.d.ts +10 -2
  35. package/dist/src/providers/java_maven.js +165 -1
  36. package/dist/src/providers/javascript_bun.d.ts +10 -0
  37. package/dist/src/providers/javascript_bun.js +100 -0
  38. package/dist/src/providers/javascript_npm.d.ts +6 -0
  39. package/dist/src/providers/javascript_npm.js +21 -0
  40. package/dist/src/providers/javascript_pnpm.js +6 -2
  41. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  42. package/dist/src/providers/marker_evaluator.js +191 -0
  43. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  44. package/dist/src/providers/oci_dockerfile.js +177 -0
  45. package/dist/src/providers/processors/yarn_berry_processor.d.ts +2 -2
  46. package/dist/src/providers/processors/yarn_berry_processor.js +6 -2
  47. package/dist/src/providers/processors/yarn_classic_processor.d.ts +2 -2
  48. package/dist/src/providers/python_controller.d.ts +5 -1
  49. package/dist/src/providers/python_controller.js +69 -8
  50. package/dist/src/providers/python_pip.d.ts +5 -0
  51. package/dist/src/providers/python_pip.js +5 -5
  52. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  53. package/dist/src/providers/python_pip_pyproject.js +146 -0
  54. package/dist/src/providers/python_poetry.d.ts +64 -8
  55. package/dist/src/providers/python_poetry.js +165 -25
  56. package/dist/src/providers/python_uv.d.ts +22 -2
  57. package/dist/src/providers/python_uv.js +157 -6
  58. package/dist/src/providers/rust_cargo.d.ts +6 -2
  59. package/dist/src/providers/rust_cargo.js +105 -19
  60. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  61. package/dist/src/remediate.d.ts +21 -0
  62. package/dist/src/remediate.js +149 -0
  63. package/dist/src/remediation.d.ts +57 -0
  64. package/dist/src/remediation.js +436 -0
  65. package/dist/src/remediation_report.d.ts +40 -0
  66. package/dist/src/remediation_report.js +159 -0
  67. package/dist/src/sbom.d.ts +7 -1
  68. package/dist/src/sbom.js +4 -2
  69. package/dist/src/tools.d.ts +28 -2
  70. package/dist/src/tools.js +58 -0
  71. package/dist/src/updaters/maven_updater.d.ts +26 -0
  72. package/dist/src/updaters/maven_updater.js +322 -0
  73. package/dist/src/updaters/toml_updater.d.ts +33 -0
  74. package/dist/src/updaters/toml_updater.js +244 -0
  75. package/dist/src/workspace.d.ts +9 -0
  76. package/dist/src/workspace.js +1 -1
  77. package/package.json +27 -24
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Extracts actionable remediation instructions from a DA AnalysisReport response.
3
+ *
4
+ * Walks the provider/source/dependency/issue tree, collects fixedIn and trustedContent
5
+ * remediation data, applies provider priority resolution, and uses the configured
6
+ * version selection strategy to resolve conflicts. Dependencies with no remediation
7
+ * data are skipped.
8
+ *
9
+ * @param {object} analysisReport - raw DA AnalysisReport JSON response
10
+ * @param {object} [options] - extraction options
11
+ * @param {string[]} [options.providerPriority] - provider names in descending priority order.
12
+ * The first entry has the highest priority. Providers not listed share the lowest priority.
13
+ * When omitted or empty, all providers are treated equally and the highest fix version wins.
14
+ * @param {object} [options.versionStrategy] - version selection strategy with selectVersion
15
+ * and resolveConflict methods. Defaults to closestCoverageStrategy.
16
+ * @returns {Array<{purl: string, groupId: string, artifactId: string, currentVersion: string, fixedInVersion: string, fixedInPurl: string, provider: string, source: string, advisories: Array<{id: string, url: string}>, severity: string, cves: string[]}>}
17
+ */
18
+ export function extractRemediations(analysisReport: object, options?: {
19
+ providerPriority?: string[] | undefined;
20
+ versionStrategy?: object | undefined;
21
+ }): Array<{
22
+ purl: string;
23
+ groupId: string;
24
+ artifactId: string;
25
+ currentVersion: string;
26
+ fixedInVersion: string;
27
+ fixedInPurl: string;
28
+ provider: string;
29
+ source: string;
30
+ advisories: Array<{
31
+ id: string;
32
+ url: string;
33
+ }>;
34
+ severity: string;
35
+ cves: string[];
36
+ }>;
37
+ /**
38
+ * Version selection strategy that prefers the closest compatible version
39
+ * within the same major version stream. Falls back to the lowest cross-major
40
+ * version when no same-major option exists.
41
+ * @type {{selectVersion: function(string[], string): string, resolveConflict: function(object, object): string}}
42
+ */
43
+ export const closestCoverageStrategy: {
44
+ selectVersion: (arg0: string[], arg1: string) => string;
45
+ resolveConflict: (arg0: object, arg1: object) => string;
46
+ };
47
+ /**
48
+ * Version selection strategy that always picks the highest version regardless
49
+ * of major version distance. Guarantees maximum CVE coverage but may produce
50
+ * large version jumps. This is the original behavior before pluggable strategies.
51
+ * @type {{selectVersion: function(string[], string): string, resolveConflict: function(object, object): string}}
52
+ */
53
+ export const highestStrategy: {
54
+ selectVersion: (arg0: string[], arg1: string) => string;
55
+ resolveConflict: (arg0: object, arg1: object) => string;
56
+ };
57
+ export const SEVERITY_ORDER: string[];
@@ -0,0 +1,436 @@
1
+ import { PackageURL } from 'packageurl-js';
2
+ /**
3
+ * Extracts the major version segment from a version string.
4
+ * @param {string} version
5
+ * @returns {string} the first dot-separated segment
6
+ */
7
+ function getMajorVersion(version) {
8
+ return version.split('.')[0] || '';
9
+ }
10
+ /**
11
+ * Version selection strategy that prefers the closest compatible version
12
+ * within the same major version stream. Falls back to the lowest cross-major
13
+ * version when no same-major option exists.
14
+ * @type {{selectVersion: function(string[], string): string, resolveConflict: function(object, object): string}}
15
+ */
16
+ export const closestCoverageStrategy = {
17
+ selectVersion(fixedInVersions, currentVersion) {
18
+ const currentMajor = getMajorVersion(currentVersion);
19
+ const sameMajor = fixedInVersions.filter(v => getMajorVersion(v) === currentMajor);
20
+ if (sameMajor.length > 0) {
21
+ return sameMajor.sort((a, b) => compareVersions(b, a))[0];
22
+ }
23
+ return fixedInVersions.sort((a, b) => compareVersions(a, b))[0];
24
+ },
25
+ resolveConflict(existing, candidate) {
26
+ if (existing._fromTrustedContent && !candidate._fromTrustedContent) {
27
+ return 'existing';
28
+ }
29
+ if (!existing._fromTrustedContent && candidate._fromTrustedContent) {
30
+ return 'candidate';
31
+ }
32
+ const currentMajor = getMajorVersion(existing.currentVersion);
33
+ const existingSameMajor = getMajorVersion(existing.fixedInVersion) === currentMajor;
34
+ const candidateSameMajor = getMajorVersion(candidate.fixedInVersion) === currentMajor;
35
+ if (existingSameMajor && !candidateSameMajor) {
36
+ return 'existing';
37
+ }
38
+ if (!existingSameMajor && candidateSameMajor) {
39
+ return 'candidate';
40
+ }
41
+ return compareVersions(candidate.fixedInVersion, existing.fixedInVersion) > 0
42
+ ? 'candidate'
43
+ : 'existing';
44
+ },
45
+ };
46
+ /**
47
+ * Version selection strategy that always picks the highest version regardless
48
+ * of major version distance. Guarantees maximum CVE coverage but may produce
49
+ * large version jumps. This is the original behavior before pluggable strategies.
50
+ * @type {{selectVersion: function(string[], string): string, resolveConflict: function(object, object): string}}
51
+ */
52
+ export const highestStrategy = {
53
+ selectVersion(fixedInVersions) {
54
+ return fixedInVersions.sort((a, b) => compareVersions(b, a))[0];
55
+ },
56
+ resolveConflict(existing, candidate) {
57
+ if (existing._fromTrustedContent && !candidate._fromTrustedContent) {
58
+ return 'existing';
59
+ }
60
+ if (!existing._fromTrustedContent && candidate._fromTrustedContent) {
61
+ return 'candidate';
62
+ }
63
+ return compareVersions(candidate.fixedInVersion, existing.fixedInVersion) > 0
64
+ ? 'candidate'
65
+ : 'existing';
66
+ },
67
+ };
68
+ /**
69
+ * Extracts actionable remediation instructions from a DA AnalysisReport response.
70
+ *
71
+ * Walks the provider/source/dependency/issue tree, collects fixedIn and trustedContent
72
+ * remediation data, applies provider priority resolution, and uses the configured
73
+ * version selection strategy to resolve conflicts. Dependencies with no remediation
74
+ * data are skipped.
75
+ *
76
+ * @param {object} analysisReport - raw DA AnalysisReport JSON response
77
+ * @param {object} [options] - extraction options
78
+ * @param {string[]} [options.providerPriority] - provider names in descending priority order.
79
+ * The first entry has the highest priority. Providers not listed share the lowest priority.
80
+ * When omitted or empty, all providers are treated equally and the highest fix version wins.
81
+ * @param {object} [options.versionStrategy] - version selection strategy with selectVersion
82
+ * and resolveConflict methods. Defaults to closestCoverageStrategy.
83
+ * @returns {Array<{purl: string, groupId: string, artifactId: string, currentVersion: string, fixedInVersion: string, fixedInPurl: string, provider: string, source: string, advisories: Array<{id: string, url: string}>, severity: string, cves: string[]}>}
84
+ */
85
+ export function extractRemediations(analysisReport, options = {}) {
86
+ if (!analysisReport || !analysisReport.providers) {
87
+ return [];
88
+ }
89
+ const priorityMap = buildPriorityMap(options.providerPriority);
90
+ const strategy = options.versionStrategy || closestCoverageStrategy;
91
+ const remediationsByDep = new Map();
92
+ const rankByDep = new Map();
93
+ for (const [providerName, providerReport] of Object.entries(analysisReport.providers)) {
94
+ const providerRank = priorityMap.get(providerName) ?? 0;
95
+ extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep, strategy);
96
+ extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep);
97
+ }
98
+ return Array.from(remediationsByDep.values())
99
+ .map((entry) => {
100
+ delete entry._fromTrustedContent;
101
+ return entry;
102
+ })
103
+ .sort((a, b) => a.purl.localeCompare(b.purl));
104
+ }
105
+ /**
106
+ * Builds a priority lookup map from a provider priority array.
107
+ * First entry gets the highest numeric priority.
108
+ * @param {string[]} [providerPriority]
109
+ * @returns {Map<string, number>}
110
+ */
111
+ function buildPriorityMap(providerPriority) {
112
+ const map = new Map();
113
+ if (!providerPriority || providerPriority.length === 0) {
114
+ return map;
115
+ }
116
+ for (let i = 0; i < providerPriority.length; i++) {
117
+ map.set(providerPriority[i], providerPriority.length - i);
118
+ }
119
+ return map;
120
+ }
121
+ /**
122
+ * Extracts remediations from the sources/dependencies/issues tree of a provider report.
123
+ * @param {object} providerReport
124
+ * @param {string} providerName
125
+ * @param {number} providerRank - numeric priority rank for this provider
126
+ * @param {Map<string, object>} remediationsByDep - accumulator keyed by dependency PURL
127
+ * @param {Map<string, number>} rankByDep - tracks current winning rank per dependency
128
+ * @param {object} strategy - version selection strategy
129
+ */
130
+ function extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep, strategy) {
131
+ if (!providerReport.sources) {
132
+ return;
133
+ }
134
+ for (const [sourceName, sourceReport] of Object.entries(providerReport.sources)) {
135
+ if (!sourceReport.dependencies) {
136
+ continue;
137
+ }
138
+ for (const dep of sourceReport.dependencies) {
139
+ if (!dep.issues) {
140
+ continue;
141
+ }
142
+ for (const issue of dep.issues) {
143
+ processIssueRemediation(issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep, strategy);
144
+ }
145
+ }
146
+ }
147
+ }
148
+ /**
149
+ * Processes a single issue's remediation data and merges it into the accumulator.
150
+ * @param {object} issue - issue object containing remediation and CVE data
151
+ * @param {object} dep - dependency object containing the ref PURL
152
+ * @param {string} providerName
153
+ * @param {string} sourceName
154
+ * @param {number} providerRank
155
+ * @param {Map<string, object>} remediationsByDep
156
+ * @param {Map<string, number>} rankByDep
157
+ * @param {object} strategy - version selection strategy
158
+ */
159
+ function processIssueRemediation(issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep, strategy) {
160
+ const depPurl = dep.ref;
161
+ if (!depPurl) {
162
+ return;
163
+ }
164
+ let parsedDep;
165
+ try {
166
+ parsedDep = PackageURL.fromString(depPurl);
167
+ }
168
+ catch {
169
+ return;
170
+ }
171
+ const currentVersion = parsedDep.version || '';
172
+ const fixedInPurl = getFixedInPurl(issue, depPurl, strategy, currentVersion);
173
+ if (!fixedInPurl) {
174
+ return;
175
+ }
176
+ const isTrustedContent = !!(issue.remediation && issue.remediation.trustedContent && issue.remediation.trustedContent.ref);
177
+ let parsedFix;
178
+ try {
179
+ parsedFix = PackageURL.fromString(fixedInPurl);
180
+ }
181
+ catch {
182
+ return;
183
+ }
184
+ const fixedInVersion = parsedFix.version;
185
+ if (!fixedInVersion) {
186
+ return;
187
+ }
188
+ const cveId = issue.id || issue.cve;
189
+ const severity = issue.severity || 'UNKNOWN';
190
+ const advisories = extractAdvisories(issue);
191
+ const existing = remediationsByDep.get(depPurl);
192
+ if (!existing) {
193
+ remediationsByDep.set(depPurl, {
194
+ purl: depPurl,
195
+ groupId: parsedDep.namespace || '',
196
+ artifactId: parsedDep.name,
197
+ currentVersion,
198
+ fixedInVersion,
199
+ fixedInPurl,
200
+ provider: providerName,
201
+ source: sourceName,
202
+ advisories,
203
+ severity: severity.toUpperCase(),
204
+ cves: cveId ? [cveId] : [],
205
+ _fromTrustedContent: isTrustedContent,
206
+ });
207
+ rankByDep.set(depPurl, providerRank);
208
+ return;
209
+ }
210
+ if (cveId && !existing.cves.includes(cveId)) {
211
+ existing.cves.push(cveId);
212
+ }
213
+ mergeAdvisories(existing.advisories, advisories);
214
+ const existingRank = rankByDep.get(depPurl);
215
+ if (providerRank > existingRank) {
216
+ existing.fixedInVersion = fixedInVersion;
217
+ existing.fixedInPurl = fixedInPurl;
218
+ existing.provider = providerName;
219
+ existing.source = sourceName;
220
+ existing.severity = higherSeverity(existing.severity, severity);
221
+ existing._fromTrustedContent = isTrustedContent;
222
+ rankByDep.set(depPurl, providerRank);
223
+ }
224
+ else if (providerRank === existingRank) {
225
+ const winner = strategy.resolveConflict({ fixedInVersion: existing.fixedInVersion, _fromTrustedContent: existing._fromTrustedContent, currentVersion }, { fixedInVersion, _fromTrustedContent: isTrustedContent, currentVersion });
226
+ if (winner === 'candidate') {
227
+ existing.fixedInVersion = fixedInVersion;
228
+ existing.fixedInPurl = fixedInPurl;
229
+ existing.provider = providerName;
230
+ existing.source = sourceName;
231
+ existing._fromTrustedContent = isTrustedContent;
232
+ }
233
+ existing.severity = higherSeverity(existing.severity, severity);
234
+ }
235
+ }
236
+ /**
237
+ * Extracts remediations from the recommendations section of a provider report.
238
+ * Merges CVEs and advisories into existing entries when present.
239
+ * @param {object} providerReport
240
+ * @param {string} providerName
241
+ * @param {number} providerRank
242
+ * @param {Map<string, object>} remediationsByDep
243
+ * @param {Map<string, number>} rankByDep
244
+ */
245
+ function extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep) {
246
+ if (!providerReport.recommendations || !providerReport.recommendations.dependencies) {
247
+ return;
248
+ }
249
+ for (const dep of providerReport.recommendations.dependencies) {
250
+ if (!dep.recommendation || !dep.ref) {
251
+ continue;
252
+ }
253
+ const recommendedPurl = dep.recommendation.ref || dep.recommendation;
254
+ if (typeof recommendedPurl !== 'string') {
255
+ continue;
256
+ }
257
+ let parsedDep;
258
+ let parsedRec;
259
+ try {
260
+ parsedDep = PackageURL.fromString(dep.ref);
261
+ parsedRec = PackageURL.fromString(recommendedPurl);
262
+ }
263
+ catch {
264
+ continue;
265
+ }
266
+ const fixedInVersion = parsedRec.version;
267
+ if (!fixedInVersion) {
268
+ continue;
269
+ }
270
+ const depPurl = dep.ref;
271
+ const existing = remediationsByDep.get(depPurl);
272
+ if (!existing) {
273
+ remediationsByDep.set(depPurl, {
274
+ purl: depPurl,
275
+ groupId: parsedDep.namespace || '',
276
+ artifactId: parsedDep.name,
277
+ currentVersion: parsedDep.version || '',
278
+ fixedInVersion,
279
+ fixedInPurl: recommendedPurl,
280
+ provider: providerName,
281
+ source: 'recommendation',
282
+ advisories: [],
283
+ severity: 'UNKNOWN',
284
+ cves: [],
285
+ });
286
+ rankByDep.set(depPurl, providerRank);
287
+ continue;
288
+ }
289
+ const existingRank = rankByDep.get(depPurl);
290
+ if (providerRank > existingRank) {
291
+ existing.fixedInVersion = fixedInVersion;
292
+ existing.fixedInPurl = recommendedPurl;
293
+ existing.provider = providerName;
294
+ existing.source = 'recommendation';
295
+ rankByDep.set(depPurl, providerRank);
296
+ }
297
+ else if (providerRank === existingRank) {
298
+ if (compareVersions(fixedInVersion, existing.fixedInVersion) > 0) {
299
+ existing.fixedInVersion = fixedInVersion;
300
+ existing.fixedInPurl = recommendedPurl;
301
+ existing.provider = providerName;
302
+ existing.source = 'recommendation';
303
+ }
304
+ }
305
+ }
306
+ }
307
+ /**
308
+ * Gets the fixedIn PURL from an issue's remediation, preferring trustedContent.
309
+ * When fixedIn is an array of version strings (not PURLs), uses the strategy's
310
+ * selectVersion to pick the best candidate and constructs a PURL from the dependency ref.
311
+ * @param {object} issue
312
+ * @param {string} depPurl - the dependency PURL, used to construct fixedIn PURLs from version strings
313
+ * @param {object} strategy - version selection strategy
314
+ * @param {string} currentVersion - the dependency's current version
315
+ * @returns {string|undefined}
316
+ */
317
+ function getFixedInPurl(issue, depPurl, strategy, currentVersion) {
318
+ if (!issue.remediation) {
319
+ return undefined;
320
+ }
321
+ if (issue.remediation.trustedContent && issue.remediation.trustedContent.ref) {
322
+ return issue.remediation.trustedContent.ref;
323
+ }
324
+ const fixedIn = issue.remediation.fixedIn;
325
+ if (!fixedIn) {
326
+ return undefined;
327
+ }
328
+ if (typeof fixedIn === 'string') {
329
+ return fixedIn;
330
+ }
331
+ if (Array.isArray(fixedIn) && fixedIn.length > 0) {
332
+ const version = fixedIn.length > 1
333
+ ? strategy.selectVersion(fixedIn, currentVersion)
334
+ : fixedIn[0];
335
+ if (typeof version === 'string' && version.startsWith('pkg:')) {
336
+ return version;
337
+ }
338
+ if (typeof version === 'string' && depPurl) {
339
+ try {
340
+ const parsed = PackageURL.fromString(depPurl);
341
+ parsed.version = version;
342
+ return parsed.toString();
343
+ }
344
+ catch {
345
+ return undefined;
346
+ }
347
+ }
348
+ }
349
+ return undefined;
350
+ }
351
+ /**
352
+ * Extracts advisory objects from an issue.
353
+ * @param {object} issue
354
+ * @returns {Array<{id: string, url: string}>}
355
+ */
356
+ function extractAdvisories(issue) {
357
+ const advisories = [];
358
+ if (issue.remediation && issue.remediation.trustedContent) {
359
+ const tc = issue.remediation.trustedContent;
360
+ if (tc.advisory) {
361
+ advisories.push({
362
+ id: tc.advisory.id || tc.advisory,
363
+ url: tc.advisory.url || '',
364
+ });
365
+ }
366
+ }
367
+ if (issue.advisories) {
368
+ for (const adv of issue.advisories) {
369
+ advisories.push({
370
+ id: adv.id || adv,
371
+ url: adv.url || '',
372
+ });
373
+ }
374
+ }
375
+ return advisories;
376
+ }
377
+ /**
378
+ * Merges new advisories into existing list, avoiding duplicates by id.
379
+ * @param {Array<{id: string, url: string}>} existing
380
+ * @param {Array<{id: string, url: string}>} incoming
381
+ */
382
+ function mergeAdvisories(existing, incoming) {
383
+ const seen = new Set(existing.map(a => a.id));
384
+ for (const adv of incoming) {
385
+ if (!seen.has(adv.id)) {
386
+ existing.push(adv);
387
+ seen.add(adv.id);
388
+ }
389
+ }
390
+ }
391
+ /**
392
+ * Compares two version strings segment by segment.
393
+ * Returns positive if a > b, negative if a < b, zero if equal.
394
+ * Falls back to lexicographic comparison for non-numeric segments.
395
+ * @param {string} a
396
+ * @param {string} b
397
+ * @returns {number}
398
+ */
399
+ function compareVersions(a, b) {
400
+ const partsA = a.split('.');
401
+ const partsB = b.split('.');
402
+ const len = Math.max(partsA.length, partsB.length);
403
+ for (let i = 0; i < len; i++) {
404
+ const segA = partsA[i] || '';
405
+ const segB = partsB[i] || '';
406
+ const numA = Number(segA);
407
+ const numB = Number(segB);
408
+ if (Number.isFinite(numA) && Number.isFinite(numB)) {
409
+ const diff = numA - numB;
410
+ if (diff !== 0) {
411
+ return diff;
412
+ }
413
+ }
414
+ else {
415
+ const cmp = segA.localeCompare(segB);
416
+ if (cmp !== 0) {
417
+ return cmp;
418
+ }
419
+ }
420
+ }
421
+ return 0;
422
+ }
423
+ export const SEVERITY_ORDER = ['UNKNOWN', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL'];
424
+ /**
425
+ * Returns the higher of two severity strings, normalized to uppercase.
426
+ * @param {string} a
427
+ * @param {string} b
428
+ * @returns {string}
429
+ */
430
+ function higherSeverity(a, b) {
431
+ const upperA = a.toUpperCase();
432
+ const upperB = b.toUpperCase();
433
+ const indexA = SEVERITY_ORDER.indexOf(upperA);
434
+ const indexB = SEVERITY_ORDER.indexOf(upperB);
435
+ return indexA >= indexB ? upperA : upperB;
436
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Generates a formatted report from an array of remediation entries.
3
+ *
4
+ * @param {Array<{purl: string, groupId: string, artifactId: string, currentVersion: string,
5
+ * fixedInVersion: string, fixedInPurl: string, provider: string, source: string,
6
+ * advisories: Array<{id: string, url: string}>, severity: string, cves: string[]}>} remediations
7
+ * @param {object} [options]
8
+ * @param {'dependency'|'bundle'} [options.groupBy='dependency'] - grouping strategy
9
+ * @param {'markdown'|'json'} [options.format='markdown'] - output format
10
+ * @param {boolean} [options.dryRun=false] - when true, produces tabular summary
11
+ * @returns {string}
12
+ */
13
+ export function generateReport(remediations: Array<{
14
+ purl: string;
15
+ groupId: string;
16
+ artifactId: string;
17
+ currentVersion: string;
18
+ fixedInVersion: string;
19
+ fixedInPurl: string;
20
+ provider: string;
21
+ source: string;
22
+ advisories: Array<{
23
+ id: string;
24
+ url: string;
25
+ }>;
26
+ severity: string;
27
+ cves: string[];
28
+ }>, options?: {
29
+ groupBy?: "dependency" | "bundle" | undefined;
30
+ format?: "markdown" | "json" | undefined;
31
+ dryRun?: boolean | undefined;
32
+ }): string;
33
+ /**
34
+ * Generates a deterministic deduplication key for a remediation entry.
35
+ * Suitable for checking existing PR titles or branch names.
36
+ *
37
+ * @param {object} remediation - a single remediation entry
38
+ * @returns {string} stable key in the form `groupId:artifactId:newVersion`
39
+ */
40
+ export function generateDeduplicationKey(remediation: object): string;
@@ -0,0 +1,159 @@
1
+ /**
2
+ * Report generator that transforms remediation extractor output into structured
3
+ * markdown for PR bodies, CLI dry-run output, and JSON.
4
+ */
5
+ import { SEVERITY_ORDER } from './remediation.js';
6
+ /**
7
+ * Generates a formatted report from an array of remediation entries.
8
+ *
9
+ * @param {Array<{purl: string, groupId: string, artifactId: string, currentVersion: string,
10
+ * fixedInVersion: string, fixedInPurl: string, provider: string, source: string,
11
+ * advisories: Array<{id: string, url: string}>, severity: string, cves: string[]}>} remediations
12
+ * @param {object} [options]
13
+ * @param {'dependency'|'bundle'} [options.groupBy='dependency'] - grouping strategy
14
+ * @param {'markdown'|'json'} [options.format='markdown'] - output format
15
+ * @param {boolean} [options.dryRun=false] - when true, produces tabular summary
16
+ * @returns {string}
17
+ */
18
+ export function generateReport(remediations, options = {}) {
19
+ const { groupBy = 'dependency', format = 'markdown', dryRun = false } = options;
20
+ if (!remediations || remediations.length === 0) {
21
+ return format === 'json' ? '[]' : 'No remediations found.';
22
+ }
23
+ if (format === 'json') {
24
+ return JSON.stringify(remediations, null, '\t');
25
+ }
26
+ if (dryRun) {
27
+ return generateDryRunReport(remediations);
28
+ }
29
+ if (groupBy === 'bundle') {
30
+ return generateBundledReport(remediations);
31
+ }
32
+ return generatePerDependencyReport(remediations);
33
+ }
34
+ /**
35
+ * Generates a per-dependency markdown report with one section per remediation entry.
36
+ * @param {Array<object>} remediations
37
+ * @returns {string}
38
+ */
39
+ function generatePerDependencyReport(remediations) {
40
+ const sections = remediations.map(rem => {
41
+ const depName = rem.groupId
42
+ ? `${rem.groupId}:${rem.artifactId}`
43
+ : rem.artifactId;
44
+ const lines = [
45
+ `## Security Update: ${depName} ${rem.currentVersion} → ${rem.fixedInVersion}`,
46
+ '',
47
+ `**Provider:** ${rem.provider} | **Source:** ${rem.source}`,
48
+ '',
49
+ ];
50
+ if (rem.cves && rem.cves.length > 0) {
51
+ lines.push('### Vulnerabilities resolved');
52
+ lines.push('');
53
+ lines.push('| CVE | Severity | Advisory |');
54
+ lines.push('| --- | --- | --- |');
55
+ const advisoryLinks = formatAdvisoryLinks(rem.advisories);
56
+ for (const cve of rem.cves) {
57
+ lines.push(`| ${cve} | ${rem.severity} | ${advisoryLinks} |`);
58
+ }
59
+ }
60
+ return lines.join('\n');
61
+ });
62
+ return sections.join('\n\n');
63
+ }
64
+ /**
65
+ * Generates a bundled markdown report grouping all remediations by severity.
66
+ * @param {Array<object>} remediations
67
+ * @returns {string}
68
+ */
69
+ function generateBundledReport(remediations) {
70
+ const lines = ['# Security Update Summary', ''];
71
+ const bySeverity = groupBySeverity(remediations);
72
+ for (const severity of [...SEVERITY_ORDER].reverse()) {
73
+ const group = bySeverity.get(severity);
74
+ if (!group || group.length === 0) {
75
+ continue;
76
+ }
77
+ lines.push(`## ${severity}`);
78
+ lines.push('');
79
+ lines.push('| Dependency | Current | Fixed | Provider | CVEs | Advisory |');
80
+ lines.push('| --- | --- | --- | --- | --- | --- |');
81
+ for (const rem of group) {
82
+ const depName = rem.groupId
83
+ ? `${rem.groupId}:${rem.artifactId}`
84
+ : rem.artifactId;
85
+ const cves = (rem.cves || []).join(', ');
86
+ const advisoryLinks = formatAdvisoryLinks(rem.advisories);
87
+ lines.push(`| ${depName} | ${rem.currentVersion} | ${rem.fixedInVersion}`
88
+ + ` | ${rem.provider} | ${cves} | ${advisoryLinks} |`);
89
+ }
90
+ lines.push('');
91
+ }
92
+ return lines.join('\n').trimEnd();
93
+ }
94
+ /**
95
+ * Generates a tabular dry-run summary of proposed changes.
96
+ * @param {Array<object>} remediations
97
+ * @returns {string}
98
+ */
99
+ function generateDryRunReport(remediations) {
100
+ const lines = [
101
+ 'Proposed dependency updates:',
102
+ '',
103
+ '| Dependency | Current | Fixed | Severity | Provider |',
104
+ '| --- | --- | --- | --- | --- |',
105
+ ];
106
+ for (const rem of remediations) {
107
+ const depName = rem.groupId
108
+ ? `${rem.groupId}:${rem.artifactId}`
109
+ : rem.artifactId;
110
+ lines.push(`| ${depName} | ${rem.currentVersion} | ${rem.fixedInVersion}`
111
+ + ` | ${rem.severity} | ${rem.provider} |`);
112
+ }
113
+ return lines.join('\n');
114
+ }
115
+ /**
116
+ * Groups remediations by their severity.
117
+ * @param {Array<object>} remediations
118
+ * @returns {Map<string, Array<object>>}
119
+ */
120
+ function groupBySeverity(remediations) {
121
+ const map = new Map();
122
+ for (const severity of SEVERITY_ORDER) {
123
+ map.set(severity, []);
124
+ }
125
+ for (const rem of remediations) {
126
+ const sev = rem.severity || 'UNKNOWN';
127
+ if (!map.has(sev)) {
128
+ map.set(sev, []);
129
+ }
130
+ map.get(sev).push(rem);
131
+ }
132
+ return map;
133
+ }
134
+ /**
135
+ * Formats advisory entries into markdown links or plain text.
136
+ * @param {Array<{id: string, url: string}>} advisories
137
+ * @returns {string}
138
+ */
139
+ function formatAdvisoryLinks(advisories) {
140
+ if (!advisories || advisories.length === 0) {
141
+ return '-';
142
+ }
143
+ return advisories
144
+ .map(adv => adv.url ? `[${adv.id}](${adv.url})` : adv.id)
145
+ .join(', ');
146
+ }
147
+ /**
148
+ * Generates a deterministic deduplication key for a remediation entry.
149
+ * Suitable for checking existing PR titles or branch names.
150
+ *
151
+ * @param {object} remediation - a single remediation entry
152
+ * @returns {string} stable key in the form `groupId:artifactId:newVersion`
153
+ */
154
+ export function generateDeduplicationKey(remediation) {
155
+ const group = remediation.groupId || '';
156
+ const artifact = remediation.artifactId || '';
157
+ const version = remediation.fixedInVersion || '';
158
+ return `${group}:${artifact}:${version}`;
159
+ }