-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
fetchGithubAPIData.ts
79 lines (63 loc) · 2.24 KB
/
fetchGithubAPIData.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 { DescriptionSource } from "./aiprdescription/descriptionconfig";
import { isWithinTokenLimit } from "gpt-tokenizer";
type DescriptionContextPromise = Promise<[string | undefined, string[] | undefined]>;
type DescriptionContext = Awaited<DescriptionContextPromise>;
interface Commit {
commit: {
message: string;
};
}
export const getPRDiff = async (url: string) => {
const response = await fetch(
url,
{ headers: { Accept: "application/vnd.github.v3.diff" } },
);
const diff = await response.text();
return diff.replace(/.*/, "").substring(1);
};
export const getPRCommitMessages = async (url: string) => {
const response = await fetch(url);
const data = await response.json();
if (!Array.isArray(data.commits)) {
return undefined;
}
const commitMessages: string[] = data.commits?.map((commit: Commit) => commit.commit.message);
return commitMessages;
};
export const getDescriptionContext = async (url: string, source: DescriptionSource): DescriptionContextPromise => {
let promises: [Promise<string | undefined>, Promise<string[] | undefined>] = [Promise.resolve(undefined), Promise.resolve(undefined)];
if (source === "diff") {
promises = [getPRDiff(url), Promise.resolve(undefined)];
} else if (source === "commitMessage") {
promises = [Promise.resolve(undefined), getPRCommitMessages(url)];
} else {
promises = [getPRDiff(url), getPRCommitMessages(url)];
}
const response = await Promise.all(promises);
return response;
};
export const isOutOfContextBounds = (context: DescriptionContext, limit: number): boolean => {
let text = "";
if (context[0]) {
text += context[0];
}
if (context[1]) {
text += context[1].join("");
}
return isWithinTokenLimit(text, limit) === false;
};
export const isPublicRepository = async (url: string): Promise<boolean> => {
try {
const { username, repoName } = url.match(
/^https?:\/\/(www\.)?github.com\/(?<username>[\w.-]+)\/?(?<repoName>[\w.-]+)?/,
)?.groups ?? {};
if (!username || !repoName) {
return false;
}
const response = await fetch(`https://api.github.com/repos/${username}/${repoName}`);
const data = await response.json();
return (response.status === 200 && data.private === false);
} catch (e: unknown) {
return false;
}
};