Skip to content

Commit fe7d14b

Browse files
committed
Sort Gist files by user-defined sort order
This will sort the files in an exported Gist by the user-defined sort order. It does so by prefixing the files with `result-{index}-` where the `index` is the 1-based index of the repository in the sort order. It will automatically pad the index with leading zeros to ensure that the files are sorted in the correct order. Unfortunately, we can't just use `{index}-` because numbers sort before the `_` character, which is used in the summary filename to place it first. There are also some changes in how we determine which repositories to export since we need to know in advance how many zeroes we need to pad the index with. There should be no functional changes in which repositories are actually exported.
1 parent 6350ac7 commit fe7d14b

2 files changed

Lines changed: 49 additions & 29 deletions

File tree

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

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -187,45 +187,29 @@ export async function exportVariantAnalysisResults(
187187
throw new UserCancellationException("Cancelled");
188188
}
189189

190+
const repositories = filterAndSortRepositoriesWithResults(
191+
variantAnalysis.scannedRepos,
192+
filterSort,
193+
)?.filter(
194+
(repo) =>
195+
repo.resultCount &&
196+
repoStates.find((r) => r.repositoryId === repo.repository.id)
197+
?.downloadStatus ===
198+
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
199+
);
200+
190201
async function* getAnalysesResults(): AsyncGenerator<
191202
[VariantAnalysisScannedRepository, VariantAnalysisScannedRepositoryResult]
192203
> {
193204
if (!variantAnalysis) {
194205
return;
195206
}
196207

197-
const repositories = filterAndSortRepositoriesWithResults(
198-
variantAnalysis.scannedRepos,
199-
filterSort,
200-
);
201208
if (!repositories) {
202209
return;
203210
}
204211

205212
for (const repo of repositories) {
206-
const repoState = repoStates.find(
207-
(r) => r.repositoryId === repo.repository.id,
208-
);
209-
210-
// Do not export if it has not yet completed or the download has not yet succeeded.
211-
if (
212-
repoState?.downloadStatus !==
213-
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded
214-
) {
215-
continue;
216-
}
217-
218-
if (repo.resultCount == 0) {
219-
yield [
220-
repo,
221-
{
222-
variantAnalysisId: variantAnalysis.id,
223-
repositoryId: repo.repository.id,
224-
},
225-
];
226-
continue;
227-
}
228-
229213
const result = await variantAnalysisManager.loadResults(
230214
variantAnalysis.id,
231215
repo.repository.fullName,
@@ -259,6 +243,7 @@ export async function exportVariantAnalysisResults(
259243
exportedResultsDirectory,
260244
variantAnalysis,
261245
getAnalysesResults(),
246+
repositories?.length ?? 0,
262247
exportFormat,
263248
progress,
264249
token,
@@ -272,6 +257,7 @@ export async function exportVariantAnalysisAnalysisResults(
272257
analysesResults: AsyncIterable<
273258
[VariantAnalysisScannedRepository, VariantAnalysisScannedRepositoryResult]
274259
>,
260+
expectedAnalysesResultsCount: number,
275261
exportFormat: "gist" | "local",
276262
progress: ProgressCallback,
277263
token: CancellationToken,
@@ -289,6 +275,7 @@ export async function exportVariantAnalysisAnalysisResults(
289275
const { markdownFiles, summaries } = await generateVariantAnalysisMarkdown(
290276
variantAnalysis,
291277
analysesResults,
278+
expectedAnalysesResultsCount,
292279
exportFormat,
293280
);
294281
const description = buildVariantAnalysisGistDescription(

extensions/ql-vscode/src/remote-queries/remote-queries-markdown-generation.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,25 @@ export async function generateVariantAnalysisMarkdown(
9494
results: AsyncIterable<
9595
[VariantAnalysisScannedRepository, VariantAnalysisScannedRepositoryResult]
9696
>,
97+
expectedResultsCount: number,
9798
linkType: MarkdownLinkType,
9899
): Promise<VariantAnalysisMarkdown> {
99100
const resultsFiles: MarkdownFile[] = [];
100101
const summaries: RepositorySummary[] = [];
102+
101103
for await (const [scannedRepo, result] of results) {
102-
if (!scannedRepo.resultCount || scannedRepo.resultCount === 0) {
104+
if (!scannedRepo.resultCount) {
103105
continue;
104106
}
105107

106108
// Append nwo and results count to the summary table
107109
const fullName = scannedRepo.repository.fullName;
108-
const fileName = createFileName(fullName);
110+
const fileName = createVariantAnalysisFileName(
111+
fullName,
112+
resultsFiles.length,
113+
expectedResultsCount,
114+
linkType,
115+
);
109116
summaries.push({
110117
fileName,
111118
repository: scannedRepo.repository,
@@ -482,6 +489,32 @@ function createFileName(nwo: string) {
482489
return `${owner}-${repo}`;
483490
}
484491

492+
/**
493+
* Creates the name of the markdown file for a given repository nwo.
494+
* This name doesn't include the file extension.
495+
*/
496+
function createVariantAnalysisFileName(
497+
fullName: string,
498+
index: number,
499+
expectedResultsCount: number,
500+
linkType: MarkdownLinkType,
501+
) {
502+
const baseName = createFileName(fullName);
503+
if (linkType === "gist") {
504+
const requiredNumberOfDecimals = Math.ceil(
505+
Math.log10(expectedResultsCount),
506+
);
507+
508+
const prefix = (index + 1)
509+
.toString()
510+
.padStart(requiredNumberOfDecimals, "0");
511+
512+
return `result-${prefix}-${baseName}`;
513+
}
514+
515+
return baseName;
516+
}
517+
485518
/**
486519
* Escape characters that could be interpreted as HTML instead of raw code.
487520
*/

0 commit comments

Comments
 (0)