Skip to content

Commit 0e4ae83

Browse files
committed
`
1 parent 3b1ff0f commit 0e4ae83

File tree

5 files changed

+136
-14
lines changed

5 files changed

+136
-14
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 a status bar item for the CodeQL CLI to show the current version. [#741](https://github.com/github/vscode-codeql/pull/741)
66
- Fix version constraint for flagging CLI support of non-destructive updates [#744](https://github.com/github/vscode-codeql/pull/744)
7+
- Add a _More Information_ button in the telemetry popup. [#742](https://github.com/github/vscode-codeql/pull/742)
78

89
## 1.4.1 - 29 January 2021
910

extensions/ql-vscode/src/helpers.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import * as yaml from 'js-yaml';
44
import * as path from 'path';
55
import {
66
ExtensionContext,
7+
Uri,
78
window as Window,
8-
workspace
9+
workspace,
10+
env
911
} from 'vscode';
1012
import { CodeQLCliServer } from './cli';
1113
import { logger } from './logging';
@@ -100,6 +102,41 @@ export async function showBinaryChoiceDialog(message: string, modal = true): Pro
100102
return chosenItem?.title === yesItem.title;
101103
}
102104

105+
/**
106+
* Opens a modal dialog for the user to make a yes/no choice.
107+
*
108+
* @param message The message to show.
109+
* @param modal If true (the default), show a modal dialog box, otherwise dialog is non-modal and can
110+
* be closed even if the user does not make a choice.
111+
*
112+
* @return
113+
* `true` if the user clicks 'Yes',
114+
* `false` if the user clicks 'No' or cancels the dialog,
115+
* `undefined` if the dialog is closed without the user making a choice.
116+
*/
117+
export async function showBinaryChoiceWithUrlDialog(message: string, url: string): Promise<boolean | undefined> {
118+
const urlItem = { title: 'More Information', isCloseAffordance: false };
119+
const yesItem = { title: 'Yes', isCloseAffordance: false };
120+
const noItem = { title: 'No', isCloseAffordance: true };
121+
let chosenItem;
122+
123+
// Keep the dialog open as long as the user is clicking the 'more information' option.
124+
// To prevent an infinite loop, if the user clicks 'more information' 5 times, close the dialog and return cancelled
125+
let count = 0;
126+
do {
127+
chosenItem = await Window.showInformationMessage(message, { modal: true }, urlItem, yesItem, noItem);
128+
if (chosenItem === urlItem) {
129+
await env.openExternal(Uri.parse(url, true));
130+
}
131+
count++;
132+
} while (chosenItem === urlItem && count < 5);
133+
134+
if (!chosenItem || chosenItem.title === urlItem.title) {
135+
return undefined;
136+
}
137+
return chosenItem.title === yesItem.title;
138+
}
139+
103140
/**
104141
* Show an information message with a customisable action.
105142
* @param message The message to show.

extensions/ql-vscode/src/telemetry.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ConfigListener, CANARY_FEATURES, ENABLE_TELEMETRY, GLOBAL_ENABLE_TELEME
44
import * as appInsights from 'applicationinsights';
55
import { logger } from './logging';
66
import { UserCancellationException } from './commandRunner';
7-
import { showBinaryChoiceDialog } from './helpers';
7+
import { showBinaryChoiceWithUrlDialog } from './helpers';
88

99
// Key is injected at build time through the APP_INSIGHTS_KEY environment variable.
1010
const key = 'REPLACE-APP-INSIGHTS-KEY';
@@ -164,14 +164,9 @@ export class TelemetryListener extends ConfigListener {
164164
let result = undefined;
165165
if (GLOBAL_ENABLE_TELEMETRY.getValue()) {
166166
// Extension won't start until this completes.
167-
result = await showBinaryChoiceDialog(
168-
'Does the CodeQL Extension by GitHub have your permission to collect usage data and metrics to help us improve CodeQL for VSCode?\n\nFor details of what we collect and how we use it, see https://github.com/github/vscode-codeql/blob/main/extensions/ql-vscode/TELEMETRY.md.',
169-
// We make this dialog modal for now.
170-
// Note that non-modal dialogs allow for markdown in their text, but modal dialogs do not.
171-
// If we do decide to keep this dialog as modal, then this implementation can change and
172-
// we no longer need to call Promise.race. Before committing this PR, we need to make
173-
// this decision.
174-
true
167+
result = await showBinaryChoiceWithUrlDialog(
168+
'Does the CodeQL Extension by GitHub have your permission to collect usage data and metrics to help us improve CodeQL for VSCode?',
169+
'https://github.com/github/vscode-codeql/blob/main/extensions/ql-vscode/TELEMETRY.md'
175170
);
176171
}
177172
if (result !== undefined) {

extensions/ql-vscode/src/vscode-tests/no-workspace/helpers.test.ts

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { expect } from 'chai';
22
import 'mocha';
3-
import { ExtensionContext, Memento } from 'vscode';
3+
import { ExtensionContext, Memento, window } from 'vscode';
44
import * as yaml from 'js-yaml';
55
import * as tmp from 'tmp';
66
import * as path from 'path';
77
import * as fs from 'fs-extra';
88
import * as sinon from 'sinon';
99

10-
import { getInitialQueryContents, InvocationRateLimiter, isLikelyDbLanguageFolder } from '../../helpers';
10+
import { getInitialQueryContents, InvocationRateLimiter, isLikelyDbLanguageFolder, showBinaryChoiceDialog, showBinaryChoiceWithUrlDialog, showInformationMessageWithAction } from '../../helpers';
1111
import { reportStreamProgress } from '../../commandRunner';
12+
import Sinon = require('sinon');
13+
import { fail } from 'assert';
1214

1315
describe('helpers', () => {
1416
let sandbox: sinon.SinonSandbox;
@@ -225,4 +227,91 @@ describe('helpers', () => {
225227
message: 'My prefix (Size unknown)',
226228
});
227229
});
230+
231+
describe('open dialog', () => {
232+
let showInformationMessageSpy: Sinon.SinonStub;
233+
beforeEach(() => {
234+
showInformationMessageSpy = sandbox.stub(window, 'showInformationMessage');
235+
});
236+
237+
it('should show a binary choice dialog and return `yes`', (done) => {
238+
// pretend user chooses 'yes'
239+
showInformationMessageSpy.onCall(0).resolvesArg(2);
240+
const res = showBinaryChoiceDialog('xxx');
241+
res.then((val) => {
242+
expect(val).to.eq(true);
243+
done();
244+
}).catch(e => fail(e));
245+
});
246+
247+
it('should show a binary choice dialog and return `no`', (done) => {
248+
// pretend user chooses 'no'
249+
showInformationMessageSpy.onCall(0).resolvesArg(3);
250+
const res = showBinaryChoiceDialog('xxx');
251+
res.then((val) => {
252+
expect(val).to.eq(false);
253+
done();
254+
}).catch(e => fail(e));
255+
});
256+
257+
it('should show an info dialog and confirm the action', (done) => {
258+
// pretend user chooses to run action
259+
showInformationMessageSpy.onCall(0).resolvesArg(1);
260+
const res = showInformationMessageWithAction('xxx', 'yyy');
261+
res.then((val) => {
262+
expect(val).to.eq(true);
263+
done();
264+
}).catch(e => fail(e));
265+
});
266+
267+
it('should show an action dialog and avoid choosing the action', (done) => {
268+
// pretend user does not choose to run action
269+
showInformationMessageSpy.onCall(0).resolves(undefined);
270+
const res = showInformationMessageWithAction('xxx', 'yyy');
271+
res.then((val) => {
272+
expect(val).to.eq(false);
273+
done();
274+
}).catch(e => fail(e));
275+
});
276+
277+
it('should show a binary choice dialog with a url and return `yes`', (done) => {
278+
// pretend user clicks on the url twice and then clicks 'yes'
279+
showInformationMessageSpy.onCall(0).resolvesArg(2);
280+
showInformationMessageSpy.onCall(1).resolvesArg(2);
281+
showInformationMessageSpy.onCall(2).resolvesArg(3);
282+
const res = showBinaryChoiceWithUrlDialog('xxx', 'invalid:url');
283+
res.then((val) => {
284+
expect(val).to.eq(true);
285+
done();
286+
}).catch(e => fail(e));
287+
});
288+
289+
it('should show a binary choice dialog with a url and return `no`', (done) => {
290+
// pretend user clicks on the url twice and then clicks 'no'
291+
showInformationMessageSpy.onCall(0).resolvesArg(2);
292+
showInformationMessageSpy.onCall(1).resolvesArg(2);
293+
showInformationMessageSpy.onCall(2).resolvesArg(4);
294+
const res = showBinaryChoiceWithUrlDialog('xxx', 'invalid:url');
295+
res.then((val) => {
296+
expect(val).to.eq(false);
297+
done();
298+
}).catch(e => fail(e));
299+
});
300+
301+
it('should show a binary choice dialog and exit after clcking `more info` 5 times', (done) => {
302+
// pretend user clicks on the url twice and then clicks 'no'
303+
showInformationMessageSpy.onCall(0).resolvesArg(2);
304+
showInformationMessageSpy.onCall(1).resolvesArg(2);
305+
showInformationMessageSpy.onCall(2).resolvesArg(2);
306+
showInformationMessageSpy.onCall(3).resolvesArg(2);
307+
showInformationMessageSpy.onCall(4).resolvesArg(2);
308+
const res = showBinaryChoiceWithUrlDialog('xxx', 'invalid:url');
309+
res.then((val) => {
310+
// No choie was made
311+
expect(val).to.eq(undefined);
312+
expect(showInformationMessageSpy.getCalls().length).to.eq(5);
313+
done();
314+
}).catch(e => fail(e));
315+
});
316+
});
228317
});

extensions/ql-vscode/src/vscode-tests/no-workspace/telemetry.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describe('telemetry reporting', function() {
249249
});
250250

251251
it('should request permission if popup has never been seen before', async () => {
252-
sandbox.stub(window, 'showInformationMessage').resolvesArg(2 /* "yes" item */);
252+
sandbox.stub(window, 'showInformationMessage').resolvesArg(3 /* "yes" item */);
253253
await ctx.globalState.update('telemetry-request-viewed', false);
254254
await enableTelemetry('codeQL.telemetry', false);
255255

@@ -262,7 +262,7 @@ describe('telemetry reporting', function() {
262262
});
263263

264264
it('should prevent telemetry if permission is denied', async () => {
265-
sandbox.stub(window, 'showInformationMessage').resolvesArg(3 /* "no" item */);
265+
sandbox.stub(window, 'showInformationMessage').resolvesArg(4 /* "no" item */);
266266
await ctx.globalState.update('telemetry-request-viewed', false);
267267
await enableTelemetry('codeQL.telemetry', true);
268268

0 commit comments

Comments
 (0)