Merge pull request #3029 from github/nora/progress-reporting-code-search
Code Search: use withProgress to indicate api request progress
This commit is contained in:
@@ -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,
|
||||
@@ -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);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
ProgressLocation,
|
||||
QuickPickItem,
|
||||
TreeView,
|
||||
TreeViewExpansionEvent,
|
||||
@@ -7,7 +6,10 @@ import {
|
||||
window,
|
||||
workspace,
|
||||
} from "vscode";
|
||||
import { UserCancellationException } from "../../common/vscode/progress";
|
||||
import {
|
||||
UserCancellationException,
|
||||
withProgress,
|
||||
} from "../../common/vscode/progress";
|
||||
import {
|
||||
getNwoFromGitHubUrl,
|
||||
isValidGitHubNwo,
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user