Skip to content

Commit 70529a8

Browse files
committed
Add "View SARIF" command to query history context menu.
1 parent 7db6bc8 commit 70529a8

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
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
- Better formatting and autoindentation when adding QLDoc comments to `.ql` and `.qll` files.
66
- Allow for more flexibility when opening a database in the workspace. A user can now choose the actual database folder, or the nested `db-*` folder.
7+
- Add query history menu command for viewing corresponding SARIF file.
78

89
## 1.2.0 - 19 May 2020
910

extensions/ql-vscode/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@
284284
"command": "codeQLQueryHistory.showQueryText",
285285
"title": "Show Query Text"
286286
},
287+
{
288+
"command": "codeQLQueryHistory.viewSarif",
289+
"title": "View SARIF"
290+
},
287291
{
288292
"command": "codeQLQueryResults.nextPathStep",
289293
"title": "CodeQL: Show Next Step on Path"
@@ -388,6 +392,11 @@
388392
"group": "9_qlCommands",
389393
"when": "view == codeQLQueryHistory"
390394
},
395+
{
396+
"command": "codeQLQueryHistory.viewSarif",
397+
"group": "9_qlCommands",
398+
"when": "view == codeQLQueryHistory"
399+
},
391400
{
392401
"command": "codeQLTests.showOutputDifferences",
393402
"group": "qltest@1",

extensions/ql-vscode/src/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ export class InterfaceManager extends DisposableObject {
402402
const sarif = await interpretResults(
403403
this.cliServer,
404404
metadata,
405-
resultsPaths.resultsPath,
405+
resultsPaths,
406406
sourceInfo
407407
);
408408
// For performance reasons, limit the number of results we try

extensions/ql-vscode/src/query-history.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,22 @@ export class QueryHistoryManager {
257257
}
258258
}
259259

260+
async handleViewSarif(queryHistoryItem: CompletedQuery) {
261+
try {
262+
const hasInterpretedResults = await queryHistoryItem.query.hasInterpretedResults();
263+
if (hasInterpretedResults) {
264+
const textDocument = await vscode.workspace.openTextDocument(vscode.Uri.file(queryHistoryItem.query.resultsPaths.interpretedResultsPath));
265+
await vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One);
266+
}
267+
else {
268+
const label = queryHistoryItem.getLabel();
269+
helpers.showAndLogInformationMessage(`Query ${label} has no interpreted results.`);
270+
}
271+
} catch (e) {
272+
helpers.showAndLogErrorMessage(e.message);
273+
}
274+
}
275+
260276
async getQueryText(queryHistoryItem: CompletedQuery): Promise<string> {
261277
if (queryHistoryItem.options.queryText) {
262278
return queryHistoryItem.options.queryText;
@@ -296,6 +312,7 @@ export class QueryHistoryManager {
296312
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.setLabel', this.handleSetLabel.bind(this)));
297313
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.showQueryLog', this.handleShowQueryLog.bind(this)));
298314
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.showQueryText', this.handleShowQueryText.bind(this)));
315+
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.viewSarif', this.handleViewSarif.bind(this)));
299316
ctx.subscriptions.push(vscode.commands.registerCommand('codeQLQueryHistory.itemClicked', async (item) => {
300317
return this.handleItemClicked(item);
301318
}));

extensions/ql-vscode/src/query-results.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as cli from './cli';
55
import * as sarif from 'sarif';
66
import * as fs from 'fs-extra';
77
import * as path from 'path';
8-
import { RawResultsSortState, SortedResultSetInfo, DatabaseInfo, QueryMetadata, InterpretedResultsSortState } from "./interface-types";
8+
import { RawResultsSortState, SortedResultSetInfo, DatabaseInfo, QueryMetadata, InterpretedResultsSortState, ResultsPaths } from "./interface-types";
99
import { QueryHistoryConfig } from "./config";
1010
import { QueryHistoryItemOptions } from "./query-history";
1111

@@ -123,9 +123,8 @@ export class CompletedQuery implements QueryWithResults {
123123
/**
124124
* Call cli command to interpret results.
125125
*/
126-
export async function interpretResults(server: cli.CodeQLCliServer, metadata: QueryMetadata | undefined, resultsPath: string, sourceInfo?: cli.SourceInfo): Promise<sarif.Log> {
127-
const interpretedResultsPath = resultsPath + ".interpreted.sarif";
128-
126+
export async function interpretResults(server: cli.CodeQLCliServer, metadata: QueryMetadata | undefined, resultsPaths: ResultsPaths, sourceInfo?: cli.SourceInfo): Promise<sarif.Log> {
127+
const { resultsPath, interpretedResultsPath } = resultsPaths;
129128
if (await fs.pathExists(interpretedResultsPath)) {
130129
return JSON.parse(await fs.readFile(interpretedResultsPath, 'utf8'));
131130
}

0 commit comments

Comments
 (0)