@trustify-da/trustify-da-javascript-client 0.3.0-ea.0c4e031 → 0.3.0-ea.0fbc1c1

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.
@@ -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
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Updates dependency versions in a Gradle version catalog (libs.versions.toml) file.
3
+ *
4
+ * Supports two version declaration patterns:
5
+ * - Centralized: [versions] section defines version aliases, [libraries] references via version.ref
6
+ * - Inline: [libraries] entries include version = "x.y.z" directly
7
+ *
8
+ * Uses smol-toml parse() to locate keys, then performs position-based string replacement
9
+ * on the raw content to preserve formatting and comments.
10
+ *
11
+ * @param {string} tomlContent - raw TOML file content
12
+ * @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges
13
+ * @returns {{content: string, applied: Array<{groupId: string, artifactId: string, newVersion: string, oldVersion: string}>, skipped: Array<{groupId: string, artifactId: string, newVersion: string, reason: string}>}}
14
+ */
15
+ export function updateTomlVersions(tomlContent: string, versionChanges: Array<{
16
+ groupId: string;
17
+ artifactId: string;
18
+ newVersion: string;
19
+ }>): {
20
+ content: string;
21
+ applied: Array<{
22
+ groupId: string;
23
+ artifactId: string;
24
+ newVersion: string;
25
+ oldVersion: string;
26
+ }>;
27
+ skipped: Array<{
28
+ groupId: string;
29
+ artifactId: string;
30
+ newVersion: string;
31
+ reason: string;
32
+ }>;
33
+ };
@@ -0,0 +1,244 @@
1
+ import { parse as parseToml } from 'smol-toml';
2
+ /**
3
+ * Updates dependency versions in a Gradle version catalog (libs.versions.toml) file.
4
+ *
5
+ * Supports two version declaration patterns:
6
+ * - Centralized: [versions] section defines version aliases, [libraries] references via version.ref
7
+ * - Inline: [libraries] entries include version = "x.y.z" directly
8
+ *
9
+ * Uses smol-toml parse() to locate keys, then performs position-based string replacement
10
+ * on the raw content to preserve formatting and comments.
11
+ *
12
+ * @param {string} tomlContent - raw TOML file content
13
+ * @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges
14
+ * @returns {{content: string, applied: Array<{groupId: string, artifactId: string, newVersion: string, oldVersion: string}>, skipped: Array<{groupId: string, artifactId: string, newVersion: string, reason: string}>}}
15
+ */
16
+ export function updateTomlVersions(tomlContent, versionChanges) {
17
+ const applied = [];
18
+ const skipped = [];
19
+ if (!versionChanges || versionChanges.length === 0) {
20
+ return { content: tomlContent, applied, skipped };
21
+ }
22
+ let parsed;
23
+ try {
24
+ parsed = parseToml(tomlContent);
25
+ }
26
+ catch (err) {
27
+ for (const change of versionChanges) {
28
+ skipped.push({
29
+ groupId: change.groupId,
30
+ artifactId: change.artifactId,
31
+ newVersion: change.newVersion,
32
+ reason: `Failed to parse TOML: ${err.message}`
33
+ });
34
+ }
35
+ return { content: tomlContent, applied, skipped };
36
+ }
37
+ const libraries = parsed.libraries || {};
38
+ const versions = parsed.versions || {};
39
+ const libraryIndex = buildLibraryIndex(libraries);
40
+ let updatedContent = tomlContent;
41
+ for (const change of versionChanges) {
42
+ const moduleKey = `${change.groupId}:${change.artifactId}`;
43
+ const alias = libraryIndex.get(moduleKey);
44
+ if (!alias) {
45
+ skipped.push({
46
+ groupId: change.groupId,
47
+ artifactId: change.artifactId,
48
+ newVersion: change.newVersion,
49
+ reason: `No library entry found for module ${moduleKey}`
50
+ });
51
+ continue;
52
+ }
53
+ const libEntry = libraries[alias];
54
+ const versionRef = getVersionRef(libEntry);
55
+ if (versionRef) {
56
+ const oldVersion = versions[versionRef];
57
+ if (oldVersion === undefined) {
58
+ skipped.push({
59
+ groupId: change.groupId,
60
+ artifactId: change.artifactId,
61
+ newVersion: change.newVersion,
62
+ reason: `Version ref "${versionRef}" not found in [versions] section`
63
+ });
64
+ continue;
65
+ }
66
+ if (oldVersion === change.newVersion) {
67
+ skipped.push({
68
+ groupId: change.groupId,
69
+ artifactId: change.artifactId,
70
+ newVersion: change.newVersion,
71
+ reason: `Version already at ${change.newVersion}`
72
+ });
73
+ continue;
74
+ }
75
+ updatedContent = replaceVersionInSection(updatedContent, versionRef, oldVersion, change.newVersion);
76
+ applied.push({
77
+ groupId: change.groupId,
78
+ artifactId: change.artifactId,
79
+ newVersion: change.newVersion,
80
+ oldVersion
81
+ });
82
+ }
83
+ else {
84
+ const inlineVersion = getInlineVersion(libEntry);
85
+ if (inlineVersion === undefined) {
86
+ skipped.push({
87
+ groupId: change.groupId,
88
+ artifactId: change.artifactId,
89
+ newVersion: change.newVersion,
90
+ reason: `No version.ref or inline version found for library "${alias}"`
91
+ });
92
+ continue;
93
+ }
94
+ if (inlineVersion === change.newVersion) {
95
+ skipped.push({
96
+ groupId: change.groupId,
97
+ artifactId: change.artifactId,
98
+ newVersion: change.newVersion,
99
+ reason: `Version already at ${change.newVersion}`
100
+ });
101
+ continue;
102
+ }
103
+ const beforeInline = updatedContent;
104
+ updatedContent = replaceInlineVersion(updatedContent, alias, inlineVersion, change.newVersion);
105
+ if (updatedContent !== beforeInline) {
106
+ applied.push({
107
+ groupId: change.groupId,
108
+ artifactId: change.artifactId,
109
+ newVersion: change.newVersion,
110
+ oldVersion: inlineVersion
111
+ });
112
+ }
113
+ else {
114
+ skipped.push({
115
+ groupId: change.groupId,
116
+ artifactId: change.artifactId,
117
+ newVersion: change.newVersion,
118
+ reason: `Inline version replacement did not match for library "${alias}"`
119
+ });
120
+ }
121
+ }
122
+ }
123
+ return { content: updatedContent, applied, skipped };
124
+ }
125
+ /**
126
+ * Builds a map from "groupId:artifactId" to the TOML library alias.
127
+ * @param {object} libraries - parsed [libraries] section
128
+ * @returns {Map<string, string>}
129
+ */
130
+ function buildLibraryIndex(libraries) {
131
+ const index = new Map();
132
+ for (const [alias, entry] of Object.entries(libraries)) {
133
+ const module = getModule(entry);
134
+ if (module) {
135
+ index.set(module, alias);
136
+ }
137
+ }
138
+ return index;
139
+ }
140
+ /**
141
+ * Extracts the module identifier from a library entry.
142
+ * Handles both string shorthand ("group:artifact:version") and object notation
143
+ * ({ module = "group:artifact" } or { group = "...", name = "..." }).
144
+ * @param {string|object} entry
145
+ * @returns {string|undefined} "groupId:artifactId"
146
+ */
147
+ function getModule(entry) {
148
+ if (typeof entry === 'string') {
149
+ const parts = entry.split(':');
150
+ if (parts.length >= 2) {
151
+ return `${parts[0]}:${parts[1]}`;
152
+ }
153
+ return undefined;
154
+ }
155
+ if (entry.module) {
156
+ const parts = entry.module.split(':');
157
+ if (parts.length >= 2) {
158
+ return `${parts[0]}:${parts[1]}`;
159
+ }
160
+ return entry.module;
161
+ }
162
+ if (entry.group && entry.name) {
163
+ return `${entry.group}:${entry.name}`;
164
+ }
165
+ return undefined;
166
+ }
167
+ /**
168
+ * Extracts the version.ref from a library entry, if present.
169
+ * @param {string|object} entry
170
+ * @returns {string|undefined}
171
+ */
172
+ function getVersionRef(entry) {
173
+ if (typeof entry === 'object' && entry.version) {
174
+ if (typeof entry.version === 'object' && entry.version.ref) {
175
+ return entry.version.ref;
176
+ }
177
+ }
178
+ return undefined;
179
+ }
180
+ /**
181
+ * Extracts an inline version string from a library entry.
182
+ * Handles { version = "1.2.3" } and string shorthand "group:artifact:version".
183
+ * @param {string|object} entry
184
+ * @returns {string|undefined}
185
+ */
186
+ function getInlineVersion(entry) {
187
+ if (typeof entry === 'string') {
188
+ const parts = entry.split(':');
189
+ if (parts.length >= 3) {
190
+ return parts[2];
191
+ }
192
+ return undefined;
193
+ }
194
+ if (typeof entry === 'object' && entry.version) {
195
+ if (typeof entry.version === 'string') {
196
+ return entry.version;
197
+ }
198
+ }
199
+ return undefined;
200
+ }
201
+ /**
202
+ * Replaces a version value using position-based string replacement.
203
+ * Targets lines matching: key = "oldVersion"
204
+ * @param {string} content - raw TOML content
205
+ * @param {string} key - version alias key
206
+ * @param {string} oldVersion - current version string
207
+ * @param {string} newVersion - replacement version string
208
+ * @returns {string} updated content
209
+ */
210
+ function replaceVersionInSection(content, key, oldVersion, newVersion) {
211
+ const escapedKey = escapeRegExp(key);
212
+ const escapedOld = escapeRegExp(oldVersion);
213
+ const pattern = new RegExp(`^(\\s*${escapedKey}\\s*=\\s*")${escapedOld}("\\s*)$`, 'm');
214
+ return content.replace(pattern, (_, p1, p2) => `${p1}${newVersion}${p2}`);
215
+ }
216
+ /**
217
+ * Replaces an inline version in a library entry.
218
+ * Targets patterns like: version = "oldVersion" or version ="oldVersion"
219
+ * on the line containing the library alias.
220
+ * @param {string} content - raw TOML content
221
+ * @param {string} alias - library alias
222
+ * @param {string} oldVersion - current version string
223
+ * @param {string} newVersion - replacement version string
224
+ * @returns {string} updated content
225
+ */
226
+ function replaceInlineVersion(content, alias, oldVersion, newVersion) {
227
+ const escapedAlias = escapeRegExp(alias);
228
+ const escapedOld = escapeRegExp(oldVersion);
229
+ const pattern = new RegExp(`^(\\s*${escapedAlias}\\s*=\\s*\\{[^}]*version\\s*=\\s*")${escapedOld}("[^}]*\\}\\s*)$`, 'm');
230
+ const result = content.replace(pattern, (_, p1, p2) => `${p1}${newVersion}${p2}`);
231
+ if (result !== content) {
232
+ return result;
233
+ }
234
+ const stringPattern = new RegExp(`^(\\s*${escapedAlias}\\s*=\\s*"[^:]+:[^:]+:)${escapedOld}("\\s*)$`, 'm');
235
+ return content.replace(stringPattern, (_, p1, p2) => `${p1}${newVersion}${p2}`);
236
+ }
237
+ /**
238
+ * Escapes special regex characters in a string.
239
+ * @param {string} str
240
+ * @returns {string}
241
+ */
242
+ function escapeRegExp(str) {
243
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
244
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trustify-da/trustify-da-javascript-client",
3
- "version": "0.3.0-ea.0c4e031",
3
+ "version": "0.3.0-ea.0fbc1c1",
4
4
  "description": "Code-Ready Dependency Analytics JavaScript API.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/guacsec/trustify-da-javascript-client#README.md",