Skip to content

Commit b470061

Browse files
committed
Move octokit initialization to db panel to send log messages to the user
1 parent fef2880 commit b470061

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import {
1818
showAndLogErrorMessage,
1919
showAndLogInformationMessage,
20+
showAndLogWarningMessage,
2021
} from "../../helpers";
2122
import { DisposableObject } from "../../pure/disposable-object";
2223
import {
@@ -38,6 +39,9 @@ import { DatabasePanelCommands } from "../../common/commands";
3839
import { App } from "../../common/app";
3940
import { getCodeSearchRepositories } from "../../variant-analysis/gh-api/gh-api-client";
4041
import { QueryLanguage } from "../../common/query-language";
42+
import { retry } from "@octokit/plugin-retry";
43+
import { throttling } from "@octokit/plugin-throttling";
44+
import { Octokit } from "@octokit/rest";
4145

4246
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
4347
remoteDatabaseKind: string;
@@ -402,10 +406,10 @@ export class DbPanel extends DisposableObject {
402406
progress.report({ increment: 10 });
403407

404408
const repositories = await getCodeSearchRepositories(
405-
this.app.credentials,
406409
`${codeSearchQuery} ${languagePrompt}`,
407410
progress,
408411
token,
412+
await this.getOctokitForSearch(),
409413
);
410414

411415
token.onCancellationRequested(() => {
@@ -499,4 +503,30 @@ export class DbPanel extends DisposableObject {
499503
);
500504
}
501505
}
506+
507+
// since we don't have access to the extensions log methods we initialize octokit here
508+
private async getOctokitForSearch(): Promise<Octokit> {
509+
const MyOctokit = Octokit.plugin(throttling);
510+
const auth = await this.app.credentials.getAccessToken();
511+
512+
const octokit = new MyOctokit({
513+
auth,
514+
retry,
515+
throttle: {
516+
onRateLimit: (retryAfter: number, options: any): boolean => {
517+
void showAndLogWarningMessage(
518+
`Request quota exhausted for request ${options.method} ${options.url}. Retrying after ${retryAfter} seconds!`,
519+
);
520+
521+
return true;
522+
},
523+
onSecondaryRateLimit: (_retryAfter: number, options: any): void => {
524+
void showAndLogWarningMessage(
525+
`SecondaryRateLimit detected for request ${options.method} ${options.url}`,
526+
);
527+
},
528+
},
529+
});
530+
return octokit;
531+
}
502532
}

extensions/ql-vscode/src/variant-analysis/gh-api/gh-api-client.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,18 @@ import {
99
import { Repository } from "./repository";
1010
import { Progress } from "vscode";
1111
import { CancellationToken } from "vscode-jsonrpc";
12-
import { throttling } from "@octokit/plugin-throttling";
1312
import { Octokit } from "@octokit/rest";
14-
import { showAndLogWarningMessage } from "../../helpers";
1513

1614
export async function getCodeSearchRepositories(
17-
credentials: Credentials,
1815
query: string,
1916
progress: Progress<{
2017
message?: string | undefined;
2118
increment?: number | undefined;
2219
}>,
2320
token: CancellationToken,
21+
octokit: Octokit,
2422
): Promise<string[]> {
2523
let nwos: string[] = [];
26-
const MyOctokit = Octokit.plugin(throttling);
27-
const auth = await credentials.getAccessToken();
28-
29-
const octokit = new MyOctokit({
30-
auth,
31-
throttle: {
32-
onRateLimit: (retryAfter: number, options: any): boolean => {
33-
void showAndLogWarningMessage(
34-
`Request quota exhausted for request ${options.method} ${options.url}. Retrying after ${retryAfter} seconds!`,
35-
);
36-
37-
return true;
38-
},
39-
onSecondaryRateLimit: (_retryAfter: number, options: any): void => {
40-
void showAndLogWarningMessage(
41-
`SecondaryRateLimit detected for request ${options.method} ${options.url}`,
42-
);
43-
},
44-
},
45-
});
4624

4725
for await (const response of octokit.paginate.iterator(
4826
octokit.rest.search.code,

0 commit comments

Comments
 (0)