Skip to content

Commit

Permalink
Merge pull request #3029 from github/nora/progress-reporting-code-search
Browse files Browse the repository at this point in the history
Code Search: use withProgress to indicate api request progress
  • Loading branch information
norascheuch authored Oct 27, 2023
2 parents 43e60b2 + 5caf11e commit 3399212
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
28 changes: 16 additions & 12 deletions extensions/ql-vscode/src/databases/code-search-api.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { throttling } from "@octokit/plugin-throttling";
import { Octokit } from "@octokit/rest";
import { Progress, CancellationToken } from "vscode";
import { CancellationToken } from "vscode";
import { Credentials } from "../common/authentication";
import { BaseLogger } from "../common/logging";
import { AppOctokit } from "../common/octokit";
import { UserCancellationException } from "../common/vscode/progress";
import {
ProgressCallback,
UserCancellationException,
} from "../common/vscode/progress";

export async function getCodeSearchRepositories(
query: string,
progress: Progress<{
message?: string | undefined;
increment?: number | undefined;
}>,
progress: ProgressCallback,
token: CancellationToken,
credentials: Credentials,
logger: BaseLogger,
): Promise<string[]> {
const nwos: string[] = [];
const octokit = await provideOctokitWithThrottling(credentials, logger);
let i = 0;

for await (const response of octokit.paginate.iterator(
octokit.rest.search.code,
Expand All @@ -26,13 +27,16 @@ export async function getCodeSearchRepositories(
per_page: 100,
},
)) {
i++;
nwos.push(...response.data.map((item) => item.repository.full_name));
// calculate progress bar: 80% of the progress bar is used for the code search
const totalNumberOfRequests = Math.ceil(response.data.total_count / 100);
// 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
const increment =
totalNumberOfRequests < 10 ? 80 / totalNumberOfRequests : 8;
progress.report({ increment });
const totalNumberOfResultPages = Math.ceil(response.data.total_count / 100);
const totalNumberOfRequests =
totalNumberOfResultPages > 10 ? 10 : totalNumberOfResultPages;
progress({
maxStep: totalNumberOfRequests,
step: i,
message: "Sending API requests to get Code Search results.",
});

if (token.isCancellationRequested) {
throw new UserCancellationException("Code search cancelled.", true);
Expand Down
25 changes: 14 additions & 11 deletions extensions/ql-vscode/src/databases/ui/db-panel.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
ProgressLocation,
QuickPickItem,
TreeView,
TreeViewExpansionEvent,
Uri,
window,
workspace,
} from "vscode";
import { UserCancellationException } from "../../common/vscode/progress";
import {
UserCancellationException,
withProgress,
} from "../../common/vscode/progress";
import {
getNwoFromGitHubUrl,
isValidGitHubNwo,
Expand Down Expand Up @@ -406,15 +408,8 @@ export class DbPanel extends DisposableObject {
return;
}

await window.withProgress(
{
location: ProgressLocation.Notification,
title: "Searching for repositories... This might take a while",
cancellable: true,
},
await withProgress(
async (progress, token) => {
progress.report({ increment: 10 });

const repositories = await getCodeSearchRepositories(
`${codeSearchQuery} ${languagePrompt}`,
progress,
Expand All @@ -427,10 +422,18 @@ export class DbPanel extends DisposableObject {
throw new UserCancellationException("Code search cancelled.", true);
}

progress.report({ increment: 10, message: "Processing results..." });
progress({
maxStep: 12,
step: 12,
message: "Processing results...",
});

await this.dbManager.addNewRemoteReposToList(repositories, listName);
},
{
title: "Searching for repositories...",
cancellable: true,
},
);
}

Expand Down

0 comments on commit 3399212

Please sign in to comment.