@niwa-labs/forge 0.0.0-stage → 0.0.0
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 +21 -0
- package/README.md +52 -2
- package/package.json +30 -4
- package/src/actions.ts +389 -0
- package/src/cockpit/actions.ts +94 -0
- package/src/cockpit/index.tsx +19 -0
- package/src/cockpit/page.tsx +158 -0
- package/src/cockpit/pulls.ts +103 -0
- package/src/flow.ts +232 -0
- package/src/gitlab.ts +263 -0
- package/src/index.ts +765 -0
- package/src/merge.ts +309 -0
- package/src/normalize.ts +186 -0
- package/src/observe.ts +464 -0
- package/src/prs.ts +122 -0
- package/src/queries.ts +152 -0
- package/src/record-types.ts +353 -0
- package/src/records.ts +8 -0
- package/src/secrets.ts +40 -0
- package/src/servers.ts +121 -0
- package/src/targets.ts +82 -0
- package/src/workflows/push-reaction.ts +59 -0
- package/test/actions.test.ts +235 -0
- package/test/cockpit-actions.test.ts +55 -0
- package/test/cockpit-pulls.test.ts +58 -0
- package/test/fake-gitlab.ts +452 -0
- package/test/flow.test.ts +239 -0
- package/test/gitlab.test.ts +91 -0
- package/test/merge.test.ts +251 -0
- package/test/observe.test.ts +454 -0
- package/test/prs.test.ts +125 -0
- package/test/push-reaction.test.ts +92 -0
- package/test/queries.test.ts +144 -0
- package/test/script-surface.test.ts +185 -0
- package/test/support.ts +109 -0
- package/test/targets.test.ts +42 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
// A GitLab server in memory: the part of the REST API the forge module speaks, over a model a test
|
|
2
|
+
// changes by hand. Every request is kept, with its headers, so a test sees what was asked and with
|
|
3
|
+
// which token. A read answers an ETag and a `304` to a matching `If-None-Match`; a test may queue an
|
|
4
|
+
// answer of its own (a 429, a 409) for the next request that matches a path.
|
|
5
|
+
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
|
|
8
|
+
export interface FakeMr {
|
|
9
|
+
iid: number;
|
|
10
|
+
title: string;
|
|
11
|
+
description: string;
|
|
12
|
+
state: "opened" | "closed" | "merged" | "locked";
|
|
13
|
+
source_branch: string;
|
|
14
|
+
target_branch: string;
|
|
15
|
+
sha: string;
|
|
16
|
+
draft: boolean;
|
|
17
|
+
author: string;
|
|
18
|
+
merge_commit_sha: string | null;
|
|
19
|
+
squash_commit_sha: string | null;
|
|
20
|
+
merge_when_pipeline_succeeds: boolean;
|
|
21
|
+
in_train: boolean;
|
|
22
|
+
approvals: string[];
|
|
23
|
+
updated_at: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface FakePipeline {
|
|
27
|
+
id: number;
|
|
28
|
+
sha: string;
|
|
29
|
+
ref: string;
|
|
30
|
+
status: string;
|
|
31
|
+
source: string;
|
|
32
|
+
tag: boolean;
|
|
33
|
+
updated_at: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface FakeJob {
|
|
37
|
+
id: number;
|
|
38
|
+
pipeline: number;
|
|
39
|
+
name: string;
|
|
40
|
+
stage: string;
|
|
41
|
+
status: string;
|
|
42
|
+
ref: string;
|
|
43
|
+
/** The log: text, or the bytes of a log that is not valid UTF-8. */
|
|
44
|
+
trace: string | Uint8Array;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface FakeNote {
|
|
48
|
+
id: number;
|
|
49
|
+
body: string;
|
|
50
|
+
author: string;
|
|
51
|
+
resolvable: boolean;
|
|
52
|
+
resolved: boolean;
|
|
53
|
+
system: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface FakeDiscussion {
|
|
57
|
+
id: string;
|
|
58
|
+
mr: number;
|
|
59
|
+
notes: FakeNote[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface FakeEvent {
|
|
63
|
+
id: number;
|
|
64
|
+
action_name: string;
|
|
65
|
+
target_type: string | null;
|
|
66
|
+
target_iid: number | null;
|
|
67
|
+
author: { username: string };
|
|
68
|
+
push_data?: { ref: string; ref_type: "branch" | "tag"; commit_from: string | null; commit_to: string | null };
|
|
69
|
+
note?: { id: number; body: string; noteable_type: string; noteable_iid: number; author: { username: string } };
|
|
70
|
+
created_at: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface FakeProject {
|
|
74
|
+
id: number;
|
|
75
|
+
path_with_namespace: string;
|
|
76
|
+
mrs: Map<number, FakeMr>;
|
|
77
|
+
pipelines: FakePipeline[];
|
|
78
|
+
jobs: FakeJob[];
|
|
79
|
+
discussions: FakeDiscussion[];
|
|
80
|
+
events: FakeEvent[];
|
|
81
|
+
commits: Map<string, string[]>;
|
|
82
|
+
/** The tip of each branch, by name. */
|
|
83
|
+
branches: Map<string, string>;
|
|
84
|
+
/** The variables each triggered pipeline was given, by pipeline id. */
|
|
85
|
+
variables: Map<number, Array<{ key: string; value: string }>>;
|
|
86
|
+
/** The project merges by fast-forward: a merge makes no merge commit, and a squash is the commit that lands. */
|
|
87
|
+
fastForward: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface SeenRequest {
|
|
91
|
+
method: string;
|
|
92
|
+
path: string;
|
|
93
|
+
query: Record<string, string>;
|
|
94
|
+
headers: Record<string, string>;
|
|
95
|
+
body: unknown;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
interface Queued {
|
|
99
|
+
method: string;
|
|
100
|
+
path: RegExp;
|
|
101
|
+
status: number;
|
|
102
|
+
headers: Record<string, string>;
|
|
103
|
+
body: unknown;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A commit id of 40 hex characters made from a word, so a test can name commits. */
|
|
107
|
+
export function sha(word: string): string {
|
|
108
|
+
return createHash("sha1").update(word).digest("hex");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export class FakeGitLab {
|
|
112
|
+
readonly url: string;
|
|
113
|
+
readonly projects = new Map<number, FakeProject>();
|
|
114
|
+
readonly requests: SeenRequest[] = [];
|
|
115
|
+
/** Headers every answer carries, such as the counts of the limit. */
|
|
116
|
+
headers: Record<string, string> = {};
|
|
117
|
+
/** The time the server stamps on what changes. */
|
|
118
|
+
clock = Date.parse("2026-11-20T10:00:00Z");
|
|
119
|
+
readonly #queued: Queued[] = [];
|
|
120
|
+
#ids = 1000;
|
|
121
|
+
|
|
122
|
+
constructor(url = "https://gitlab.example.com") {
|
|
123
|
+
this.url = url;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The fetch the module is given. */
|
|
127
|
+
readonly fetch = async (url: string, init: { method: string; headers: Record<string, string>; body?: string }): Promise<Response> => {
|
|
128
|
+
const parsed = new URL(url);
|
|
129
|
+
if (!url.startsWith(this.url)) return json(502, { message: "unknown server" });
|
|
130
|
+
// The project is one segment, its path encoded: decode it alone, then the rest.
|
|
131
|
+
const raw = parsed.pathname.replace(/^\/api\/v4/, "");
|
|
132
|
+
const project = /^\/projects\/([^/]+)(.*)$/.exec(raw);
|
|
133
|
+
const path = project === null ? decodeURIComponent(raw) : `/projects/${decodeURIComponent(project[1] as string)}${decodeURIComponent(project[2] ?? "")}`;
|
|
134
|
+
const query = Object.fromEntries(parsed.searchParams);
|
|
135
|
+
const body = init.body === undefined ? undefined : JSON.parse(init.body);
|
|
136
|
+
this.requests.push({ method: init.method, path, query, headers: init.headers, body });
|
|
137
|
+
const queued = this.#queued.findIndex((one) => one.method === init.method && one.path.test(path));
|
|
138
|
+
if (queued !== -1) {
|
|
139
|
+
const [one] = this.#queued.splice(queued, 1);
|
|
140
|
+
if (one !== undefined) return json(one.status, one.body, { ...this.headers, ...one.headers });
|
|
141
|
+
}
|
|
142
|
+
if (init.headers["PRIVATE-TOKEN"] !== this.token) return json(401, { message: "401 Unauthorized" });
|
|
143
|
+
const answer = this.#paged(init.method, query, this.#route(init.method, path, query, body));
|
|
144
|
+
if (init.method === "GET" && answer.status === 200) {
|
|
145
|
+
const etag = `W/"${createHash("sha1").update(JSON.stringify(answer.body)).digest("hex")}"`;
|
|
146
|
+
if (init.headers["If-None-Match"] === etag) return new Response(null, { status: 304, headers: { ETag: etag, ...this.headers } });
|
|
147
|
+
return json(200, answer.body, { ETag: etag, ...this.headers, ...answer.headers });
|
|
148
|
+
}
|
|
149
|
+
return json(answer.status, answer.body, { ...this.headers, ...answer.headers });
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/** The token the server takes. */
|
|
153
|
+
token = "glpat-test-token";
|
|
154
|
+
|
|
155
|
+
/** Answer the next request of this method whose path matches with this, once. */
|
|
156
|
+
queue(method: string, path: RegExp, status: number, body: unknown = {}, headers: Record<string, string> = {}): void {
|
|
157
|
+
this.#queued.push({ method, path, status, headers, body });
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The requests whose path matches. */
|
|
161
|
+
seen(method: string, path: RegExp): SeenRequest[] {
|
|
162
|
+
return this.requests.filter((one) => one.method === method && path.test(one.path));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
project(id: number, path: string): FakeProject {
|
|
166
|
+
const made: FakeProject = {
|
|
167
|
+
id,
|
|
168
|
+
path_with_namespace: path,
|
|
169
|
+
mrs: new Map(),
|
|
170
|
+
pipelines: [],
|
|
171
|
+
jobs: [],
|
|
172
|
+
discussions: [],
|
|
173
|
+
events: [],
|
|
174
|
+
commits: new Map(),
|
|
175
|
+
branches: new Map(),
|
|
176
|
+
variables: new Map(),
|
|
177
|
+
fastForward: false,
|
|
178
|
+
};
|
|
179
|
+
this.projects.set(id, made);
|
|
180
|
+
return made;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The time, moved on by a second: what a change is stamped with. */
|
|
184
|
+
stamp(): string {
|
|
185
|
+
this.clock += 1000;
|
|
186
|
+
return new Date(this.clock).toISOString();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
mr(project: FakeProject, fields: Partial<FakeMr> & { iid: number; source_branch: string; sha: string }): FakeMr {
|
|
190
|
+
const made: FakeMr = {
|
|
191
|
+
title: `MR ${String(fields.iid)}`,
|
|
192
|
+
description: "",
|
|
193
|
+
state: "opened",
|
|
194
|
+
target_branch: "main",
|
|
195
|
+
draft: false,
|
|
196
|
+
author: "dev",
|
|
197
|
+
merge_commit_sha: null,
|
|
198
|
+
squash_commit_sha: null,
|
|
199
|
+
merge_when_pipeline_succeeds: false,
|
|
200
|
+
in_train: false,
|
|
201
|
+
approvals: [],
|
|
202
|
+
updated_at: this.stamp(),
|
|
203
|
+
...fields,
|
|
204
|
+
};
|
|
205
|
+
project.mrs.set(made.iid, made);
|
|
206
|
+
return made;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
pipeline(project: FakeProject, fields: Partial<FakePipeline> & { sha: string; ref: string }): FakePipeline {
|
|
210
|
+
const made: FakePipeline = { id: this.#next(), status: "running", source: "push", tag: false, updated_at: this.stamp(), ...fields };
|
|
211
|
+
project.pipelines.push(made);
|
|
212
|
+
return made;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
job(project: FakeProject, fields: Partial<FakeJob> & { pipeline: number; ref: string }): FakeJob {
|
|
216
|
+
const made: FakeJob = { id: this.#next(), name: "test", stage: "test", status: "running", trace: "", ...fields };
|
|
217
|
+
project.jobs.push(made);
|
|
218
|
+
return made;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
event(project: FakeProject, fields: Partial<FakeEvent> & { action_name: string }): FakeEvent {
|
|
222
|
+
const made: FakeEvent = { id: this.#next(), target_type: null, target_iid: null, author: { username: "dev" }, created_at: this.stamp(), ...fields };
|
|
223
|
+
project.events.push(made);
|
|
224
|
+
return made;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
#next(): number {
|
|
228
|
+
this.#ids += 1;
|
|
229
|
+
return this.#ids;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
#mrJson(project: FakeProject, mr: FakeMr): unknown {
|
|
233
|
+
return {
|
|
234
|
+
id: project.id * 10_000 + mr.iid,
|
|
235
|
+
iid: mr.iid,
|
|
236
|
+
project_id: project.id,
|
|
237
|
+
title: mr.title,
|
|
238
|
+
description: mr.description,
|
|
239
|
+
state: mr.state,
|
|
240
|
+
source_branch: mr.source_branch,
|
|
241
|
+
target_branch: mr.target_branch,
|
|
242
|
+
sha: mr.sha,
|
|
243
|
+
draft: mr.draft,
|
|
244
|
+
author: { username: mr.author },
|
|
245
|
+
merge_commit_sha: mr.merge_commit_sha,
|
|
246
|
+
squash_commit_sha: mr.squash_commit_sha,
|
|
247
|
+
merge_when_pipeline_succeeds: mr.merge_when_pipeline_succeeds,
|
|
248
|
+
updated_at: mr.updated_at,
|
|
249
|
+
web_url: `${this.url}/${project.path_with_namespace}/-/merge_requests/${String(mr.iid)}`,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** A pipeline as the server shows it: a list gives the short form, which does not say whether the ref is a tag. */
|
|
254
|
+
#pipelineJson(project: FakeProject, pipeline: FakePipeline, short = false): unknown {
|
|
255
|
+
const fields = { id: pipeline.id, project_id: project.id, sha: pipeline.sha, ref: pipeline.ref, status: pipeline.status, source: pipeline.source, updated_at: pipeline.updated_at };
|
|
256
|
+
return short ? fields : { ...fields, tag: pipeline.tag };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
#jobJson(job: FakeJob): unknown {
|
|
260
|
+
return { id: job.id, name: job.name, stage: job.stage, status: job.status, ref: job.ref, pipeline: { id: job.pipeline } };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** A list with `per_page` is cut to the page asked for, and says which page comes next, as the server does. */
|
|
264
|
+
#paged(method: string, query: Record<string, string>, answer: { status: number; body: unknown; headers?: Record<string, string> }): { status: number; body: unknown; headers?: Record<string, string> } {
|
|
265
|
+
if (method !== "GET" || answer.status !== 200 || !Array.isArray(answer.body) || query["per_page"] === undefined) return answer;
|
|
266
|
+
const size = Number(query["per_page"]);
|
|
267
|
+
const page = Number(query["page"] ?? "1");
|
|
268
|
+
const items = answer.body.slice((page - 1) * size, page * size);
|
|
269
|
+
const more = answer.body.length > page * size;
|
|
270
|
+
return { status: 200, body: items, headers: { ...answer.headers, ...(more ? { "X-Next-Page": String(page + 1) } : {}) } };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
#route(method: string, path: string, query: Record<string, string>, body: any): { status: number; body: unknown; headers?: Record<string, string> } {
|
|
274
|
+
const project = [...this.projects.values()]
|
|
275
|
+
.filter((one) => path === `/projects/${String(one.id)}` || path.startsWith(`/projects/${String(one.id)}/`) || path === `/projects/${one.path_with_namespace}`)
|
|
276
|
+
.at(0);
|
|
277
|
+
if (project === undefined) return { status: 404, body: { message: "404 Project Not Found" } };
|
|
278
|
+
const rest = path.startsWith(`/projects/${String(project.id)}`) ? path.slice(`/projects/${String(project.id)}`.length) : "";
|
|
279
|
+
const m = (pattern: RegExp) => pattern.exec(rest);
|
|
280
|
+
const mrOf = (iid: string | undefined) => project.mrs.get(Number(iid));
|
|
281
|
+
const notFound = { status: 404, body: { message: "404 Not found" } };
|
|
282
|
+
let hit: RegExpExecArray | null;
|
|
283
|
+
|
|
284
|
+
if (method === "GET" && rest === "") return { status: 200, body: { id: project.id, path_with_namespace: project.path_with_namespace } };
|
|
285
|
+
|
|
286
|
+
if (method === "GET" && rest === "/merge_requests") {
|
|
287
|
+
const items = [...project.mrs.values()].filter(
|
|
288
|
+
(mr) =>
|
|
289
|
+
(query["source_branch"] === undefined || mr.source_branch === query["source_branch"]) &&
|
|
290
|
+
(query["state"] === undefined || query["state"] === "all" || mr.state === query["state"]) &&
|
|
291
|
+
(query["updated_after"] === undefined || mr.updated_at > query["updated_after"]),
|
|
292
|
+
);
|
|
293
|
+
return { status: 200, body: items.map((mr) => this.#mrJson(project, mr)) };
|
|
294
|
+
}
|
|
295
|
+
if (method === "POST" && rest === "/merge_requests") {
|
|
296
|
+
const iid = Math.max(0, ...project.mrs.keys()) + 1;
|
|
297
|
+
const head = project.branches.get(body.source_branch) ?? sha(`${String(body.source_branch)}-head`);
|
|
298
|
+
const mr = this.mr(project, { iid, source_branch: body.source_branch, target_branch: body.target_branch, title: body.title, description: body.description ?? "", sha: head });
|
|
299
|
+
return { status: 201, body: this.#mrJson(project, mr) };
|
|
300
|
+
}
|
|
301
|
+
if ((hit = m(/^\/merge_requests\/(\d+)$/))) {
|
|
302
|
+
const mr = mrOf(hit[1]);
|
|
303
|
+
if (mr === undefined) return notFound;
|
|
304
|
+
if (method === "GET") return { status: 200, body: this.#mrJson(project, mr) };
|
|
305
|
+
if (method === "PUT") {
|
|
306
|
+
if (body.title !== undefined) mr.title = body.title;
|
|
307
|
+
if (body.description !== undefined) mr.description = body.description;
|
|
308
|
+
if (body.target_branch !== undefined) mr.target_branch = body.target_branch;
|
|
309
|
+
if (body.state_event === "close" && mr.state === "opened") mr.state = "closed";
|
|
310
|
+
if (body.state_event === "reopen" && mr.state === "closed") mr.state = "opened";
|
|
311
|
+
mr.updated_at = this.stamp();
|
|
312
|
+
return { status: 200, body: this.#mrJson(project, mr) };
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (method === "PUT" && (hit = m(/^\/merge_requests\/(\d+)\/merge$/))) {
|
|
316
|
+
const mr = mrOf(hit[1]);
|
|
317
|
+
if (mr === undefined) return notFound;
|
|
318
|
+
if (mr.state !== "opened") return { status: 405, body: { message: "405 Method Not Allowed" } };
|
|
319
|
+
if (body.sha !== undefined && body.sha !== mr.sha) return { status: 409, body: { message: "SHA does not match HEAD of source branch" } };
|
|
320
|
+
if (body.merge_when_pipeline_succeeds === true || body.auto_merge === true) {
|
|
321
|
+
mr.merge_when_pipeline_succeeds = true;
|
|
322
|
+
mr.updated_at = this.stamp();
|
|
323
|
+
return { status: 200, body: this.#mrJson(project, mr) };
|
|
324
|
+
}
|
|
325
|
+
mr.state = "merged";
|
|
326
|
+
if (!project.fastForward) mr.merge_commit_sha = sha(`merge-${String(mr.iid)}-${mr.sha}`);
|
|
327
|
+
if (body.squash === true) mr.squash_commit_sha = sha(`squash-${String(mr.iid)}`);
|
|
328
|
+
mr.updated_at = this.stamp();
|
|
329
|
+
return { status: 200, body: this.#mrJson(project, mr) };
|
|
330
|
+
}
|
|
331
|
+
if (method === "POST" && (hit = m(/^\/merge_requests\/(\d+)\/cancel_merge_when_pipeline_succeeds$/))) {
|
|
332
|
+
const mr = mrOf(hit[1]);
|
|
333
|
+
if (mr === undefined) return notFound;
|
|
334
|
+
if (!mr.merge_when_pipeline_succeeds) return { status: 406, body: { message: "406 Not Acceptable" } };
|
|
335
|
+
mr.merge_when_pipeline_succeeds = false;
|
|
336
|
+
return { status: 201, body: this.#mrJson(project, mr) };
|
|
337
|
+
}
|
|
338
|
+
if ((hit = m(/^\/merge_trains\/merge_requests\/(\d+)$/))) {
|
|
339
|
+
const mr = mrOf(hit[1]);
|
|
340
|
+
if (mr === undefined) return notFound;
|
|
341
|
+
if (method === "GET") return mr.in_train ? { status: 200, body: { merge_request: { iid: mr.iid }, status: "idle" } } : notFound;
|
|
342
|
+
if (method === "POST") {
|
|
343
|
+
if (body.sha !== undefined && body.sha !== mr.sha) return { status: 409, body: { message: "SHA does not match HEAD of source branch" } };
|
|
344
|
+
mr.in_train = true;
|
|
345
|
+
return { status: 201, body: [{ id: 1, merge_request: { iid: mr.iid }, status: "idle" }] };
|
|
346
|
+
}
|
|
347
|
+
if (method === "DELETE") {
|
|
348
|
+
if (!mr.in_train) return notFound;
|
|
349
|
+
mr.in_train = false;
|
|
350
|
+
return { status: 204, body: null };
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (method === "GET" && (hit = m(/^\/merge_requests\/(\d+)\/pipelines$/))) {
|
|
354
|
+
const mr = mrOf(hit[1]);
|
|
355
|
+
if (mr === undefined) return notFound;
|
|
356
|
+
const items = project.pipelines.filter((one) => one.ref.startsWith(`refs/merge-requests/${String(mr.iid)}/`)).reverse();
|
|
357
|
+
return { status: 200, body: items.map((one) => this.#pipelineJson(project, one, true)) };
|
|
358
|
+
}
|
|
359
|
+
if (method === "GET" && (hit = m(/^\/merge_requests\/(\d+)\/discussions$/))) {
|
|
360
|
+
return { status: 200, body: project.discussions.filter((one) => one.mr === Number(hit?.[1])).map((one) => ({ id: one.id, notes: one.notes.map((note) => ({ ...note, author: { username: note.author } })) })) };
|
|
361
|
+
}
|
|
362
|
+
if (method === "POST" && (hit = m(/^\/merge_requests\/(\d+)\/discussions$/))) {
|
|
363
|
+
const note: FakeNote = { id: this.#next(), body: body.body, author: "niwa", resolvable: true, resolved: false, system: false };
|
|
364
|
+
const discussion: FakeDiscussion = { id: `d${String(note.id)}`, mr: Number(hit[1]), notes: [note] };
|
|
365
|
+
project.discussions.push(discussion);
|
|
366
|
+
return { status: 201, body: { id: discussion.id, notes: [{ ...note, author: { username: note.author } }] } };
|
|
367
|
+
}
|
|
368
|
+
if (method === "POST" && (hit = m(/^\/merge_requests\/(\d+)\/discussions\/([^/]+)\/notes$/))) {
|
|
369
|
+
const discussion = project.discussions.find((one) => one.id === hit?.[2]);
|
|
370
|
+
if (discussion === undefined) return notFound;
|
|
371
|
+
const note: FakeNote = { id: this.#next(), body: body.body, author: "niwa", resolvable: true, resolved: false, system: false };
|
|
372
|
+
discussion.notes.push(note);
|
|
373
|
+
return { status: 201, body: { ...note, author: { username: note.author } } };
|
|
374
|
+
}
|
|
375
|
+
if (method === "PUT" && (hit = m(/^\/merge_requests\/(\d+)\/discussions\/([^/]+)$/))) {
|
|
376
|
+
const discussion = project.discussions.find((one) => one.id === hit?.[2]);
|
|
377
|
+
if (discussion === undefined) return notFound;
|
|
378
|
+
for (const note of discussion.notes) note.resolved = query["resolved"] === "true";
|
|
379
|
+
return { status: 200, body: { id: discussion.id, notes: discussion.notes.map((note) => ({ ...note, author: { username: note.author } })) } };
|
|
380
|
+
}
|
|
381
|
+
if (method === "GET" && rest === "/pipelines") {
|
|
382
|
+
const items = project.pipelines
|
|
383
|
+
.filter((one) => (query["sha"] === undefined || one.sha === query["sha"]) && (query["updated_after"] === undefined || one.updated_at > query["updated_after"]))
|
|
384
|
+
.reverse();
|
|
385
|
+
return { status: 200, body: items.map((one) => this.#pipelineJson(project, one, true)) };
|
|
386
|
+
}
|
|
387
|
+
if (method === "POST" && rest === "/pipeline") {
|
|
388
|
+
const made = this.pipeline(project, { sha: sha(`${String(body.ref)}-tip`), ref: body.ref, status: "created", source: "api" });
|
|
389
|
+
project.variables.set(made.id, body.variables ?? []);
|
|
390
|
+
return { status: 201, body: this.#pipelineJson(project, made) };
|
|
391
|
+
}
|
|
392
|
+
if ((hit = m(/^\/pipelines\/(\d+)(\/retry|\/cancel|\/jobs)?$/))) {
|
|
393
|
+
const pipeline = project.pipelines.find((one) => one.id === Number(hit?.[1]));
|
|
394
|
+
if (pipeline === undefined) return notFound;
|
|
395
|
+
if (method === "GET" && hit[2] === undefined) return { status: 200, body: this.#pipelineJson(project, pipeline) };
|
|
396
|
+
if (method === "GET" && hit[2] === "/jobs") return { status: 200, body: project.jobs.filter((one) => one.pipeline === pipeline.id).map((one) => this.#jobJson(one)) };
|
|
397
|
+
if (method === "POST" && hit[2] === "/retry") {
|
|
398
|
+
pipeline.status = "running";
|
|
399
|
+
pipeline.updated_at = this.stamp();
|
|
400
|
+
return { status: 201, body: this.#pipelineJson(project, pipeline) };
|
|
401
|
+
}
|
|
402
|
+
if (method === "POST" && hit[2] === "/cancel") {
|
|
403
|
+
pipeline.status = "canceled";
|
|
404
|
+
pipeline.updated_at = this.stamp();
|
|
405
|
+
return { status: 200, body: this.#pipelineJson(project, pipeline) };
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if ((hit = m(/^\/jobs\/(\d+)(\/retry|\/cancel|\/play|\/trace)?$/))) {
|
|
409
|
+
const job = project.jobs.find((one) => one.id === Number(hit?.[1]));
|
|
410
|
+
if (job === undefined) return notFound;
|
|
411
|
+
if (method === "GET" && hit[2] === undefined) return { status: 200, body: this.#jobJson(job) };
|
|
412
|
+
if (method === "GET" && hit[2] === "/trace") return { status: 200, body: job.trace };
|
|
413
|
+
if (method === "POST" && hit[2] === "/retry") {
|
|
414
|
+
const again = this.job(project, { pipeline: job.pipeline, ref: job.ref, name: job.name, stage: job.stage, status: "pending" });
|
|
415
|
+
return { status: 201, body: this.#jobJson(again) };
|
|
416
|
+
}
|
|
417
|
+
if (method === "POST" && hit[2] === "/cancel") {
|
|
418
|
+
job.status = "canceled";
|
|
419
|
+
return { status: 201, body: this.#jobJson(job) };
|
|
420
|
+
}
|
|
421
|
+
if (method === "POST" && hit[2] === "/play") {
|
|
422
|
+
if (job.status !== "manual") return { status: 400, body: { message: "400 Unplayable Job" } };
|
|
423
|
+
job.status = "pending";
|
|
424
|
+
return { status: 200, body: this.#jobJson(job) };
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (method === "GET" && rest.startsWith("/repository/branches/")) {
|
|
428
|
+
const name = decodeURIComponent(rest.slice("/repository/branches/".length));
|
|
429
|
+
const id = project.branches.get(name);
|
|
430
|
+
return id === undefined ? notFound : { status: 200, body: { name, commit: { id } } };
|
|
431
|
+
}
|
|
432
|
+
if (method === "GET" && rest === "/events") {
|
|
433
|
+
const items = [...project.events].sort((a, b) => (query["sort"] === "asc" ? a.id - b.id : b.id - a.id));
|
|
434
|
+
return { status: 200, body: items };
|
|
435
|
+
}
|
|
436
|
+
if (method === "GET" && (hit = m(/^\/repository\/commits\/([0-9a-f]+)\/merge_requests$/))) {
|
|
437
|
+
return { status: 200, body: [...project.mrs.values()].filter((mr) => mr.sha === hit?.[1]).map((mr) => this.#mrJson(project, mr)) };
|
|
438
|
+
}
|
|
439
|
+
if (method === "GET" && (hit = m(/^\/repository\/commits\/([0-9a-f]+)$/))) {
|
|
440
|
+
const parents = project.commits.get(hit[1] as string);
|
|
441
|
+
if (parents === undefined) return notFound;
|
|
442
|
+
return { status: 200, body: { id: hit[1], parent_ids: parents } };
|
|
443
|
+
}
|
|
444
|
+
return { status: 404, body: { message: `404 no route ${method} ${rest}` } };
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function json(status: number, body: unknown, headers: Record<string, string> = {}): Response {
|
|
449
|
+
if (body instanceof Uint8Array) return new Response(new Uint8Array(body), { status, headers: { "Content-Type": "text/plain", ...headers } });
|
|
450
|
+
if (typeof body === "string") return new Response(body, { status, headers: { "Content-Type": "text/plain", ...headers } });
|
|
451
|
+
return new Response(body === null ? null : JSON.stringify(body), { status, headers: { "Content-Type": "application/json", ...headers } });
|
|
452
|
+
}
|