@@ -4,8 +4,10 @@ import * as archiveFilesystemProvider from './archive-filesystem-provider';
44import { DistributionConfigListener , QueryServerConfigListener , QueryHistoryConfigListener } from './config' ;
55import { DatabaseManager } from './databases' ;
66import { DatabaseUI } from './databases-ui' ;
7- import { DistributionUpdateCheckResultKind , DistributionManager , FindDistributionResult , FindDistributionResultKind , GithubApiError ,
8- DEFAULT_DISTRIBUTION_VERSION_CONSTRAINT , GithubRateLimitedError } from './distribution' ;
7+ import {
8+ DistributionUpdateCheckResultKind , DistributionManager , FindDistributionResult , FindDistributionResultKind , GithubApiError ,
9+ DEFAULT_DISTRIBUTION_VERSION_CONSTRAINT , GithubRateLimitedError
10+ } from './distribution' ;
911import * as helpers from './helpers' ;
1012import { spawnIdeServer } from './ide-server' ;
1113import { InterfaceManager , WebviewReveal } from './interface' ;
@@ -83,29 +85,32 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
8385 helpers . showAndLogErrorMessage ( `Can't execute ${ command } : waiting to finish loading CodeQL CLI.` ) ;
8486 } ) ;
8587
86- interface ReportingConfig {
88+ interface DistributionUpdateConfig {
89+ isUserInitiated : boolean ;
8790 shouldDisplayMessageWhenNoUpdates : boolean ;
88- shouldErrorIfUpdateFails : boolean ;
8991 }
9092
91- async function installOrUpdateDistributionWithProgressTitle ( progressTitle : string , reportingConfig : ReportingConfig ) : Promise < void > {
92- const result = await distributionManager . checkForUpdatesToExtensionManagedDistribution ( ) ;
93+ async function installOrUpdateDistributionWithProgressTitle ( progressTitle : string , config : DistributionUpdateConfig ) : Promise < void > {
94+ const minSecondsSinceLastUpdateCheck = config . isUserInitiated ? 0 : 86400 ;
95+ const noUpdatesLoggingFunc = config . shouldDisplayMessageWhenNoUpdates ?
96+ helpers . showAndLogInformationMessage : async ( message : string ) => logger . log ( message ) ;
97+ const result = await distributionManager . checkForUpdatesToExtensionManagedDistribution ( minSecondsSinceLastUpdateCheck ) ;
9398 switch ( result . kind ) {
99+ case DistributionUpdateCheckResultKind . AlreadyCheckedRecentlyResult :
100+ logger . log ( "Didn't perform CodeQL CLI update check since a check was already performed within the previous " +
101+ `${ minSecondsSinceLastUpdateCheck } seconds.` ) ;
102+ break ;
94103 case DistributionUpdateCheckResultKind . AlreadyUpToDate :
95- if ( reportingConfig . shouldDisplayMessageWhenNoUpdates ) {
96- helpers . showAndLogInformationMessage ( "CodeQL CLI already up to date." ) ;
97- }
104+ await noUpdatesLoggingFunc ( "CodeQL CLI already up to date." ) ;
98105 break ;
99- case DistributionUpdateCheckResultKind . InvalidDistributionLocation :
100- if ( reportingConfig . shouldDisplayMessageWhenNoUpdates ) {
101- helpers . showAndLogErrorMessage ( "CodeQL CLI is installed externally so could not be updated." ) ;
102- }
106+ case DistributionUpdateCheckResultKind . InvalidLocation :
107+ await noUpdatesLoggingFunc ( "CodeQL CLI is installed externally so could not be updated." ) ;
103108 break ;
104109 case DistributionUpdateCheckResultKind . UpdateAvailable :
105110 if ( beganMainExtensionActivation ) {
106111 const updateAvailableMessage = `Version "${ result . updatedRelease . name } " of the CodeQL CLI is now available. ` +
107112 "The update will be installed after Visual Studio Code restarts. Restart now to upgrade?" ;
108- ctx . globalState . update ( shouldUpdateOnNextActivationKey , true ) ;
113+ await ctx . globalState . update ( shouldUpdateOnNextActivationKey , true ) ;
109114 if ( await helpers . showInformationMessageWithAction ( updateAvailableMessage , "Restart and Upgrade" ) ) {
110115 await commands . executeCommand ( "workbench.action.reloadWindow" ) ;
111116 }
@@ -118,7 +123,7 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
118123 await helpers . withProgress ( progressOptions , progress =>
119124 distributionManager . installExtensionManagedDistributionRelease ( result . updatedRelease , progress ) ) ;
120125
121- ctx . globalState . update ( shouldUpdateOnNextActivationKey , false ) ;
126+ await ctx . globalState . update ( shouldUpdateOnNextActivationKey , false ) ;
122127 helpers . showAndLogInformationMessage ( `CodeQL CLI updated to version "${ result . updatedRelease . name } ".` ) ;
123128 }
124129 break ;
@@ -127,7 +132,7 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
127132 }
128133 }
129134
130- async function installOrUpdateDistribution ( reportingConfig : ReportingConfig ) : Promise < void > {
135+ async function installOrUpdateDistribution ( config : DistributionUpdateConfig ) : Promise < void > {
131136 if ( isInstallingOrUpdatingDistribution ) {
132137 throw new Error ( "Already installing or updating CodeQL CLI" ) ;
133138 }
@@ -137,11 +142,11 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
137142 const messageText = willUpdateCodeQl ? "Updating CodeQL CLI" :
138143 codeQlInstalled ? "Checking for updates to CodeQL CLI" : "Installing CodeQL CLI" ;
139144 try {
140- await installOrUpdateDistributionWithProgressTitle ( messageText , reportingConfig ) ;
145+ await installOrUpdateDistributionWithProgressTitle ( messageText , config ) ;
141146 } catch ( e ) {
142147 // Don't rethrow the exception, because if the config is changed, we want to be able to retry installing
143148 // or updating the distribution.
144- const alertFunction = ( codeQlInstalled && ! reportingConfig . shouldErrorIfUpdateFails ) ?
149+ const alertFunction = ( codeQlInstalled && ! config . isUserInitiated ) ?
145150 helpers . showAndLogWarningMessage : helpers . showAndLogErrorMessage ;
146151 const taskDescription = ( willUpdateCodeQl ? "update" :
147152 codeQlInstalled ? "check for updates to" : "install" ) + " CodeQL CLI" ;
@@ -180,8 +185,8 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
180185 return result ;
181186 }
182187
183- async function installOrUpdateThenTryActivate ( reportingConfig : ReportingConfig ) : Promise < void > {
184- await installOrUpdateDistribution ( reportingConfig ) ;
188+ async function installOrUpdateThenTryActivate ( config : DistributionUpdateConfig ) : Promise < void > {
189+ await installOrUpdateDistribution ( config ) ;
185190
186191 // Display the warnings even if the extension has already activated.
187192 const distributionResult = await getDistributionDisplayingDistributionWarnings ( ) ;
@@ -194,26 +199,26 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
194199 const chosenAction = await helpers . showAndLogErrorMessage ( `Can't execute ${ command } : missing CodeQL CLI.` , installActionName ) ;
195200 if ( chosenAction === installActionName ) {
196201 installOrUpdateThenTryActivate ( {
197- shouldDisplayMessageWhenNoUpdates : false ,
198- shouldErrorIfUpdateFails : true
202+ isUserInitiated : true ,
203+ shouldDisplayMessageWhenNoUpdates : false
199204 } ) ;
200205 }
201206 } ) ;
202207 }
203208 }
204209
205210 ctx . subscriptions . push ( distributionConfigListener . onDidChangeDistributionConfiguration ( ( ) => installOrUpdateThenTryActivate ( {
206- shouldDisplayMessageWhenNoUpdates : false ,
207- shouldErrorIfUpdateFails : true
211+ isUserInitiated : true ,
212+ shouldDisplayMessageWhenNoUpdates : false
208213 } ) ) ) ;
209214 ctx . subscriptions . push ( commands . registerCommand ( checkForUpdatesCommand , ( ) => installOrUpdateThenTryActivate ( {
210- shouldDisplayMessageWhenNoUpdates : true ,
211- shouldErrorIfUpdateFails : true
215+ isUserInitiated : true ,
216+ shouldDisplayMessageWhenNoUpdates : true
212217 } ) ) ) ;
213218
214219 await installOrUpdateThenTryActivate ( {
215- shouldDisplayMessageWhenNoUpdates : false ,
216- shouldErrorIfUpdateFails : ! ! ctx . globalState . get ( shouldUpdateOnNextActivationKey )
220+ isUserInitiated : ! ! ctx . globalState . get ( shouldUpdateOnNextActivationKey ) ,
221+ shouldDisplayMessageWhenNoUpdates : false
217222 } ) ;
218223}
219224
0 commit comments