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" ;
1213
1314const KEY_PREFIX = 'javaupgrade.notificationManager' ;
@@ -17,6 +18,8 @@ const BUTTON_TEXT_UPGRADE = "Upgrade Now";
1718const BUTTON_TEXT_FIX_CVE = "Fix Now" ;
1819const BUTTON_TEXT_INSTALL_AND_UPGRADE = "Install Extension and Upgrade" ;
1920const BUTTON_TEXT_INSTALL_AND_FIX_CVE = "Install Extension and Fix" ;
21+ const BUTTON_TEXT_UPDATE_AND_UPGRADE = "Update Extension and Upgrade" ;
22+ const BUTTON_TEXT_UPDATE_AND_FIX_CVE = "Update Extension and Fix" ;
2023const BUTTON_TEXT_NOT_NOW = "Not Now" ;
2124
2225const SECONDS_IN_A_DAY = 24 * 60 * 60 ;
@@ -26,6 +29,61 @@ function getNowTs() {
2629 return Number ( new Date ( ) ) / 1000 ;
2730}
2831
32+ export type { ExtensionState } from "../utility" ;
33+
34+ export interface NotificationContent {
35+ message : string ;
36+ upgradeButtonText : string ;
37+ fixCVEButtonText : string ;
38+ }
39+
40+ export function getExtensionState ( extensionVersion : string | undefined ) : ExtensionState {
41+ if ( ! extensionVersion ) {
42+ return "not-installed" ;
43+ }
44+ if ( semver . gte ( extensionVersion , Upgrade . MIN_APPMOD_VERSION ) ) {
45+ return "up-to-date" ;
46+ }
47+ return "outdated" ;
48+ }
49+
50+ export function buildNotificationContent (
51+ issues : UpgradeIssue [ ] ,
52+ extensionState : ExtensionState ,
53+ ) : NotificationContent {
54+ const cveIssues = issues . filter (
55+ ( i ) : i is CveUpgradeIssue => i . reason === UpgradeReason . CVE
56+ ) ;
57+ const nonCVEIssues = issues . filter (
58+ ( i ) => i . reason !== UpgradeReason . CVE
59+ ) ;
60+ const hasCVEIssue = cveIssues . length > 0 ;
61+
62+ const message = hasCVEIssue
63+ ? buildCVENotificationMessage ( cveIssues , extensionState )
64+ : buildNotificationMessage ( nonCVEIssues [ 0 ] , extensionState ) ;
65+
66+ let upgradeButtonText : string ;
67+ let fixCVEButtonText : string ;
68+
69+ switch ( extensionState ) {
70+ case "up-to-date" :
71+ upgradeButtonText = BUTTON_TEXT_UPGRADE ;
72+ fixCVEButtonText = BUTTON_TEXT_FIX_CVE ;
73+ break ;
74+ case "outdated" :
75+ upgradeButtonText = BUTTON_TEXT_UPDATE_AND_UPGRADE ;
76+ fixCVEButtonText = BUTTON_TEXT_UPDATE_AND_FIX_CVE ;
77+ break ;
78+ case "not-installed" :
79+ upgradeButtonText = BUTTON_TEXT_INSTALL_AND_UPGRADE ;
80+ fixCVEButtonText = BUTTON_TEXT_INSTALL_AND_FIX_CVE ;
81+ break ;
82+ }
83+
84+ return { message, upgradeButtonText, fixCVEButtonText } ;
85+ }
86+
2987class NotificationManager implements IUpgradeIssuesRenderer {
3088 private hasShown = false ;
3189 private context ?: ExtensionContext ;
@@ -42,16 +100,6 @@ class NotificationManager implements IUpgradeIssuesRenderer {
42100 return ;
43101 }
44102
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-
55103 if ( ! this . shouldShow ( ) ) {
56104 return ;
57105 }
@@ -61,28 +109,27 @@ class NotificationManager implements IUpgradeIssuesRenderer {
61109 }
62110 this . hasShown = true ;
63111
64- const hasExtension = ! ! extensions . getExtension ( ExtensionName . APP_MODERNIZATION_UPGRADE_FOR_JAVA ) ;
65- const prompt = buildFixPrompt ( issue ) ;
112+ const ext = extensions . getExtension ( ExtensionName . APP_MODERNIZATION_UPGRADE_FOR_JAVA ) ;
113+ const extensionState = getExtensionState ( ext ?. packageJSON ?. version ) ;
114+ const { message, upgradeButtonText, fixCVEButtonText } = buildNotificationContent ( issues , extensionState ) ;
66115
67- let notificationMessage = "" ;
116+ const hasCVEIssue = issues . some ( i => i . reason === UpgradeReason . CVE ) ;
117+ const issue = hasCVEIssue
118+ ? issues . find ( ( i ) : i is CveUpgradeIssue => i . reason === UpgradeReason . CVE ) !
119+ : issues . find ( i => i . reason !== UpgradeReason . CVE ) ! ;
120+ const prompt = buildFixPrompt ( issue ) ;
68121
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 ;
76122 sendInfo ( operationId , {
77123 operationName : "java.dependency.upgradeNotification.show" ,
124+ extensionState,
78125 } ) ;
79126
80127 const buttons = hasCVEIssue
81128 ? [ fixCVEButtonText , BUTTON_TEXT_NOT_NOW ]
82129 : [ upgradeButtonText , BUTTON_TEXT_NOT_NOW ] ;
83130
84131 const selection = await window . showInformationMessage (
85- notificationMessage ,
132+ message ,
86133 ...buttons
87134 ) ;
88135 sendInfo ( operationId , {
@@ -93,7 +140,8 @@ class NotificationManager implements IUpgradeIssuesRenderer {
93140 switch ( selection ) {
94141 case fixCVEButtonText :
95142 case upgradeButtonText : {
96- commands . executeCommand ( Commands . JAVA_UPGRADE_WITH_COPILOT , prompt ) ;
143+ const source = selection === fixCVEButtonText ? "cve" : "upgrade" ;
144+ commands . executeCommand ( Commands . JAVA_UPGRADE_WITH_COPILOT , prompt , source ) ;
97145 break ;
98146 }
99147 case BUTTON_TEXT_NOT_NOW : {
0 commit comments