use withProgress

This commit is contained in:
Nora
2023-10-27 12:28:47 +00:00
parent f4a2d8572c
commit 5c3c8ffa1b
2 changed files with 30 additions and 23 deletions

View File

@@ -1,23 +1,24 @@
import { throttling } from "@octokit/plugin-throttling"; import { throttling } from "@octokit/plugin-throttling";
import { Octokit } from "@octokit/rest"; import { Octokit } from "@octokit/rest";
import { Progress, CancellationToken } from "vscode"; import { CancellationToken } from "vscode";
import { Credentials } from "../common/authentication"; import { Credentials } from "../common/authentication";
import { BaseLogger } from "../common/logging"; import { BaseLogger } from "../common/logging";
import { AppOctokit } from "../common/octokit"; import { AppOctokit } from "../common/octokit";
import { UserCancellationException } from "../common/vscode/progress"; import {
ProgressCallback,
UserCancellationException,
} from "../common/vscode/progress";
export async function getCodeSearchRepositories( export async function getCodeSearchRepositories(
query: string, query: string,
progress: Progress<{ progress: ProgressCallback,
message?: string | undefined;
increment?: number | undefined;
}>,
token: CancellationToken, token: CancellationToken,
credentials: Credentials, credentials: Credentials,
logger: BaseLogger, logger: BaseLogger,
): Promise<string[]> { ): Promise<string[]> {
const nwos: string[] = []; const nwos: string[] = [];
const octokit = await provideOctokitWithThrottling(credentials, logger); const octokit = await provideOctokitWithThrottling(credentials, logger);
let i = 0;
for await (const response of octokit.paginate.iterator( for await (const response of octokit.paginate.iterator(
octokit.rest.search.code, octokit.rest.search.code,
@@ -26,13 +27,16 @@ export async function getCodeSearchRepositories(
per_page: 100, per_page: 100,
}, },
)) { )) {
i++;
nwos.push(...response.data.map((item) => item.repository.full_name)); 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 totalNumberOfResults = Math.ceil(response.data.total_count / 100);
const totalNumberOfRequests = Math.ceil(response.data.total_count / 100); const totalNumberOfRequests =
// 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 totalNumberOfResults > 10 ? 10 : totalNumberOfResults;
const increment = progress({
totalNumberOfRequests < 10 ? 80 / totalNumberOfRequests : 8; maxStep: totalNumberOfRequests,
progress.report({ increment }); step: i,
message: "Sending api requests to get code search results.",
});
if (token.isCancellationRequested) { if (token.isCancellationRequested) {
throw new UserCancellationException("Code search cancelled.", true); throw new UserCancellationException("Code search cancelled.", true);

View File

@@ -1,5 +1,4 @@
import { import {
ProgressLocation,
QuickPickItem, QuickPickItem,
TreeView, TreeView,
TreeViewExpansionEvent, TreeViewExpansionEvent,
@@ -7,7 +6,10 @@ import {
window, window,
workspace, workspace,
} from "vscode"; } from "vscode";
import { UserCancellationException } from "../../common/vscode/progress"; import {
UserCancellationException,
withProgress,
} from "../../common/vscode/progress";
import { import {
getNwoFromGitHubUrl, getNwoFromGitHubUrl,
isValidGitHubNwo, isValidGitHubNwo,
@@ -406,15 +408,8 @@ export class DbPanel extends DisposableObject {
return; return;
} }
await window.withProgress( await withProgress(
{
location: ProgressLocation.Notification,
title: "Searching for repositories... This might take a while",
cancellable: true,
},
async (progress, token) => { async (progress, token) => {
progress.report({ increment: 10 });
const repositories = await getCodeSearchRepositories( const repositories = await getCodeSearchRepositories(
`${codeSearchQuery} ${languagePrompt}`, `${codeSearchQuery} ${languagePrompt}`,
progress, progress,
@@ -427,10 +422,18 @@ export class DbPanel extends DisposableObject {
throw new UserCancellationException("Code search cancelled.", true); 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); await this.dbManager.addNewRemoteReposToList(repositories, listName);
}, },
{
title: "Searching for repositories...",
cancellable: true,
},
); );
} }