-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathgithub.ts
More file actions
236 lines (214 loc) · 7.57 KB
/
Copy pathgithub.ts
File metadata and controls
236 lines (214 loc) · 7.57 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* GitHub API types and fetch helpers.
* Reads GITHUB_TOKEN and DIGEST_REPO from environment at call time.
*/
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface RepoConfig {
/** Short identifier used for filenames */
id: string;
/** GitHub owner/repo slug */
repo: string;
/** Human-readable display name */
name: string;
/**
* Fetch multiple pages until items older than `since` are reached.
* Use for high-volume repos with many daily updates.
*/
paginated?: boolean;
}
export interface GitHubUser {
login: string;
}
export interface GitHubLabel {
name: string;
}
export interface GitHubReactions {
"+1": number;
}
export interface GitHubItem {
number: number;
title: string;
state: string;
user: GitHubUser;
labels: GitHubLabel[];
created_at: string;
updated_at: string;
comments: number;
reactions?: GitHubReactions;
body?: string | null;
html_url: string;
pull_request?: unknown;
}
export interface GitHubRelease {
tag_name: string;
name: string;
body?: string | null;
published_at: string;
}
// ---------------------------------------------------------------------------
// Internals
// ---------------------------------------------------------------------------
/** Maximum pages to fetch for paginated repos (100 items/page). */
const MAX_PAGES = 5;
function headers(): Record<string, string> {
return {
Authorization: `Bearer ${process.env["GITHUB_TOKEN"] ?? ""}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
};
}
async function githubGet<T>(url: string, params: Record<string, string> = {}): Promise<T> {
const u = new URL(url);
for (const [k, v] of Object.entries(params)) u.searchParams.set(k, v);
const resp = await fetch(u.toString(), { headers: headers() });
if (!resp.ok) throw new Error(`GitHub API error ${resp.status} (${url}): ${await resp.text()}`);
return resp.json() as Promise<T>;
}
async function fetchItemPage(
repo: string,
itemType: "issues" | "pulls",
since: Date,
page: number,
): Promise<GitHubItem[]> {
const params: Record<string, string> = {
state: "all",
sort: "updated",
direction: "desc",
per_page: "100",
page: String(page),
};
// /pulls does not support `since`; filter client-side instead
if (itemType === "issues") params["since"] = since.toISOString();
const items = await githubGet<GitHubItem[]>(`https://api.github.com/repos/${repo}/${itemType}`, params);
return itemType === "pulls" ? items.filter((i) => new Date(i.updated_at) >= since) : items;
}
// ---------------------------------------------------------------------------
// Exports
// ---------------------------------------------------------------------------
/**
* Fetch items updated since `since`.
* Paginated repos: keeps fetching until a page ends before `since` or MAX_PAGES reached.
* Regular repos: single page of 50.
*/
export async function fetchRecentItems(
cfg: RepoConfig,
itemType: "issues" | "pulls",
since: Date,
): Promise<GitHubItem[]> {
if (!cfg.paginated) {
const params: Record<string, string> = {
state: "all",
sort: "updated",
direction: "desc",
per_page: "50",
};
if (itemType === "issues") params["since"] = since.toISOString();
const items = await githubGet<GitHubItem[]>(
`https://api.github.com/repos/${cfg.repo}/${itemType}`,
params,
);
return itemType === "pulls" ? items.filter((i) => new Date(i.updated_at) >= since) : items;
}
const all: GitHubItem[] = [];
for (let page = 1; page <= MAX_PAGES; page++) {
const items = await fetchItemPage(cfg.repo, itemType, since, page);
if (items.length === 0) break;
all.push(...items);
const last = items[items.length - 1];
if (last && new Date(last.updated_at) < since) break;
if (items.length < 100) break;
}
return all;
}
export async function fetchRecentReleases(repo: string, since: Date): Promise<GitHubRelease[]> {
const releases = await githubGet<GitHubRelease[]>(`https://api.github.com/repos/${repo}/releases`, {
per_page: "10",
});
return releases.filter((r) => new Date(r.published_at) >= since);
}
export async function ensureLabel(name: string, color: string): Promise<void> {
const digestRepo = process.env["DIGEST_REPO"] ?? "";
const resp = await fetch(`https://api.github.com/repos/${digestRepo}/labels`, {
method: "POST",
headers: { ...headers(), "Content-Type": "application/json" },
body: JSON.stringify({ name, color }),
});
if (!resp.ok && resp.status !== 422) {
throw new Error(`Failed to create label "${name}": ${await resp.text()}`);
}
}
/**
* Fetch trending skills data from a skills repo (e.g. anthropics/skills).
* PRs sorted by popularity (comment count); issues sorted by comments.
* No `since` filter — we want all-time hot items, not just the last 24 h.
*/
export async function fetchSkillsData(repo: string): Promise<{ prs: GitHubItem[]; issues: GitHubItem[] }> {
const [prs, issuesRaw] = await Promise.all([
githubGet<GitHubItem[]>(`https://api.github.com/repos/${repo}/pulls`, {
state: "open",
sort: "popularity",
direction: "desc",
per_page: "50",
}),
githubGet<GitHubItem[]>(`https://api.github.com/repos/${repo}/issues`, {
state: "all",
sort: "comments",
direction: "desc",
per_page: "50",
}),
]);
return { prs, issues: issuesRaw.filter((i) => !i.pull_request) };
}
const GITHUB_ISSUE_BODY_LIMIT = 65536;
const TRUNCATION_NOTICE = "\n\n---\n> ⚠️ 内容超过 GitHub Issue 上限,完整报告见提交的 Markdown 文件。";
function defangGitHubNotifications(body: string): string {
return body
.replace(
/https?:\/\/(?:www\.)?github\.com\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+\/(?:issues|pull)\/\d+(?:[/?#][^\s`<>"']*)?/g,
(match: string) => {
const trailingPunctuationMatch = match.match(/[),.!?:;]+$/);
const trailingPunctuation = trailingPunctuationMatch?.[0] ?? "";
const urlWithoutTrailingPunctuation = trailingPunctuation
? match.slice(0, -trailingPunctuation.length)
: match;
return (
urlWithoutTrailingPunctuation.replace(
/^https?:\/\/(?:www\.)?github\.com/,
"github[.]com",
) + trailingPunctuation
);
},
)
.replace(/\b([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)#(\d+)\b/g, "`$1#$2`")
.replace(/(^|[^\w`])@([A-Za-z0-9-]{1,39})\b/g, "$1@\u200B$2");
}
export async function createGitHubIssue(title: string, body: string, label: string): Promise<string> {
const digestRepo = process.env["DIGEST_REPO"] ?? "";
body = defangGitHubNotifications(body);
if (body.length > GITHUB_ISSUE_BODY_LIMIT) {
body = body.slice(0, GITHUB_ISSUE_BODY_LIMIT - TRUNCATION_NOTICE.length) + TRUNCATION_NOTICE;
}
const LABEL_COLORS: Record<string, string> = {
openclaw: "e11d48",
trending: "f9a825",
hn: "ff6600",
weekly: "7c3aed",
monthly: "0d9488",
"digest-en": "1d76db",
"openclaw-en": "f472b6",
"web-en": "6366f1",
"trending-en": "fbbf24",
"hn-en": "fb923c",
};
await ensureLabel(label, LABEL_COLORS[label] ?? "0075ca");
const resp = await fetch(`https://api.github.com/repos/${digestRepo}/issues`, {
method: "POST",
headers: { ...headers(), "Content-Type": "application/json" },
body: JSON.stringify({ title, body, labels: [label] }),
});
if (!resp.ok) throw new Error(`Failed to create issue: ${await resp.text()}`);
const data = (await resp.json()) as { html_url: string };
return data.html_url;
}