Skip to content

Commit 3399212

Browse files
authored
Merge pull request #3029 from github/nora/progress-reporting-code-search
Code Search: use withProgress to indicate api request progress
2 parents 43e60b2 + 5caf11e commit 3399212

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

extensions/ql-vscode/src/databases/code-search-api.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import { throttling } from "@octokit/plugin-throttling";
22
import { Octokit } from "@octokit/rest";
3-
import { Progress, CancellationToken } from "vscode";
3+
import { CancellationToken } from "vscode";
44
import { Credentials } from "../common/authentication";
55
import { BaseLogger } from "../common/logging";
66
import { AppOctokit } from "../common/octokit";
7-
import { UserCancellationException } from "../common/vscode/progress";
7+
import {
8+
ProgressCallback,
9+
UserCancellationException,
10+
} from "../common/vscode/progress";
811

912
export async function getCodeSearchRepositories(
1013
query: string,
11-
progress: Progress<{
12-
message?: string | undefined;
13-
increment?: number | undefined;
14-
}>,
14+
progress: ProgressCallback,
1515
token: CancellationToken,
1616
credentials: Credentials,
1717
logger: BaseLogger,
1818
): Promise<string[]> {
1919
const nwos: string[] = [];
2020
const octokit = await provideOctokitWithThrottling(credentials, logger);
21+
let i = 0;
2122

2223
for await (const response of octokit.paginate.iterator(
2324
octokit.rest.search.code,
@@ -26,13 +27,16 @@ export async function getCodeSearchRepositories(
2627
per_page: 100,
2728
},
2829
)) {
30+
i++;
2931
nwos.push(...response.data.map((item) => item.repository.full_name));
30-
// calculate progress bar: 80% of the progress bar is used for the code search
31-
const totalNumberOfRequests = Math.ceil(response.data.total_count / 100);
32-
// Since we have a maximum of 1000 responses of the api, we can use a fixed increment whenever the totalNumberOfRequests would be greater than 10
33-
const increment =
34-
totalNumberOfRequests < 10 ? 80 / totalNumberOfRequests : 8;
35-
progress.report({ increment });
32+
const totalNumberOfResultPages = Math.ceil(response.data.total_count / 100);
33+
const totalNumberOfRequests =
34+
totalNumberOfResultPages > 10 ? 10 : totalNumberOfResultPages;
35+
progress({
36+
maxStep: totalNumberOfRequests,
37+
step: i,
38+
message: "Sending API requests to get Code Search results.",
39+
});
3640

3741
if (token.isCancellationRequested) {
3842
throw new UserCancellationException("Code search cancelled.", true);

extensions/ql-vscode/src/databases/ui/db-panel.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import {
2-
ProgressLocation,
32
QuickPickItem,
43
TreeView,
54
TreeViewExpansionEvent,
65
Uri,
76
window,
87
workspace,
98
} from "vscode";
10-
import { UserCancellationException } from "../../common/vscode/progress";
9+
import {
10+
UserCancellationException,
11+
withProgress,
12+
} from "../../common/vscode/progress";
1113
import {
1214
getNwoFromGitHubUrl,
1315
isValidGitHubNwo,
@@ -406,15 +408,8 @@ export class DbPanel extends DisposableObject {
406408
return;
407409
}
408410

409-
await window.withProgress(
410-
{
411-
location: ProgressLocation.Notification,
412-
title: "Searching for repositories... This might take a while",
413-
cancellable: true,
414-
},
411+
await withProgress(
415412
async (progress, token) => {
416-
progress.report({ increment: 10 });
417-
418413
const repositories = await getCodeSearchRepositories(
419414
`${codeSearchQuery} ${languagePrompt}`,
420415
progress,
@@ -427,10 +422,18 @@ export class DbPanel extends DisposableObject {
427422
throw new UserCancellationException("Code search cancelled.", true);
428423
}
429424

430-
progress.report({ increment: 10, message: "Processing results..." });
425+
progress({
426+
maxStep: 12,
427+
step: 12,
428+
message: "Processing results...",
429+
});
431430

432431
await this.dbManager.addNewRemoteReposToList(repositories, listName);
433432
},
433+
{
434+
title: "Searching for repositories...",
435+
cancellable: true,
436+
},
434437
);
435438
}
436439

0 commit comments

Comments
 (0)