Skip to content

Commit a7a24fc

Browse files
authored
Merge pull request #2476 from github/nora/use-code-search-api
Code search: use code search api
2 parents 10bd774 + 945594d commit a7a24fc

File tree

3 files changed

+90
-47
lines changed

3 files changed

+90
-47
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+
`Rate Limit detected 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+
`Secondary Rate Limit 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: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import { getControllerRepo } from "../../variant-analysis/run-remote-query";
3636
import { getErrorMessage } from "../../pure/helpers-pure";
3737
import { DatabasePanelCommands } from "../../common/commands";
3838
import { App } from "../../common/app";
39-
import { getCodeSearchRepositories } from "../../variant-analysis/gh-api/gh-api-client";
4039
import { QueryLanguage } from "../../common/query-language";
40+
import { getCodeSearchRepositories } from "../code-search-api";
4141

4242
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
4343
remoteDatabaseKind: string;
@@ -351,13 +351,19 @@ export class DbPanel extends DisposableObject {
351351

352352
const listName = treeViewItem.dbItem.listName;
353353

354-
const languageQuickPickItems: CodeSearchQuickPickItem[] = Object.values(
355-
QueryLanguage,
356-
).map((language) => ({
357-
label: language.toString(),
358-
alwaysShow: true,
359-
language: language.toString(),
360-
}));
354+
const languageQuickPickItems: CodeSearchQuickPickItem[] = [
355+
{
356+
label: "No specific language",
357+
alwaysShow: true,
358+
language: "",
359+
},
360+
].concat(
361+
Object.values(QueryLanguage).map((language) => ({
362+
label: language.toString(),
363+
alwaysShow: true,
364+
language: language.toString(),
365+
})),
366+
);
361367

362368
const codeSearchLanguage =
363369
await window.showQuickPick<CodeSearchQuickPickItem>(
@@ -372,6 +378,10 @@ export class DbPanel extends DisposableObject {
372378
return;
373379
}
374380

381+
const languagePrompt = codeSearchLanguage.language
382+
? `language:${codeSearchLanguage.language}`
383+
: "";
384+
375385
const codeSearchQuery = await window.showInputBox({
376386
title: "GitHub Code Search",
377387
prompt:
@@ -392,10 +402,10 @@ export class DbPanel extends DisposableObject {
392402
progress.report({ increment: 10 });
393403

394404
const repositories = await getCodeSearchRepositories(
395-
this.app.credentials,
396-
`${codeSearchQuery} language:${codeSearchLanguage.language}`,
405+
`${codeSearchQuery} ${languagePrompt}`,
397406
progress,
398407
token,
408+
this.app.credentials,
399409
);
400410

401411
token.onCancellationRequested(() => {

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +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-
13-
export async function getCodeSearchRepositories(
14-
credentials: Credentials,
15-
query: string,
16-
progress: Progress<{
17-
message?: string | undefined;
18-
increment?: number | undefined;
19-
}>,
20-
token: CancellationToken,
21-
): Promise<string[]> {
22-
let nwos: string[] = [];
23-
const octokit = await credentials.getOctokit();
24-
for await (const response of octokit.paginate.iterator(
25-
octokit.rest.search.repos,
26-
{
27-
q: query,
28-
per_page: 100,
29-
},
30-
)) {
31-
nwos.push(...response.data.map((item) => item.full_name));
32-
// calculate progress bar: 80% of the progress bar is used for the code search
33-
const totalNumberOfRequests = Math.ceil(response.data.total_count / 100);
34-
// Since we have a maximum 10 of requests, we use a fixed increment whenever the totalNumberOfRequests is greater than 10
35-
const increment =
36-
totalNumberOfRequests < 10 ? 80 / totalNumberOfRequests : 8;
37-
progress.report({ increment });
38-
39-
if (token.isCancellationRequested) {
40-
nwos = [];
41-
break;
42-
}
43-
}
44-
45-
return [...new Set(nwos)];
46-
}
4710

4811
export async function submitVariantAnalysis(
4912
credentials: Credentials,

0 commit comments

Comments
 (0)