Move octokit initialization to db panel to send log messages to the user

This commit is contained in:
Nora
2023-06-02 15:34:29 +00:00
parent fef28806b1
commit b47006129d
2 changed files with 32 additions and 24 deletions

View File

@@ -17,6 +17,7 @@ import {
import {
showAndLogErrorMessage,
showAndLogInformationMessage,
showAndLogWarningMessage,
} from "../../helpers";
import { DisposableObject } from "../../pure/disposable-object";
import {
@@ -38,6 +39,9 @@ import { DatabasePanelCommands } from "../../common/commands";
import { App } from "../../common/app";
import { getCodeSearchRepositories } from "../../variant-analysis/gh-api/gh-api-client";
import { QueryLanguage } from "../../common/query-language";
import { retry } from "@octokit/plugin-retry";
import { throttling } from "@octokit/plugin-throttling";
import { Octokit } from "@octokit/rest";
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
remoteDatabaseKind: string;
@@ -402,10 +406,10 @@ export class DbPanel extends DisposableObject {
progress.report({ increment: 10 });
const repositories = await getCodeSearchRepositories(
this.app.credentials,
`${codeSearchQuery} ${languagePrompt}`,
progress,
token,
await this.getOctokitForSearch(),
);
token.onCancellationRequested(() => {
@@ -499,4 +503,30 @@ export class DbPanel extends DisposableObject {
);
}
}
// since we don't have access to the extensions log methods we initialize octokit here
private async getOctokitForSearch(): Promise<Octokit> {
const MyOctokit = Octokit.plugin(throttling);
const auth = await this.app.credentials.getAccessToken();
const octokit = new MyOctokit({
auth,
retry,
throttle: {
onRateLimit: (retryAfter: number, options: any): boolean => {
void showAndLogWarningMessage(
`Request quota exhausted for request ${options.method} ${options.url}. Retrying after ${retryAfter} seconds!`,
);
return true;
},
onSecondaryRateLimit: (_retryAfter: number, options: any): void => {
void showAndLogWarningMessage(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`,
);
},
},
});
return octokit;
}
}

View File

@@ -9,40 +9,18 @@ import {
import { Repository } from "./repository";
import { Progress } from "vscode";
import { CancellationToken } from "vscode-jsonrpc";
import { throttling } from "@octokit/plugin-throttling";
import { Octokit } from "@octokit/rest";
import { showAndLogWarningMessage } from "../../helpers";
export async function getCodeSearchRepositories(
credentials: Credentials,
query: string,
progress: Progress<{
message?: string | undefined;
increment?: number | undefined;
}>,
token: CancellationToken,
octokit: Octokit,
): Promise<string[]> {
let nwos: string[] = [];
const MyOctokit = Octokit.plugin(throttling);
const auth = await credentials.getAccessToken();
const octokit = new MyOctokit({
auth,
throttle: {
onRateLimit: (retryAfter: number, options: any): boolean => {
void showAndLogWarningMessage(
`Request quota exhausted for request ${options.method} ${options.url}. Retrying after ${retryAfter} seconds!`,
);
return true;
},
onSecondaryRateLimit: (_retryAfter: number, options: any): void => {
void showAndLogWarningMessage(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`,
);
},
},
});
for await (const response of octokit.paginate.iterator(
octokit.rest.search.code,