-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathassessmentManager.ts
More file actions
391 lines (344 loc) · 14.7 KB
/
assessmentManager.ts
File metadata and controls
391 lines (344 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as fs from 'fs';
import * as semver from 'semver';
import * as glob from 'glob';
import { promisify } from 'util';
const globAsync = promisify(glob);
import { Uri } from 'vscode';
import { Jdtls } from "../java/jdtls";
import { NodeKind, type INodeData } from "../java/nodeData";
import { type DependencyCheckItem, type UpgradeIssue, type PackageDescription, UpgradeReason } from "./type";
import { DEPENDENCY_JAVA_RUNTIME } from "./dependency.metadata";
import { Upgrade } from '../constants';
import { buildPackageId } from './utility';
import metadataManager from './metadataManager';
import { sendInfo } from 'vscode-extension-telemetry-wrapper';
import { batchGetCVEIssues } from './cve';
import { ContainerPath } from '../views/containerNode';
function packageNodeToDescription(node: INodeData): PackageDescription | null {
const version = node.metaData?.["maven.version"];
const groupId = node.metaData?.["maven.groupId"];
const artifactId = node.metaData?.["maven.artifactId"];
if (!version || !groupId || !artifactId) {
return null;
}
return { version, groupId, artifactId };
}
function getVersionRange(versions: Set<string>) : string {
const versionList = [...versions].sort((a, b) => {
const semverA = semver.coerce(a);
const semverB = semver.coerce(b);
if (!semverA || !semverB) {
return a.localeCompare(b);
}
return semver.compare(semverA, semverB);
});
if (versionList.length === 1) {
return versionList[0];
}
return `${versionList[0]}|${versionList[versionList.length - 1]}`;
}
function collectVersionRange(pkgs: PackageDescription[]): Record<string, string> {
const versionMap: Record<string, Set<string>> = {};
for (const pkg of pkgs) {
const groupId = pkg.groupId;
if (!versionMap[groupId]) {
versionMap[groupId] = new Set();
}
versionMap[groupId].add(pkg.version);
}
return Object.fromEntries(Object.entries(versionMap).map(([groupId, versions]) => [groupId, getVersionRange(versions)]));
}
function getJavaIssues(data: INodeData): UpgradeIssue[] {
const javaVersion = data.metaData?.MaxSourceVersion as number | undefined;
const { name, supportedVersion } = DEPENDENCY_JAVA_RUNTIME;
if (!javaVersion) {
return [];
}
const currentSemVer = semver.coerce(javaVersion);
const [javaRuntimeGroupId, javaRuntimeArtifactId] = Upgrade.PACKAGE_ID_FOR_JAVA_RUNTIME.split(":");
sendInfo("", {
operationName: "java.dependency.assessmentManager.getJavaVersionRange",
versionRangeByGroupId: JSON.stringify(
collectVersionRange([{
groupId: javaRuntimeGroupId,
artifactId: javaRuntimeArtifactId,
version: String(javaVersion),
}]),
),
});
if (currentSemVer && !semver.satisfies(currentSemVer, supportedVersion)) {
return [{
...DEPENDENCY_JAVA_RUNTIME,
packageId: Upgrade.PACKAGE_ID_FOR_JAVA_RUNTIME,
packageDisplayName: name,
currentVersion: String(javaVersion),
}];
}
return [];
}
function getUpgradeForDependency(versionString: string, supportedVersionDefinition: DependencyCheckItem, packageId: string): UpgradeIssue | null {
const reason = supportedVersionDefinition.reason;
switch (reason) {
case UpgradeReason.DEPRECATED: {
return {
...supportedVersionDefinition,
packageDisplayName: supportedVersionDefinition.name,
reason,
currentVersion: versionString,
packageId,
};
}
case UpgradeReason.END_OF_LIFE: {
const currentSemVer = semver.coerce(versionString);
if (currentSemVer && !semver.satisfies(currentSemVer, supportedVersionDefinition.supportedVersion)) {
return {
...supportedVersionDefinition,
packageDisplayName: supportedVersionDefinition.name,
reason,
currentVersion: versionString,
packageId,
};
}
}
}
return null;
}
function getPackageUpgradeMetadata(pkg: PackageDescription): DependencyCheckItem | null {
const { groupId, artifactId } = pkg;
const packageId = buildPackageId(groupId, artifactId);
return metadataManager.getMetadataById(packageId) ?? null;
}
function getDependencyIssue(pkg: PackageDescription): UpgradeIssue | null {
const supportedVersionDefinition = getPackageUpgradeMetadata(pkg);
const version = pkg.version;
if (!version || !supportedVersionDefinition) {
return null;
}
const { groupId, artifactId } = pkg;
const packageId = buildPackageId(groupId, artifactId);
return getUpgradeForDependency(version, supportedVersionDefinition, packageId);
}
async function getDependencyIssues(dependencies: PackageDescription[]): Promise<UpgradeIssue[]> {
const issues = dependencies.map(getDependencyIssue).filter((x): x is UpgradeIssue => Boolean(x));
const versionRangeByGroupId = collectVersionRange(dependencies.filter(pkg => getPackageUpgradeMetadata(pkg)));
if (Object.keys(versionRangeByGroupId).length > 0) {
sendInfo("", {
operationName: "java.dependency.assessmentManager.getDependencyVersionRange",
versionRangeByGroupId: JSON.stringify(versionRangeByGroupId),
});
}
return issues;
}
async function getWorkspaceIssues(projectDeps:{projectNode: INodeData, dependencies: PackageDescription[]}[]): Promise<UpgradeIssue[]> {
const issues: UpgradeIssue[] = [];
const dependenciesSet: Set<PackageDescription> = new Set();
for (const { projectNode, dependencies } of projectDeps) {
issues.push(...getJavaIssues(projectNode));
dependencies.forEach(dep => dependenciesSet.add(dep));
}
issues.push(...await getCVEIssues(Array.from(dependenciesSet)));
issues.push(...await getDependencyIssues(Array.from(dependenciesSet)));
return issues;
}
/**
* Find all pom.xml files in a directory using glob
*/
async function findAllPomFiles(dir: string): Promise<string[]> {
try {
return await globAsync('**/pom.xml', {
cwd: dir,
absolute: true,
nodir: true,
ignore: ['**/node_modules/**', '**/target/**', '**/.git/**', '**/.idea/**', '**/.vscode/**']
});
} catch {
return [];
}
}
/**
* Parse dependencies from a single pom.xml file
*/
function parseDependenciesFromSinglePom(pomPath: string): Set<string> {
const directDeps = new Set<string>();
try {
const pomContent = fs.readFileSync(pomPath, 'utf-8');
// Extract dependencies from <dependencies> section (not inside <dependencyManagement>)
// First, remove dependencyManagement sections to avoid including managed deps
const withoutDepMgmt = pomContent.replace(/<dependencyManagement>[\s\S]*?<\/dependencyManagement>/g, '');
// Match <dependency> blocks and extract groupId and artifactId
const dependencyRegex = /<dependency>\s*<groupId>([^<]+)<\/groupId>\s*<artifactId>([^<]+)<\/artifactId>/g;
let match = dependencyRegex.exec(withoutDepMgmt);
while (match !== null) {
const groupId = match[1].trim();
const artifactId = match[2].trim();
// Skip property references like ${project.groupId}
if (!groupId.includes('${') && !artifactId.includes('${')) {
directDeps.add(`${groupId}:${artifactId}`);
}
match = dependencyRegex.exec(withoutDepMgmt);
}
} catch {
// If we can't read the pom, return empty set
}
return directDeps;
}
/**
* Parse direct dependencies from all pom.xml files in the project.
* Finds all pom.xml files starting from the project root and parses them to collect dependencies.
*/
async function parseDirectDependenciesFromPom(projectPath: string): Promise<Set<string>> {
const directDeps = new Set<string>();
// Find all pom.xml files in the project starting from the project root
const allPomFiles = await findAllPomFiles(projectPath);
// Parse each pom.xml and collect dependencies
for (const pom of allPomFiles) {
const deps = parseDependenciesFromSinglePom(pom);
deps.forEach(dep => directDeps.add(dep));
}
return directDeps;
}
/**
* Find all Gradle build files in a directory using glob
*/
async function findAllGradleFiles(dir: string): Promise<string[]> {
try {
return await globAsync('**/{build.gradle,build.gradle.kts}', {
cwd: dir,
absolute: true,
nodir: true,
ignore: ['**/node_modules/**', '**/build/**', '**/.git/**', '**/.idea/**', '**/.vscode/**', '**/.gradle/**']
});
} catch {
return [];
}
}
/**
* Parse dependencies from a single Gradle build file
*/
function parseDependenciesFromSingleGradle(gradlePath: string): Set<string> {
const directDeps = new Set<string>();
try {
const gradleContent = fs.readFileSync(gradlePath, 'utf-8');
// Match common dependency configurations:
// implementation 'group:artifact:version'
// implementation "group:artifact:version"
// api 'group:artifact:version'
// compileOnly, runtimeOnly, testImplementation, etc.
const shortFormRegex = /(?:implementation|api|compile|compileOnly|runtimeOnly|testImplementation|testCompileOnly|testRuntimeOnly)\s*\(?['"]([^:'"]+):([^:'"]+)(?::[^'"]*)?['"]\)?/g;
let match = shortFormRegex.exec(gradleContent);
while (match !== null) {
const groupId = match[1].trim();
const artifactId = match[2].trim();
if (!groupId.includes('$') && !artifactId.includes('$')) {
directDeps.add(`${groupId}:${artifactId}`);
}
match = shortFormRegex.exec(gradleContent);
}
// Match map notation: implementation group: 'x', name: 'y', version: 'z'
const mapFormRegex = /(?:implementation|api|compile|compileOnly|runtimeOnly|testImplementation|testCompileOnly|testRuntimeOnly)\s*\(?group:\s*['"]([^'"]+)['"]\s*,\s*name:\s*['"]([^'"]+)['"]/g;
match = mapFormRegex.exec(gradleContent);
while (match !== null) {
const groupId = match[1].trim();
const artifactId = match[2].trim();
if (!groupId.includes('$') && !artifactId.includes('$')) {
directDeps.add(`${groupId}:${artifactId}`);
}
match = mapFormRegex.exec(gradleContent);
}
} catch {
// If we can't read the gradle file, return empty set
}
return directDeps;
}
/**
* Parse direct dependencies from all Gradle build files in the project.
* Finds all build.gradle and build.gradle.kts files and parses them to collect dependencies.
*/
async function parseDirectDependenciesFromGradle(projectPath: string): Promise<Set<string>> {
const directDeps = new Set<string>();
// Find all Gradle build files in the project
const allGradleFiles = await findAllGradleFiles(projectPath);
// Parse each gradle file and collect dependencies
for (const gradleFile of allGradleFiles) {
const deps = parseDependenciesFromSingleGradle(gradleFile);
deps.forEach(dep => directDeps.add(dep));
}
return directDeps;
}
export async function getDirectDependencies(projectNode: INodeData): Promise<PackageDescription[]> {
const projectStructureData = await Jdtls.getPackageData({ kind: NodeKind.Project, projectUri: projectNode.uri });
// Only include Maven or Gradle containers (not JRE or other containers)
const dependencyContainers = projectStructureData.filter(x =>
x.kind === NodeKind.Container &&
(x.path?.startsWith(ContainerPath.Maven) || x.path?.startsWith(ContainerPath.Gradle))
);
if (dependencyContainers.length === 0) {
return [];
}
const allPackages = await Promise.allSettled(
dependencyContainers.map(async (packageContainer) => {
const packageNodes = await Jdtls.getPackageData({
kind: NodeKind.Container,
projectUri: projectNode.uri,
path: packageContainer.path,
});
return packageNodes
.map(packageNodeToDescription)
.filter((x): x is PackageDescription => Boolean(x));
})
);
const fulfilled = allPackages.filter((x): x is PromiseFulfilledResult<PackageDescription[]> => x.status === "fulfilled");
const failedPackageCount = allPackages.length - fulfilled.length;
if (failedPackageCount > 0) {
sendInfo("", {
operationName: "java.dependency.assessmentManager.getDirectDependencies.rejected",
failedPackageCount: String(failedPackageCount),
});
}
let dependencies = fulfilled.map(x => x.value).flat();
if (!dependencies) {
sendInfo("", {
operationName: "java.dependency.assessmentManager.getDirectDependencies.noDependencyInfo"
});
return [];
}
// Determine build type from dependency containers
const isMaven = dependencyContainers.some(x => x.path?.startsWith(ContainerPath.Maven));
// Get direct dependency identifiers from build files
let directDependencyIds: Set<string> | null = null;
if (projectNode.uri && dependencyContainers.length > 0) {
try {
const projectPath = Uri.parse(projectNode.uri).fsPath;
if (isMaven) {
directDependencyIds = await parseDirectDependenciesFromPom(projectPath);
} else {
directDependencyIds = await parseDirectDependenciesFromGradle(projectPath);
}
} catch {
// Ignore errors
}
}
if (!directDependencyIds) {
sendInfo("", {
operationName: "java.dependency.assessmentManager.getDirectDependencies.noDirectDependencyInfo"
});
//TODO: fallback to return all dependencies if we cannot parse direct dependencies or just return empty?
return dependencies;
}
// Filter to only direct dependencies if we have build file info
if (directDependencyIds && directDependencyIds.size > 0) {
dependencies = dependencies.filter(pkg =>
directDependencyIds!.has(`${pkg.groupId}:${pkg.artifactId}`)
);
}
return dependencies;
}
async function getCVEIssues(dependencies: PackageDescription[]): Promise<UpgradeIssue[]> {
const gavCoordinates = dependencies.map(pkg => `${pkg.groupId}:${pkg.artifactId}:${pkg.version}`);
return batchGetCVEIssues(gavCoordinates);
}
export default {
getWorkspaceIssues,
};