22// Licensed under the MIT license.
33
44import { commands , ExtensionContext , extensions , window } from "vscode" ;
5+ import * as semver from "semver" ;
56import { UpgradeReason , type IUpgradeIssuesRenderer , type UpgradeIssue } from "../type" ;
6- import { buildCVENotificationMessage , buildFixPrompt , buildNotificationMessage } from "../utility" ;
7+ import { buildCVENotificationMessage , buildFixPrompt , buildNotificationMessage , type ExtensionState } from "../utility" ;
78import { Commands } from "../../commands" ;
89import { Settings } from "../../settings" ;
910import { instrumentOperation , sendInfo } from "vscode-extension-telemetry-wrapper" ;
10- import { ExtensionName } from "../../constants" ;
11+ import { ExtensionName , Upgrade } from "../../constants" ;
1112import { CveUpgradeIssue } from "../cve" ;
13+ import { UpgradeTelemetry } from "../telemetryConstants" ;
1214
1315const KEY_PREFIX = 'javaupgrade.notificationManager' ;
1416const NEXT_SHOW_TS_KEY = `${ KEY_PREFIX } .nextShowTs` ;
@@ -17,6 +19,8 @@ const BUTTON_TEXT_UPGRADE = "Upgrade Now";
1719const BUTTON_TEXT_FIX_CVE = "Fix Now" ;
1820const BUTTON_TEXT_INSTALL_AND_UPGRADE = "Install Extension and Upgrade" ;
1921const BUTTON_TEXT_INSTALL_AND_FIX_CVE = "Install Extension and Fix" ;
22+ const BUTTON_TEXT_UPDATE_AND_UPGRADE = "Update Extension and Upgrade" ;
23+ const BUTTON_TEXT_UPDATE_AND_FIX_CVE = "Update Extension and Fix" ;
2024const BUTTON_TEXT_NOT_NOW = "Not Now" ;
2125
2226const SECONDS_IN_A_DAY = 24 * 60 * 60 ;
@@ -26,6 +30,61 @@ function getNowTs() {
2630 return Number ( new Date ( ) ) / 1000 ;
2731}
2832
33+ export type { ExtensionState } from "../utility" ;
34+
35+ export interface NotificationContent {
36+ message : string ;
37+ upgradeButtonText : string ;
38+ fixCVEButtonText : string ;
39+ }
40+
41+ export function getExtensionState ( extensionVersion : string | undefined ) : ExtensionState {
42+ if ( ! extensionVersion ) {
43+ return "not-installed" ;
44+ }
45+ if ( semver . gte ( extensionVersion , Upgrade . MIN_APPMOD_VERSION ) ) {
46+ return "up-to-date" ;
47+ }
48+ return "outdated" ;
49+ }
50+
51+ export function buildNotificationContent (
52+ issues : UpgradeIssue [ ] ,
53+ extensionState : ExtensionState ,
54+ ) : NotificationContent {
55+ const cveIssues = issues . filter (
56+ ( i ) : i is CveUpgradeIssue => i . reason === UpgradeReason . CVE
57+ ) ;
58+ const nonCVEIssues = issues . filter (
59+ ( i ) => i . reason !== UpgradeReason . CVE
60+ ) ;
61+ const hasCVEIssue = cveIssues . length > 0 ;
62+
63+ const message = hasCVEIssue
64+ ? buildCVENotificationMessage ( cveIssues , extensionState )
65+ : buildNotificationMessage ( nonCVEIssues [ 0 ] , extensionState ) ;
66+
67+ let upgradeButtonText : string ;
68+ let fixCVEButtonText : string ;
69+
70+ switch ( extensionState ) {
71+ case "up-to-date" :
72+ upgradeButtonText = BUTTON_TEXT_UPGRADE ;
73+ fixCVEButtonText = BUTTON_TEXT_FIX_CVE ;
74+ break ;
75+ case "outdated" :
76+ upgradeButtonText = BUTTON_TEXT_UPDATE_AND_UPGRADE ;
77+ fixCVEButtonText = BUTTON_TEXT_UPDATE_AND_FIX_CVE ;
78+ break ;
79+ case "not-installed" :
80+ upgradeButtonText = BUTTON_TEXT_INSTALL_AND_UPGRADE ;
81+ fixCVEButtonText = BUTTON_TEXT_INSTALL_AND_FIX_CVE ;
82+ break ;
83+ }
84+
85+ return { message, upgradeButtonText, fixCVEButtonText } ;
86+ }
87+
2988class NotificationManager implements IUpgradeIssuesRenderer {
3089 private hasShown = false ;
3190 private context ?: ExtensionContext ;
@@ -42,16 +101,6 @@ class NotificationManager implements IUpgradeIssuesRenderer {
42101 return ;
43102 }
44103
45- // Filter to only CVE issues and cast to CveUpgradeIssue[]
46- const cveIssues = issues . filter (
47- ( i ) : i is CveUpgradeIssue => i . reason === UpgradeReason . CVE
48- ) ;
49- const nonCVEIssues = issues . filter (
50- ( i ) => i . reason !== UpgradeReason . CVE
51- ) ;
52- const hasCVEIssue = cveIssues . length > 0 ;
53- const issue = hasCVEIssue ? cveIssues [ 0 ] : nonCVEIssues [ 0 ] ;
54-
55104 if ( ! this . shouldShow ( ) ) {
56105 return ;
57106 }
@@ -61,39 +110,42 @@ class NotificationManager implements IUpgradeIssuesRenderer {
61110 }
62111 this . hasShown = true ;
63112
64- const hasExtension = ! ! extensions . getExtension ( ExtensionName . APP_MODERNIZATION_UPGRADE_FOR_JAVA ) ;
65- const prompt = buildFixPrompt ( issue ) ;
113+ const ext = extensions . getExtension ( ExtensionName . APP_MODERNIZATION_UPGRADE_FOR_JAVA ) ;
114+ const extensionState = getExtensionState ( ext ?. packageJSON ?. version ) ;
115+ const { message, upgradeButtonText, fixCVEButtonText } = buildNotificationContent ( issues , extensionState ) ;
66116
67- let notificationMessage = "" ;
117+ const hasCVEIssue = issues . some ( i => i . reason === UpgradeReason . CVE ) ;
118+ const issueType = hasCVEIssue ? "cve" : "upgrade" ;
119+ const issue = hasCVEIssue
120+ ? issues . find ( ( i ) : i is CveUpgradeIssue => i . reason === UpgradeReason . CVE ) !
121+ : issues . find ( i => i . reason !== UpgradeReason . CVE ) ! ;
122+ const prompt = buildFixPrompt ( issue ) ;
68123
69- if ( hasCVEIssue ) {
70- notificationMessage = buildCVENotificationMessage ( cveIssues , hasExtension ) ;
71- } else {
72- notificationMessage = buildNotificationMessage ( issue , hasExtension ) ;
73- }
74- const upgradeButtonText = hasExtension ? BUTTON_TEXT_UPGRADE : BUTTON_TEXT_INSTALL_AND_UPGRADE ;
75- const fixCVEButtonText = hasExtension ? BUTTON_TEXT_FIX_CVE : BUTTON_TEXT_INSTALL_AND_FIX_CVE ;
76124 sendInfo ( operationId , {
77- operationName : "java.dependency.upgradeNotification.show" ,
125+ operationName : UpgradeTelemetry . NOTIFICATION_SHOW ,
126+ extensionState,
127+ issueType,
78128 } ) ;
79129
80130 const buttons = hasCVEIssue
81131 ? [ fixCVEButtonText , BUTTON_TEXT_NOT_NOW ]
82132 : [ upgradeButtonText , BUTTON_TEXT_NOT_NOW ] ;
83133
84134 const selection = await window . showInformationMessage (
85- notificationMessage ,
135+ message ,
86136 ...buttons
87137 ) ;
88138 sendInfo ( operationId , {
89- operationName : "java.dependency.upgradeNotification.runUpgrade" ,
139+ operationName : UpgradeTelemetry . NOTIFICATION_CLICK ,
140+ extensionState,
141+ issueType,
90142 choice : selection ?? "" ,
91143 } ) ;
92144
93145 switch ( selection ) {
94146 case fixCVEButtonText :
95147 case upgradeButtonText : {
96- commands . executeCommand ( Commands . JAVA_UPGRADE_WITH_COPILOT , prompt ) ;
148+ commands . executeCommand ( Commands . JAVA_UPGRADE_WITH_COPILOT , prompt , issueType , extensionState ) ;
97149 break ;
98150 }
99151 case BUTTON_TEXT_NOT_NOW : {
0 commit comments