@ligeti/testing 0.0.0-stage → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +242 -2
- package/bin/argv-recorder.mjs +53 -0
- package/bin/clean-test-databases.mjs +146 -0
- package/bin/fake-claude.mjs +560 -0
- package/bin/fake-herdr.mjs +468 -0
- package/bin/fake-opencode.mjs +45 -0
- package/dist/agents.d.ts +5 -0
- package/dist/agents.d.ts.map +1 -0
- package/dist/agents.js +21 -0
- package/dist/agents.js.map +1 -0
- package/dist/bitbucket.d.ts +26 -0
- package/dist/bitbucket.d.ts.map +1 -0
- package/dist/bitbucket.js +162 -0
- package/dist/bitbucket.js.map +1 -0
- package/dist/callback-server.d.ts +20 -0
- package/dist/callback-server.d.ts.map +1 -0
- package/dist/callback-server.js +79 -0
- package/dist/callback-server.js.map +1 -0
- package/dist/claude-profile.d.ts +3 -0
- package/dist/claude-profile.d.ts.map +1 -0
- package/dist/claude-profile.js +16 -0
- package/dist/claude-profile.js.map +1 -0
- package/dist/env.d.ts +2 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +15 -0
- package/dist/env.js.map +1 -0
- package/dist/fake-http.d.ts +25 -0
- package/dist/fake-http.d.ts.map +1 -0
- package/dist/fake-http.js +129 -0
- package/dist/fake-http.js.map +1 -0
- package/dist/github.d.ts +184 -0
- package/dist/github.d.ts.map +1 -0
- package/dist/github.js +919 -0
- package/dist/github.js.map +1 -0
- package/dist/gitlab-merge-requests.d.ts +23 -0
- package/dist/gitlab-merge-requests.d.ts.map +1 -0
- package/dist/gitlab-merge-requests.js +228 -0
- package/dist/gitlab-merge-requests.js.map +1 -0
- package/dist/gitlab.d.ts +9 -0
- package/dist/gitlab.d.ts.map +1 -0
- package/dist/gitlab.js +13 -0
- package/dist/gitlab.js.map +1 -0
- package/dist/herdr.d.ts +9 -0
- package/dist/herdr.d.ts.map +1 -0
- package/dist/herdr.js +16 -0
- package/dist/herdr.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/jira.d.ts +7 -0
- package/dist/jira.d.ts.map +1 -0
- package/dist/jira.js +7 -0
- package/dist/jira.js.map +1 -0
- package/dist/postgres.d.ts +6 -0
- package/dist/postgres.d.ts.map +1 -0
- package/dist/postgres.js +122 -0
- package/dist/postgres.js.map +1 -0
- package/dist/recorded-turns.d.ts +9 -0
- package/dist/recorded-turns.d.ts.map +1 -0
- package/dist/recorded-turns.js +45 -0
- package/dist/recorded-turns.js.map +1 -0
- package/dist/repo.d.ts +6 -0
- package/dist/repo.d.ts.map +1 -0
- package/dist/repo.js +30 -0
- package/dist/repo.js.map +1 -0
- package/dist/telegram.d.ts +11 -0
- package/dist/telegram.d.ts.map +1 -0
- package/dist/telegram.js +37 -0
- package/dist/telegram.js.map +1 -0
- package/dist/wait.d.ts +2 -0
- package/dist/wait.d.ts.map +1 -0
- package/dist/wait.js +15 -0
- package/dist/wait.js.map +1 -0
- package/dist/worker.d.ts +19 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +56 -0
- package/dist/worker.js.map +1 -0
- package/fixtures/claude-profile/settings.json +26 -0
- package/package.json +32 -3
package/dist/github.js
ADDED
|
@@ -0,0 +1,919 @@
|
|
|
1
|
+
// ABOUTME: A real loopback GitHub REST API fake for the GitHub step packages' tests: an in-memory model of
|
|
2
|
+
// ABOUTME: repositories, issues, pull requests, checks and Actions runs behind a node:http server that records requests.
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import { parseBody, readRequestBody, } from "./fake-http.js";
|
|
5
|
+
const WEB_ORIGIN = "https://github.com";
|
|
6
|
+
const DEFAULT_TOKEN = "fake-github-token";
|
|
7
|
+
const DEFAULT_PER_PAGE = 30;
|
|
8
|
+
const MAX_PER_PAGE = 100;
|
|
9
|
+
const LABEL_COLOR = "ededed";
|
|
10
|
+
function notFound() {
|
|
11
|
+
return {
|
|
12
|
+
status: 404,
|
|
13
|
+
body: { message: "Not Found", documentation_url: "https://docs.github.com/rest" },
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function validationFailed(detail) {
|
|
17
|
+
return { status: 422, body: { message: "Validation Failed", errors: [detail] } };
|
|
18
|
+
}
|
|
19
|
+
function newRepository(owner, name) {
|
|
20
|
+
return {
|
|
21
|
+
owner,
|
|
22
|
+
name,
|
|
23
|
+
issues: new Map(),
|
|
24
|
+
labelIds: new Map(),
|
|
25
|
+
pullRequests: new Map(),
|
|
26
|
+
branches: new Map(),
|
|
27
|
+
checkRuns: new Map(),
|
|
28
|
+
checkSuites: new Map(),
|
|
29
|
+
statuses: new Map(),
|
|
30
|
+
actionsRuns: [],
|
|
31
|
+
reruns: [],
|
|
32
|
+
checkPollHooks: new Map(),
|
|
33
|
+
checkPolls: new Map(),
|
|
34
|
+
defaultBranch: "main",
|
|
35
|
+
allowedMergeMethods: ["merge", "squash", "rebase"],
|
|
36
|
+
nextNumber: 1,
|
|
37
|
+
nextId: 1,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function sameLabel(a, b) {
|
|
41
|
+
return a.toLowerCase() === b.toLowerCase();
|
|
42
|
+
}
|
|
43
|
+
function ensureLabel(repository, label) {
|
|
44
|
+
const existing = [...repository.labelIds.keys()].find((known) => sameLabel(known, label));
|
|
45
|
+
if (existing !== undefined) {
|
|
46
|
+
return existing;
|
|
47
|
+
}
|
|
48
|
+
repository.labelIds.set(label, repository.nextId);
|
|
49
|
+
repository.nextId += 1;
|
|
50
|
+
return label;
|
|
51
|
+
}
|
|
52
|
+
function labelJson(repository, label) {
|
|
53
|
+
return { id: repository.labelIds.get(label), name: label, color: LABEL_COLOR, default: false };
|
|
54
|
+
}
|
|
55
|
+
function userJson(login) {
|
|
56
|
+
return { login, id: login.length };
|
|
57
|
+
}
|
|
58
|
+
function issueJson(origin, repository, issue) {
|
|
59
|
+
const repoPath = `${repository.owner}/${repository.name}`;
|
|
60
|
+
const json = {
|
|
61
|
+
number: issue.number,
|
|
62
|
+
title: issue.title,
|
|
63
|
+
body: issue.body,
|
|
64
|
+
state: issue.state,
|
|
65
|
+
labels: issue.labels.map((label) => labelJson(repository, label)),
|
|
66
|
+
user: userJson(issue.author),
|
|
67
|
+
comments: issue.comments.length,
|
|
68
|
+
assignees: issue.assignees.map(userJson),
|
|
69
|
+
html_url: `${WEB_ORIGIN}/${repoPath}/${issue.pullRequest ? "pull" : "issues"}/${String(issue.number)}`,
|
|
70
|
+
url: `${origin}/repos/${repoPath}/issues/${String(issue.number)}`,
|
|
71
|
+
};
|
|
72
|
+
if (issue.pullRequest) {
|
|
73
|
+
json.pull_request = {
|
|
74
|
+
url: `${origin}/repos/${repoPath}/pulls/${String(issue.number)}`,
|
|
75
|
+
html_url: `${WEB_ORIGIN}/${repoPath}/pull/${String(issue.number)}`,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return json;
|
|
79
|
+
}
|
|
80
|
+
function createIssue(repository, seed) {
|
|
81
|
+
const labels = [];
|
|
82
|
+
for (const label of seed.labels ?? []) {
|
|
83
|
+
const stored = ensureLabel(repository, label);
|
|
84
|
+
if (!labels.includes(stored)) {
|
|
85
|
+
labels.push(stored);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const issue = {
|
|
89
|
+
number: repository.nextNumber,
|
|
90
|
+
title: seed.title,
|
|
91
|
+
body: seed.body ?? null,
|
|
92
|
+
state: seed.state ?? "open",
|
|
93
|
+
labels,
|
|
94
|
+
author: seed.author ?? "octocat",
|
|
95
|
+
pullRequest: seed.pullRequest ?? false,
|
|
96
|
+
comments: [],
|
|
97
|
+
assignees: [],
|
|
98
|
+
};
|
|
99
|
+
repository.nextNumber += 1;
|
|
100
|
+
repository.issues.set(issue.number, issue);
|
|
101
|
+
return issue;
|
|
102
|
+
}
|
|
103
|
+
function findIssue(context) {
|
|
104
|
+
const number = Number(context.params.number);
|
|
105
|
+
return Number.isInteger(number) ? context.repository.issues.get(number) : undefined;
|
|
106
|
+
}
|
|
107
|
+
function pageNumber(raw, fallback) {
|
|
108
|
+
const parsed = Number(raw);
|
|
109
|
+
return Number.isInteger(parsed) && parsed >= 1 ? parsed : fallback;
|
|
110
|
+
}
|
|
111
|
+
function pageLink(context, page, perPage) {
|
|
112
|
+
const url = new URL(`${context.origin}${context.request.path}`);
|
|
113
|
+
for (const [key, value] of Object.entries(context.request.query)) {
|
|
114
|
+
url.searchParams.set(key, value);
|
|
115
|
+
}
|
|
116
|
+
url.searchParams.set("page", String(page));
|
|
117
|
+
url.searchParams.set("per_page", String(perPage));
|
|
118
|
+
return url.toString();
|
|
119
|
+
}
|
|
120
|
+
function paginate(context, items, render, wrapKey) {
|
|
121
|
+
const { query } = context.request;
|
|
122
|
+
const perPage = Math.min(pageNumber(query.per_page, DEFAULT_PER_PAGE), MAX_PER_PAGE);
|
|
123
|
+
const page = pageNumber(query.page, 1);
|
|
124
|
+
const last = Math.max(1, Math.ceil(items.length / perPage));
|
|
125
|
+
const slice = items.slice((page - 1) * perPage, page * perPage).map(render);
|
|
126
|
+
const links = [];
|
|
127
|
+
if (page < last) {
|
|
128
|
+
links.push(`<${pageLink(context, page + 1, perPage)}>; rel="next"`);
|
|
129
|
+
links.push(`<${pageLink(context, last, perPage)}>; rel="last"`);
|
|
130
|
+
}
|
|
131
|
+
if (page > 1) {
|
|
132
|
+
links.push(`<${pageLink(context, page - 1, perPage)}>; rel="prev"`);
|
|
133
|
+
links.push(`<${pageLink(context, 1, perPage)}>; rel="first"`);
|
|
134
|
+
}
|
|
135
|
+
const headers = links.length > 0 ? { Link: links.join(", ") } : {};
|
|
136
|
+
return {
|
|
137
|
+
status: 200,
|
|
138
|
+
headers,
|
|
139
|
+
body: wrapKey === undefined ? slice : { total_count: items.length, [wrapKey]: slice },
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function listIssues(context) {
|
|
143
|
+
const { repository, origin } = context;
|
|
144
|
+
const { query } = context.request;
|
|
145
|
+
const state = query.state ?? "open";
|
|
146
|
+
const wanted = (query.labels ?? "")
|
|
147
|
+
.split(",")
|
|
148
|
+
.map((label) => label.trim())
|
|
149
|
+
.filter((label) => label.length > 0);
|
|
150
|
+
const matching = [...repository.issues.values()]
|
|
151
|
+
.filter((issue) => state === "all" || issue.state === state)
|
|
152
|
+
.filter((issue) => wanted.every((label) => issue.labels.some((existing) => sameLabel(existing, label))))
|
|
153
|
+
.sort((a, b) => b.number - a.number);
|
|
154
|
+
return paginate(context, matching, (issue) => issueJson(origin, repository, issue));
|
|
155
|
+
}
|
|
156
|
+
function addLabels(context) {
|
|
157
|
+
const issue = findIssue(context);
|
|
158
|
+
if (issue === undefined) {
|
|
159
|
+
return notFound();
|
|
160
|
+
}
|
|
161
|
+
const body = context.request.body;
|
|
162
|
+
const labels = body?.labels;
|
|
163
|
+
if (!Array.isArray(labels) || !labels.every((label) => typeof label === "string")) {
|
|
164
|
+
return validationFailed("labels must be an array of strings");
|
|
165
|
+
}
|
|
166
|
+
for (const label of labels) {
|
|
167
|
+
const stored = ensureLabel(context.repository, label);
|
|
168
|
+
if (!issue.labels.some((existing) => sameLabel(existing, stored))) {
|
|
169
|
+
issue.labels.push(stored);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
status: 200,
|
|
174
|
+
body: issue.labels.map((label) => labelJson(context.repository, label)),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function removeLabel(context) {
|
|
178
|
+
const issue = findIssue(context);
|
|
179
|
+
if (issue === undefined) {
|
|
180
|
+
return notFound();
|
|
181
|
+
}
|
|
182
|
+
const label = context.params.label ?? "";
|
|
183
|
+
if (!issue.labels.some((existing) => sameLabel(existing, label))) {
|
|
184
|
+
return { status: 404, body: { message: "Label does not exist" } };
|
|
185
|
+
}
|
|
186
|
+
issue.labels = issue.labels.filter((existing) => !sameLabel(existing, label));
|
|
187
|
+
return {
|
|
188
|
+
status: 200,
|
|
189
|
+
body: issue.labels.map((existing) => labelJson(context.repository, existing)),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function addComment(context) {
|
|
193
|
+
const issue = findIssue(context);
|
|
194
|
+
if (issue === undefined) {
|
|
195
|
+
return notFound();
|
|
196
|
+
}
|
|
197
|
+
const body = context.request.body;
|
|
198
|
+
if (typeof body?.body !== "string" || body.body.length === 0) {
|
|
199
|
+
return validationFailed("body is missing");
|
|
200
|
+
}
|
|
201
|
+
const comment = {
|
|
202
|
+
id: context.repository.nextId,
|
|
203
|
+
body: body.body,
|
|
204
|
+
author: "fake-github-user",
|
|
205
|
+
};
|
|
206
|
+
context.repository.nextId += 1;
|
|
207
|
+
issue.comments.push(comment);
|
|
208
|
+
const repoPath = `${context.repository.owner}/${context.repository.name}`;
|
|
209
|
+
return {
|
|
210
|
+
status: 201,
|
|
211
|
+
body: {
|
|
212
|
+
id: comment.id,
|
|
213
|
+
body: comment.body,
|
|
214
|
+
user: userJson(comment.author),
|
|
215
|
+
html_url: `${WEB_ORIGIN}/${repoPath}/issues/${String(issue.number)}#issuecomment-${String(comment.id)}`,
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
const SUCCESSFUL_CONCLUSIONS = new Set(["success", "neutral", "skipped"]);
|
|
220
|
+
function isFailedConclusion(conclusion) {
|
|
221
|
+
return conclusion !== null && !SUCCESSFUL_CONCLUSIONS.has(conclusion);
|
|
222
|
+
}
|
|
223
|
+
function takeId(repository) {
|
|
224
|
+
const id = repository.nextId;
|
|
225
|
+
repository.nextId += 1;
|
|
226
|
+
return id;
|
|
227
|
+
}
|
|
228
|
+
function fakeSha(id) {
|
|
229
|
+
return id.toString(16).padStart(40, "e");
|
|
230
|
+
}
|
|
231
|
+
function createCheckRun(repository, sha, seed, id = takeId(repository)) {
|
|
232
|
+
const conclusion = seed.conclusion === undefined ? "success" : seed.conclusion;
|
|
233
|
+
const appSlug = seed.appSlug ?? "fake-ci";
|
|
234
|
+
const suiteKey = `${sha}/${appSlug}`;
|
|
235
|
+
const checkSuiteId = seed.checkSuiteId ?? repository.checkSuites.get(suiteKey) ?? takeId(repository);
|
|
236
|
+
if (seed.checkSuiteId === undefined) {
|
|
237
|
+
repository.checkSuites.set(suiteKey, checkSuiteId);
|
|
238
|
+
}
|
|
239
|
+
const checkRun = {
|
|
240
|
+
id,
|
|
241
|
+
checkSuiteId,
|
|
242
|
+
name: seed.name,
|
|
243
|
+
status: seed.status ?? (conclusion === null ? "in_progress" : "completed"),
|
|
244
|
+
conclusion,
|
|
245
|
+
appSlug,
|
|
246
|
+
title: seed.title ?? null,
|
|
247
|
+
summary: seed.summary ?? null,
|
|
248
|
+
detailsUrl: seed.detailsUrl ?? null,
|
|
249
|
+
};
|
|
250
|
+
const existing = repository.checkRuns.get(sha) ?? [];
|
|
251
|
+
existing.push(checkRun);
|
|
252
|
+
repository.checkRuns.set(sha, existing);
|
|
253
|
+
return checkRun;
|
|
254
|
+
}
|
|
255
|
+
function createStatus(repository, sha, seed) {
|
|
256
|
+
const status = {
|
|
257
|
+
id: takeId(repository),
|
|
258
|
+
context: seed.context,
|
|
259
|
+
state: seed.state,
|
|
260
|
+
description: seed.description ?? null,
|
|
261
|
+
targetUrl: seed.targetUrl ?? null,
|
|
262
|
+
};
|
|
263
|
+
const existing = repository.statuses.get(sha) ?? [];
|
|
264
|
+
existing.push(status);
|
|
265
|
+
repository.statuses.set(sha, existing);
|
|
266
|
+
return status;
|
|
267
|
+
}
|
|
268
|
+
function createActionsJob(repository, run, jobSeed) {
|
|
269
|
+
const id = takeId(repository);
|
|
270
|
+
const checkRun = createCheckRun(repository, run.headSha, {
|
|
271
|
+
name: jobSeed.name,
|
|
272
|
+
checkSuiteId: run.checkSuiteId,
|
|
273
|
+
conclusion: jobSeed.conclusion === undefined ? "success" : jobSeed.conclusion,
|
|
274
|
+
appSlug: "github-actions",
|
|
275
|
+
...(jobSeed.status === undefined ? {} : { status: jobSeed.status }),
|
|
276
|
+
}, id);
|
|
277
|
+
const job = { id, name: jobSeed.name, checkRun, log: jobSeed.log ?? "" };
|
|
278
|
+
run.jobs.push(job);
|
|
279
|
+
return job;
|
|
280
|
+
}
|
|
281
|
+
function createActionsRun(repository, seed) {
|
|
282
|
+
const run = {
|
|
283
|
+
id: takeId(repository),
|
|
284
|
+
checkSuiteId: seed.checkSuiteId ?? takeId(repository),
|
|
285
|
+
name: seed.name ?? "ci",
|
|
286
|
+
headSha: seed.headSha,
|
|
287
|
+
status: seed.status ?? "completed",
|
|
288
|
+
conclusion: null,
|
|
289
|
+
jobs: [],
|
|
290
|
+
};
|
|
291
|
+
for (const jobSeed of seed.jobs ?? [{ name: "build" }]) {
|
|
292
|
+
createActionsJob(repository, run, jobSeed);
|
|
293
|
+
}
|
|
294
|
+
if (seed.conclusion !== undefined) {
|
|
295
|
+
run.conclusion = seed.conclusion;
|
|
296
|
+
}
|
|
297
|
+
else if (run.status === "completed") {
|
|
298
|
+
run.conclusion = run.jobs.some((job) => isFailedConclusion(job.checkRun.conclusion))
|
|
299
|
+
? "failure"
|
|
300
|
+
: "success";
|
|
301
|
+
}
|
|
302
|
+
repository.actionsRuns.push(run);
|
|
303
|
+
return run;
|
|
304
|
+
}
|
|
305
|
+
function pullJson(origin, repository, issue, pr) {
|
|
306
|
+
const repoPath = `${repository.owner}/${repository.name}`;
|
|
307
|
+
return {
|
|
308
|
+
number: pr.number,
|
|
309
|
+
title: issue.title,
|
|
310
|
+
body: issue.body,
|
|
311
|
+
state: issue.state,
|
|
312
|
+
draft: pr.mergeableState === "draft",
|
|
313
|
+
merged: pr.merged,
|
|
314
|
+
merge_commit_sha: pr.mergeCommitSha,
|
|
315
|
+
mergeable: pr.mergeable,
|
|
316
|
+
mergeable_state: pr.mergeableState,
|
|
317
|
+
user: userJson(issue.author),
|
|
318
|
+
assignees: issue.assignees.map(userJson),
|
|
319
|
+
html_url: `${WEB_ORIGIN}/${repoPath}/pull/${String(pr.number)}`,
|
|
320
|
+
url: `${origin}/repos/${repoPath}/pulls/${String(pr.number)}`,
|
|
321
|
+
head: {
|
|
322
|
+
ref: pr.headRef,
|
|
323
|
+
sha: pr.headSha,
|
|
324
|
+
label: `${repository.owner}:${pr.headRef}`,
|
|
325
|
+
repo: pr.headRepository === null ? null : { full_name: pr.headRepository },
|
|
326
|
+
},
|
|
327
|
+
base: { ref: pr.baseRef, repo: { full_name: repoPath } },
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
function findPull(context) {
|
|
331
|
+
const number = Number(context.params.number);
|
|
332
|
+
const pr = Number.isInteger(number) ? context.repository.pullRequests.get(number) : undefined;
|
|
333
|
+
const issue = pr === undefined ? undefined : context.repository.issues.get(pr.number);
|
|
334
|
+
return pr === undefined || issue === undefined ? undefined : { issue, pr };
|
|
335
|
+
}
|
|
336
|
+
function bareBranch(head) {
|
|
337
|
+
const colon = head.indexOf(":");
|
|
338
|
+
return colon === -1 ? head : head.slice(colon + 1);
|
|
339
|
+
}
|
|
340
|
+
function invalidField(field) {
|
|
341
|
+
return {
|
|
342
|
+
status: 422,
|
|
343
|
+
body: {
|
|
344
|
+
message: "Validation Failed",
|
|
345
|
+
errors: [{ resource: "PullRequest", field, code: "invalid" }],
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
function headMatches(pr, head) {
|
|
350
|
+
const colon = head.indexOf(":");
|
|
351
|
+
return (pr.headRepository?.split("/")[0] === head.slice(0, colon) &&
|
|
352
|
+
pr.headRef === head.slice(colon + 1));
|
|
353
|
+
}
|
|
354
|
+
function listPulls(context) {
|
|
355
|
+
const { repository, origin } = context;
|
|
356
|
+
const { query } = context.request;
|
|
357
|
+
if (query.head !== undefined && !/^[^:]+:.+$/.test(query.head)) {
|
|
358
|
+
return invalidField("head");
|
|
359
|
+
}
|
|
360
|
+
const state = query.state ?? "open";
|
|
361
|
+
const matching = [...repository.pullRequests.values()]
|
|
362
|
+
.filter((pr) => {
|
|
363
|
+
const issue = repository.issues.get(pr.number);
|
|
364
|
+
return issue !== undefined && (state === "all" || issue.state === state);
|
|
365
|
+
})
|
|
366
|
+
.filter((pr) => query.head === undefined || headMatches(pr, query.head))
|
|
367
|
+
.filter((pr) => query.base === undefined || pr.baseRef === query.base)
|
|
368
|
+
.sort((a, b) => b.number - a.number);
|
|
369
|
+
return paginate(context, matching, (pr) => {
|
|
370
|
+
const issue = repository.issues.get(pr.number);
|
|
371
|
+
return issue === undefined ? undefined : pullJson(origin, repository, issue, pr);
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
function createPull(context) {
|
|
375
|
+
const { repository, origin } = context;
|
|
376
|
+
const body = context.request.body;
|
|
377
|
+
if (typeof body?.title !== "string" ||
|
|
378
|
+
typeof body.head !== "string" ||
|
|
379
|
+
typeof body.base !== "string") {
|
|
380
|
+
return validationFailed("title, head and base are required");
|
|
381
|
+
}
|
|
382
|
+
const headRef = bareBranch(body.head);
|
|
383
|
+
if (!repository.branches.has(headRef)) {
|
|
384
|
+
return invalidField("head");
|
|
385
|
+
}
|
|
386
|
+
if (body.base !== repository.defaultBranch && !repository.branches.has(body.base)) {
|
|
387
|
+
return invalidField("base");
|
|
388
|
+
}
|
|
389
|
+
const duplicate = [...repository.pullRequests.values()].some((pr) => pr.headRef === headRef &&
|
|
390
|
+
pr.baseRef === body.base &&
|
|
391
|
+
repository.issues.get(pr.number)?.state === "open");
|
|
392
|
+
if (duplicate) {
|
|
393
|
+
return {
|
|
394
|
+
status: 422,
|
|
395
|
+
body: {
|
|
396
|
+
message: "Validation Failed",
|
|
397
|
+
errors: [
|
|
398
|
+
{
|
|
399
|
+
resource: "PullRequest",
|
|
400
|
+
code: "custom",
|
|
401
|
+
message: `A pull request already exists for ${repository.owner}:${headRef}.`,
|
|
402
|
+
},
|
|
403
|
+
],
|
|
404
|
+
},
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
const created = addPullRequestTo(repository, {
|
|
408
|
+
title: body.title,
|
|
409
|
+
body: typeof body.body === "string" ? body.body : null,
|
|
410
|
+
headRef,
|
|
411
|
+
baseRef: body.base,
|
|
412
|
+
author: context.state.login,
|
|
413
|
+
});
|
|
414
|
+
return { status: 201, body: pullJson(origin, repository, created.issue, created.pullRequest) };
|
|
415
|
+
}
|
|
416
|
+
function addPullRequestTo(repository, seed) {
|
|
417
|
+
const issue = createIssue(repository, {
|
|
418
|
+
title: seed.title,
|
|
419
|
+
body: seed.body ?? null,
|
|
420
|
+
author: seed.author ?? "octocat",
|
|
421
|
+
pullRequest: true,
|
|
422
|
+
});
|
|
423
|
+
const headSha = seed.headSha ?? repository.branches.get(seed.headRef) ?? fakeSha(takeId(repository));
|
|
424
|
+
repository.branches.set(seed.headRef, headSha);
|
|
425
|
+
const pullRequest = {
|
|
426
|
+
number: issue.number,
|
|
427
|
+
headRef: seed.headRef,
|
|
428
|
+
headSha,
|
|
429
|
+
baseRef: seed.baseRef ?? repository.defaultBranch,
|
|
430
|
+
mergeable: seed.mergeable === undefined ? true : seed.mergeable,
|
|
431
|
+
mergeableState: seed.mergeableState ?? "clean",
|
|
432
|
+
merged: false,
|
|
433
|
+
mergeCommitSha: null,
|
|
434
|
+
headRepository: seed.headRepository === undefined
|
|
435
|
+
? `${repository.owner}/${repository.name}`
|
|
436
|
+
: seed.headRepository,
|
|
437
|
+
mergeRequests: [],
|
|
438
|
+
};
|
|
439
|
+
repository.pullRequests.set(pullRequest.number, pullRequest);
|
|
440
|
+
return { issue, pullRequest };
|
|
441
|
+
}
|
|
442
|
+
const MERGE_METHOD_LABELS = {
|
|
443
|
+
merge: "Merge commits",
|
|
444
|
+
squash: "Squash merges",
|
|
445
|
+
rebase: "Rebase merges",
|
|
446
|
+
};
|
|
447
|
+
const MERGE_REFUSALS_BY_STATE = {
|
|
448
|
+
behind: "Head branch is out of date. Review and try the merge again.",
|
|
449
|
+
blocked: "At least 1 approving review is required by reviewers with write access.",
|
|
450
|
+
draft: "Draft pull requests cannot be merged.",
|
|
451
|
+
dirty: "Pull Request is not mergeable",
|
|
452
|
+
};
|
|
453
|
+
function mergeRefusal(pr, issue) {
|
|
454
|
+
if (pr.merged || issue.state === "closed" || pr.mergeable === false) {
|
|
455
|
+
return "Pull Request is not mergeable";
|
|
456
|
+
}
|
|
457
|
+
return MERGE_REFUSALS_BY_STATE[pr.mergeableState];
|
|
458
|
+
}
|
|
459
|
+
function mergePull(context) {
|
|
460
|
+
const found = findPull(context);
|
|
461
|
+
if (found === undefined) {
|
|
462
|
+
return notFound();
|
|
463
|
+
}
|
|
464
|
+
const { issue, pr } = found;
|
|
465
|
+
const body = context.request.body;
|
|
466
|
+
const sha = typeof body?.sha === "string" ? body.sha : undefined;
|
|
467
|
+
const mergeMethod = typeof body?.merge_method === "string" ? body.merge_method : undefined;
|
|
468
|
+
pr.mergeRequests.push({ sha, mergeMethod });
|
|
469
|
+
if (mergeMethod !== undefined && !context.repository.allowedMergeMethods.includes(mergeMethod)) {
|
|
470
|
+
return {
|
|
471
|
+
status: 405,
|
|
472
|
+
body: {
|
|
473
|
+
message: `${MERGE_METHOD_LABELS[mergeMethod] ?? mergeMethod} are not allowed on this repository.`,
|
|
474
|
+
},
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
if (sha !== undefined && sha !== pr.headSha) {
|
|
478
|
+
return {
|
|
479
|
+
status: 409,
|
|
480
|
+
body: { message: "Head branch was modified. Review and try the merge again." },
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
const refusal = mergeRefusal(pr, issue);
|
|
484
|
+
if (refusal !== undefined) {
|
|
485
|
+
return { status: 405, body: { message: refusal } };
|
|
486
|
+
}
|
|
487
|
+
pr.merged = true;
|
|
488
|
+
pr.mergeCommitSha = fakeSha(takeId(context.repository));
|
|
489
|
+
issue.state = "closed";
|
|
490
|
+
return {
|
|
491
|
+
status: 200,
|
|
492
|
+
body: { sha: pr.mergeCommitSha, merged: true, message: "Pull Request successfully merged" },
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
function assignIssue(context) {
|
|
496
|
+
const issue = findIssue(context);
|
|
497
|
+
if (issue === undefined) {
|
|
498
|
+
return notFound();
|
|
499
|
+
}
|
|
500
|
+
const body = context.request.body;
|
|
501
|
+
const assignees = body?.assignees;
|
|
502
|
+
if (!Array.isArray(assignees) || !assignees.every((login) => typeof login === "string")) {
|
|
503
|
+
return validationFailed("assignees must be an array of strings");
|
|
504
|
+
}
|
|
505
|
+
for (const login of assignees) {
|
|
506
|
+
if (!issue.assignees.includes(login)) {
|
|
507
|
+
issue.assignees.push(login);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return { status: 201, body: issueJson(context.origin, context.repository, issue) };
|
|
511
|
+
}
|
|
512
|
+
function checkRunJson(repository, sha, checkRun) {
|
|
513
|
+
return {
|
|
514
|
+
id: checkRun.id,
|
|
515
|
+
name: checkRun.name,
|
|
516
|
+
head_sha: sha,
|
|
517
|
+
check_suite: { id: checkRun.checkSuiteId },
|
|
518
|
+
status: checkRun.status,
|
|
519
|
+
conclusion: checkRun.conclusion,
|
|
520
|
+
html_url: `${WEB_ORIGIN}/${repository.owner}/${repository.name}/runs/${String(checkRun.id)}`,
|
|
521
|
+
details_url: checkRun.detailsUrl,
|
|
522
|
+
app: { slug: checkRun.appSlug, name: checkRun.appSlug },
|
|
523
|
+
output: { title: checkRun.title, summary: checkRun.summary },
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
function listCheckRuns(context) {
|
|
527
|
+
const { repository } = context;
|
|
528
|
+
const sha = context.params.sha ?? "";
|
|
529
|
+
if (context.request.query.page === undefined || context.request.query.page === "1") {
|
|
530
|
+
const poll = repository.checkPolls.get(sha) ?? 0;
|
|
531
|
+
repository.checkPolls.set(sha, poll + 1);
|
|
532
|
+
repository.checkPollHooks.get(sha)?.(poll);
|
|
533
|
+
}
|
|
534
|
+
const all = repository.checkRuns.get(sha) ?? [];
|
|
535
|
+
const latestIds = new Map();
|
|
536
|
+
for (const checkRun of all) {
|
|
537
|
+
const key = `${String(checkRun.checkSuiteId)}/${checkRun.name}`;
|
|
538
|
+
latestIds.set(key, Math.max(latestIds.get(key) ?? 0, checkRun.id));
|
|
539
|
+
}
|
|
540
|
+
const listed = context.request.query.filter === "all"
|
|
541
|
+
? all
|
|
542
|
+
: all.filter((checkRun) => latestIds.get(`${String(checkRun.checkSuiteId)}/${checkRun.name}`) === checkRun.id);
|
|
543
|
+
return paginate(context, listed, (checkRun) => checkRunJson(repository, sha, checkRun), "check_runs");
|
|
544
|
+
}
|
|
545
|
+
function listStatuses(context) {
|
|
546
|
+
const statuses = [...(context.repository.statuses.get(context.params.sha ?? "") ?? [])].reverse();
|
|
547
|
+
return paginate(context, statuses, (status) => ({
|
|
548
|
+
id: status.id,
|
|
549
|
+
context: status.context,
|
|
550
|
+
state: status.state,
|
|
551
|
+
description: status.description,
|
|
552
|
+
target_url: status.targetUrl,
|
|
553
|
+
}));
|
|
554
|
+
}
|
|
555
|
+
function listActionsRuns(context) {
|
|
556
|
+
const { repository } = context;
|
|
557
|
+
const headSha = context.request.query.head_sha;
|
|
558
|
+
const runs = repository.actionsRuns
|
|
559
|
+
.filter((run) => headSha === undefined || run.headSha === headSha)
|
|
560
|
+
.sort((a, b) => b.id - a.id);
|
|
561
|
+
return paginate(context, runs, (run) => ({
|
|
562
|
+
id: run.id,
|
|
563
|
+
check_suite_id: run.checkSuiteId,
|
|
564
|
+
name: run.name,
|
|
565
|
+
head_sha: run.headSha,
|
|
566
|
+
status: run.status,
|
|
567
|
+
conclusion: run.conclusion,
|
|
568
|
+
html_url: `${WEB_ORIGIN}/${repository.owner}/${repository.name}/actions/runs/${String(run.id)}`,
|
|
569
|
+
}), "workflow_runs");
|
|
570
|
+
}
|
|
571
|
+
function jobLogRedirect(context) {
|
|
572
|
+
const jobId = Number(context.params.id);
|
|
573
|
+
const known = context.repository.actionsRuns.some((run) => run.jobs.some((job) => job.id === jobId));
|
|
574
|
+
if (!known) {
|
|
575
|
+
return notFound();
|
|
576
|
+
}
|
|
577
|
+
const { owner, name } = context.repository;
|
|
578
|
+
return {
|
|
579
|
+
status: 302,
|
|
580
|
+
headers: { Location: `${context.origin}/_logs/${owner}/${name}/${String(jobId)}` },
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
const NOT_RERUNNABLE_CONCLUSIONS = new Set(["startup_failure", "action_required"]);
|
|
584
|
+
function rerun(failedJobsOnly) {
|
|
585
|
+
return (context) => {
|
|
586
|
+
const runId = Number(context.params.id);
|
|
587
|
+
const run = context.repository.actionsRuns.find((candidate) => candidate.id === runId);
|
|
588
|
+
if (run === undefined) {
|
|
589
|
+
return notFound();
|
|
590
|
+
}
|
|
591
|
+
if (run.status !== "completed") {
|
|
592
|
+
return { status: 403, body: { message: "Cannot rerun a workflow that is not completed" } };
|
|
593
|
+
}
|
|
594
|
+
if (run.conclusion !== null && NOT_RERUNNABLE_CONCLUSIONS.has(run.conclusion)) {
|
|
595
|
+
return {
|
|
596
|
+
status: 422,
|
|
597
|
+
body: { message: `Cannot re-run a workflow run that concluded ${run.conclusion}` },
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
context.repository.reruns.push({ runId, failedJobsOnly });
|
|
601
|
+
run.jobs.forEach((job, index) => {
|
|
602
|
+
if (failedJobsOnly && !isFailedConclusion(job.checkRun.conclusion)) {
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
const checkRun = createCheckRun(context.repository, run.headSha, {
|
|
606
|
+
name: job.name,
|
|
607
|
+
checkSuiteId: run.checkSuiteId,
|
|
608
|
+
status: "queued",
|
|
609
|
+
conclusion: null,
|
|
610
|
+
appSlug: "github-actions",
|
|
611
|
+
});
|
|
612
|
+
run.jobs[index] = { id: checkRun.id, name: job.name, checkRun, log: "" };
|
|
613
|
+
});
|
|
614
|
+
run.status = "queued";
|
|
615
|
+
run.conclusion = null;
|
|
616
|
+
return { status: 201, body: {} };
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
function deleteRef(context) {
|
|
620
|
+
const ref = context.params.ref ?? "";
|
|
621
|
+
const branch = ref.startsWith("heads/") ? ref.slice("heads/".length) : undefined;
|
|
622
|
+
if (branch === undefined || !context.repository.branches.delete(branch)) {
|
|
623
|
+
return { status: 422, body: { message: "Reference does not exist" } };
|
|
624
|
+
}
|
|
625
|
+
return { status: 204 };
|
|
626
|
+
}
|
|
627
|
+
const ROUTES = [
|
|
628
|
+
route("GET", "/repos/:owner/:repo", (context) => ({
|
|
629
|
+
status: 200,
|
|
630
|
+
body: {
|
|
631
|
+
name: context.repository.name,
|
|
632
|
+
full_name: `${context.repository.owner}/${context.repository.name}`,
|
|
633
|
+
default_branch: context.repository.defaultBranch,
|
|
634
|
+
},
|
|
635
|
+
})),
|
|
636
|
+
route("GET", "/repos/:owner/:repo/pulls", listPulls),
|
|
637
|
+
route("POST", "/repos/:owner/:repo/pulls", createPull),
|
|
638
|
+
route("GET", "/repos/:owner/:repo/pulls/:number", (context) => {
|
|
639
|
+
const found = findPull(context);
|
|
640
|
+
return found === undefined
|
|
641
|
+
? notFound()
|
|
642
|
+
: { status: 200, body: pullJson(context.origin, context.repository, found.issue, found.pr) };
|
|
643
|
+
}),
|
|
644
|
+
route("PUT", "/repos/:owner/:repo/pulls/:number/merge", mergePull),
|
|
645
|
+
route("POST", "/repos/:owner/:repo/issues/:number/assignees", assignIssue),
|
|
646
|
+
route("GET", "/repos/:owner/:repo/commits/:sha/check-runs", listCheckRuns),
|
|
647
|
+
route("GET", "/repos/:owner/:repo/commits/:sha/statuses", listStatuses),
|
|
648
|
+
route("GET", "/repos/:owner/:repo/actions/runs", listActionsRuns),
|
|
649
|
+
route("GET", "/repos/:owner/:repo/actions/jobs/:id/logs", jobLogRedirect),
|
|
650
|
+
route("POST", "/repos/:owner/:repo/actions/runs/:id/rerun", rerun(false)),
|
|
651
|
+
route("POST", "/repos/:owner/:repo/actions/runs/:id/rerun-failed-jobs", rerun(true)),
|
|
652
|
+
route("DELETE", "/repos/:owner/:repo/git/refs/*ref", deleteRef),
|
|
653
|
+
route("GET", "/repos/:owner/:repo/issues", listIssues),
|
|
654
|
+
route("GET", "/repos/:owner/:repo/issues/:number", (context) => {
|
|
655
|
+
const issue = findIssue(context);
|
|
656
|
+
return issue === undefined
|
|
657
|
+
? notFound()
|
|
658
|
+
: { status: 200, body: issueJson(context.origin, context.repository, issue) };
|
|
659
|
+
}),
|
|
660
|
+
route("POST", "/repos/:owner/:repo/issues/:number/labels", addLabels),
|
|
661
|
+
route("DELETE", "/repos/:owner/:repo/issues/:number/labels/:label", removeLabel),
|
|
662
|
+
route("POST", "/repos/:owner/:repo/issues/:number/comments", addComment),
|
|
663
|
+
];
|
|
664
|
+
function route(method, pattern, handler) {
|
|
665
|
+
return { method, segments: pattern.split("/").slice(1), handler };
|
|
666
|
+
}
|
|
667
|
+
function matchRoute(routes, method, path) {
|
|
668
|
+
const actual = path.split("/").slice(1);
|
|
669
|
+
for (const candidate of routes) {
|
|
670
|
+
if (candidate.method !== method) {
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
const last = candidate.segments.at(-1) ?? "";
|
|
674
|
+
const wildcard = last.startsWith("*");
|
|
675
|
+
const fixed = wildcard ? candidate.segments.length - 1 : candidate.segments.length;
|
|
676
|
+
if (wildcard ? actual.length <= fixed : actual.length !== fixed) {
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
const params = {};
|
|
680
|
+
const matches = candidate.segments.slice(0, fixed).every((segment, index) => {
|
|
681
|
+
const value = actual[index];
|
|
682
|
+
if (value === undefined) {
|
|
683
|
+
return false;
|
|
684
|
+
}
|
|
685
|
+
if (segment.startsWith(":")) {
|
|
686
|
+
params[segment.slice(1)] = decodeURIComponent(value);
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
689
|
+
return segment === value;
|
|
690
|
+
});
|
|
691
|
+
if (matches) {
|
|
692
|
+
if (wildcard) {
|
|
693
|
+
params[last.slice(1)] = actual.slice(fixed).map(decodeURIComponent).join("/");
|
|
694
|
+
}
|
|
695
|
+
return { route: candidate, params };
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
return undefined;
|
|
699
|
+
}
|
|
700
|
+
export async function startFakeGitHub(options = {}) {
|
|
701
|
+
const token = options.token ?? DEFAULT_TOKEN;
|
|
702
|
+
const overrides = new Map();
|
|
703
|
+
const requests = [];
|
|
704
|
+
const sockets = new Set();
|
|
705
|
+
const state = {
|
|
706
|
+
repositories: new Map(),
|
|
707
|
+
login: options.login ?? "fake-github-user",
|
|
708
|
+
repositoryFor(owner, name) {
|
|
709
|
+
const key = `${owner}/${name}`;
|
|
710
|
+
let repository = state.repositories.get(key);
|
|
711
|
+
if (repository === undefined) {
|
|
712
|
+
repository = newRepository(owner, name);
|
|
713
|
+
state.repositories.set(key, repository);
|
|
714
|
+
}
|
|
715
|
+
return repository;
|
|
716
|
+
},
|
|
717
|
+
};
|
|
718
|
+
let closed = false;
|
|
719
|
+
let origin = "";
|
|
720
|
+
function serveJobLog(path) {
|
|
721
|
+
const [, , owner, repo, id] = path.split("/");
|
|
722
|
+
const job = state.repositories
|
|
723
|
+
.get(`${owner ?? ""}/${repo ?? ""}`)
|
|
724
|
+
?.actionsRuns.flatMap((run) => run.jobs)
|
|
725
|
+
.find((candidate) => candidate.id === Number(id));
|
|
726
|
+
return job === undefined
|
|
727
|
+
? notFound()
|
|
728
|
+
: { status: 200, headers: { "Content-Type": "text/plain; charset=utf-8" }, body: job.log };
|
|
729
|
+
}
|
|
730
|
+
async function respondTo(request) {
|
|
731
|
+
if (request.method === "GET" && request.path.startsWith("/_logs/")) {
|
|
732
|
+
return serveJobLog(request.path);
|
|
733
|
+
}
|
|
734
|
+
if (request.headers.authorization !== `Bearer ${token}`) {
|
|
735
|
+
return {
|
|
736
|
+
status: 401,
|
|
737
|
+
body: {
|
|
738
|
+
message: "Bad credentials",
|
|
739
|
+
documentation_url: "https://docs.github.com/rest",
|
|
740
|
+
},
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
const overrideKey = `${request.method.toUpperCase()} ${request.path}`;
|
|
744
|
+
const override = overrides.get(overrideKey);
|
|
745
|
+
if (override !== undefined) {
|
|
746
|
+
const callIndex = override.calls;
|
|
747
|
+
override.calls += 1;
|
|
748
|
+
return resolveOverride(override.handler, request, callIndex);
|
|
749
|
+
}
|
|
750
|
+
if (request.method === "GET" && request.path === "/user") {
|
|
751
|
+
return { status: 200, body: { login: state.login, id: state.login.length } };
|
|
752
|
+
}
|
|
753
|
+
const matched = matchRoute(ROUTES, request.method, request.path);
|
|
754
|
+
if (matched === undefined) {
|
|
755
|
+
return {
|
|
756
|
+
status: 501,
|
|
757
|
+
body: { message: `fake github: no route for ${overrideKey}` },
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
const { owner, repo } = matched.params;
|
|
761
|
+
const repository = owner !== undefined && repo !== undefined
|
|
762
|
+
? state.repositories.get(`${owner}/${repo}`)
|
|
763
|
+
: undefined;
|
|
764
|
+
if (repository === undefined) {
|
|
765
|
+
return notFound();
|
|
766
|
+
}
|
|
767
|
+
return matched.route.handler({
|
|
768
|
+
request,
|
|
769
|
+
params: matched.params,
|
|
770
|
+
origin,
|
|
771
|
+
repository,
|
|
772
|
+
state,
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
const server = createServer((req, res) => {
|
|
776
|
+
void (async () => {
|
|
777
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
778
|
+
const query = {};
|
|
779
|
+
for (const [key, value] of url.searchParams.entries()) {
|
|
780
|
+
query[key] = value;
|
|
781
|
+
}
|
|
782
|
+
const body = parseBody(await readRequestBody(req), req.headers["content-type"]);
|
|
783
|
+
const request = {
|
|
784
|
+
method: req.method ?? "GET",
|
|
785
|
+
path: url.pathname,
|
|
786
|
+
query,
|
|
787
|
+
headers: req.headers,
|
|
788
|
+
body,
|
|
789
|
+
};
|
|
790
|
+
requests.push(request);
|
|
791
|
+
const response = await respondTo(request);
|
|
792
|
+
if (response.delayMs !== undefined && response.delayMs > 0) {
|
|
793
|
+
await new Promise((resolve) => setTimeout(resolve, response.delayMs));
|
|
794
|
+
}
|
|
795
|
+
if (res.destroyed || res.writableEnded) {
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
res.writeHead(response.status ?? 200, {
|
|
799
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
800
|
+
...response.headers,
|
|
801
|
+
});
|
|
802
|
+
res.end(response.body === undefined
|
|
803
|
+
? ""
|
|
804
|
+
: typeof response.body === "string" && response.headers?.["Content-Type"] !== undefined
|
|
805
|
+
? response.body
|
|
806
|
+
: JSON.stringify(response.body));
|
|
807
|
+
})().catch((err) => {
|
|
808
|
+
if (res.destroyed || res.writableEnded) {
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
812
|
+
res.end(JSON.stringify({ message: `fake github: handler error: ${String(err)}` }));
|
|
813
|
+
});
|
|
814
|
+
});
|
|
815
|
+
server.on("connection", (socket) => {
|
|
816
|
+
sockets.add(socket);
|
|
817
|
+
socket.on("close", () => sockets.delete(socket));
|
|
818
|
+
});
|
|
819
|
+
await new Promise((resolve) => {
|
|
820
|
+
server.listen(0, "127.0.0.1", resolve);
|
|
821
|
+
});
|
|
822
|
+
const address = server.address();
|
|
823
|
+
if (address === null || typeof address === "string") {
|
|
824
|
+
throw new Error("fake github: server did not bind to a network address");
|
|
825
|
+
}
|
|
826
|
+
origin = `http://127.0.0.1:${String(address.port)}`;
|
|
827
|
+
return {
|
|
828
|
+
origin,
|
|
829
|
+
apiUrl: origin,
|
|
830
|
+
token,
|
|
831
|
+
requests,
|
|
832
|
+
repositoryUrl: (owner, name) => `${WEB_ORIGIN}/${owner}/${name}`,
|
|
833
|
+
issueUrl: (owner, name, number) => `${WEB_ORIGIN}/${owner}/${name}/issues/${String(number)}`,
|
|
834
|
+
repository: (owner, name) => state.repositoryFor(owner, name),
|
|
835
|
+
addIssue(owner, name, seed) {
|
|
836
|
+
const repository = state.repositoryFor(owner, name);
|
|
837
|
+
const labels = [...(seed.labels ?? [])];
|
|
838
|
+
for (const label of labels) {
|
|
839
|
+
ensureLabel(repository, label);
|
|
840
|
+
}
|
|
841
|
+
const issue = {
|
|
842
|
+
number: repository.nextNumber,
|
|
843
|
+
title: seed.title,
|
|
844
|
+
body: seed.body ?? null,
|
|
845
|
+
state: seed.state ?? "open",
|
|
846
|
+
labels,
|
|
847
|
+
author: seed.author ?? "octocat",
|
|
848
|
+
pullRequest: seed.pullRequest ?? false,
|
|
849
|
+
comments: [],
|
|
850
|
+
assignees: [],
|
|
851
|
+
};
|
|
852
|
+
repository.nextNumber += 1;
|
|
853
|
+
repository.issues.set(issue.number, issue);
|
|
854
|
+
return issue;
|
|
855
|
+
},
|
|
856
|
+
addBranch(owner, name, branch, sha) {
|
|
857
|
+
state.repositoryFor(owner, name).branches.set(branch, sha);
|
|
858
|
+
},
|
|
859
|
+
addPullRequest: (owner, name, seed) => addPullRequestTo(state.repositoryFor(owner, name), seed),
|
|
860
|
+
pullRequest(owner, name, number) {
|
|
861
|
+
const pr = state.repositories.get(`${owner}/${name}`)?.pullRequests.get(number);
|
|
862
|
+
if (pr === undefined) {
|
|
863
|
+
throw new Error(`fake github: no pull request ${owner}/${name}#${String(number)}`);
|
|
864
|
+
}
|
|
865
|
+
return pr;
|
|
866
|
+
},
|
|
867
|
+
pullRequestUrl: (owner, name, number) => `${WEB_ORIGIN}/${owner}/${name}/pull/${String(number)}`,
|
|
868
|
+
addCheckRun: (owner, name, sha, seed) => createCheckRun(state.repositoryFor(owner, name), sha, seed),
|
|
869
|
+
addStatus: (owner, name, sha, seed) => createStatus(state.repositoryFor(owner, name), sha, seed),
|
|
870
|
+
onCheckPoll(owner, name, sha, hook) {
|
|
871
|
+
state.repositoryFor(owner, name).checkPollHooks.set(sha, hook);
|
|
872
|
+
},
|
|
873
|
+
addActionsRun: (owner, name, seed) => createActionsRun(state.repositoryFor(owner, name), seed),
|
|
874
|
+
addActionsJob: (owner, name, run, seed) => createActionsJob(state.repositoryFor(owner, name), run, seed),
|
|
875
|
+
issue(owner, name, number) {
|
|
876
|
+
const issue = state.repositories.get(`${owner}/${name}`)?.issues.get(number);
|
|
877
|
+
if (issue === undefined) {
|
|
878
|
+
throw new Error(`fake github: no issue ${owner}/${name}#${String(number)}`);
|
|
879
|
+
}
|
|
880
|
+
return issue;
|
|
881
|
+
},
|
|
882
|
+
respond(method, path, handler) {
|
|
883
|
+
overrides.set(`${method.toUpperCase()} ${path}`, { handler, calls: 0 });
|
|
884
|
+
},
|
|
885
|
+
async close() {
|
|
886
|
+
if (closed) {
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
closed = true;
|
|
890
|
+
for (const socket of sockets) {
|
|
891
|
+
socket.destroy();
|
|
892
|
+
}
|
|
893
|
+
await new Promise((resolve, reject) => {
|
|
894
|
+
server.close((err) => {
|
|
895
|
+
if (err) {
|
|
896
|
+
reject(err);
|
|
897
|
+
}
|
|
898
|
+
else {
|
|
899
|
+
resolve();
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
});
|
|
903
|
+
},
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
function resolveOverride(handler, request, callIndex) {
|
|
907
|
+
if (typeof handler === "function") {
|
|
908
|
+
return handler(request);
|
|
909
|
+
}
|
|
910
|
+
if (Array.isArray(handler)) {
|
|
911
|
+
const response = handler[Math.min(callIndex, handler.length - 1)];
|
|
912
|
+
if (response === undefined) {
|
|
913
|
+
throw new Error("fake github: empty response list registered for a route");
|
|
914
|
+
}
|
|
915
|
+
return response;
|
|
916
|
+
}
|
|
917
|
+
return handler;
|
|
918
|
+
}
|
|
919
|
+
//# sourceMappingURL=github.js.map
|