Personal Telegram developer console for GitHub
feat: add GitHub Actions workflow service methods
davoodmehraban89 committed
Oct 3, 2026 at 05:38 UTC
5715d9a77862cfee13e8641183e6b8fd13fc9df3
1 file changed
+109
-8
src/github.js
+109
-8
@@ -23,19 +23,40 @@ export class GitHubService {
23
};
24
}
25
26
- async requestJson(url, label) {
27
- const res = await fetch(url, { headers: this.getHeaders() });
28
- if (!res.ok) {
29
- let detail = '';
26
+ async requestJson(url, label, { method = 'GET', body, allowEmpty = false } = {}) {
27
+ const headers = this.getHeaders();
28
+ const options = { method, headers };
29
+ if (body !== undefined) {
30
+ options.body = JSON.stringify(body);
31
+ options.headers = { ...headers, 'Content-Type': 'application/json' };
32
+ }
33
+
34
+ const res = await fetch(url, options);
35
+ const raw = await res.text();
36
+ let parsed = null;
37
+ if (raw) {
38
try {
31
- const body = await res.json();
32
- detail = body?.message ? ` ${body.message}` : '';
39
+ parsed = JSON.parse(raw);
40
} catch {
34
- detail = '';
41
+ parsed = null;
42
}
43
+ }
44
+
45
+ if (!res.ok) {
46
+ const providerMessage = parsed?.message || raw.slice(0, 240);
47
+ const detail = providerMessage ? ` ${providerMessage}` : '';
48
throw new Error(`${label} failed: ${res.status}${detail}`);
49
}
38
- return res.json();
50
+
51
+ if (allowEmpty) {
52
+ return { accepted: true, status: res.status };
53
+ }
54
+
55
+ if (!raw) return null;
56
+ if (parsed === null) {
57
+ throw new Error(`${label} failed: invalid JSON response`);
58
+ }
59
+ return parsed;
60
}
61
62
repoUrl(repo, suffix = '') {
@@ -81,6 +102,40 @@ export class GitHubService {
102
};
103
}
104
105
+ mapWorkflowRun(run = {}) {
106
+ return {
107
+ id: run.id,
108
+ name: run.name || 'Workflow',
109
+ status: run.status || '',
110
+ conclusion: run.conclusion || null,
111
+ event: run.event || '',
112
+ branch: run.head_branch || '',
113
+ sha: String(run.head_sha || '').slice(0, 7),
114
+ runNumber: run.run_number ?? null,
115
+ url: run.html_url || '',
116
+ createdAt: run.created_at || '',
117
+ updatedAt: run.updated_at || ''
118
+ };
119
+ }
120
+
121
+ mapWorkflowJob(job = {}) {
122
+ return {
123
+ id: job.id,
124
+ name: job.name || 'Job',
125
+ status: job.status || '',
126
+ conclusion: job.conclusion || null,
127
+ startedAt: job.started_at || '',
128
+ completedAt: job.completed_at || '',
129
+ url: job.html_url || '',
130
+ steps: (job.steps || []).slice(0, 30).map(step => ({
131
+ number: step.number,
132
+ name: step.name || 'Step',
133
+ status: step.status || '',
134
+ conclusion: step.conclusion || null
135
+ }))
136
+ };
137
+ }
138
+
139
async listRepositories() {
140
const data = await this.requestJson(
141
`${this.baseUrl}/user/repos?sort=updated&per_page=20&affiliation=owner,collaborator,organization_member`,
@@ -215,4 +270,50 @@ export class GitHubService {
270
}))
271
};
272
}
273
+
274
+ async listWorkflowRuns(repoRef, { perPage = 10 } = {}) {
275
+ const count = Math.max(1, Math.min(20, Number(perPage) || 10));
276
+ const { apiBase } = await this.getRepositoryContext(repoRef);
277
+ const data = await this.requestJson(
278
+ `${apiBase}/actions/runs?per_page=${count}`,
279
+ 'GitHub workflow runs'
280
+ );
281
+ return (data.workflow_runs || []).map(run => this.mapWorkflowRun(run));
282
+ }
283
+
284
+ async getWorkflowRun(repoRef, runId) {
285
+ const { apiBase } = await this.getRepositoryContext(repoRef);
286
+ const data = await this.requestJson(
287
+ `${apiBase}/actions/runs/${encodeURIComponent(String(runId))}`,
288
+ 'GitHub workflow run'
289
+ );
290
+ return this.mapWorkflowRun(data);
291
+ }
292
+
293
+ async listWorkflowJobs(repoRef, runId) {
294
+ const { apiBase } = await this.getRepositoryContext(repoRef);
295
+ const data = await this.requestJson(
296
+ `${apiBase}/actions/runs/${encodeURIComponent(String(runId))}/jobs?per_page=20`,
297
+ 'GitHub workflow jobs'
298
+ );
299
+ return (data.jobs || []).map(job => this.mapWorkflowJob(job));
300
+ }
301
+
302
+ async rerunFailedWorkflowRun(repoRef, runId) {
303
+ const { apiBase } = await this.getRepositoryContext(repoRef);
304
+ return this.requestJson(
305
+ `${apiBase}/actions/runs/${encodeURIComponent(String(runId))}/rerun-failed-jobs`,
306
+ 'GitHub rerun failed jobs',
307
+ { method: 'POST', allowEmpty: true }
308
+ );
309
+ }
310
+
311
+ async cancelWorkflowRun(repoRef, runId) {
312
+ const { apiBase } = await this.getRepositoryContext(repoRef);
313
+ return this.requestJson(
314
+ `${apiBase}/actions/runs/${encodeURIComponent(String(runId))}/cancel`,
315
+ 'GitHub cancel workflow run',
316
+ { method: 'POST', allowEmpty: true }
317
+ );
318
+ }
319
}