-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathcode-search-api.ts
79 lines (71 loc) · 2.4 KB
/
code-search-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { throttling } from "@octokit/plugin-throttling";
import type { Octokit } from "@octokit/rest";
import type { CancellationToken } from "vscode";
import type { Credentials } from "../common/authentication";
import type { BaseLogger } from "../common/logging";
import { AppOctokit } from "../common/octokit";
import type { ProgressCallback } from "../common/vscode/progress";
import { UserCancellationException } from "../common/vscode/progress";
import type { EndpointDefaults } from "@octokit/types";
import { getOctokitBaseUrl } from "../common/vscode/octokit";
export async function getCodeSearchRepositories(
query: string,
progress: ProgressCallback,
token: CancellationToken,
credentials: Credentials,
logger: BaseLogger,
): Promise<string[]> {
const nwos: string[] = [];
const octokit = await provideOctokitWithThrottling(credentials, logger);
let i = 0;
for await (const response of octokit.paginate.iterator(
octokit.rest.search.code,
{
q: query,
per_page: 100,
},
)) {
i++;
nwos.push(...response.data.map((item) => item.repository.full_name));
const totalNumberOfResultPages = Math.ceil(response.data.total_count / 100);
const totalNumberOfRequests =
totalNumberOfResultPages > 10 ? 10 : totalNumberOfResultPages;
progress({
maxStep: totalNumberOfRequests,
step: i,
message: "Sending API requests to get Code Search results.",
});
if (token.isCancellationRequested) {
throw new UserCancellationException("Code search cancelled.", true);
}
}
return [...new Set(nwos)];
}
async function provideOctokitWithThrottling(
credentials: Credentials,
logger: BaseLogger,
): Promise<Octokit> {
const MyOctokit = AppOctokit.plugin(throttling);
const auth = await credentials.getAccessToken();
const octokit = new MyOctokit({
auth,
baseUrl: getOctokitBaseUrl(),
throttle: {
onRateLimit: (retryAfter: number, options: EndpointDefaults): boolean => {
void logger.log(
`Rate Limit detected for request ${options.method} ${options.url}. Retrying after ${retryAfter} seconds!`,
);
return true;
},
onSecondaryRateLimit: (
_retryAfter: number,
options: EndpointDefaults,
): void => {
void logger.log(
`Secondary Rate Limit detected for request ${options.method} ${options.url}`,
);
},
},
});
return octokit;
}