Skip to content

Commit 876b92a

Browse files
committed
Move code search api call to its own file
1 parent b470061 commit 876b92a

File tree

3 files changed

+72
-70
lines changed

3 files changed

+72
-70
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { retry } from "@octokit/plugin-retry";
2+
import { throttling } from "@octokit/plugin-throttling";
3+
import { Octokit } from "@octokit/rest";
4+
import { Progress, CancellationToken } from "vscode";
5+
import { showAndLogWarningMessage } from "../helpers";
6+
import { Credentials } from "../common/authentication";
7+
8+
export async function getCodeSearchRepositories(
9+
query: string,
10+
progress: Progress<{
11+
message?: string | undefined;
12+
increment?: number | undefined;
13+
}>,
14+
token: CancellationToken,
15+
credentials: Credentials,
16+
): Promise<string[]> {
17+
let nwos: string[] = [];
18+
const octokit = await provideOctokitWithThrottling(credentials);
19+
20+
for await (const response of octokit.paginate.iterator(
21+
octokit.rest.search.code,
22+
{
23+
q: query,
24+
per_page: 100,
25+
},
26+
)) {
27+
nwos.push(...response.data.map((item) => item.repository.full_name));
28+
// calculate progress bar: 80% of the progress bar is used for the code search
29+
const totalNumberOfRequests = Math.ceil(response.data.total_count / 100);
30+
// 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
31+
const increment =
32+
totalNumberOfRequests < 10 ? 80 / totalNumberOfRequests : 8;
33+
progress.report({ increment });
34+
35+
if (token.isCancellationRequested) {
36+
nwos = [];
37+
break;
38+
}
39+
}
40+
41+
return [...new Set(nwos)];
42+
}
43+
44+
async function provideOctokitWithThrottling(
45+
credentials: Credentials,
46+
): Promise<Octokit> {
47+
const MyOctokit = Octokit.plugin(throttling);
48+
const auth = await credentials.getAccessToken();
49+
50+
const octokit = new MyOctokit({
51+
auth,
52+
retry,
53+
throttle: {
54+
onRateLimit: (retryAfter: number, options: any): boolean => {
55+
void showAndLogWarningMessage(
56+
`Request quota exhausted for request ${options.method} ${options.url}. Retrying after ${retryAfter} seconds!`,
57+
);
58+
59+
return true;
60+
},
61+
onSecondaryRateLimit: (_retryAfter: number, options: any): void => {
62+
void showAndLogWarningMessage(
63+
`SecondaryRateLimit detected for request ${options.method} ${options.url}`,
64+
);
65+
},
66+
},
67+
});
68+
69+
return octokit;
70+
}

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

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
import {
1818
showAndLogErrorMessage,
1919
showAndLogInformationMessage,
20-
showAndLogWarningMessage,
2120
} from "../../helpers";
2221
import { DisposableObject } from "../../pure/disposable-object";
2322
import {
@@ -37,11 +36,8 @@ import { getControllerRepo } from "../../variant-analysis/run-remote-query";
3736
import { getErrorMessage } from "../../pure/helpers-pure";
3837
import { DatabasePanelCommands } from "../../common/commands";
3938
import { App } from "../../common/app";
40-
import { getCodeSearchRepositories } from "../../variant-analysis/gh-api/gh-api-client";
4139
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";
40+
import { getCodeSearchRepositories } from "../code-search-api";
4541

4642
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
4743
remoteDatabaseKind: string;
@@ -409,7 +405,7 @@ export class DbPanel extends DisposableObject {
409405
`${codeSearchQuery} ${languagePrompt}`,
410406
progress,
411407
token,
412-
await this.getOctokitForSearch(),
408+
this.app.credentials,
413409
);
414410

415411
token.onCancellationRequested(() => {
@@ -503,30 +499,4 @@ export class DbPanel extends DisposableObject {
503499
);
504500
}
505501
}
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-
}
532502
}

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,6 @@ import {
77
VariantAnalysisSubmissionRequest,
88
} from "./variant-analysis";
99
import { Repository } from "./repository";
10-
import { Progress } from "vscode";
11-
import { CancellationToken } from "vscode-jsonrpc";
12-
import { Octokit } from "@octokit/rest";
13-
14-
export async function getCodeSearchRepositories(
15-
query: string,
16-
progress: Progress<{
17-
message?: string | undefined;
18-
increment?: number | undefined;
19-
}>,
20-
token: CancellationToken,
21-
octokit: Octokit,
22-
): Promise<string[]> {
23-
let nwos: string[] = [];
24-
25-
for await (const response of octokit.paginate.iterator(
26-
octokit.rest.search.code,
27-
{
28-
q: query,
29-
per_page: 100,
30-
},
31-
)) {
32-
nwos.push(...response.data.map((item) => item.repository.full_name));
33-
// calculate progress bar: 80% of the progress bar is used for the code search
34-
const totalNumberOfRequests = Math.ceil(response.data.total_count / 100);
35-
// 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
36-
const increment =
37-
totalNumberOfRequests < 10 ? 80 / totalNumberOfRequests : 8;
38-
progress.report({ increment });
39-
40-
if (token.isCancellationRequested) {
41-
nwos = [];
42-
break;
43-
}
44-
}
45-
46-
return [...new Set(nwos)];
47-
}
4810

4911
export async function submitVariantAnalysis(
5012
credentials: Credentials,

0 commit comments

Comments
 (0)