-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.ts
More file actions
495 lines (448 loc) · 18.1 KB
/
Copy pathgithub.ts
File metadata and controls
495 lines (448 loc) · 18.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/**
* GitHub REST client for dsh-github-intelligence.
*
* Anonymous by default; an optional token raises the rate limit. All public
* methods honor cancellation and are wrapped in a short TTL cache so agents
* can compose several tools without burning the anonymous budget.
* @module dsh-github-intelligence/github
*/
export interface GitHubClientOptions {
/** Optional GitHub token; raises the 60/hour anonymous limit. */
token?: string
/** User-Agent header sent with every request. */
userAgent: string
/** Request timeout in milliseconds. */
timeoutMs: number
/** Maximum characters kept from a release body preview. */
bodyPreviewChars: number
/** TTL of the in-memory response cache. */
cacheTtlMs: number
}
export interface GitHubRelease {
tagName: string
name: string | null
publishedAt: string | null
prerelease: boolean
htmlUrl: string
bodyPreview: string | null
}
export interface GitHubRepo {
fullName: string
description: string | null
homepage: string | null
stars: number
forks: number
openIssues: number
language: string | null
license: string | null
topics: string[]
defaultBranch: string | null
archived: boolean
createdAt: string | null
updatedAt: string | null
pushedAt: string | null
htmlUrl: string
}
export interface GitHubRepoHit {
fullName: string
description: string | null
stars: number
language: string | null
updatedAt: string | null
htmlUrl: string
}
export interface GitHubIssue {
number: number
title: string
state: 'open' | 'closed'
user: string
comments: number
createdAt: string | null
htmlUrl: string
}
export interface GitHubPull {
number: number
title: string
state: 'open' | 'closed'
user: string
createdAt: string | null
mergedAt: string | null
htmlUrl: string
}
export interface GitHubContributor {
login: string
contributions: number
avatarUrl: string | null
}
export interface GitHubCommit {
sha: string
message: string
author: string | null
date: string | null
htmlUrl: string
}
export interface GitHubNotification {
id: string
unread: boolean
reason: string
updatedAt: string | null
lastReadAt: string | null
repository: {
fullName: string
htmlUrl: string
}
subject: {
title: string
type: string
apiUrl: string | null
htmlUrl: string | null
latestCommentUrl: string | null
}
}
export interface GitHubCommunityProfile {
healthPercentage: number
files: {
codeOfConduct: boolean
contributing: boolean
issueTemplate: boolean
pullRequestTemplate: boolean
readme: boolean
security: boolean
license: boolean
}
}
export type IssueState = 'open' | 'closed' | 'all'
export type PullState = 'open' | 'closed' | 'all'
const API_ROOT = 'https://api.github.com'
function asString(value: unknown): string | null {
return typeof value === 'string' && value.length > 0 ? value : null
}
function asNumber(value: unknown): number {
return typeof value === 'number' && Number.isFinite(value) ? value : 0
}
function asBoolean(value: unknown): boolean {
return value === true
}
function asStringArray(value: unknown): string[] {
return Array.isArray(value) ? value.filter((v): v is string => typeof v === 'string') : []
}
function preview(value: unknown, maxChars: number): string | null {
const text = asString(value)
if (text === null) return null
return text.length <= maxChars ? text : `${text.slice(0, maxChars)}…`
}
function firstLine(value: unknown): string | null {
const text = asString(value)
if (text === null) return null
const index = text.indexOf('\n')
return index === -1 ? text : text.slice(0, index)
}
function subjectHtmlUrl(value: unknown): string | null {
const url = asString(value)
if (url === null) return null
const match = /^https:\/\/api\.github\.com\/repos\/([^/]+)\/([^/]+)\/(issues|pulls|discussions|commits)\/([^/?#]+)/.exec(url)
if (match === null) return null
const [, owner, repo, surface, id] = match
const webSurface = surface === 'pulls' ? 'pull' : surface === 'commits' ? 'commit' : surface
return `https://github.com/${owner}/${repo}/${webSurface}/${id}`
}
/**
* The GitHub REST client used by every tool in this plugin.
*/
export class GitHubClient {
private readonly cache = new Map<string, { expires: number; value: unknown }>()
constructor(private readonly options: GitHubClientOptions) {}
private withDeadline(signal: AbortSignal): AbortSignal {
return AbortSignal.any([signal, AbortSignal.timeout(this.options.timeoutMs)])
}
private async cached<T>(key: string, loader: () => Promise<T>): Promise<T> {
const now = Date.now()
const hit = this.cache.get(key)
if (hit !== undefined && hit.expires > now) return hit.value as T
const value = await loader()
this.cache.set(key, { expires: now + this.options.cacheTtlMs, value })
return value
}
/**
* Raw cached request used by generated catalog tools.
* @param path - API path (starting with /).
* @param signal - caller cancellation signal.
* @param cacheKey - optional cache key; defaults to the path.
*/
raw<T>(path: string, signal: AbortSignal, cacheKey?: string): Promise<T> {
const key = cacheKey ?? path
return this.cached(key, () => this.request<T>(path, signal))
}
private async request<T>(path: string, signal: AbortSignal): Promise<T> {
const headers: Record<string, string> = {
accept: 'application/vnd.github+json',
'user-agent': this.options.userAgent,
}
if (this.options.token !== undefined) headers.authorization = `Bearer ${this.options.token}`
let response: Response
try {
response = await fetch(`${API_ROOT}${path}`, { headers, signal: this.withDeadline(signal) })
} catch (error: unknown) {
if (signal.aborted) throw new Error('GitHub request cancelled')
if (error instanceof Error && error.name === 'TimeoutError') {
throw new Error(`GitHub request timed out after ${this.options.timeoutMs}ms`)
}
throw new Error(`GitHub network request failed: ${error instanceof Error ? error.message : String(error)}`)
}
if (!response.ok) {
if (response.status === 403 && response.headers.get('x-ratelimit-remaining') === '0') {
throw new Error(
'GitHub API rate limit exceeded (60 requests/hour without a token). '
+ 'Set `githubToken` in the plugin config to raise the limit.',
)
}
if (response.status === 401) {
throw new Error('GitHub API rejected the configured token (401). Check `githubToken` in the plugin config.')
}
if (response.status === 404) {
throw new Error('GitHub resource not found. Check owner/repo spelling and that the repository is public.')
}
if (response.status === 429) {
throw new Error('GitHub API is rate limiting this request (429). Wait a moment and retry, or configure `githubToken`.')
}
throw new Error(`GitHub API error ${response.status} for ${path}`)
}
return await response.json() as T
}
private encode(owner: string, repo: string): string {
return `${encodeURIComponent(owner)}/${encodeURIComponent(repo)}`
}
getRepo(owner: string, repo: string, signal: AbortSignal): Promise<GitHubRepo> {
const path = `/repos/${this.encode(owner, repo)}`
return this.cached(`repo:${path}`, async () => this.parseRepo(await this.request<unknown>(path, signal)))
}
listReleases(owner: string, repo: string, limit: number, signal: AbortSignal): Promise<GitHubRelease[]> {
const perPage = Math.min(Math.max(Math.trunc(limit), 1), 50)
const path = `/repos/${this.encode(owner, repo)}/releases?per_page=${perPage}`
return this.cached(`releases:${path}`, async () => {
const data = await this.request<unknown[]>(path, signal)
return data.map((entry) => this.parseRelease(entry))
})
}
searchRepos(query: string, limit: number, sort: 'stars' | 'updated', signal: AbortSignal): Promise<GitHubRepoHit[]> {
const perPage = Math.min(Math.max(Math.trunc(limit), 1), 50)
const path = `/search/repositories?q=${encodeURIComponent(query)}&sort=${sort}&order=desc&per_page=${perPage}`
return this.cached(`search:${path}`, async () => {
const data = await this.request<{ items?: unknown }>(path, signal)
const items = Array.isArray(data.items) ? data.items : []
return items.map((entry) => this.parseRepoHit(entry))
})
}
/** List a user's own repositories sorted by stars (forks excluded by default). */
listUserRepos(username: string, limit: number, signal: AbortSignal): Promise<GitHubRepoHit[]> {
const perPage = Math.min(Math.max(Math.trunc(limit), 1), 50)
const path = `/users/${encodeURIComponent(username)}/repos?type=owner&sort=stars&order=desc&per_page=${perPage}`
return this.cached(`user-repos:${path}`, async () => {
const data = await this.request<unknown[]>(path, signal)
return data.map((entry) => this.parseRepoHit(entry))
})
}
/** Approximate "trending": repositories created recently, sorted by stars. */
trending(limit: number, language: string | undefined, sinceDays: number, signal: AbortSignal): Promise<GitHubRepoHit[]> {
const perPage = Math.min(Math.max(Math.trunc(limit), 1), 50)
const since = new Date(Date.now() - sinceDays * 86_400_000).toISOString().slice(0, 10)
let query = `created:>${since}`
if (language !== undefined && language.trim() !== '') query += ` language:${language.trim()}`
return this.searchRepos(query, perPage, 'stars', signal)
}
listIssues(owner: string, repo: string, state: IssueState, limit: number, signal: AbortSignal): Promise<GitHubIssue[]> {
const perPage = Math.min(Math.max(Math.trunc(limit), 1), 50)
const path = `/repos/${this.encode(owner, repo)}/issues?state=${state}&sort=created&direction=desc&per_page=${perPage}`
return this.cached(`issues:${path}`, async () => {
const data = await this.request<unknown[]>(path, signal)
// The issues endpoint also returns pull requests; they carry a `pull_request` key.
return data
.filter((entry) => (entry as Record<string, unknown>).pull_request === undefined)
.map((entry) => this.parseIssue(entry))
})
}
listPulls(owner: string, repo: string, state: PullState, limit: number, signal: AbortSignal): Promise<GitHubPull[]> {
const perPage = Math.min(Math.max(Math.trunc(limit), 1), 50)
const path = `/repos/${this.encode(owner, repo)}/pulls?state=${state}&sort=created&direction=desc&per_page=${perPage}`
return this.cached(`pulls:${path}`, async () => {
const data = await this.request<unknown[]>(path, signal)
return data.map((entry) => this.parsePull(entry))
})
}
listContributors(owner: string, repo: string, limit: number, signal: AbortSignal): Promise<GitHubContributor[]> {
const perPage = Math.min(Math.max(Math.trunc(limit), 1), 50)
const path = `/repos/${this.encode(owner, repo)}/contributors?per_page=${perPage}`
return this.cached(`contributors:${path}`, async () => {
const data = await this.request<unknown[]>(path, signal)
return data.map((entry) => this.parseContributor(entry))
})
}
recentCommits(owner: string, repo: string, limit: number, signal: AbortSignal): Promise<GitHubCommit[]> {
const perPage = Math.min(Math.max(Math.trunc(limit), 1), 50)
const path = `/repos/${this.encode(owner, repo)}/commits?per_page=${perPage}`
return this.cached(`commits:${path}`, async () => {
const data = await this.request<unknown[]>(path, signal)
return data.map((entry) => this.parseCommit(entry))
})
}
/** Repository community-health files and GitHub's own health percentage. */
getCommunityProfile(owner: string, repo: string, signal: AbortSignal): Promise<GitHubCommunityProfile> {
const path = `/repos/${this.encode(owner, repo)}/community/profile`
return this.cached(`community:${path}`, async () => {
const raw = await this.request<Record<string, unknown>>(path, signal)
const files = raw.files !== null && typeof raw.files === 'object'
? raw.files as Record<string, unknown>
: {}
const present = (key: string): boolean => files[key] !== null && files[key] !== undefined
return {
healthPercentage: Math.min(Math.max(asNumber(raw.health_percentage), 0), 100),
files: {
codeOfConduct: present('code_of_conduct') || present('code_of_conduct_file'),
contributing: present('contributing'),
issueTemplate: present('issue_template'),
pullRequestTemplate: present('pull_request_template'),
readme: present('readme'),
security: present('security'),
license: present('license'),
},
}
})
}
/** Authenticated notification inbox, exposed as a read-only maintainer queue. */
async listNotifications(
all: boolean,
participating: boolean,
limit: number,
signal: AbortSignal,
): Promise<GitHubNotification[]> {
if (this.options.token === undefined || this.options.token === '') {
throw new Error(
'github-intelligence: github_notifications requires `githubToken` with notification read access.',
)
}
const perPage = Math.min(Math.max(Math.trunc(limit), 1), 50)
const path = `/notifications?all=${all}&participating=${participating}&per_page=${perPage}`
return await this.cached(`notifications:${path}`, async () => {
const data = await this.request<unknown[]>(path, signal)
return data.map((entry) => this.parseNotification(entry))
})
}
private parseRelease(entry: unknown): GitHubRelease {
const raw = entry as Record<string, unknown>
return {
tagName: asString(raw.tag_name) ?? 'unknown',
name: asString(raw.name),
publishedAt: asString(raw.published_at),
prerelease: asBoolean(raw.prerelease),
htmlUrl: asString(raw.html_url) ?? '',
bodyPreview: preview(raw.body, this.options.bodyPreviewChars),
}
}
private parseRepo(entry: unknown): GitHubRepo {
const raw = entry as Record<string, unknown>
const license = raw.license as Record<string, unknown> | null
return {
fullName: asString(raw.full_name) ?? 'unknown',
description: asString(raw.description),
homepage: asString(raw.homepage),
stars: asNumber(raw.stargazers_count),
forks: asNumber(raw.forks_count),
openIssues: asNumber(raw.open_issues_count),
language: asString(raw.language),
license: license !== null && typeof license === 'object' ? asString(license.spdx_id) : null,
topics: asStringArray(raw.topics),
defaultBranch: asString(raw.default_branch),
archived: asBoolean(raw.archived),
createdAt: asString(raw.created_at),
updatedAt: asString(raw.updated_at),
pushedAt: asString(raw.pushed_at),
htmlUrl: asString(raw.html_url) ?? '',
}
}
private parseRepoHit(entry: unknown): GitHubRepoHit {
const raw = entry as Record<string, unknown>
return {
fullName: asString(raw.full_name) ?? 'unknown',
description: asString(raw.description),
stars: asNumber(raw.stargazers_count),
language: asString(raw.language),
updatedAt: asString(raw.updated_at),
htmlUrl: asString(raw.html_url) ?? '',
}
}
private parseIssue(entry: unknown): GitHubIssue {
const raw = entry as Record<string, unknown>
const user = raw.user as Record<string, unknown> | null
return {
number: asNumber(raw.number),
title: asString(raw.title) ?? 'untitled',
state: raw.state === 'closed' ? 'closed' : 'open',
user: user !== null && typeof user === 'object' ? asString(user.login) ?? 'unknown' : 'unknown',
comments: asNumber(raw.comments),
createdAt: asString(raw.created_at),
htmlUrl: asString(raw.html_url) ?? '',
}
}
private parsePull(entry: unknown): GitHubPull {
const raw = entry as Record<string, unknown>
const user = raw.user as Record<string, unknown> | null
return {
number: asNumber(raw.number),
title: asString(raw.title) ?? 'untitled',
state: raw.state === 'closed' ? 'closed' : 'open',
user: user !== null && typeof user === 'object' ? asString(user.login) ?? 'unknown' : 'unknown',
createdAt: asString(raw.created_at),
mergedAt: asString(raw.merged_at),
htmlUrl: asString(raw.html_url) ?? '',
}
}
private parseContributor(entry: unknown): GitHubContributor {
const raw = entry as Record<string, unknown>
return {
login: asString(raw.login) ?? 'unknown',
contributions: asNumber(raw.contributions),
avatarUrl: asString(raw.avatar_url),
}
}
private parseCommit(entry: unknown): GitHubCommit {
const raw = entry as Record<string, unknown>
const commit = raw.commit as Record<string, unknown> | null
const author = commit !== null && typeof commit === 'object'
? commit.author as Record<string, unknown> | null
: null
return {
sha: asString(raw.sha) ?? '',
message: firstLine(commit !== null && typeof commit === 'object' ? commit.message : null) ?? '',
author: author !== null && typeof author === 'object' ? asString(author.name) : null,
date: author !== null && typeof author === 'object' ? asString(author.date) : null,
htmlUrl: asString(raw.html_url) ?? '',
}
}
private parseNotification(entry: unknown): GitHubNotification {
const raw = entry as Record<string, unknown>
const repository = raw.repository !== null && typeof raw.repository === 'object'
? raw.repository as Record<string, unknown>
: {}
const subject = raw.subject !== null && typeof raw.subject === 'object'
? raw.subject as Record<string, unknown>
: {}
return {
id: asString(raw.id) ?? '',
unread: asBoolean(raw.unread),
reason: asString(raw.reason) ?? 'unknown',
updatedAt: asString(raw.updated_at),
lastReadAt: asString(raw.last_read_at),
repository: {
fullName: asString(repository.full_name) ?? 'unknown',
htmlUrl: asString(repository.html_url) ?? '',
},
subject: {
title: asString(subject.title) ?? 'untitled',
type: asString(subject.type) ?? 'unknown',
apiUrl: asString(subject.url),
htmlUrl: subjectHtmlUrl(subject.url),
latestCommentUrl: asString(subject.latest_comment_url),
},
}
}
}