Skip to content

Commit 9367d5f

Browse files
authored
MRVA: Export results to local markdown files (#1344)
1 parent 50ec97a commit 9367d5f

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,18 @@ export class QueryHistoryManager extends DisposableObject {
763763
}
764764
}
765765

766+
async getQueryHistoryItemDirectory(queryHistoryItem: QueryHistoryInfo): Promise<string> {
767+
if (queryHistoryItem.t === 'local') {
768+
if (queryHistoryItem.completedQuery) {
769+
return queryHistoryItem.completedQuery.query.querySaveDir;
770+
}
771+
} else if (queryHistoryItem.t === 'remote') {
772+
return path.join(this.queryStorageDir, queryHistoryItem.queryId);
773+
}
774+
775+
throw new Error('Unable to get query directory');
776+
}
777+
766778
async handleOpenQueryDirectory(
767779
singleItem: QueryHistoryInfo,
768780
multiSelect: QueryHistoryInfo[]

extensions/ql-vscode/src/remote-queries/export-results.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { window, commands, Uri, ExtensionContext, QuickPickItem } from 'vscode';
1+
import * as path from 'path';
2+
import * as fs from 'fs-extra';
3+
4+
import { window, commands, Uri, ExtensionContext, QuickPickItem, workspace, ViewColumn } from 'vscode';
25
import { Credentials } from '../authentication';
36
import { UserCancellationException } from '../commandRunner';
4-
import { showInformationMessageWithAction, showAndLogInformationMessage } from '../helpers';
7+
import { showInformationMessageWithAction } from '../helpers';
58
import { logger } from '../logging';
69
import { QueryHistoryManager } from '../query-history';
710
import { createGist } from './gh-actions-api-client';
@@ -41,9 +44,10 @@ export async function exportRemoteQueryResults(
4144
if (exportFormat === gistOption) {
4245
await exportResultsToGist(ctx, query, analysesResults);
4346
} else if (exportFormat === localMarkdownOption) {
44-
// TODO: Write function that creates local markdown files
45-
// const markdownFiles = generateMarkdown(query, analysesResults, 'local');
46-
void showAndLogInformationMessage('Local markdown export not yet available');
47+
const queryDirectoryPath = await queryHistoryManager.getQueryHistoryItemDirectory(
48+
queryHistoryItem
49+
);
50+
await exportResultsToLocalMarkdown(queryDirectoryPath, query, analysesResults);
4751
}
4852
}
4953

@@ -95,3 +99,31 @@ async function exportResultsToGist(
9599
}
96100
}
97101
}
102+
103+
/**
104+
* Converts the results of a remote query to markdown and saves the files locally
105+
* in the query directory (where query results and metadata are also saved).
106+
*/
107+
async function exportResultsToLocalMarkdown(
108+
queryDirectoryPath: string,
109+
query: RemoteQuery,
110+
analysesResults: AnalysisResults[]
111+
) {
112+
const markdownFiles = generateMarkdown(query, analysesResults, 'local');
113+
const exportedResultsPath = path.join(queryDirectoryPath, 'exported-results');
114+
await fs.ensureDir(exportedResultsPath);
115+
for (const markdownFile of markdownFiles) {
116+
const filePath = path.join(exportedResultsPath, `${markdownFile.fileName}.md`);
117+
await fs.writeFile(filePath, markdownFile.content.join('\n'), 'utf8');
118+
}
119+
const shouldOpenExportedResults = await showInformationMessageWithAction(
120+
`Variant analysis results exported to \"${exportedResultsPath}\".`,
121+
'Open exported results'
122+
);
123+
if (shouldOpenExportedResults) {
124+
const summaryFilePath = path.join(exportedResultsPath, '_summary.md');
125+
const summaryFile = await workspace.openTextDocument(summaryFilePath);
126+
await window.showTextDocument(summaryFile, ViewColumn.One);
127+
await commands.executeCommand('revealFileInOS', Uri.file(summaryFilePath));
128+
}
129+
}

0 commit comments

Comments
 (0)