Personal Telegram developer console for GitHub
feat: add bounded GitHub Advanced read APIs
davoodmehraban89 committed
Oct 3, 2026 at 09:12 UTC
e746e0018c5581df3a69fb68ffbb3d67a7da91c0
1 file changed
+212
-8
src/github.js
+212
-8
@@ -1,5 +1,19 @@
1
import { DEFAULT_CONFIG } from './config.js';
2
3
+const clampPage = value => Math.max(1, Math.min(100, Number(value) || 1));
4
+const clampPerPage = (value, fallback = 10) => Math.max(1, Math.min(20, Number(value) || fallback));
5
+const boundedPatch = value => String(value || '').slice(0, 1200);
6
+const numericId = (value, label) => {
7
+ const text = String(value ?? '');
8
+ if (!/^\d+$/.test(text)) throw new Error(`Invalid ${label} ID`);
9
+ return text;
10
+};
11
+const fullSha = (value, label = 'Git commit') => {
12
+ const text = String(value || '');
13
+ if (!/^[a-f0-9]{40}$/i.test(text)) throw new Error(`Invalid ${label} SHA`);
14
+ return text;
15
+};
16
+
17
export class GitHubService {
18
constructor(token = '', owner = DEFAULT_CONFIG.defaultOwner) {
19
this.token = token;
@@ -59,6 +73,20 @@ export class GitHubService {
73
return parsed;
74
}
75
76
+ async requestText(url, label, { maxChars = 12000 } = {}) {
77
+ const res = await fetch(url, { method: 'GET', headers: this.getHeaders() });
78
+ const raw = await res.text();
79
+ if (!res.ok) {
80
+ const detail = raw ? ` ${raw.slice(0, 240)}` : '';
81
+ throw new Error(`${label} failed: ${res.status}${detail}`);
82
+ }
83
+ const limit = Math.max(100, Math.min(20000, Number(maxChars) || 12000));
84
+ return {
85
+ text: raw.length > limit ? raw.slice(-limit) : raw,
86
+ truncated: raw.length > limit
87
+ };
88
+ }
89
+
90
repoUrl(repo, suffix = '') {
91
return `${this.baseUrl}/repos/${encodeURIComponent(this.owner)}/${encodeURIComponent(repo)}${suffix}`;
92
}
@@ -136,6 +164,17 @@ export class GitHubService {
164
};
165
}
166
167
+ mapPullRequestFile(file = {}) {
168
+ return {
169
+ filename: file.filename || '',
170
+ status: file.status || '',
171
+ additions: file.additions ?? 0,
172
+ deletions: file.deletions ?? 0,
173
+ changes: file.changes ?? 0,
174
+ patch: boundedPatch(file.patch)
175
+ };
176
+ }
177
+
178
async listRepositories() {
179
const data = await this.requestJson(
180
`${this.baseUrl}/user/repos?sort=updated&per_page=20&affiliation=owner,collaborator,organization_member`,
@@ -156,10 +195,12 @@ export class GitHubService {
195
return this.mapRepository(raw);
196
}
197
159
- async listPullRequests(repoRef) {
198
+ async listPullRequests(repoRef, { perPage = 10, page = 1 } = {}) {
199
+ const count = clampPerPage(perPage, 10);
200
+ const pageNumber = clampPage(page);
201
const { apiBase } = await this.getRepositoryContext(repoRef);
202
const data = await this.requestJson(
162
- `${apiBase}/pulls?state=all&per_page=10`,
203
+ `${apiBase}/pulls?state=all&per_page=${count}&page=${pageNumber}`,
204
'GitHub pull requests'
205
);
206
@@ -174,6 +215,39 @@ export class GitHubService {
215
}));
216
}
217
218
+ async getPullRequest(repoRef, prNumber) {
219
+ const number = numericId(prNumber, 'pull request');
220
+ const { apiBase } = await this.getRepositoryContext(repoRef);
221
+ const pr = await this.requestJson(`${apiBase}/pulls/${number}`, 'GitHub pull request');
222
+ return {
223
+ number: pr.number,
224
+ title: pr.title || '',
225
+ author: pr.user?.login || 'unknown',
226
+ state: pr.state || '',
227
+ merged: !!(pr.merged ?? pr.merged_at),
228
+ body: pr.body || '',
229
+ url: pr.html_url || '',
230
+ head: pr.head?.ref || '',
231
+ headSha: pr.head?.sha || '',
232
+ base: pr.base?.ref || '',
233
+ additions: pr.additions ?? 0,
234
+ deletions: pr.deletions ?? 0,
235
+ changedFiles: pr.changed_files ?? 0
236
+ };
237
+ }
238
+
239
+ async listPullRequestFiles(repoRef, prNumber, { perPage = 20, page = 1 } = {}) {
240
+ const number = numericId(prNumber, 'pull request');
241
+ const count = clampPerPage(perPage, 20);
242
+ const pageNumber = clampPage(page);
243
+ const { apiBase } = await this.getRepositoryContext(repoRef);
244
+ const data = await this.requestJson(
245
+ `${apiBase}/pulls/${number}/files?per_page=${count}&page=${pageNumber}`,
246
+ 'GitHub pull request files'
247
+ );
248
+ return data.map(file => this.mapPullRequestFile(file));
249
+ }
250
+
251
async listCommits(repoRef) {
252
const { apiBase } = await this.getRepositoryContext(repoRef);
253
const data = await this.requestJson(
@@ -189,6 +263,24 @@ export class GitHubService {
263
}));
264
}
265
266
+ async listCommitChecks(repoRef, sha, { perPage = 20, page = 1 } = {}) {
267
+ const commitSha = fullSha(sha);
268
+ const count = clampPerPage(perPage, 20);
269
+ const pageNumber = clampPage(page);
270
+ const { apiBase } = await this.getRepositoryContext(repoRef);
271
+ const data = await this.requestJson(
272
+ `${apiBase}/commits/${commitSha}/check-runs?per_page=${count}&page=${pageNumber}`,
273
+ 'GitHub check runs'
274
+ );
275
+ return (data.check_runs || []).map(check => ({
276
+ id: check.id,
277
+ name: check.name || 'Check',
278
+ status: check.status || '',
279
+ conclusion: check.conclusion || null,
280
+ url: check.html_url || ''
281
+ }));
282
+ }
283
+
284
async listFiles(repoRef, path = '') {
285
const { apiBase } = await this.getRepositoryContext(repoRef);
286
const suffix = path
@@ -272,8 +364,8 @@ export class GitHubService {
364
}
365
366
async listWorkflowRuns(repoRef, { perPage = 10, page = 1 } = {}) {
275
- const count = Math.max(1, Math.min(20, Number(perPage) || 10));
276
- const pageNumber = Math.max(1, Math.min(100, Number(page) || 1));
367
+ const count = clampPerPage(perPage, 10);
368
+ const pageNumber = clampPage(page);
369
const { apiBase } = await this.getRepositoryContext(repoRef);
370
const pageQuery = pageNumber > 1 ? `&page=${pageNumber}` : '';
371
const data = await this.requestJson(
@@ -284,36 +376,148 @@ export class GitHubService {
376
}
377
378
async getWorkflowRun(repoRef, runId) {
379
+ const id = numericId(runId, 'workflow run');
380
const { apiBase } = await this.getRepositoryContext(repoRef);
381
const data = await this.requestJson(
289
- `${apiBase}/actions/runs/${encodeURIComponent(String(runId))}`,
382
+ `${apiBase}/actions/runs/${id}`,
383
'GitHub workflow run'
384
);
385
return this.mapWorkflowRun(data);
386
}
387
388
async listWorkflowJobs(repoRef, runId) {
389
+ const id = numericId(runId, 'workflow run');
390
const { apiBase } = await this.getRepositoryContext(repoRef);
391
const data = await this.requestJson(
298
- `${apiBase}/actions/runs/${encodeURIComponent(String(runId))}/jobs?per_page=20`,
392
+ `${apiBase}/actions/runs/${id}/jobs?per_page=20`,
393
'GitHub workflow jobs'
394
);
395
return (data.jobs || []).map(job => this.mapWorkflowJob(job));
396
}
397
398
+ async getWorkflowJob(repoRef, jobId) {
399
+ const id = numericId(jobId, 'workflow job');
400
+ const { apiBase } = await this.getRepositoryContext(repoRef);
401
+ const data = await this.requestJson(`${apiBase}/actions/jobs/${id}`, 'GitHub workflow job');
402
+ return this.mapWorkflowJob(data);
403
+ }
404
+
405
+ async getWorkflowJobLog(repoRef, jobId, { maxChars = 12000 } = {}) {
406
+ const id = numericId(jobId, 'workflow job');
407
+ const { apiBase } = await this.getRepositoryContext(repoRef);
408
+ return this.requestText(`${apiBase}/actions/jobs/${id}/logs`, 'GitHub workflow job log', { maxChars });
409
+ }
410
+
411
+ async listWorkflowArtifacts(repoRef, runId, { perPage = 20, page = 1 } = {}) {
412
+ const id = numericId(runId, 'workflow run');
413
+ const count = clampPerPage(perPage, 20);
414
+ const pageNumber = clampPage(page);
415
+ const { apiBase } = await this.getRepositoryContext(repoRef);
416
+ const data = await this.requestJson(
417
+ `${apiBase}/actions/runs/${id}/artifacts?per_page=${count}&page=${pageNumber}`,
418
+ 'GitHub workflow artifacts'
419
+ );
420
+ return (data.artifacts || []).map(artifact => ({
421
+ id: artifact.id,
422
+ name: artifact.name || 'Artifact',
423
+ size: artifact.size_in_bytes ?? 0,
424
+ expired: !!artifact.expired,
425
+ createdAt: artifact.created_at || '',
426
+ expiresAt: artifact.expires_at || '',
427
+ downloadUrl: artifact.archive_download_url || ''
428
+ }));
429
+ }
430
+
431
+ async listBranches(repoRef, { perPage = 20, page = 1 } = {}) {
432
+ const count = clampPerPage(perPage, 20);
433
+ const pageNumber = clampPage(page);
434
+ const { apiBase } = await this.getRepositoryContext(repoRef);
435
+ const data = await this.requestJson(
436
+ `${apiBase}/branches?per_page=${count}&page=${pageNumber}`,
437
+ 'GitHub branches'
438
+ );
439
+ return data.map(branch => ({
440
+ name: branch.name || '',
441
+ sha: branch.commit?.sha || '',
442
+ protected: !!branch.protected
443
+ }));
444
+ }
445
+
446
+ async compareWithDefault(repoRef, headSha) {
447
+ const head = fullSha(headSha);
448
+ const { raw, apiBase } = await this.getRepositoryContext(repoRef);
449
+ const base = raw.default_branch || 'main';
450
+ const data = await this.requestJson(
451
+ `${apiBase}/compare/${encodeURIComponent(base)}...${head}`,
452
+ 'GitHub compare'
453
+ );
454
+ return {
455
+ base,
456
+ head,
457
+ status: data.status || '',
458
+ aheadBy: data.ahead_by ?? 0,
459
+ behindBy: data.behind_by ?? 0,
460
+ totalCommits: data.total_commits ?? 0,
461
+ baseSha: data.base_commit?.sha || '',
462
+ mergeBaseSha: data.merge_base_commit?.sha || '',
463
+ files: (data.files || []).slice(0, 20).map(file => this.mapPullRequestFile(file))
464
+ };
465
+ }
466
+
467
+ async listIssues(repoRef, { perPage = 10, page = 1 } = {}) {
468
+ const count = clampPerPage(perPage, 10);
469
+ const pageNumber = clampPage(page);
470
+ const { apiBase } = await this.getRepositoryContext(repoRef);
471
+ const data = await this.requestJson(
472
+ `${apiBase}/issues?state=all&per_page=${count}&page=${pageNumber}`,
473
+ 'GitHub issues'
474
+ );
475
+ return data
476
+ .filter(issue => !issue.pull_request)
477
+ .map(issue => ({
478
+ number: issue.number,
479
+ title: issue.title || '',
480
+ state: issue.state || '',
481
+ author: issue.user?.login || 'unknown',
482
+ comments: issue.comments ?? 0,
483
+ url: issue.html_url || ''
484
+ }));
485
+ }
486
+
487
+ async listReleases(repoRef, { perPage = 10, page = 1 } = {}) {
488
+ const count = clampPerPage(perPage, 10);
489
+ const pageNumber = clampPage(page);
490
+ const { apiBase } = await this.getRepositoryContext(repoRef);
491
+ const data = await this.requestJson(
492
+ `${apiBase}/releases?per_page=${count}&page=${pageNumber}`,
493
+ 'GitHub releases'
494
+ );
495
+ return data.map(release => ({
496
+ id: release.id,
497
+ tag: release.tag_name || '',
498
+ name: release.name || release.tag_name || 'Release',
499
+ draft: !!release.draft,
500
+ prerelease: !!release.prerelease,
501
+ publishedAt: release.published_at || release.created_at || '',
502
+ url: release.html_url || ''
503
+ }));
504
+ }
505
+
506
async rerunFailedWorkflowRun(repoRef, runId) {
507
+ const id = numericId(runId, 'workflow run');
508
const { apiBase } = await this.getRepositoryContext(repoRef);
509
return this.requestJson(
307
- `${apiBase}/actions/runs/${encodeURIComponent(String(runId))}/rerun-failed-jobs`,
510
+ `${apiBase}/actions/runs/${id}/rerun-failed-jobs`,
511
'GitHub rerun failed jobs',
512
{ method: 'POST', allowEmpty: true }
513
);
514
}
515
516
async cancelWorkflowRun(repoRef, runId) {
517
+ const id = numericId(runId, 'workflow run');
518
const { apiBase } = await this.getRepositoryContext(repoRef);
519
return this.requestJson(
316
- `${apiBase}/actions/runs/${encodeURIComponent(String(runId))}/cancel`,
520
+ `${apiBase}/actions/runs/${id}/cancel`,
521
'GitHub cancel workflow run',
522
{ method: 'POST', allowEmpty: true }
523
);