Skip to content

Commit bf7eb04

Browse files
feat: NotificationManager
1 parent 3988de5 commit bf7eb04

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import { commands, window } from "vscode";
5+
import type { FileIssues } from "../type";
6+
import { buildFixPrompt, buildMessage } from "../utility";
7+
import { Commands } from "../../commands";
8+
9+
class NotificationManager {
10+
private hasShown = false;
11+
12+
13+
async refresh(issues: FileIssues) {
14+
const targetIssue = Object.values(issues)[0];
15+
16+
if (!targetIssue) {
17+
return;
18+
}
19+
20+
if (this.hasShown) {
21+
return;
22+
}
23+
24+
const buttonText = "Upgrade";
25+
const selection = await window.showInformationMessage(buildMessage(targetIssue), buttonText);
26+
this.hasShown = true;
27+
if (selection === buttonText) {
28+
commands.executeCommand(Commands.VIEW_TRIGGER_JAVA_UPGRADE_TOOL, buildFixPrompt(targetIssue));
29+
}
30+
}
31+
}
32+
33+
const notificationManager = new NotificationManager();
34+
export default notificationManager;

src/upgrade/type.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
export type DependencyCheckItem = { name: string, supportedVersion: string };
22
export type DependencyCheckMetadata = Record<string, DependencyCheckItem>;
3-
export type DependencyCheckResult = DependencyCheckItem & { packageRuleUsed: string };
3+
export type DependencyCheckResult = DependencyCheckItem & { packageRuleUsed: string };
4+
5+
export enum UpgradeReason {
6+
END_OF_LIFE,
7+
CVE,
8+
ENGINE_TOO_OLD,
9+
};
10+
11+
export type UpgradeIssue = {
12+
packageId: string;
13+
packageDisplayName?: string;
14+
reason: UpgradeReason;
15+
currentVersion: string;
16+
suggestedVersion?: string;
17+
};
18+
19+
export type FileIssues = Record</* packageId */string, UpgradeIssue>;

src/upgrade/utility.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
import { UpgradeReason, type UpgradeIssue } from "./type";
2+
3+
export function buildMessage(issue: UpgradeIssue): string {
4+
const { packageId, packageDisplayName, currentVersion, reason } = issue;
5+
const name = packageDisplayName ?? packageId;
6+
7+
switch (reason) {
8+
case UpgradeReason.END_OF_LIFE: {
9+
return `Your project dependency ${name} (${currentVersion}) is in end-of-life. Consider upgrading using GitHub Copilot for better security and performance.`;
10+
}
11+
case UpgradeReason.CVE: {
12+
return `Your project dependency ${name} (${currentVersion}) has CVE. Consider upgrading using GitHub Copilot for better security.`;
13+
}
14+
case UpgradeReason.ENGINE_TOO_OLD: {
15+
return `Your project Java version (${currentVersion}) is too old. Consider upgrading using GitHub Copilot for better performance and features.`;
16+
}
17+
}
18+
}
19+
20+
export function buildFixPrompt(issue: UpgradeIssue): string {
21+
const { packageId, packageDisplayName, reason, suggestedVersion } = issue;
22+
const name = packageDisplayName ?? packageId;
23+
24+
const suffix = [
25+
...(suggestedVersion ? [`The target version is ${suggestedVersion}.`] : [])
26+
];
27+
28+
switch (reason) {
29+
case UpgradeReason.END_OF_LIFE: {
30+
return [`Upgrade the package ${name} using Java Upgrade Tool.`, ...suffix].join(" ");
31+
}
32+
case UpgradeReason.CVE: {
33+
return [`Upgrade the package ${name} to resolve CVE using Java Upgrade Tool.`, ...suffix].join(" ");
34+
}
35+
case UpgradeReason.ENGINE_TOO_OLD: {
36+
return [`Upgrade Java version using Java Upgrade Tool.`, ...suffix].join(" ");
37+
}
38+
}
39+
}
40+
141
export function buildPackageId(groupId: string, artifactId: string): string {
242
return `${groupId}:${artifactId}`;
343
}

0 commit comments

Comments
 (0)