Skip to content

Commit 0b56092

Browse files
authored
Merge pull request #297 from aeisenberg/aeisenberg/updates
fix: Avoid auto-updating the codeql binaries
2 parents deb544a + 4fce213 commit 0b56092

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

extensions/ql-vscode/CHANGELOG.md

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

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

89
## 1.1.0 - 17 March 2020
910

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: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,36 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
9393
interface DistributionUpdateConfig {
9494
isUserInitiated: boolean;
9595
shouldDisplayMessageWhenNoUpdates: boolean;
96+
allowAutoUpdating: boolean;
9697
}
9798

9899
async function installOrUpdateDistributionWithProgressTitle(progressTitle: string, config: DistributionUpdateConfig): Promise<void> {
99100
const minSecondsSinceLastUpdateCheck = config.isUserInitiated ? 0 : 86400;
100101
const noUpdatesLoggingFunc = config.shouldDisplayMessageWhenNoUpdates ?
101102
helpers.showAndLogInformationMessage : async (message: string) => logger.log(message);
102103
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+
103108
switch (result.kind) {
104109
case DistributionUpdateCheckResultKind.AlreadyCheckedRecentlyResult:
105110
logger.log("Didn't perform CodeQL CLI update check since a check was already performed within the previous " +
106111
`${minSecondsSinceLastUpdateCheck} seconds.`);
107112
break;
108113
case DistributionUpdateCheckResultKind.AlreadyUpToDate:
109-
await noUpdatesLoggingFunc("CodeQL CLI already up to date.");
114+
await noUpdatesLoggingFunc('CodeQL CLI already up to date.');
110115
break;
111116
case DistributionUpdateCheckResultKind.InvalidLocation:
112-
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.');
113118
break;
114119
case DistributionUpdateCheckResultKind.UpdateAvailable:
115-
if (beganMainExtensionActivation) {
120+
if (beganMainExtensionActivation || !allowAutoUpdating) {
116121
const updateAvailableMessage = `Version "${result.updatedRelease.name}" of the CodeQL CLI is now available. ` +
117-
"The update will be installed after Visual Studio Code restarts. Restart now to upgrade?";
122+
'Do you wish to upgrade?';
118123
await ctx.globalState.update(shouldUpdateOnNextActivationKey, true);
119-
if (await helpers.showInformationMessageWithAction(updateAvailableMessage, "Restart and Upgrade")) {
120-
await commands.executeCommand("workbench.action.reloadWindow");
124+
if (await helpers.showInformationMessageWithAction(updateAvailableMessage, 'Restart and Upgrade')) {
125+
await commands.executeCommand('workbench.action.reloadWindow');
121126
}
122127
} else {
123128
const progressOptions: ProgressOptions = {
@@ -144,8 +149,12 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
144149
isInstallingOrUpdatingDistribution = true;
145150
const codeQlInstalled = await distributionManager.getCodeQlPathWithoutVersionCheck() !== undefined;
146151
const willUpdateCodeQl = ctx.globalState.get(shouldUpdateOnNextActivationKey);
147-
const messageText = willUpdateCodeQl ? "Updating CodeQL CLI" :
148-
codeQlInstalled ? "Checking for updates to CodeQL CLI" : "Installing CodeQL CLI";
152+
const messageText = willUpdateCodeQl
153+
? "Updating CodeQL CLI"
154+
: codeQlInstalled
155+
? "Checking for updates to CodeQL CLI"
156+
: "Installing CodeQL CLI";
157+
149158
try {
150159
await installOrUpdateDistributionWithProgressTitle(messageText, config);
151160
} catch (e) {
@@ -207,7 +216,8 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
207216
if (chosenAction === installActionName) {
208217
installOrUpdateThenTryActivate({
209218
isUserInitiated: true,
210-
shouldDisplayMessageWhenNoUpdates: false
219+
shouldDisplayMessageWhenNoUpdates: false,
220+
allowAutoUpdating: true
211221
});
212222
}
213223
});
@@ -216,16 +226,22 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
216226

217227
ctx.subscriptions.push(distributionConfigListener.onDidChangeDistributionConfiguration(() => installOrUpdateThenTryActivate({
218228
isUserInitiated: true,
219-
shouldDisplayMessageWhenNoUpdates: false
229+
shouldDisplayMessageWhenNoUpdates: false,
230+
allowAutoUpdating: true
220231
})));
221232
ctx.subscriptions.push(commands.registerCommand(checkForUpdatesCommand, () => installOrUpdateThenTryActivate({
222233
isUserInitiated: true,
223-
shouldDisplayMessageWhenNoUpdates: true
234+
shouldDisplayMessageWhenNoUpdates: true,
235+
allowAutoUpdating: true
224236
})));
225237

226238
await installOrUpdateThenTryActivate({
227239
isUserInitiated: !!ctx.globalState.get(shouldUpdateOnNextActivationKey),
228-
shouldDisplayMessageWhenNoUpdates: false
240+
shouldDisplayMessageWhenNoUpdates: false,
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)
229245
});
230246
}
231247

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)