@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
package/src/index.ts
ADDED
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The forge module: a GitLab server in the words of pull requests, pipelines, jobs, checks and
|
|
3
|
+
* comments.
|
|
4
|
+
*
|
|
5
|
+
* - Reads (`forge.pr.read`, `forge.checks.read`, `forge.pipeline.read`, `forge.job.read`, `forge.job.log`,
|
|
6
|
+
* `forge.comment.read`) target the repository, `<host>/<path>`.
|
|
7
|
+
* - Writes on a pull request (`forge.pr.ensure`, `.retarget`, `.close`, `forge.comment.*`) target it,
|
|
8
|
+
* `<host>/<path>/pr/<n>`, and land in the external world.
|
|
9
|
+
* - `forge.pr.merge` is irreversible and is not repeated after a cut: its check reads the pull request
|
|
10
|
+
* back. A merge the server takes for later is shown by `forge.pr.merge.pending` with the action that
|
|
11
|
+
* calls it off, because a hold of niwa cannot stop it.
|
|
12
|
+
* - Pipelines and jobs (`forge.pipeline.trigger`, `.retry`, `.cancel`, `forge.job.retry`, `.cancel`,
|
|
13
|
+
* `.run`) target the ref they run on, `<host>/<path>/ref/<ref>`. The module does not know what a
|
|
14
|
+
* pipeline does, and one on a protected branch or a tag may publish, so they are irreversible: a
|
|
15
|
+
* grant names each of them, and a hold refuses them.
|
|
16
|
+
*
|
|
17
|
+
* The token of a server is a secret reference resolved by the host where the call runs; it reaches a
|
|
18
|
+
* request header and nothing else. `fetch` may be given, so a test speaks to a fake server.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { realpathSync } from "node:fs";
|
|
22
|
+
import { fileURLToPath } from "node:url";
|
|
23
|
+
|
|
24
|
+
import { IntegrityError, RecordRefused, action } from "@niwa-labs/core";
|
|
25
|
+
import type { JournalReader } from "@niwa-labs/core";
|
|
26
|
+
import { BUILTIN_WORKFLOW, COCKPIT_MODULE, SCRIPT_PACKAGE, plugin } from "@niwa-labs/plugin";
|
|
27
|
+
import type { Plugin } from "@niwa-labs/plugin";
|
|
28
|
+
|
|
29
|
+
import {
|
|
30
|
+
forgeChecksRead,
|
|
31
|
+
forgeCommentCreate,
|
|
32
|
+
forgeCommentRead,
|
|
33
|
+
forgeCommentReply,
|
|
34
|
+
forgeCommentResolve,
|
|
35
|
+
forgeJobCancel,
|
|
36
|
+
forgeJobLog,
|
|
37
|
+
forgeJobRead,
|
|
38
|
+
forgeJobRetry,
|
|
39
|
+
forgeJobRun,
|
|
40
|
+
forgePipelineCancel,
|
|
41
|
+
forgePipelineRead,
|
|
42
|
+
forgePipelineRetry,
|
|
43
|
+
forgePipelineTrigger,
|
|
44
|
+
forgePrClose,
|
|
45
|
+
forgePrEnsure,
|
|
46
|
+
forgePrMerge,
|
|
47
|
+
forgePrMergeCancel,
|
|
48
|
+
forgePrList,
|
|
49
|
+
forgePrMergePending,
|
|
50
|
+
forgePrRead,
|
|
51
|
+
forgePrRetarget,
|
|
52
|
+
forgePushConfig,
|
|
53
|
+
forgeRepoList,
|
|
54
|
+
forgeRepoStatus,
|
|
55
|
+
forgeRepoSync,
|
|
56
|
+
forgeRepoUnwatch,
|
|
57
|
+
forgeRepoWatch,
|
|
58
|
+
} from "./actions.ts";
|
|
59
|
+
import type { RepoStatus } from "./actions.ts";
|
|
60
|
+
import { ForgeError } from "./gitlab.ts";
|
|
61
|
+
import type { FetchLike, GitLab } from "./gitlab.ts";
|
|
62
|
+
import { cancelMerge, checkCancel, checkMerge, merge, pendingMerges, settleMerges, unresolvedClaims } from "./merge.ts";
|
|
63
|
+
import type { CheckPlace } from "./merge.ts";
|
|
64
|
+
import { checksOf, lookupsOf, pipelineOf, pullRequestOf, runStateOf } from "./normalize.ts";
|
|
65
|
+
import { DEFAULT_INTERVALS, Poller, Seen, sync } from "./observe.ts";
|
|
66
|
+
import type { PollIntervals } from "./observe.ts";
|
|
67
|
+
import { observedPrs } from "./prs.ts";
|
|
68
|
+
import type { GitLabJob, GitLabMr, GitLabPipeline } from "./normalize.ts";
|
|
69
|
+
import { FORGE_TYPES, MergeStarted, Pushed, RepoUnwatched, RepoWatched } from "./record-types.ts";
|
|
70
|
+
import type { Pipeline, Repo } from "./record-types.ts";
|
|
71
|
+
import { resolveSecret } from "./secrets.ts";
|
|
72
|
+
import { Servers, projectPath, repoKey, watchedRepos, watching } from "./servers.ts";
|
|
73
|
+
import type { ForgeServer } from "./servers.ts";
|
|
74
|
+
|
|
75
|
+
export * from "./records.ts";
|
|
76
|
+
export { ForgeError } from "./gitlab.ts";
|
|
77
|
+
export type { FetchLike, ForgeErrorCode } from "./gitlab.ts";
|
|
78
|
+
export type { ForgeServer } from "./servers.ts";
|
|
79
|
+
export { DEFAULT_INTERVALS, Poller, pollInterval } from "./observe.ts";
|
|
80
|
+
export type { PollIntervals } from "./observe.ts";
|
|
81
|
+
|
|
82
|
+
/** What the forge module is given. */
|
|
83
|
+
export interface ForgeOptions {
|
|
84
|
+
/** The GitLab servers it speaks to. */
|
|
85
|
+
readonly servers: readonly ForgeServer[];
|
|
86
|
+
/** How it reaches a server. Default: the global `fetch`. */
|
|
87
|
+
readonly fetch?: FetchLike;
|
|
88
|
+
/** Where `NIWA_SECRET_<name>` is read. Default: the environment of the host's process. */
|
|
89
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
90
|
+
/**
|
|
91
|
+
* How the watched repositories are polled: `false` polls them only when `forge.repo.sync` asks.
|
|
92
|
+
* Default: every 20 seconds while a run reads one, every 2 minutes after a change, every 5 after an
|
|
93
|
+
* hour of quiet.
|
|
94
|
+
*/
|
|
95
|
+
readonly poll?: Partial<PollIntervals> | false;
|
|
96
|
+
/**
|
|
97
|
+
* The built-in workflow that tells, on a push to a branch, which git worktrees were made from it
|
|
98
|
+
* and are now behind. A worktree belongs to the repository when the remote its base pushes to is that
|
|
99
|
+
* repository; `checkouts` may add local checkouts of a repository, `<host>/<path>` to absolute paths,
|
|
100
|
+
* for clones whose remote is not verified. Off by default: it needs the workflow and git modules on the host.
|
|
101
|
+
*/
|
|
102
|
+
readonly pushReaction?: { readonly checkouts?: Readonly<Record<string, readonly string[]>> };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The script of the push reaction, as it ships with the module. */
|
|
106
|
+
const PUSH_REACTION_SCRIPT = fileURLToPath(new URL("./workflows/push-reaction.ts", import.meta.url));
|
|
107
|
+
|
|
108
|
+
/** The most bytes of a job log a read answers by default. */
|
|
109
|
+
const LOG_BYTES = 65_536;
|
|
110
|
+
|
|
111
|
+
/** A discussion of a merge request, as much of it as the module reads. */
|
|
112
|
+
interface GitLabDiscussion {
|
|
113
|
+
id: string;
|
|
114
|
+
notes: Array<{ id: number; body: string; author?: { username?: string }; resolvable?: boolean; resolved?: boolean; system?: boolean }>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A path with its links resolved, as the git module records a repository; the path itself when it does not exist. */
|
|
118
|
+
function real(path: string): string {
|
|
119
|
+
try {
|
|
120
|
+
return realpathSync(path);
|
|
121
|
+
} catch {
|
|
122
|
+
return path;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The forge module. */
|
|
127
|
+
export function forge(options: ForgeOptions): Plugin {
|
|
128
|
+
const fetchOf: FetchLike = options.fetch ?? ((url, init) => fetch(url, init));
|
|
129
|
+
const env = options.env ?? process.env;
|
|
130
|
+
|
|
131
|
+
const checkouts = Object.fromEntries(
|
|
132
|
+
Object.entries(options.pushReaction?.checkouts ?? {}).map(([repo, paths]) => [repo, paths.map((path) => real(path))]),
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
return plugin("forge", (api) => {
|
|
136
|
+
api.declare(...FORGE_TYPES);
|
|
137
|
+
// The page of this plugin in the cockpit: a client module, offered by the path of its source.
|
|
138
|
+
api.offer(COCKPIT_MODULE, { id: "forge", entry: fileURLToPath(new URL("./cockpit/index.tsx", import.meta.url)) });
|
|
139
|
+
// A delivery script imports the flow helpers and builders by the name of this package: a stored copy finds it here.
|
|
140
|
+
api.offer(SCRIPT_PACKAGE, { name: "@niwa-labs/forge", root: fileURLToPath(new URL("..", import.meta.url)) });
|
|
141
|
+
if (options.pushReaction !== undefined) {
|
|
142
|
+
api.offer(BUILTIN_WORKFLOW, {
|
|
143
|
+
name: "push-reaction",
|
|
144
|
+
file: PUSH_REACTION_SCRIPT,
|
|
145
|
+
description: "Tell which worktrees a push to their base left behind",
|
|
146
|
+
triggers: [Pushed],
|
|
147
|
+
});
|
|
148
|
+
api.action(action(forgePushConfig, { recovery: "repeat", effect: "read", world: "niwa", run: () => ({ checkouts }) }));
|
|
149
|
+
}
|
|
150
|
+
const journal = (): JournalReader | undefined => api.journal?.();
|
|
151
|
+
const servers = new Servers(options.servers, { home: () => api.home(), now: () => Date.now(), fetch: fetchOf, env });
|
|
152
|
+
const gitlab = (host: string): GitLab => servers.client(host);
|
|
153
|
+
const intervals: PollIntervals = options.poll === false ? DEFAULT_INTERVALS : { ...DEFAULT_INTERVALS, ...options.poll };
|
|
154
|
+
const poller = new Poller(intervals);
|
|
155
|
+
const mergeCheck: CheckPlace = {
|
|
156
|
+
journal, gitlab,
|
|
157
|
+
write: async (type, data, id) => { await api.emit(type, data, { id }); },
|
|
158
|
+
};
|
|
159
|
+
const active = new Set<string>();
|
|
160
|
+
const during = async <T>(operation: string, work: () => Promise<T>): Promise<T> => {
|
|
161
|
+
active.add(operation);
|
|
162
|
+
try { return await work(); }
|
|
163
|
+
finally { active.delete(operation); }
|
|
164
|
+
};
|
|
165
|
+
const reconcile = async (repo: Repo) => {
|
|
166
|
+
for (const claim of unresolvedClaims(journal())) {
|
|
167
|
+
if (active.has(claim.operation) || claim.pr.repo.host !== repo.host || claim.pr.repo.projectId !== repo.projectId) continue;
|
|
168
|
+
const check = claim.cancel ? checkCancel : checkMerge;
|
|
169
|
+
await check(mergeCheck, claim.operation);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
/** The repository of a name; a caller that reads it is waited on, so it is polled more often. */
|
|
173
|
+
const repoOf = (name: { host: string; path: string }) => {
|
|
174
|
+
poller.touch(repoKey(name), Date.now());
|
|
175
|
+
return servers.repo(name, journal());
|
|
176
|
+
};
|
|
177
|
+
/** What each repository's records hold, kept from pass to pass: a pass takes only what was written since. */
|
|
178
|
+
const projections = new Map<string, Seen>();
|
|
179
|
+
/** How the polls of each repository have gone since the host started, by `<host>/<path>`. */
|
|
180
|
+
const statuses = new Map<string, Omit<RepoStatus, "repo">>();
|
|
181
|
+
/** One pass over a watched repository at a time, so a version is never claimed twice. */
|
|
182
|
+
const passes = new Map<string, Promise<number>>();
|
|
183
|
+
const pass = (key: string): Promise<number> => {
|
|
184
|
+
const watched = watching(journal()).get(key);
|
|
185
|
+
const reader = journal();
|
|
186
|
+
if (watched === undefined || reader === undefined) return Promise.reject(new ForgeError("not-watched", `${key} is not watched`));
|
|
187
|
+
const before = passes.get(key) ?? Promise.resolve(0);
|
|
188
|
+
let missed = false;
|
|
189
|
+
const next = before
|
|
190
|
+
.catch(() => 0)
|
|
191
|
+
.then(async () => {
|
|
192
|
+
await reconcile(watched.repo);
|
|
193
|
+
const now = journal();
|
|
194
|
+
if (now === undefined) throw new ForgeError("not-watched", "the host is not serving");
|
|
195
|
+
// A path that names another project than before (a rename, a new project under the old path) starts afresh.
|
|
196
|
+
const known = projections.get(key);
|
|
197
|
+
const seen = known?.repo.projectId === watched.repo.projectId ? known : new Seen(watched.repo);
|
|
198
|
+
projections.set(key, seen);
|
|
199
|
+
return sync(
|
|
200
|
+
{ gitlab: gitlab(watched.repo.host), journal: now, emit: (type, data, opts) => api.emit(type, data, opts), seen, gap: () => (missed = true) },
|
|
201
|
+
watched.repo,
|
|
202
|
+
watched.since,
|
|
203
|
+
);
|
|
204
|
+
})
|
|
205
|
+
.then(
|
|
206
|
+
(written) => {
|
|
207
|
+
const at = Date.now();
|
|
208
|
+
statuses.set(key, { lastPassAt: at, lastOkAt: at, ...(missed ? { eventsMissed: true } : {}) });
|
|
209
|
+
return written;
|
|
210
|
+
},
|
|
211
|
+
(error: unknown) => {
|
|
212
|
+
statuses.set(key, { ...statuses.get(key), lastPassAt: Date.now(), lastError: error instanceof Error ? error.message : String(error) });
|
|
213
|
+
throw error;
|
|
214
|
+
},
|
|
215
|
+
);
|
|
216
|
+
passes.set(key, next);
|
|
217
|
+
return next.finally(() => {
|
|
218
|
+
if (passes.get(key) === next) passes.delete(key);
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/** Settle the pending merges of a repository that is not watched: the server is asked, nothing else is read. */
|
|
223
|
+
const settle = async (key: string, repo: Repo | undefined): Promise<number> => {
|
|
224
|
+
const reader = journal();
|
|
225
|
+
if (repo === undefined || reader === undefined) return 0;
|
|
226
|
+
await reconcile(repo);
|
|
227
|
+
let written = 0;
|
|
228
|
+
await settleMerges(
|
|
229
|
+
{
|
|
230
|
+
gitlab: gitlab(repo.host),
|
|
231
|
+
journal: reader,
|
|
232
|
+
write: async (type, data, id) => {
|
|
233
|
+
try {
|
|
234
|
+
await api.emit(type, data, { id });
|
|
235
|
+
written += 1;
|
|
236
|
+
} catch (error) {
|
|
237
|
+
// The same id with other content: an earlier pass wrote this one already.
|
|
238
|
+
if (!(error instanceof IntegrityError) && !(error instanceof RecordRefused && error.code === "stale-merge")) throw error;
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
repo,
|
|
243
|
+
);
|
|
244
|
+
const at = Date.now();
|
|
245
|
+
statuses.set(key, { lastPassAt: at, lastOkAt: at });
|
|
246
|
+
return written;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
if (options.poll !== false) {
|
|
250
|
+
api.service((ctx) => {
|
|
251
|
+
let stopped = false;
|
|
252
|
+
let cancel = () => {};
|
|
253
|
+
const tick = async (): Promise<void> => {
|
|
254
|
+
const now = Date.now();
|
|
255
|
+
const watchedNow = watching(journal());
|
|
256
|
+
// A merge armed in a repository nobody watches is still settled against the server.
|
|
257
|
+
const unwatched = new Map<string, Repo>();
|
|
258
|
+
for (const one of [...pendingMerges(journal()), ...unresolvedClaims(journal())]) {
|
|
259
|
+
const key = repoKey(one.pr.repo);
|
|
260
|
+
if (!watchedNow.has(key)) unwatched.set(key, one.pr.repo);
|
|
261
|
+
}
|
|
262
|
+
const hostOf = (key: string) => key.slice(0, key.indexOf("/"));
|
|
263
|
+
const due = poller.due([...watchedNow.keys(), ...unwatched.keys()], now, (one) => (servers.hosts().includes(hostOf(one)) ? gitlab(hostOf(one)).waitMs() : 1));
|
|
264
|
+
for (const key of due) {
|
|
265
|
+
if (stopped) return;
|
|
266
|
+
const written = await (watchedNow.has(key) ? pass(key) : settle(key, unwatched.get(key))).catch(() => 0);
|
|
267
|
+
poller.passed(key, Date.now(), written);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
const step = intervals.awaited;
|
|
271
|
+
const again = () => {
|
|
272
|
+
if (stopped) return;
|
|
273
|
+
cancel = ctx.schedule(ctx.now() + step, () => tick().finally(again));
|
|
274
|
+
};
|
|
275
|
+
again();
|
|
276
|
+
return () => {
|
|
277
|
+
stopped = true;
|
|
278
|
+
cancel();
|
|
279
|
+
};
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
const pipelinesOf = async (repo: Repo, raw: readonly GitLabPipeline[]): Promise<Pipeline[]> => {
|
|
283
|
+
const lookups = lookupsOf(gitlab(repo.host), repo);
|
|
284
|
+
const made = await Promise.all(raw.map((one) => pipelineOf(repo, one, lookups)));
|
|
285
|
+
return made.filter((one): one is Pipeline => one !== undefined);
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
// --- Repositories ---------------------------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
api.action(
|
|
291
|
+
action(forgeRepoWatch, {
|
|
292
|
+
recovery: "repeat",
|
|
293
|
+
effect: "record",
|
|
294
|
+
world: "niwa",
|
|
295
|
+
idempotent: true,
|
|
296
|
+
async run(input, ctx) {
|
|
297
|
+
const known = watchedRepos(journal()).get(repoKey(input.repo));
|
|
298
|
+
if (known !== undefined) return known;
|
|
299
|
+
const repo = await servers.lookup(input.repo);
|
|
300
|
+
const latest = await gitlab(repo.host).get<Array<{ id: number }>>(projectPath(repo, "/events"), { sort: "desc", per_page: 1 });
|
|
301
|
+
await ctx.emit(RepoWatched, { repo, since: latest[0]?.id ?? 0 });
|
|
302
|
+
return repo;
|
|
303
|
+
},
|
|
304
|
+
}),
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
api.action(
|
|
308
|
+
action(forgeRepoUnwatch, {
|
|
309
|
+
recovery: "repeat",
|
|
310
|
+
effect: "record",
|
|
311
|
+
world: "niwa",
|
|
312
|
+
idempotent: true,
|
|
313
|
+
async run(input, ctx) {
|
|
314
|
+
const known = watchedRepos(journal()).get(repoKey(input.repo));
|
|
315
|
+
if (known === undefined) return { unwatched: false };
|
|
316
|
+
await ctx.emit(RepoUnwatched, { repo: known });
|
|
317
|
+
return { unwatched: true };
|
|
318
|
+
},
|
|
319
|
+
}),
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
api.action(
|
|
323
|
+
action(forgeRepoList, {
|
|
324
|
+
recovery: "repeat",
|
|
325
|
+
effect: "read",
|
|
326
|
+
world: "niwa",
|
|
327
|
+
run: () => ({ items: [...watchedRepos(journal()).values()] }),
|
|
328
|
+
}),
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
api.action(
|
|
332
|
+
action(forgeRepoSync, {
|
|
333
|
+
// A pass writes each fact under an id of its own: a pass made again writes nothing twice.
|
|
334
|
+
recovery: "repeat",
|
|
335
|
+
effect: "record",
|
|
336
|
+
world: "niwa",
|
|
337
|
+
async run(input) {
|
|
338
|
+
const key = repoKey(input.repo);
|
|
339
|
+
const written = await pass(key);
|
|
340
|
+
poller.passed(key, Date.now(), written);
|
|
341
|
+
return { written };
|
|
342
|
+
},
|
|
343
|
+
}),
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
api.action(
|
|
347
|
+
action(forgeRepoStatus, {
|
|
348
|
+
recovery: "repeat",
|
|
349
|
+
effect: "read",
|
|
350
|
+
world: "niwa",
|
|
351
|
+
run: (input) => ({
|
|
352
|
+
items: [...watching(journal())]
|
|
353
|
+
.filter(([key]) => input.repo === undefined || key === repoKey(input.repo))
|
|
354
|
+
.map(([key, one]) => ({ repo: one.repo, ...statuses.get(key) })),
|
|
355
|
+
}),
|
|
356
|
+
}),
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
// --- Reads ----------------------------------------------------------------------------------
|
|
360
|
+
|
|
361
|
+
api.action(
|
|
362
|
+
action(forgePrRead, {
|
|
363
|
+
recovery: "repeat",
|
|
364
|
+
effect: "read",
|
|
365
|
+
world: "external",
|
|
366
|
+
async run(input) {
|
|
367
|
+
const repo = await repoOf(input.repo);
|
|
368
|
+
const client = gitlab(repo.host);
|
|
369
|
+
const path = `/merge_requests/${String(input.number)}`;
|
|
370
|
+
const mr = await client.get<GitLabMr>(projectPath(repo, path));
|
|
371
|
+
const pr = pullRequestOf(repo, mr);
|
|
372
|
+
if (!input.landing || pr.state !== "open") return pr;
|
|
373
|
+
const raw = await client.list<GitLabPipeline>(projectPath(repo, `${path}/pipelines`));
|
|
374
|
+
const ref = `refs/merge-requests/${input.number}`;
|
|
375
|
+
const latest = new Map<string, GitLabPipeline>();
|
|
376
|
+
for (const pipeline of raw) {
|
|
377
|
+
if (pipeline.ref !== `${ref}/head` && pipeline.ref !== `${ref}/merge`) continue;
|
|
378
|
+
if ((latest.get(pipeline.ref)?.id ?? 0) < pipeline.id) latest.set(pipeline.ref, pipeline);
|
|
379
|
+
}
|
|
380
|
+
const pipelines = (await pipelinesOf(repo, [...latest.values()])).filter(one => one.sourceSha === pr.head);
|
|
381
|
+
const target = await client.get<{ commit: { id: string } }>(projectPath(repo, `/repository/branches/${encodeURIComponent(pr.base)}`)).catch((error: unknown) => {
|
|
382
|
+
if (error instanceof ForgeError && error.code === "not-found") return undefined;
|
|
383
|
+
throw error;
|
|
384
|
+
});
|
|
385
|
+
if (!target) return pr;
|
|
386
|
+
return { ...pr, landing: { targetSha: target.commit.id, pipelines, conditionalMerge: false } };
|
|
387
|
+
},
|
|
388
|
+
}),
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
api.action(
|
|
392
|
+
action(forgeChecksRead, {
|
|
393
|
+
recovery: "repeat",
|
|
394
|
+
effect: "read",
|
|
395
|
+
world: "external",
|
|
396
|
+
async run(input) {
|
|
397
|
+
const repo = await repoOf(input.repo);
|
|
398
|
+
const client = gitlab(repo.host);
|
|
399
|
+
const mr = await client.get<GitLabMr>(projectPath(repo, `/merge_requests/${String(input.number)}`));
|
|
400
|
+
const raw = await client.list<GitLabPipeline>(projectPath(repo, `/merge_requests/${String(input.number)}/pipelines`));
|
|
401
|
+
const ofHead = (await pipelinesOf(repo, raw)).filter((one) => one.sourceSha === mr.sha);
|
|
402
|
+
return { pr: { repo, number: input.number }, head: mr.sha, ...checksOf(ofHead) };
|
|
403
|
+
},
|
|
404
|
+
}),
|
|
405
|
+
);
|
|
406
|
+
|
|
407
|
+
api.action(
|
|
408
|
+
action(forgePipelineRead, {
|
|
409
|
+
recovery: "repeat",
|
|
410
|
+
effect: "read",
|
|
411
|
+
world: "external",
|
|
412
|
+
async run(input) {
|
|
413
|
+
const repo = await repoOf(input.repo);
|
|
414
|
+
const client = gitlab(repo.host);
|
|
415
|
+
const raw =
|
|
416
|
+
input.pipeline !== undefined
|
|
417
|
+
? [await client.get<GitLabPipeline>(projectPath(repo, `/pipelines/${String(input.pipeline)}`))]
|
|
418
|
+
: await client.list<GitLabPipeline>(projectPath(repo, "/pipelines"), input.sha === undefined ? {} : { sha: input.sha });
|
|
419
|
+
const items = await pipelinesOf(repo, raw);
|
|
420
|
+
// A merge result runs on the merge commit of its own, so the pipelines of a change are found
|
|
421
|
+
// through the pull requests whose head is the commit, each of which lists its own pipelines.
|
|
422
|
+
if (input.sha !== undefined && input.pipeline === undefined) {
|
|
423
|
+
const seen = new Set(items.map((one) => one.pipeline));
|
|
424
|
+
const requests = await client.list<GitLabMr>(projectPath(repo, `/repository/commits/${input.sha}/merge_requests`));
|
|
425
|
+
for (const mr of requests) {
|
|
426
|
+
const ofRequest = await pipelinesOf(repo, await client.list<GitLabPipeline>(projectPath(repo, `/merge_requests/${String(mr.iid)}/pipelines`)));
|
|
427
|
+
for (const one of ofRequest) {
|
|
428
|
+
if (one.sourceSha !== input.sha || seen.has(one.pipeline)) continue;
|
|
429
|
+
seen.add(one.pipeline);
|
|
430
|
+
items.push(one);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
const wanted = input.kind === undefined ? items : items.filter((one) => one.kind === input.kind);
|
|
435
|
+
return { items: wanted.sort((a, b) => b.pipeline - a.pipeline) };
|
|
436
|
+
},
|
|
437
|
+
}),
|
|
438
|
+
);
|
|
439
|
+
|
|
440
|
+
const jobOf = (job: GitLabJob) => ({
|
|
441
|
+
job: job.id,
|
|
442
|
+
pipeline: job.pipeline.id,
|
|
443
|
+
name: job.name,
|
|
444
|
+
stage: job.stage,
|
|
445
|
+
ref: job.ref,
|
|
446
|
+
state: runStateOf(job.status),
|
|
447
|
+
manual: job.status === "manual",
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
api.action(
|
|
451
|
+
action(forgeJobRead, {
|
|
452
|
+
recovery: "repeat",
|
|
453
|
+
effect: "read",
|
|
454
|
+
world: "external",
|
|
455
|
+
async run(input) {
|
|
456
|
+
const repo = await repoOf(input.repo);
|
|
457
|
+
const client = gitlab(repo.host);
|
|
458
|
+
if (input.job !== undefined) return { items: [jobOf(await client.get<GitLabJob>(projectPath(repo, `/jobs/${String(input.job)}`)))] };
|
|
459
|
+
if (input.pipeline === undefined) throw new ForgeError("refused", "forge.job.read needs a pipeline or a job");
|
|
460
|
+
return { items: (await client.list<GitLabJob>(projectPath(repo, `/pipelines/${String(input.pipeline)}/jobs`))).map(jobOf) };
|
|
461
|
+
},
|
|
462
|
+
}),
|
|
463
|
+
);
|
|
464
|
+
|
|
465
|
+
api.action(
|
|
466
|
+
action(forgeJobLog, {
|
|
467
|
+
recovery: "repeat",
|
|
468
|
+
effect: "read",
|
|
469
|
+
world: "external",
|
|
470
|
+
async run(input) {
|
|
471
|
+
const repo = await repoOf(input.repo);
|
|
472
|
+
const keep = input.bytes ?? LOG_BYTES;
|
|
473
|
+
const { text, size } = await gitlab(repo.host).tail(projectPath(repo, `/jobs/${String(input.job)}/trace`), keep);
|
|
474
|
+
return { job: input.job, log: text, size, truncated: size > keep };
|
|
475
|
+
},
|
|
476
|
+
}),
|
|
477
|
+
);
|
|
478
|
+
|
|
479
|
+
api.action(
|
|
480
|
+
action(forgeCommentRead, {
|
|
481
|
+
recovery: "repeat",
|
|
482
|
+
effect: "read",
|
|
483
|
+
world: "external",
|
|
484
|
+
async run(input) {
|
|
485
|
+
const repo = await repoOf(input.repo);
|
|
486
|
+
const discussions = await gitlab(repo.host).list<GitLabDiscussion>(projectPath(repo, `/merge_requests/${String(input.number)}/discussions`));
|
|
487
|
+
return {
|
|
488
|
+
items: discussions.flatMap((discussion) =>
|
|
489
|
+
discussion.notes.map((note) => ({
|
|
490
|
+
comment: note.id,
|
|
491
|
+
discussion: discussion.id,
|
|
492
|
+
author: note.author?.username ?? "",
|
|
493
|
+
body: note.body,
|
|
494
|
+
resolvable: note.resolvable ?? false,
|
|
495
|
+
resolved: note.resolved ?? false,
|
|
496
|
+
system: note.system ?? false,
|
|
497
|
+
})),
|
|
498
|
+
),
|
|
499
|
+
};
|
|
500
|
+
},
|
|
501
|
+
}),
|
|
502
|
+
);
|
|
503
|
+
|
|
504
|
+
// --- Writes on a pull request ---------------------------------------------------------------
|
|
505
|
+
|
|
506
|
+
api.action(
|
|
507
|
+
action(forgePrEnsure, {
|
|
508
|
+
// An open pull request of the branch is found again, so a repeat updates the one it made.
|
|
509
|
+
recovery: "repeat",
|
|
510
|
+
effect: "write",
|
|
511
|
+
world: "external",
|
|
512
|
+
idempotent: true,
|
|
513
|
+
async run(input) {
|
|
514
|
+
const repo = await repoOf(input.repo);
|
|
515
|
+
const client = gitlab(repo.host);
|
|
516
|
+
const fields = {
|
|
517
|
+
title: input.draft === true && !input.title.startsWith("Draft:") ? `Draft: ${input.title}` : input.title,
|
|
518
|
+
target_branch: input.base,
|
|
519
|
+
...(input.description === undefined ? {} : { description: input.description }),
|
|
520
|
+
};
|
|
521
|
+
const open =
|
|
522
|
+
input.number !== undefined
|
|
523
|
+
? await client.get<GitLabMr>(projectPath(repo, `/merge_requests/${String(input.number)}`))
|
|
524
|
+
: (await client.list<GitLabMr>(projectPath(repo, "/merge_requests"), { source_branch: input.branch, state: "opened" }))[0];
|
|
525
|
+
if (open !== undefined) {
|
|
526
|
+
if (open.source_branch !== input.branch) {
|
|
527
|
+
throw new ForgeError("refused", `pull request ${String(open.iid)} is of the branch "${open.source_branch}", not "${input.branch}"`);
|
|
528
|
+
}
|
|
529
|
+
// Without a number the call is granted on pr/new: it may find the pull request it opened, never change one.
|
|
530
|
+
if (input.number === undefined) {
|
|
531
|
+
const same = open.title === fields.title && open.target_branch === fields.target_branch && (fields.description === undefined || open.description === fields.description);
|
|
532
|
+
if (!same) throw new ForgeError("refused", `pull request ${String(open.iid)} of the branch "${input.branch}" is open: name it with number to change it`);
|
|
533
|
+
return { pr: { repo, number: open.iid }, created: false, head: open.sha };
|
|
534
|
+
}
|
|
535
|
+
if (open.state === "merged") throw new ForgeError("merged", `pull request ${String(open.iid)} is merged: there is no open pull request to ensure`);
|
|
536
|
+
if (open.state === "locked") throw new ForgeError("refused", `pull request ${String(open.iid)} is locked and cannot be changed now`);
|
|
537
|
+
const mr = await client.send<GitLabMr>("PUT", projectPath(repo, `/merge_requests/${String(open.iid)}`), {
|
|
538
|
+
...fields,
|
|
539
|
+
...(open.state === "closed" ? { state_event: "reopen" } : {}),
|
|
540
|
+
});
|
|
541
|
+
if (mr.state !== "opened") throw new ForgeError("refused", `pull request ${String(mr.iid)} is ${mr.state} after the update, not open`);
|
|
542
|
+
return { pr: { repo, number: mr.iid }, created: false, head: mr.sha };
|
|
543
|
+
}
|
|
544
|
+
const mr = await client.send<GitLabMr>("POST", projectPath(repo, "/merge_requests"), { source_branch: input.branch, ...fields });
|
|
545
|
+
return { pr: { repo, number: mr.iid }, created: true, head: mr.sha };
|
|
546
|
+
},
|
|
547
|
+
}),
|
|
548
|
+
);
|
|
549
|
+
|
|
550
|
+
api.action(
|
|
551
|
+
action(forgePrRetarget, {
|
|
552
|
+
recovery: "repeat",
|
|
553
|
+
effect: "write",
|
|
554
|
+
world: "external",
|
|
555
|
+
idempotent: true,
|
|
556
|
+
async run(input) {
|
|
557
|
+
const repo = await repoOf(input.repo);
|
|
558
|
+
const mr = await gitlab(repo.host).send<GitLabMr>("PUT", projectPath(repo, `/merge_requests/${String(input.number)}`), { target_branch: input.base });
|
|
559
|
+
return { pr: { repo, number: mr.iid }, base: mr.target_branch };
|
|
560
|
+
},
|
|
561
|
+
}),
|
|
562
|
+
);
|
|
563
|
+
|
|
564
|
+
api.action(
|
|
565
|
+
action(forgePrClose, {
|
|
566
|
+
recovery: "repeat",
|
|
567
|
+
effect: "write",
|
|
568
|
+
world: "external",
|
|
569
|
+
idempotent: true,
|
|
570
|
+
async run(input) {
|
|
571
|
+
const repo = await repoOf(input.repo);
|
|
572
|
+
const client = gitlab(repo.host);
|
|
573
|
+
const path = projectPath(repo, `/merge_requests/${String(input.number)}`);
|
|
574
|
+
const before = await client.get<GitLabMr>(path);
|
|
575
|
+
if (before.state !== "opened") return { pr: { repo, number: input.number }, closed: false };
|
|
576
|
+
await client.send("PUT", path, { state_event: "close" });
|
|
577
|
+
return { pr: { repo, number: input.number }, closed: true };
|
|
578
|
+
},
|
|
579
|
+
}),
|
|
580
|
+
);
|
|
581
|
+
|
|
582
|
+
api.action(
|
|
583
|
+
action(forgeCommentCreate, {
|
|
584
|
+
// Two calls make two comments: a call that was cut off is not made again.
|
|
585
|
+
recovery: "do-not-repeat",
|
|
586
|
+
effect: "write",
|
|
587
|
+
world: "external",
|
|
588
|
+
async run(input) {
|
|
589
|
+
const repo = await repoOf(input.repo);
|
|
590
|
+
const made = await gitlab(repo.host).send<GitLabDiscussion>("POST", projectPath(repo, `/merge_requests/${String(input.number)}/discussions`), {
|
|
591
|
+
body: input.body,
|
|
592
|
+
});
|
|
593
|
+
const note = made.notes[0];
|
|
594
|
+
if (note === undefined) throw new ForgeError("server", "the server made a discussion with no comment");
|
|
595
|
+
return { comment: note.id, discussion: made.id };
|
|
596
|
+
},
|
|
597
|
+
}),
|
|
598
|
+
);
|
|
599
|
+
|
|
600
|
+
api.action(
|
|
601
|
+
action(forgeCommentReply, {
|
|
602
|
+
recovery: "do-not-repeat",
|
|
603
|
+
effect: "write",
|
|
604
|
+
world: "external",
|
|
605
|
+
async run(input) {
|
|
606
|
+
const repo = await repoOf(input.repo);
|
|
607
|
+
const note = await gitlab(repo.host).send<{ id: number }>(
|
|
608
|
+
"POST",
|
|
609
|
+
projectPath(repo, `/merge_requests/${String(input.number)}/discussions/${encodeURIComponent(input.discussion)}/notes`),
|
|
610
|
+
{ body: input.body },
|
|
611
|
+
);
|
|
612
|
+
return { comment: note.id, discussion: input.discussion };
|
|
613
|
+
},
|
|
614
|
+
}),
|
|
615
|
+
);
|
|
616
|
+
|
|
617
|
+
api.action(
|
|
618
|
+
action(forgeCommentResolve, {
|
|
619
|
+
recovery: "repeat",
|
|
620
|
+
effect: "write",
|
|
621
|
+
world: "external",
|
|
622
|
+
idempotent: true,
|
|
623
|
+
async run(input) {
|
|
624
|
+
const repo = await repoOf(input.repo);
|
|
625
|
+
const resolved = input.resolved ?? true;
|
|
626
|
+
await gitlab(repo.host).send(
|
|
627
|
+
"PUT",
|
|
628
|
+
projectPath(repo, `/merge_requests/${String(input.number)}/discussions/${encodeURIComponent(input.discussion)}?resolved=${String(resolved)}`),
|
|
629
|
+
);
|
|
630
|
+
return { discussion: input.discussion, resolved };
|
|
631
|
+
},
|
|
632
|
+
}),
|
|
633
|
+
);
|
|
634
|
+
|
|
635
|
+
// --- Merging --------------------------------------------------------------------------------
|
|
636
|
+
|
|
637
|
+
api.action(
|
|
638
|
+
action(forgePrMerge, {
|
|
639
|
+
terminalClaim: MergeStarted.name,
|
|
640
|
+
recovery: "do-not-repeat",
|
|
641
|
+
effect: "irreversible",
|
|
642
|
+
world: "external",
|
|
643
|
+
destructive: true,
|
|
644
|
+
async run(input, ctx) {
|
|
645
|
+
if (input.expectTarget !== undefined) return { outcome: "refused" as const, why: "GitLab cannot condition a merge on the target commit" };
|
|
646
|
+
const repo = await repoOf(input.repo);
|
|
647
|
+
return during(ctx.operationId, () => merge(
|
|
648
|
+
gitlab(repo.host),
|
|
649
|
+
{ pr: { repo, number: input.number }, expectHead: input.expectHead, method: input.method, when: input.when ?? "now" },
|
|
650
|
+
ctx,
|
|
651
|
+
));
|
|
652
|
+
},
|
|
653
|
+
check: operation => checkMerge(mergeCheck, operation),
|
|
654
|
+
}),
|
|
655
|
+
);
|
|
656
|
+
|
|
657
|
+
api.action(
|
|
658
|
+
action(forgePrMergeCancel, {
|
|
659
|
+
recovery: "do-not-repeat",
|
|
660
|
+
effect: "write",
|
|
661
|
+
world: "external",
|
|
662
|
+
check: operation => checkCancel(mergeCheck, operation),
|
|
663
|
+
async run(input, ctx) {
|
|
664
|
+
const repo = await repoOf(input.repo);
|
|
665
|
+
const cancelled = await during(ctx.operationId, () => cancelMerge(gitlab(repo.host), { repo, number: input.number }, journal(), ctx));
|
|
666
|
+
return { cancelled };
|
|
667
|
+
},
|
|
668
|
+
}),
|
|
669
|
+
);
|
|
670
|
+
|
|
671
|
+
api.action(
|
|
672
|
+
action(forgePrList, {
|
|
673
|
+
recovery: "repeat",
|
|
674
|
+
effect: "read",
|
|
675
|
+
world: "niwa",
|
|
676
|
+
run: (input) => ({ items: observedPrs(journal(), input) }),
|
|
677
|
+
}),
|
|
678
|
+
);
|
|
679
|
+
|
|
680
|
+
api.action(
|
|
681
|
+
action(forgePrMergePending, {
|
|
682
|
+
recovery: "repeat",
|
|
683
|
+
effect: "read",
|
|
684
|
+
world: "niwa",
|
|
685
|
+
run: () => ({ items: pendingMerges(journal()) }),
|
|
686
|
+
}),
|
|
687
|
+
);
|
|
688
|
+
|
|
689
|
+
// --- Pipelines and jobs ---------------------------------------------------------------------
|
|
690
|
+
|
|
691
|
+
/** The ref of a pipeline, which must be the one the call was granted for. */
|
|
692
|
+
const onRef = async (client: GitLab, repo: Repo, what: string, path: string, ref: string): Promise<void> => {
|
|
693
|
+
const found = await client.get<{ ref: string }>(path);
|
|
694
|
+
if (found.ref !== ref) throw new ForgeError("wrong-ref", `${what} ran on "${found.ref}", not on "${ref}" the call names`);
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
api.action(
|
|
698
|
+
action(forgePipelineTrigger, {
|
|
699
|
+
recovery: "do-not-repeat",
|
|
700
|
+
effect: "irreversible",
|
|
701
|
+
world: "external",
|
|
702
|
+
async run(input) {
|
|
703
|
+
const repo = await repoOf(input.repo);
|
|
704
|
+
// Every secret is resolved before the request: one that cannot be given stops the call.
|
|
705
|
+
const variables = Object.entries(input.variables ?? {}).map(([key, value]) => ({
|
|
706
|
+
key,
|
|
707
|
+
value: typeof value === "string" ? value : resolveSecret(api.home(), value.secret, env),
|
|
708
|
+
}));
|
|
709
|
+
const secrets = Object.values(input.variables ?? {}).flatMap((value, at) => (typeof value === "string" ? [] : [variables[at]?.value ?? ""]));
|
|
710
|
+
// The server may repeat what it was given: the client takes each secret value out of an error.
|
|
711
|
+
const made = await gitlab(repo.host).send<GitLabPipeline>(
|
|
712
|
+
"POST",
|
|
713
|
+
projectPath(repo, "/pipeline"),
|
|
714
|
+
{ ref: input.ref, ...(variables.length === 0 ? {} : { variables }) },
|
|
715
|
+
secrets,
|
|
716
|
+
);
|
|
717
|
+
return { pipeline: made.id, ref: made.ref, sha: made.sha, state: runStateOf(made.status) };
|
|
718
|
+
},
|
|
719
|
+
}),
|
|
720
|
+
);
|
|
721
|
+
|
|
722
|
+
for (const [ref, verb, recovery] of [
|
|
723
|
+
[forgePipelineRetry, "retry", "do-not-repeat"],
|
|
724
|
+
[forgePipelineCancel, "cancel", "repeat"],
|
|
725
|
+
] as const) {
|
|
726
|
+
api.action(
|
|
727
|
+
action(ref, {
|
|
728
|
+
recovery,
|
|
729
|
+
effect: "irreversible",
|
|
730
|
+
world: "external",
|
|
731
|
+
async run(input) {
|
|
732
|
+
const repo = await repoOf(input.repo);
|
|
733
|
+
const client = gitlab(repo.host);
|
|
734
|
+
const path = projectPath(repo, `/pipelines/${String(input.pipeline)}`);
|
|
735
|
+
await onRef(client, repo, `pipeline ${String(input.pipeline)}`, path, input.ref);
|
|
736
|
+
const done = await client.send<GitLabPipeline>("POST", `${path}/${verb}`);
|
|
737
|
+
return { pipeline: done.id, state: runStateOf(done.status) };
|
|
738
|
+
},
|
|
739
|
+
}),
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
for (const [ref, verb, recovery] of [
|
|
744
|
+
[forgeJobRetry, "retry", "do-not-repeat"],
|
|
745
|
+
[forgeJobCancel, "cancel", "repeat"],
|
|
746
|
+
[forgeJobRun, "play", "do-not-repeat"],
|
|
747
|
+
] as const) {
|
|
748
|
+
api.action(
|
|
749
|
+
action(ref, {
|
|
750
|
+
recovery,
|
|
751
|
+
effect: "irreversible",
|
|
752
|
+
world: "external",
|
|
753
|
+
async run(input) {
|
|
754
|
+
const repo = await repoOf(input.repo);
|
|
755
|
+
const client = gitlab(repo.host);
|
|
756
|
+
const path = projectPath(repo, `/jobs/${String(input.job)}`);
|
|
757
|
+
await onRef(client, repo, `job ${String(input.job)}`, path, input.ref);
|
|
758
|
+
const done = await client.send<GitLabJob>("POST", `${path}/${verb}`);
|
|
759
|
+
return { job: done.id, state: runStateOf(done.status) };
|
|
760
|
+
},
|
|
761
|
+
}),
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
}
|