Skip to content

Commit 4fce213

Browse files
committed
fix: Allow autoupdating if no distribution installed
1 parent 8ed7b99 commit 4fce213

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

extensions/ql-vscode/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- Add new command in query history view to view the log file of a
66
query.
7-
- Avoid updating the CodeQL binaries without user acknowledgment.
7+
- Request user acknowledgment before updating the CodeQL binaries.
88

99
## 1.1.0 - 17 March 2020
1010

extensions/ql-vscode/src/distribution.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ export class DistributionManager implements DistributionProvider {
9494
};
9595
}
9696

97+
public async hasDistribution(): Promise<boolean> {
98+
const result = await this.getDistribution();
99+
return result.kind !== FindDistributionResultKind.NoDistribution;
100+
}
101+
97102
/**
98103
* Returns the path to a possibly-compatible CodeQL launcher binary, or undefined if a binary not be found.
99104
*/
@@ -208,7 +213,11 @@ class ExtensionSpecificDistributionManager {
208213
const extensionSpecificRelease = this.getInstalledRelease();
209214
const latestRelease = await this.getLatestRelease();
210215

211-
if (extensionSpecificRelease !== undefined && codeQlPath !== undefined && latestRelease.id === extensionSpecificRelease.id) {
216+
if (
217+
extensionSpecificRelease !== undefined &&
218+
codeQlPath !== undefined &&
219+
latestRelease.id === extensionSpecificRelease.id
220+
) {
212221
return createAlreadyUpToDateResult();
213222
}
214223
return createUpdateAvailableResult(latestRelease);

extensions/ql-vscode/src/extension.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,36 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
9393
interface DistributionUpdateConfig {
9494
isUserInitiated: boolean;
9595
shouldDisplayMessageWhenNoUpdates: boolean;
96-
avoidAutoUpdating: boolean;
96+
allowAutoUpdating: boolean;
9797
}
9898

9999
async function installOrUpdateDistributionWithProgressTitle(progressTitle: string, config: DistributionUpdateConfig): Promise<void> {
100100
const minSecondsSinceLastUpdateCheck = config.isUserInitiated ? 0 : 86400;
101101
const noUpdatesLoggingFunc = config.shouldDisplayMessageWhenNoUpdates ?
102102
helpers.showAndLogInformationMessage : async (message: string) => logger.log(message);
103103
const result = await distributionManager.checkForUpdatesToExtensionManagedDistribution(minSecondsSinceLastUpdateCheck);
104+
105+
// We do want to auto update if there is no distribution at all
106+
const allowAutoUpdating = config.allowAutoUpdating || !await distributionManager.hasDistribution();
107+
104108
switch (result.kind) {
105109
case DistributionUpdateCheckResultKind.AlreadyCheckedRecentlyResult:
106110
logger.log("Didn't perform CodeQL CLI update check since a check was already performed within the previous " +
107111
`${minSecondsSinceLastUpdateCheck} seconds.`);
108112
break;
109113
case DistributionUpdateCheckResultKind.AlreadyUpToDate:
110-
await noUpdatesLoggingFunc("CodeQL CLI already up to date.");
114+
await noUpdatesLoggingFunc('CodeQL CLI already up to date.');
111115
break;
112116
case DistributionUpdateCheckResultKind.InvalidLocation:
113-
await noUpdatesLoggingFunc("CodeQL CLI is installed externally so could not be updated.");
117+
await noUpdatesLoggingFunc('CodeQL CLI is installed externally so could not be updated.');
114118
break;
115119
case DistributionUpdateCheckResultKind.UpdateAvailable:
116-
if (beganMainExtensionActivation || config.avoidAutoUpdating) {
120+
if (beganMainExtensionActivation || !allowAutoUpdating) {
117121
const updateAvailableMessage = `Version "${result.updatedRelease.name}" of the CodeQL CLI is now available. ` +
118-
"The update will be installed after Visual Studio Code restarts. Restart now to upgrade?";
122+
'Do you wish to upgrade?';
119123
await ctx.globalState.update(shouldUpdateOnNextActivationKey, true);
120-
if (await helpers.showInformationMessageWithAction(updateAvailableMessage, "Restart and Upgrade")) {
121-
await commands.executeCommand("workbench.action.reloadWindow");
124+
if (await helpers.showInformationMessageWithAction(updateAvailableMessage, 'Restart and Upgrade')) {
125+
await commands.executeCommand('workbench.action.reloadWindow');
122126
}
123127
} else {
124128
const progressOptions: ProgressOptions = {
@@ -148,8 +152,8 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
148152
const messageText = willUpdateCodeQl
149153
? "Updating CodeQL CLI"
150154
: codeQlInstalled
151-
? "Checking for updates to CodeQL CLI"
152-
: "Installing CodeQL CLI";
155+
? "Checking for updates to CodeQL CLI"
156+
: "Installing CodeQL CLI";
153157

154158
try {
155159
await installOrUpdateDistributionWithProgressTitle(messageText, config);
@@ -213,7 +217,7 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
213217
installOrUpdateThenTryActivate({
214218
isUserInitiated: true,
215219
shouldDisplayMessageWhenNoUpdates: false,
216-
avoidAutoUpdating: false
220+
allowAutoUpdating: true
217221
});
218222
}
219223
});
@@ -223,21 +227,21 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
223227
ctx.subscriptions.push(distributionConfigListener.onDidChangeDistributionConfiguration(() => installOrUpdateThenTryActivate({
224228
isUserInitiated: true,
225229
shouldDisplayMessageWhenNoUpdates: false,
226-
227-
// only auto update on startup if the user has previously requested an update
228-
// otherwise, ask user to accept the update
229-
avoidAutoUpdating: !!ctx.globalState.get(shouldUpdateOnNextActivationKey)
230+
allowAutoUpdating: true
230231
})));
231232
ctx.subscriptions.push(commands.registerCommand(checkForUpdatesCommand, () => installOrUpdateThenTryActivate({
232233
isUserInitiated: true,
233234
shouldDisplayMessageWhenNoUpdates: true,
234-
avoidAutoUpdating: false
235+
allowAutoUpdating: true
235236
})));
236237

237238
await installOrUpdateThenTryActivate({
238239
isUserInitiated: !!ctx.globalState.get(shouldUpdateOnNextActivationKey),
239240
shouldDisplayMessageWhenNoUpdates: false,
240-
avoidAutoUpdating: true
241+
242+
// only auto update on startup if the user has previously requested an update
243+
// otherwise, ask user to accept the update
244+
allowAutoUpdating: !!ctx.globalState.get(shouldUpdateOnNextActivationKey)
241245
});
242246
}
243247

extensions/ql-vscode/src/helpers.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,12 @@ export class InvocationRateLimiter<T> {
179179
public async invokeFunctionIfIntervalElapsed(minSecondsSinceLastInvocation: number): Promise<InvocationRateLimiterResult<T>> {
180180
const updateCheckStartDate = this._createDate();
181181
const lastInvocationDate = this.getLastInvocationDate();
182-
if (minSecondsSinceLastInvocation && lastInvocationDate && lastInvocationDate <= updateCheckStartDate &&
183-
lastInvocationDate.getTime() + minSecondsSinceLastInvocation * 1000 > updateCheckStartDate.getTime()) {
182+
if (
183+
minSecondsSinceLastInvocation &&
184+
lastInvocationDate &&
185+
lastInvocationDate <= updateCheckStartDate &&
186+
lastInvocationDate.getTime() + minSecondsSinceLastInvocation * 1000 > updateCheckStartDate.getTime()
187+
) {
184188
return createRateLimitedResult();
185189
}
186190
const result = await this._func();

0 commit comments

Comments
 (0)