Merge pull request #1588 from github/koesie10/open-query-text
Add ability to open the query text
This commit is contained in:
@@ -114,6 +114,7 @@ import {
|
||||
VariantAnalysisScannedRepository as ApiVariantAnalysisScannedRepository
|
||||
} from './remote-queries/gh-api/variant-analysis';
|
||||
import { VariantAnalysisManager } from './remote-queries/variant-analysis-manager';
|
||||
import { createVariantAnalysisContentProvider } from './remote-queries/variant-analysis-content-provider';
|
||||
|
||||
/**
|
||||
* extension.ts
|
||||
@@ -485,6 +486,7 @@ async function activateWithInstalledDistribution(
|
||||
await fs.ensureDir(variantAnalysisStorageDir);
|
||||
const variantAnalysisManager = new VariantAnalysisManager(ctx, cliServer, variantAnalysisStorageDir, logger);
|
||||
ctx.subscriptions.push(variantAnalysisManager);
|
||||
ctx.subscriptions.push(workspace.registerTextDocumentContentProvider('codeql-variant-analysis', createVariantAnalysisContentProvider(variantAnalysisManager)));
|
||||
|
||||
void logger.log('Initializing remote queries manager.');
|
||||
const rqm = new RemoteQueriesManager(ctx, cliServer, queryStorageDir, logger, variantAnalysisManager);
|
||||
|
||||
@@ -468,6 +468,10 @@ export interface OpenQueryFileMessage {
|
||||
t: 'openQueryFile';
|
||||
}
|
||||
|
||||
export interface OpenQueryTextMessage {
|
||||
t: 'openQueryText';
|
||||
}
|
||||
|
||||
export type ToVariantAnalysisMessage =
|
||||
| SetVariantAnalysisMessage
|
||||
| SetRepoResultsMessage
|
||||
@@ -477,4 +481,5 @@ export type FromVariantAnalysisMessage =
|
||||
| ViewLoadedMsg
|
||||
| StopVariantAnalysisMessage
|
||||
| RequestRepositoryResultsMessage
|
||||
| OpenQueryFileMessage;
|
||||
| OpenQueryFileMessage
|
||||
| OpenQueryTextMessage;
|
||||
|
||||
@@ -251,6 +251,8 @@ export async function runRemoteQuery(
|
||||
throw new UserCancellationException(`Found unsupported language: ${language}`);
|
||||
}
|
||||
|
||||
const queryText = await fs.readFile(queryFile, 'utf8');
|
||||
|
||||
const variantAnalysisSubmission: VariantAnalysisSubmission = {
|
||||
startTime: queryStartTime,
|
||||
actionRepoRef: actionBranch,
|
||||
@@ -260,6 +262,7 @@ export async function runRemoteQuery(
|
||||
filePath: queryFile,
|
||||
pack: base64Pack,
|
||||
language: variantAnalysisLanguage,
|
||||
text: queryText,
|
||||
},
|
||||
databases: {
|
||||
repositories: repoSelection.repositories,
|
||||
|
||||
@@ -7,7 +7,8 @@ export interface VariantAnalysis {
|
||||
query: {
|
||||
name: string,
|
||||
filePath: string,
|
||||
language: VariantAnalysisQueryLanguage
|
||||
language: VariantAnalysisQueryLanguage,
|
||||
text: string,
|
||||
},
|
||||
databases: {
|
||||
repositories?: string[],
|
||||
@@ -114,6 +115,7 @@ export interface VariantAnalysisSubmission {
|
||||
name: string,
|
||||
filePath: string,
|
||||
language: VariantAnalysisQueryLanguage,
|
||||
text: string,
|
||||
|
||||
// Base64 encoded query pack.
|
||||
pack: string,
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { TextDocumentContentProvider, Uri } from 'vscode';
|
||||
import { URLSearchParams } from 'url';
|
||||
import { showAndLogWarningMessage } from '../helpers';
|
||||
import { SHOW_QUERY_TEXT_MSG } from '../query-history';
|
||||
import { VariantAnalysisManager } from './variant-analysis-manager';
|
||||
|
||||
export const createVariantAnalysisContentProvider = (variantAnalysisManager: VariantAnalysisManager): TextDocumentContentProvider => ({
|
||||
async provideTextDocumentContent(uri: Uri): Promise<string | undefined> {
|
||||
const params = new URLSearchParams(uri.query);
|
||||
|
||||
const variantAnalysisIdString = params.get('variantAnalysisId');
|
||||
if (!variantAnalysisIdString) {
|
||||
void showAndLogWarningMessage('Unable to show query text. No variant analysis ID provided.');
|
||||
return undefined;
|
||||
}
|
||||
const variantAnalysisId = parseInt(variantAnalysisIdString);
|
||||
|
||||
const variantAnalysis = await variantAnalysisManager.getVariantAnalysis(variantAnalysisId);
|
||||
if (!variantAnalysis) {
|
||||
void showAndLogWarningMessage('Unable to show query text. No variant analysis found.');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return SHOW_QUERY_TEXT_MSG + variantAnalysis.query.text;
|
||||
}
|
||||
});
|
||||
@@ -183,6 +183,7 @@ export class VariantAnalysisManager extends DisposableObject implements VariantA
|
||||
name: `Variant analysis ${variantAnalysisId}`,
|
||||
filePath: `variant_analysis_${variantAnalysisId}.ql`,
|
||||
language: variantAnalysisResponse.query_language as VariantAnalysisQueryLanguage,
|
||||
text: '',
|
||||
},
|
||||
databases: {},
|
||||
executionStartTime: 0,
|
||||
|
||||
@@ -27,7 +27,8 @@ export function processVariantAnalysis(
|
||||
query: {
|
||||
name: submission.query.name,
|
||||
filePath: submission.query.filePath,
|
||||
language: submission.query.language
|
||||
language: submission.query.language,
|
||||
text: submission.query.text,
|
||||
},
|
||||
databases: submission.databases,
|
||||
executionStartTime: submission.startTime
|
||||
@@ -52,11 +53,7 @@ export function processUpdatedVariantAnalysis(
|
||||
const variantAnalysis: VariantAnalysis = {
|
||||
id: response.id,
|
||||
controllerRepoId: response.controller_repo.id,
|
||||
query: {
|
||||
name: previousVariantAnalysis.query.name,
|
||||
filePath: previousVariantAnalysis.query.filePath,
|
||||
language: previousVariantAnalysis.query.language
|
||||
},
|
||||
query: previousVariantAnalysis.query,
|
||||
databases: previousVariantAnalysis.databases,
|
||||
executionStartTime: previousVariantAnalysis.executionStartTime,
|
||||
status: processApiStatus(response.status),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { commands, ExtensionContext, ViewColumn, window as Window, workspace } from 'vscode';
|
||||
import { commands, ExtensionContext, Uri, ViewColumn, window as Window, workspace } from 'vscode';
|
||||
import { URLSearchParams } from 'url';
|
||||
import { AbstractWebview, WebviewPanelConfig } from '../abstract-webview';
|
||||
import { logger } from '../logging';
|
||||
import { FromVariantAnalysisMessage, ToVariantAnalysisMessage } from '../pure/interface-types';
|
||||
@@ -92,6 +93,9 @@ export class VariantAnalysisView extends AbstractWebview<ToVariantAnalysisMessag
|
||||
case 'openQueryFile':
|
||||
await this.openQueryFile();
|
||||
break;
|
||||
case 'openQueryText':
|
||||
await this.openQueryText();
|
||||
break;
|
||||
default:
|
||||
assertNever(msg);
|
||||
}
|
||||
@@ -130,4 +134,29 @@ export class VariantAnalysisView extends AbstractWebview<ToVariantAnalysisMessag
|
||||
void showAndLogWarningMessage(`Could not open file: ${variantAnalysis.query.filePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async openQueryText(): Promise<void> {
|
||||
const variantAnalysis = await this.manager.getVariantAnalysis(this.variantAnalysisId);
|
||||
if (!variantAnalysis) {
|
||||
void showAndLogWarningMessage('Could not open variant analysis query text. Variant analysis not found.');
|
||||
return;
|
||||
}
|
||||
|
||||
const filename = variantAnalysis.query.filePath;
|
||||
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
variantAnalysisId: variantAnalysis.id.toString(),
|
||||
});
|
||||
const uri = Uri.from({
|
||||
scheme: 'codeql-variant-analysis',
|
||||
path: filename,
|
||||
query: params.toString(),
|
||||
});
|
||||
const doc = await workspace.openTextDocument(uri);
|
||||
await Window.showTextDocument(doc, { preview: false });
|
||||
} catch (error) {
|
||||
void showAndLogWarningMessage('Could not open variant analysis query text. Failed to open text document.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,12 @@ const openQueryFile = () => {
|
||||
});
|
||||
};
|
||||
|
||||
const openQueryText = () => {
|
||||
vscode.postMessage({
|
||||
t: 'openQueryText',
|
||||
});
|
||||
};
|
||||
|
||||
export function VariantAnalysis({
|
||||
variantAnalysis: initialVariantAnalysis,
|
||||
repoStates: initialRepoStates = [],
|
||||
@@ -75,7 +81,7 @@ export function VariantAnalysis({
|
||||
<VariantAnalysisHeader
|
||||
variantAnalysis={variantAnalysis}
|
||||
onOpenQueryFileClick={openQueryFile}
|
||||
onViewQueryTextClick={() => console.log('View query')}
|
||||
onViewQueryTextClick={openQueryText}
|
||||
onStopQueryClick={() => console.log('Stop query')}
|
||||
onCopyRepositoryListClick={() => console.log('Copy repository list')}
|
||||
onExportResultsClick={() => console.log('Export results')}
|
||||
|
||||
@@ -17,6 +17,7 @@ describe(VariantAnalysisAnalyzedRepos.name, () => {
|
||||
name: 'Example query',
|
||||
filePath: 'example.ql',
|
||||
language: VariantAnalysisQueryLanguage.Javascript,
|
||||
text: 'import javascript\nselect 1',
|
||||
},
|
||||
databases: {},
|
||||
executionStartTime: 1611234567890,
|
||||
|
||||
@@ -16,6 +16,7 @@ describe(VariantAnalysisOutcomePanels.name, () => {
|
||||
name: 'Example query',
|
||||
filePath: 'example.ql',
|
||||
language: VariantAnalysisQueryLanguage.Javascript,
|
||||
text: 'import javascript\nselect 1',
|
||||
},
|
||||
databases: {},
|
||||
executionStartTime: 1611234567890,
|
||||
|
||||
@@ -31,6 +31,7 @@ describe('Variant Analysis processor', function() {
|
||||
'filePath': 'query-file-path',
|
||||
'language': VariantAnalysisQueryLanguage.Javascript,
|
||||
'name': 'query-name',
|
||||
'text': mockSubmission.query.text,
|
||||
},
|
||||
'databases': {
|
||||
'repositories': ['1', '2', '3'],
|
||||
|
||||
@@ -10,6 +10,7 @@ export function createMockSubmission(): VariantAnalysisSubmission {
|
||||
name: 'query-name',
|
||||
filePath: 'query-file-path',
|
||||
language: VariantAnalysisQueryLanguage.Javascript,
|
||||
text: 'query-text',
|
||||
pack: 'base64-encoded-string',
|
||||
},
|
||||
databases: {
|
||||
|
||||
@@ -20,7 +20,8 @@ export function createMockVariantAnalysis(
|
||||
query: {
|
||||
name: 'a-query-name',
|
||||
filePath: 'a-query-file-path',
|
||||
language: VariantAnalysisQueryLanguage.Javascript
|
||||
language: VariantAnalysisQueryLanguage.Javascript,
|
||||
text: 'a-query-text',
|
||||
},
|
||||
databases: {
|
||||
repositories: ['1', '2', '3'],
|
||||
|
||||
Reference in New Issue
Block a user