-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathnotificationManager.ts
More file actions
145 lines (123 loc) · 5.66 KB
/
Copy pathnotificationManager.ts
File metadata and controls
145 lines (123 loc) · 5.66 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { commands, ExtensionContext, window } from "vscode";
import { UpgradeReason, type IUpgradeIssuesRenderer, type UpgradeIssue } from "../type";
import { buildCVENotificationMessage, buildFixPrompt, buildNotificationMessage, getExtensionState } from "../utility";
import { Commands } from "../../commands";
import { Settings } from "../../settings";
import { instrumentOperation, sendInfo } from "vscode-extension-telemetry-wrapper";
import { ExtensionName, Upgrade } from "../../constants";
import { CveUpgradeIssue } from "../cve";
const KEY_PREFIX = 'javaupgrade.notificationManager';
const NEXT_SHOW_TS_KEY = `${KEY_PREFIX}.nextShowTs`;
const BUTTON_TEXT_UPGRADE = "Upgrade Now";
const BUTTON_TEXT_FIX_CVE = "Fix Now";
const BUTTON_TEXT_INSTALL_AND_UPGRADE = "Install Extension and Upgrade";
const BUTTON_TEXT_INSTALL_AND_FIX_CVE = "Install Extension and Fix";
const BUTTON_TEXT_UPDATE_AND_UPGRADE = "Update Extension and Upgrade";
const BUTTON_TEXT_UPDATE_AND_FIX_CVE = "Update Extension and Fix";
const BUTTON_TEXT_NOT_NOW = "Not Now";
const SECONDS_IN_A_DAY = 24 * 60 * 60;
const SECONDS_COUNT_BEFORE_NOTIFICATION_RESHOW = 10 * SECONDS_IN_A_DAY;
function getNowTs() {
return Number(new Date()) / 1000;
}
class NotificationManager implements IUpgradeIssuesRenderer {
private hasShown = false;
private context?: ExtensionContext;
initialize(context: ExtensionContext) {
this.context = context;
}
async render(issues: UpgradeIssue[]) {
return (instrumentOperation(
"java.dependency.showUpgradeNotification",
async (operationId: string) => {
if (issues.length === 0) {
return;
}
// Filter to only CVE issues and cast to CveUpgradeIssue[]
const cveIssues = issues.filter(
(i): i is CveUpgradeIssue => i.reason === UpgradeReason.CVE
);
const nonCVEIssues = issues.filter(
(i) => i.reason !== UpgradeReason.CVE
);
const hasCVEIssue = cveIssues.length > 0;
const issue = hasCVEIssue ? cveIssues[0] : nonCVEIssues[0];
if (!this.shouldShow()) {
return;
}
if (this.hasShown) {
return;
}
this.hasShown = true;
const extensionState = getExtensionState(ExtensionName.APP_MODERNIZATION_UPGRADE_FOR_JAVA);
const prompt = buildFixPrompt(issue);
let notificationMessage = "";
if (hasCVEIssue) {
notificationMessage = buildCVENotificationMessage(cveIssues, extensionState);
} else {
notificationMessage = buildNotificationMessage(issue, extensionState);
}
let upgradeButtonText: string;
let fixCVEButtonText: string;
switch (extensionState) {
case "up-to-date":
upgradeButtonText = BUTTON_TEXT_UPGRADE;
fixCVEButtonText = BUTTON_TEXT_FIX_CVE;
break;
case "outdated":
upgradeButtonText = BUTTON_TEXT_UPDATE_AND_UPGRADE;
fixCVEButtonText = BUTTON_TEXT_UPDATE_AND_FIX_CVE;
break;
case "not-installed":
upgradeButtonText = BUTTON_TEXT_INSTALL_AND_UPGRADE;
fixCVEButtonText = BUTTON_TEXT_INSTALL_AND_FIX_CVE;
break;
}
sendInfo(operationId, {
operationName: "java.dependency.upgradeNotification.show",
extensionState,
source: hasCVEIssue ? Upgrade.SOURCE_CVE : Upgrade.SOURCE_JAVA_UPGRADE,
});
const buttons = hasCVEIssue
? [fixCVEButtonText, BUTTON_TEXT_NOT_NOW]
: [upgradeButtonText, BUTTON_TEXT_NOT_NOW];
const selection = await window.showInformationMessage(
notificationMessage,
...buttons
);
sendInfo(operationId, {
operationName: "java.dependency.upgradeNotification.runUpgrade",
choice: selection ?? "",
});
switch (selection) {
case fixCVEButtonText: {
commands.executeCommand(Commands.JAVA_UPGRADE_WITH_COPILOT, prompt, Upgrade.SOURCE_CVE);
break;
}
case upgradeButtonText: {
commands.executeCommand(Commands.JAVA_UPGRADE_WITH_COPILOT, prompt, Upgrade.SOURCE_JAVA_UPGRADE);
break;
}
case BUTTON_TEXT_NOT_NOW: {
this.setNextShowTs(getNowTs() + SECONDS_COUNT_BEFORE_NOTIFICATION_RESHOW);
break;
}
}
}
))();
}
private shouldShow() {
return Settings.getEnableDependencyCheckup()
&& ((this.getNextShowTs() ?? 0) <= getNowTs());
}
private getNextShowTs() {
return this.context?.globalState.get<number>(NEXT_SHOW_TS_KEY);
}
private setNextShowTs(num: number) {
return this.context?.globalState.update(NEXT_SHOW_TS_KEY, num);
}
}
const notificationManager = new NotificationManager();
export default notificationManager;