Move code search api call to its own file
This commit is contained in:
70
extensions/ql-vscode/src/databases/code-search-api.ts
Normal file
70
extensions/ql-vscode/src/databases/code-search-api.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { retry } from "@octokit/plugin-retry";
|
||||
import { throttling } from "@octokit/plugin-throttling";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import { Progress, CancellationToken } from "vscode";
|
||||
import { showAndLogWarningMessage } from "../helpers";
|
||||
import { Credentials } from "../common/authentication";
|
||||
|
||||
export async function getCodeSearchRepositories(
|
||||
query: string,
|
||||
progress: Progress<{
|
||||
message?: string | undefined;
|
||||
increment?: number | undefined;
|
||||
}>,
|
||||
token: CancellationToken,
|
||||
credentials: Credentials,
|
||||
): Promise<string[]> {
|
||||
let nwos: string[] = [];
|
||||
const octokit = await provideOctokitWithThrottling(credentials);
|
||||
|
||||
for await (const response of octokit.paginate.iterator(
|
||||
octokit.rest.search.code,
|
||||
{
|
||||
q: query,
|
||||
per_page: 100,
|
||||
},
|
||||
)) {
|
||||
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 totalNumberOfRequests = Math.ceil(response.data.total_count / 100);
|
||||
// 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
|
||||
const increment =
|
||||
totalNumberOfRequests < 10 ? 80 / totalNumberOfRequests : 8;
|
||||
progress.report({ increment });
|
||||
|
||||
if (token.isCancellationRequested) {
|
||||
nwos = [];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [...new Set(nwos)];
|
||||
}
|
||||
|
||||
async function provideOctokitWithThrottling(
|
||||
credentials: Credentials,
|
||||
): Promise<Octokit> {
|
||||
const MyOctokit = Octokit.plugin(throttling);
|
||||
const auth = await 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;
|
||||
}
|
||||
@@ -17,7 +17,6 @@ import {
|
||||
import {
|
||||
showAndLogErrorMessage,
|
||||
showAndLogInformationMessage,
|
||||
showAndLogWarningMessage,
|
||||
} from "../../helpers";
|
||||
import { DisposableObject } from "../../pure/disposable-object";
|
||||
import {
|
||||
@@ -37,11 +36,8 @@ import { getControllerRepo } from "../../variant-analysis/run-remote-query";
|
||||
import { getErrorMessage } from "../../pure/helpers-pure";
|
||||
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";
|
||||
import { getCodeSearchRepositories } from "../code-search-api";
|
||||
|
||||
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
|
||||
remoteDatabaseKind: string;
|
||||
@@ -409,7 +405,7 @@ export class DbPanel extends DisposableObject {
|
||||
`${codeSearchQuery} ${languagePrompt}`,
|
||||
progress,
|
||||
token,
|
||||
await this.getOctokitForSearch(),
|
||||
this.app.credentials,
|
||||
);
|
||||
|
||||
token.onCancellationRequested(() => {
|
||||
@@ -503,30 +499,4 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,44 +7,6 @@ import {
|
||||
VariantAnalysisSubmissionRequest,
|
||||
} from "./variant-analysis";
|
||||
import { Repository } from "./repository";
|
||||
import { Progress } from "vscode";
|
||||
import { CancellationToken } from "vscode-jsonrpc";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
|
||||
export async function getCodeSearchRepositories(
|
||||
query: string,
|
||||
progress: Progress<{
|
||||
message?: string | undefined;
|
||||
increment?: number | undefined;
|
||||
}>,
|
||||
token: CancellationToken,
|
||||
octokit: Octokit,
|
||||
): Promise<string[]> {
|
||||
let nwos: string[] = [];
|
||||
|
||||
for await (const response of octokit.paginate.iterator(
|
||||
octokit.rest.search.code,
|
||||
{
|
||||
q: query,
|
||||
per_page: 100,
|
||||
},
|
||||
)) {
|
||||
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 totalNumberOfRequests = Math.ceil(response.data.total_count / 100);
|
||||
// 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
|
||||
const increment =
|
||||
totalNumberOfRequests < 10 ? 80 / totalNumberOfRequests : 8;
|
||||
progress.report({ increment });
|
||||
|
||||
if (token.isCancellationRequested) {
|
||||
nwos = [];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [...new Set(nwos)];
|
||||
}
|
||||
|
||||
export async function submitVariantAnalysis(
|
||||
credentials: Credentials,
|
||||
|
||||
Reference in New Issue
Block a user