@aigile/engine 0.0.0-stage → 0.2.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/dist/agents/code-reviewer.d.ts +37 -0
- package/dist/agents/code-reviewer.d.ts.map +1 -0
- package/dist/agents/code-reviewer.js +192 -0
- package/dist/agents/code-reviewer.js.map +1 -0
- package/dist/agents/engineer.d.ts +118 -0
- package/dist/agents/engineer.d.ts.map +1 -0
- package/dist/agents/engineer.js +528 -0
- package/dist/agents/engineer.js.map +1 -0
- package/dist/agents/monitor.d.ts +66 -0
- package/dist/agents/monitor.d.ts.map +1 -0
- package/dist/agents/monitor.js +281 -0
- package/dist/agents/monitor.js.map +1 -0
- package/dist/agents/orchestrator.d.ts +49 -0
- package/dist/agents/orchestrator.d.ts.map +1 -0
- package/dist/agents/orchestrator.js +204 -0
- package/dist/agents/orchestrator.js.map +1 -0
- package/dist/agents/release-manager.d.ts +76 -0
- package/dist/agents/release-manager.d.ts.map +1 -0
- package/dist/agents/release-manager.js +312 -0
- package/dist/agents/release-manager.js.map +1 -0
- package/dist/agents/spec-analyst.d.ts +102 -0
- package/dist/agents/spec-analyst.d.ts.map +1 -0
- package/dist/agents/spec-analyst.js +406 -0
- package/dist/agents/spec-analyst.js.map +1 -0
- package/dist/agents/verifier.d.ts +41 -0
- package/dist/agents/verifier.d.ts.map +1 -0
- package/dist/agents/verifier.js +173 -0
- package/dist/agents/verifier.js.map +1 -0
- package/dist/ai-service.d.ts +52 -0
- package/dist/ai-service.d.ts.map +1 -0
- package/dist/ai-service.js +164 -0
- package/dist/ai-service.js.map +1 -0
- package/dist/build-service.d.ts +65 -0
- package/dist/build-service.d.ts.map +1 -0
- package/dist/build-service.js +140 -0
- package/dist/build-service.js.map +1 -0
- package/dist/checkout.d.ts +57 -0
- package/dist/checkout.d.ts.map +1 -0
- package/dist/checkout.js +187 -0
- package/dist/checkout.js.map +1 -0
- package/dist/command-runner.d.ts +67 -0
- package/dist/command-runner.d.ts.map +1 -0
- package/dist/command-runner.js +149 -0
- package/dist/command-runner.js.map +1 -0
- package/dist/conversations.d.ts +18 -0
- package/dist/conversations.d.ts.map +1 -0
- package/dist/conversations.js +34 -0
- package/dist/conversations.js.map +1 -0
- package/dist/data-access.d.ts +34 -0
- package/dist/data-access.d.ts.map +1 -0
- package/dist/data-access.js +85 -0
- package/dist/data-access.js.map +1 -0
- package/dist/git-host.d.ts +43 -0
- package/dist/git-host.d.ts.map +1 -0
- package/dist/git-host.js +83 -0
- package/dist/git-host.js.map +1 -0
- package/dist/in-memory-store.d.ts +25 -0
- package/dist/in-memory-store.d.ts.map +1 -0
- package/dist/in-memory-store.js +52 -0
- package/dist/in-memory-store.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/milestones.d.ts +36 -0
- package/dist/milestones.d.ts.map +1 -0
- package/dist/milestones.js +50 -0
- package/dist/milestones.js.map +1 -0
- package/dist/pull-request-sync.d.ts +29 -0
- package/dist/pull-request-sync.d.ts.map +1 -0
- package/dist/pull-request-sync.js +79 -0
- package/dist/pull-request-sync.js.map +1 -0
- package/dist/questions.d.ts +37 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +79 -0
- package/dist/questions.js.map +1 -0
- package/dist/queue.d.ts +51 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +108 -0
- package/dist/queue.js.map +1 -0
- package/dist/reviews.d.ts +17 -0
- package/dist/reviews.d.ts.map +1 -0
- package/dist/reviews.js +42 -0
- package/dist/reviews.js.map +1 -0
- package/dist/router.d.ts +13 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +22 -0
- package/dist/router.js.map +1 -0
- package/dist/scheduler.d.ts +57 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +134 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/services.d.ts +359 -0
- package/dist/services.d.ts.map +1 -0
- package/dist/services.js +674 -0
- package/dist/services.js.map +1 -0
- package/dist/spec-resolver.d.ts +69 -0
- package/dist/spec-resolver.d.ts.map +1 -0
- package/dist/spec-resolver.js +200 -0
- package/dist/spec-resolver.js.map +1 -0
- package/dist/text-diff.d.ts +16 -0
- package/dist/text-diff.d.ts.map +1 -0
- package/dist/text-diff.js +51 -0
- package/dist/text-diff.js.map +1 -0
- package/dist/tool-sandbox.d.ts +47 -0
- package/dist/tool-sandbox.d.ts.map +1 -0
- package/dist/tool-sandbox.js +245 -0
- package/dist/tool-sandbox.js.map +1 -0
- package/dist/tracks.d.ts +58 -0
- package/dist/tracks.d.ts.map +1 -0
- package/dist/tracks.js +149 -0
- package/dist/tracks.js.map +1 -0
- package/dist/verifier.d.ts +27 -0
- package/dist/verifier.d.ts.map +1 -0
- package/dist/verifier.js +99 -0
- package/dist/verifier.js.map +1 -0
- package/package.json +31 -4
- package/README.md +0 -3
package/dist/services.js
ADDED
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
import { ADMIN_APP_ROLES, DECIDING_APP_ROLES, COLLECTIONS, conflictSchema, conflictStatusSchema, messageSchema, newId, nowIso, organizationSchema, parseRole, parseUser, fullName, projectSchema, roleAssignmentSchema, roleSchema, specSchema, userSchema, } from '@aigile/core';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { DataAccess } from './data-access.js';
|
|
4
|
+
import { diffLines } from './text-diff.js';
|
|
5
|
+
export const CHILDREN_POLICIES = ['refuse', 'reparent', 'delete'];
|
|
6
|
+
/** An error the API can map to a status code. */
|
|
7
|
+
export class ServiceError extends Error {
|
|
8
|
+
status;
|
|
9
|
+
constructor(status, message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.status = status;
|
|
12
|
+
this.name = 'ServiceError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const notFound = (what, id) => new ServiceError(404, `${what} ${id} not found`);
|
|
16
|
+
function parse(schema, input) {
|
|
17
|
+
const result = schema.safeParse(input);
|
|
18
|
+
if (!result.success) {
|
|
19
|
+
const issue = result.error.issues[0];
|
|
20
|
+
throw new ServiceError(400, issue ? `${issue.path.join('.') || 'input'}: ${issue.message}` : 'invalid input');
|
|
21
|
+
}
|
|
22
|
+
return result.data;
|
|
23
|
+
}
|
|
24
|
+
/** Drop undefined entries so a partial update can be spread over the current entity. */
|
|
25
|
+
function defined(obj) {
|
|
26
|
+
return Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined));
|
|
27
|
+
}
|
|
28
|
+
const stamp = { id: true, createdAt: true, updatedAt: true };
|
|
29
|
+
export const organizationInputSchema = organizationSchema.omit(stamp);
|
|
30
|
+
/** What a person may change about themselves. */
|
|
31
|
+
export const profileInputSchema = userSchema.pick({ firstName: true, lastName: true, email: true, jobTitle: true, organizationId: true }).partial();
|
|
32
|
+
/** What an admin may change about a user: the profile fields plus `disabled`. */
|
|
33
|
+
export const userAdminInputSchema = userSchema
|
|
34
|
+
.pick({ firstName: true, lastName: true, email: true, jobTitle: true, organizationId: true, managerId: true, disabled: true })
|
|
35
|
+
.partial();
|
|
36
|
+
/** The ranking is not typed in: it comes from the order of the roles (RoleService.reorder). */
|
|
37
|
+
export const roleInputSchema = roleSchema.omit(stamp).omit({ ranking: true });
|
|
38
|
+
export const RANKING_STEP = 10;
|
|
39
|
+
export const roleAssignmentInputSchema = roleAssignmentSchema.omit(stamp);
|
|
40
|
+
/** Kept for callers that still read it; administration follows the application role now. */
|
|
41
|
+
export const ADMIN_RANKING = 80;
|
|
42
|
+
export const projectInputSchema = projectSchema.omit(stamp);
|
|
43
|
+
export const specInputSchema = specSchema.omit(stamp).omit({ authorUserId: true });
|
|
44
|
+
export class OrganizationService {
|
|
45
|
+
store;
|
|
46
|
+
data;
|
|
47
|
+
constructor(store) {
|
|
48
|
+
this.store = store;
|
|
49
|
+
this.data = new DataAccess(store);
|
|
50
|
+
}
|
|
51
|
+
list() {
|
|
52
|
+
return this.data.organizations();
|
|
53
|
+
}
|
|
54
|
+
async get(id) {
|
|
55
|
+
const raw = await this.store.get(COLLECTIONS.organizations, id);
|
|
56
|
+
if (raw === null)
|
|
57
|
+
throw notFound('organization', id);
|
|
58
|
+
return organizationSchema.parse(raw);
|
|
59
|
+
}
|
|
60
|
+
async create(input, actor) {
|
|
61
|
+
const data = parse(organizationInputSchema, input);
|
|
62
|
+
if (data.parentId !== null)
|
|
63
|
+
await this.get(data.parentId);
|
|
64
|
+
const ts = nowIso();
|
|
65
|
+
const org = { id: newId(), ...data, createdAt: ts, updatedAt: ts };
|
|
66
|
+
await this.store.put(COLLECTIONS.organizations, org.id, org, {
|
|
67
|
+
message: `Create organization ${org.name}`,
|
|
68
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
69
|
+
});
|
|
70
|
+
return org;
|
|
71
|
+
}
|
|
72
|
+
async update(id, input, actor) {
|
|
73
|
+
const current = await this.get(id);
|
|
74
|
+
const data = defined(parse(organizationInputSchema.partial(), input));
|
|
75
|
+
const next = { ...current, ...data, updatedAt: nowIso() };
|
|
76
|
+
if (next.parentId !== null) {
|
|
77
|
+
if (next.parentId === id)
|
|
78
|
+
throw new ServiceError(400, 'an organization cannot be its own parent');
|
|
79
|
+
await this.assertNotDescendant(id, next.parentId);
|
|
80
|
+
}
|
|
81
|
+
await this.store.put(COLLECTIONS.organizations, id, next, {
|
|
82
|
+
message: `Update organization ${next.name}`,
|
|
83
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
84
|
+
});
|
|
85
|
+
return next;
|
|
86
|
+
}
|
|
87
|
+
async delete(id, actor, children = 'refuse') {
|
|
88
|
+
const org = await this.get(id);
|
|
89
|
+
const [orgs, projects] = await Promise.all([this.data.organizations(), this.data.projects()]);
|
|
90
|
+
const childOrgs = orgs.filter((o) => o.parentId === id);
|
|
91
|
+
const ownProjects = projects.filter((p) => p.organizationId === id);
|
|
92
|
+
const write = (message) => ({ message, ...(actor.author ? { author: actor.author } : {}) });
|
|
93
|
+
if (children === 'refuse') {
|
|
94
|
+
if (childOrgs.length > 0)
|
|
95
|
+
throw new ServiceError(409, 'organization has child organizations');
|
|
96
|
+
if (ownProjects.length > 0)
|
|
97
|
+
throw new ServiceError(409, 'organization has projects');
|
|
98
|
+
}
|
|
99
|
+
else if (children === 'reparent') {
|
|
100
|
+
if (org.parentId === null && childOrgs.length + ownProjects.length > 0) {
|
|
101
|
+
throw new ServiceError(409, 'a root organization has no parent to move its children to');
|
|
102
|
+
}
|
|
103
|
+
for (const child of childOrgs) {
|
|
104
|
+
await this.store.put(COLLECTIONS.organizations, child.id, { ...child, parentId: org.parentId, updatedAt: nowIso() }, write(`Move organization ${child.name} up`));
|
|
105
|
+
}
|
|
106
|
+
for (const project of ownProjects) {
|
|
107
|
+
await this.store.put(COLLECTIONS.projects, project.id, { ...project, organizationId: org.parentId, updatedAt: nowIso() }, write(`Move project ${project.name} up`));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
for (const child of childOrgs)
|
|
112
|
+
await this.delete(child.id, actor, 'delete');
|
|
113
|
+
const projectService = new ProjectService(this.store);
|
|
114
|
+
for (const project of ownProjects)
|
|
115
|
+
await projectService.delete(project.id, actor, 'delete');
|
|
116
|
+
}
|
|
117
|
+
await remapReferences(this.store, 'organizationIds', id, children === 'reparent' ? org.parentId : null, actor);
|
|
118
|
+
await this.store.delete(COLLECTIONS.organizations, id, write(`Delete organization ${org.name}`));
|
|
119
|
+
}
|
|
120
|
+
/** Refuse a parent that is the organization itself or one of its descendants (a cycle). */
|
|
121
|
+
async assertNotDescendant(id, candidateParent) {
|
|
122
|
+
const byId = new Map((await this.data.organizations()).map((o) => [o.id, o]));
|
|
123
|
+
let cursor = candidateParent;
|
|
124
|
+
const seen = new Set();
|
|
125
|
+
while (cursor !== null) {
|
|
126
|
+
if (cursor === id)
|
|
127
|
+
throw new ServiceError(400, 'that parent is inside this organization');
|
|
128
|
+
if (seen.has(cursor))
|
|
129
|
+
break;
|
|
130
|
+
seen.add(cursor);
|
|
131
|
+
const parent = byId.get(cursor);
|
|
132
|
+
if (!parent)
|
|
133
|
+
throw notFound('organization', cursor);
|
|
134
|
+
cursor = parent.parentId;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export class ProjectService {
|
|
139
|
+
store;
|
|
140
|
+
data;
|
|
141
|
+
constructor(store) {
|
|
142
|
+
this.store = store;
|
|
143
|
+
this.data = new DataAccess(store);
|
|
144
|
+
}
|
|
145
|
+
list() {
|
|
146
|
+
return this.data.projects();
|
|
147
|
+
}
|
|
148
|
+
async get(id) {
|
|
149
|
+
const raw = await this.store.get(COLLECTIONS.projects, id);
|
|
150
|
+
if (raw === null)
|
|
151
|
+
throw notFound('project', id);
|
|
152
|
+
return projectSchema.parse(raw);
|
|
153
|
+
}
|
|
154
|
+
async create(input, actor) {
|
|
155
|
+
const data = parse(projectInputSchema, input);
|
|
156
|
+
await this.assertReferences(data.organizationId, data.parentProjectId, null);
|
|
157
|
+
const ts = nowIso();
|
|
158
|
+
const project = { id: newId(), ...data, createdAt: ts, updatedAt: ts };
|
|
159
|
+
await this.store.put(COLLECTIONS.projects, project.id, project, {
|
|
160
|
+
message: `Create project ${project.name}`,
|
|
161
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
162
|
+
});
|
|
163
|
+
return project;
|
|
164
|
+
}
|
|
165
|
+
async update(id, input, actor) {
|
|
166
|
+
const current = await this.get(id);
|
|
167
|
+
const data = defined(parse(projectInputSchema.partial(), input));
|
|
168
|
+
const next = { ...current, ...data, updatedAt: nowIso() };
|
|
169
|
+
await this.assertReferences(next.organizationId, next.parentProjectId, id);
|
|
170
|
+
await this.store.put(COLLECTIONS.projects, id, next, {
|
|
171
|
+
message: `Update project ${next.name}`,
|
|
172
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
173
|
+
});
|
|
174
|
+
return next;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* With `reparent`, child projects move to this project's parent (or become
|
|
178
|
+
* top-level projects of the organization); specs and role scopes move to the
|
|
179
|
+
* parent project, or are dropped when there is none, since moving them to the
|
|
180
|
+
* organization would widen what they apply to.
|
|
181
|
+
*/
|
|
182
|
+
async delete(id, actor, children = 'refuse') {
|
|
183
|
+
const project = await this.get(id);
|
|
184
|
+
const childProjects = (await this.data.projects()).filter((p) => p.parentProjectId === id);
|
|
185
|
+
const write = (message) => ({ message, ...(actor.author ? { author: actor.author } : {}) });
|
|
186
|
+
if (children === 'refuse') {
|
|
187
|
+
if (childProjects.length > 0)
|
|
188
|
+
throw new ServiceError(409, 'project has child projects');
|
|
189
|
+
}
|
|
190
|
+
else if (children === 'reparent') {
|
|
191
|
+
for (const child of childProjects) {
|
|
192
|
+
await this.store.put(COLLECTIONS.projects, child.id, { ...child, parentProjectId: project.parentProjectId, updatedAt: nowIso() }, write(`Move project ${child.name} up`));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
for (const child of childProjects)
|
|
197
|
+
await this.delete(child.id, actor, 'delete');
|
|
198
|
+
}
|
|
199
|
+
await remapReferences(this.store, 'projectIds', id, children === 'reparent' ? project.parentProjectId : null, actor);
|
|
200
|
+
await this.store.delete(COLLECTIONS.projects, id, write(`Delete project ${project.name}`));
|
|
201
|
+
}
|
|
202
|
+
async assertReferences(organizationId, parentProjectId, selfId) {
|
|
203
|
+
if ((await this.store.get(COLLECTIONS.organizations, organizationId)) === null) {
|
|
204
|
+
throw notFound('organization', organizationId);
|
|
205
|
+
}
|
|
206
|
+
if (parentProjectId === null)
|
|
207
|
+
return;
|
|
208
|
+
if (parentProjectId === selfId)
|
|
209
|
+
throw new ServiceError(400, 'a project cannot be its own parent');
|
|
210
|
+
const byId = new Map((await this.data.projects()).map((p) => [p.id, p]));
|
|
211
|
+
let cursor = parentProjectId;
|
|
212
|
+
const seen = new Set();
|
|
213
|
+
while (cursor !== null) {
|
|
214
|
+
if (cursor === selfId)
|
|
215
|
+
throw new ServiceError(400, 'that parent is inside this project');
|
|
216
|
+
if (seen.has(cursor))
|
|
217
|
+
break;
|
|
218
|
+
seen.add(cursor);
|
|
219
|
+
const parent = byId.get(cursor);
|
|
220
|
+
if (!parent)
|
|
221
|
+
throw notFound('project', cursor);
|
|
222
|
+
cursor = parent.parentProjectId;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Specs and role assignments that reference a deleted organization or project:
|
|
228
|
+
* the reference moves to `replacement`, or goes when there is none. An
|
|
229
|
+
* assignment left with no scope at all is removed, not widened to everywhere.
|
|
230
|
+
*/
|
|
231
|
+
async function remapReferences(store, field, removedId, replacement, actor) {
|
|
232
|
+
const write = (message) => ({ message, ...(actor.author ? { author: actor.author } : {}) });
|
|
233
|
+
const remap = (ids) => {
|
|
234
|
+
const next = ids.filter((x) => x !== removedId);
|
|
235
|
+
if (replacement && ids.includes(removedId) && !next.includes(replacement))
|
|
236
|
+
next.push(replacement);
|
|
237
|
+
return next;
|
|
238
|
+
};
|
|
239
|
+
const specs = (await store.list(COLLECTIONS.specs)).map((s) => specSchema.parse(s));
|
|
240
|
+
for (const spec of specs) {
|
|
241
|
+
if (!spec[field].includes(removedId))
|
|
242
|
+
continue;
|
|
243
|
+
await store.put(COLLECTIONS.specs, spec.id, { ...spec, [field]: remap(spec[field]), updatedAt: nowIso() }, write(`Remap spec ${spec.title}`));
|
|
244
|
+
}
|
|
245
|
+
const assignments = (await store.list(COLLECTIONS.roleAssignments)).map((a) => roleAssignmentSchema.parse(a));
|
|
246
|
+
for (const a of assignments) {
|
|
247
|
+
if (!a[field].includes(removedId))
|
|
248
|
+
continue;
|
|
249
|
+
const next = { ...a, [field]: remap(a[field]), updatedAt: nowIso() };
|
|
250
|
+
if (next.organizationIds.length + next.projectIds.length === 0) {
|
|
251
|
+
await store.delete(COLLECTIONS.roleAssignments, a.id, write(`Remove role assignment of ${a.userId}: its scope is gone`));
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
await store.put(COLLECTIONS.roleAssignments, a.id, next, write(`Remap role assignment of ${a.userId}`));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
export class SpecService {
|
|
259
|
+
store;
|
|
260
|
+
data;
|
|
261
|
+
constructor(store) {
|
|
262
|
+
this.store = store;
|
|
263
|
+
this.data = new DataAccess(store);
|
|
264
|
+
}
|
|
265
|
+
list() {
|
|
266
|
+
return this.data.specs();
|
|
267
|
+
}
|
|
268
|
+
async get(id) {
|
|
269
|
+
const raw = await this.store.get(COLLECTIONS.specs, id);
|
|
270
|
+
if (raw === null)
|
|
271
|
+
throw notFound('spec', id);
|
|
272
|
+
return specSchema.parse(raw);
|
|
273
|
+
}
|
|
274
|
+
async create(input, actor) {
|
|
275
|
+
return (await this.createRevision(input, actor)).spec;
|
|
276
|
+
}
|
|
277
|
+
/** Create, and say which revision the datastore gave it (what the Spec Analyst reviews). */
|
|
278
|
+
async createRevision(input, actor) {
|
|
279
|
+
const data = parse(specInputSchema, input);
|
|
280
|
+
await this.assertMappings(data);
|
|
281
|
+
const ts = nowIso();
|
|
282
|
+
const spec = { id: newId(), ...data, authorUserId: actor.userId, createdAt: ts, updatedAt: ts };
|
|
283
|
+
const revision = await this.store.put(COLLECTIONS.specs, spec.id, spec, {
|
|
284
|
+
message: `Create spec ${spec.title}`,
|
|
285
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
286
|
+
});
|
|
287
|
+
return { spec, revision };
|
|
288
|
+
}
|
|
289
|
+
/** Every update is a new revision; the author of the spec stays its creator. */
|
|
290
|
+
async update(id, input, actor) {
|
|
291
|
+
return (await this.revise(id, input, actor)).spec;
|
|
292
|
+
}
|
|
293
|
+
/** Update, and say which revision the datastore gave it. */
|
|
294
|
+
async revise(id, input, actor) {
|
|
295
|
+
const current = await this.get(id);
|
|
296
|
+
const data = defined(parse(specInputSchema.partial(), input));
|
|
297
|
+
const next = { ...current, ...data, updatedAt: nowIso() };
|
|
298
|
+
await this.assertMappings(next);
|
|
299
|
+
const revision = await this.store.put(COLLECTIONS.specs, id, next, {
|
|
300
|
+
message: `Revise spec ${next.title}`,
|
|
301
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
302
|
+
});
|
|
303
|
+
return { spec: next, revision };
|
|
304
|
+
}
|
|
305
|
+
async delete(id, actor) {
|
|
306
|
+
const spec = await this.get(id);
|
|
307
|
+
await this.store.delete(COLLECTIONS.specs, id, {
|
|
308
|
+
message: `Delete spec ${spec.title}`,
|
|
309
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
/** Revisions newest first, the first one marked current. */
|
|
313
|
+
async revisions(id) {
|
|
314
|
+
await this.get(id);
|
|
315
|
+
const history = await this.data.specHistory(id);
|
|
316
|
+
return history.map((meta, index) => ({ ...meta, current: index === 0 }));
|
|
317
|
+
}
|
|
318
|
+
async atRevision(id, revision) {
|
|
319
|
+
const spec = await this.data.specAtRevision(id, revision);
|
|
320
|
+
if (spec === null)
|
|
321
|
+
throw notFound(`revision ${revision} of spec`, id);
|
|
322
|
+
return spec;
|
|
323
|
+
}
|
|
324
|
+
/** Line diff of the content from one revision to another (`to` defaults to the current spec). */
|
|
325
|
+
async diff(id, from, to) {
|
|
326
|
+
const [before, after] = await Promise.all([this.atRevision(id, from), to ? this.atRevision(id, to) : this.get(id)]);
|
|
327
|
+
return diffLines(before.content, after.content);
|
|
328
|
+
}
|
|
329
|
+
async assertMappings(spec) {
|
|
330
|
+
const targets = [
|
|
331
|
+
['organization', COLLECTIONS.organizations, spec.organizationIds],
|
|
332
|
+
['project', COLLECTIONS.projects, spec.projectIds],
|
|
333
|
+
['role', COLLECTIONS.roles, spec.roleIds],
|
|
334
|
+
['user', COLLECTIONS.users, spec.userIds],
|
|
335
|
+
];
|
|
336
|
+
for (const [what, collection, ids] of targets) {
|
|
337
|
+
for (const id of ids) {
|
|
338
|
+
if ((await this.store.get(collection, id)) === null)
|
|
339
|
+
throw notFound(what, id);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
export class UserService {
|
|
345
|
+
store;
|
|
346
|
+
constructor(store) {
|
|
347
|
+
this.store = store;
|
|
348
|
+
}
|
|
349
|
+
async list() {
|
|
350
|
+
const raw = await this.store.list(COLLECTIONS.users);
|
|
351
|
+
return raw.map((u) => parseUser(u));
|
|
352
|
+
}
|
|
353
|
+
async get(id) {
|
|
354
|
+
const raw = await this.store.get(COLLECTIONS.users, id);
|
|
355
|
+
if (raw === null)
|
|
356
|
+
throw notFound('user', id);
|
|
357
|
+
return parseUser(raw);
|
|
358
|
+
}
|
|
359
|
+
/** A person's own profile fields; credentials and roles are not touched here. */
|
|
360
|
+
async updateProfile(id, input, actor) {
|
|
361
|
+
return this.apply(id, parse(profileInputSchema, input), actor, 'Update profile of');
|
|
362
|
+
}
|
|
363
|
+
/** An admin's edit of a user: the profile fields and `disabled`. */
|
|
364
|
+
async adminUpdate(id, input, actor) {
|
|
365
|
+
return this.apply(id, parse(userAdminInputSchema, input), actor, 'Update user');
|
|
366
|
+
}
|
|
367
|
+
/** A manager must exist, must not be the person, and must not report back to them, however far up. */
|
|
368
|
+
async assertManager(userId, managerId) {
|
|
369
|
+
if (managerId === userId)
|
|
370
|
+
throw new ServiceError(400, 'a person cannot be their own manager');
|
|
371
|
+
const users = await this.list();
|
|
372
|
+
const byId = new Map(users.map((u) => [u.id, u]));
|
|
373
|
+
if (!byId.has(managerId))
|
|
374
|
+
throw notFound('user', managerId);
|
|
375
|
+
const seen = new Set();
|
|
376
|
+
let cursor = managerId;
|
|
377
|
+
while (cursor !== null) {
|
|
378
|
+
if (cursor === userId)
|
|
379
|
+
throw new ServiceError(400, 'that would make the management chain a loop');
|
|
380
|
+
if (seen.has(cursor))
|
|
381
|
+
break;
|
|
382
|
+
seen.add(cursor);
|
|
383
|
+
cursor = byId.get(cursor)?.managerId ?? null;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
async apply(id, patch, actor, verb) {
|
|
387
|
+
const current = await this.get(id);
|
|
388
|
+
const data = defined(patch);
|
|
389
|
+
if (data.email) {
|
|
390
|
+
const clash = (await this.list()).find((u) => u.id !== id && u.email?.toLowerCase() === data.email.toLowerCase());
|
|
391
|
+
if (clash)
|
|
392
|
+
throw new ServiceError(409, `the email ${data.email} belongs to ${fullName(clash)}`);
|
|
393
|
+
data.email = data.email.toLowerCase();
|
|
394
|
+
}
|
|
395
|
+
if (data.organizationId && (await this.store.get(COLLECTIONS.organizations, data.organizationId)) === null) {
|
|
396
|
+
throw notFound('organization', data.organizationId);
|
|
397
|
+
}
|
|
398
|
+
if (data.managerId)
|
|
399
|
+
await this.assertManager(id, data.managerId);
|
|
400
|
+
const next = { ...current, ...data, updatedAt: nowIso() };
|
|
401
|
+
await this.store.put(COLLECTIONS.users, id, next, {
|
|
402
|
+
message: `${verb} ${next.email ?? next.id}`,
|
|
403
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
404
|
+
});
|
|
405
|
+
return next;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
export class RoleService {
|
|
409
|
+
store;
|
|
410
|
+
constructor(store) {
|
|
411
|
+
this.store = store;
|
|
412
|
+
}
|
|
413
|
+
async list() {
|
|
414
|
+
const raw = await this.store.list(COLLECTIONS.roles);
|
|
415
|
+
return raw.map((r) => parseRole(r)).sort((a, b) => b.ranking - a.ranking);
|
|
416
|
+
}
|
|
417
|
+
async get(id) {
|
|
418
|
+
const raw = await this.store.get(COLLECTIONS.roles, id);
|
|
419
|
+
if (raw === null)
|
|
420
|
+
throw notFound('role', id);
|
|
421
|
+
return parseRole(raw);
|
|
422
|
+
}
|
|
423
|
+
/** A new role joins at the bottom of the order: one step below the lowest. */
|
|
424
|
+
async create(input, actor) {
|
|
425
|
+
const data = parse(roleInputSchema, input);
|
|
426
|
+
await this.assertNameFree(data.name, null);
|
|
427
|
+
const existing = await this.list();
|
|
428
|
+
const lowest = existing.length > 0 ? Math.min(...existing.map((r) => r.ranking)) : RANKING_STEP;
|
|
429
|
+
const ts = nowIso();
|
|
430
|
+
const role = { id: newId(), ...data, ranking: lowest - RANKING_STEP, createdAt: ts, updatedAt: ts };
|
|
431
|
+
await this.store.put(COLLECTIONS.roles, role.id, role, {
|
|
432
|
+
message: `Create role ${role.name}`,
|
|
433
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
434
|
+
});
|
|
435
|
+
// Keep every ranking a positive multiple of the step, strongest first.
|
|
436
|
+
const ordered = await this.reorder([...existing.map((r) => r.id), role.id], actor);
|
|
437
|
+
return ordered.find((r) => r.id === role.id) ?? role;
|
|
438
|
+
}
|
|
439
|
+
async update(id, input, actor) {
|
|
440
|
+
const current = await this.get(id);
|
|
441
|
+
const data = defined(parse(roleInputSchema.partial(), input));
|
|
442
|
+
if (data.name !== undefined)
|
|
443
|
+
await this.assertNameFree(data.name, id);
|
|
444
|
+
const next = { ...current, ...data, updatedAt: nowIso() };
|
|
445
|
+
await this.store.put(COLLECTIONS.roles, id, next, {
|
|
446
|
+
message: `Update role ${next.name}`,
|
|
447
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
448
|
+
});
|
|
449
|
+
return next;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Set the precedence from an ordering of every role, strongest first; the
|
|
453
|
+
* rankings become multiples of RANKING_STEP from the top. Every role must
|
|
454
|
+
* appear exactly once.
|
|
455
|
+
*/
|
|
456
|
+
async reorder(ids, actor) {
|
|
457
|
+
const roles = await this.list();
|
|
458
|
+
const known = new Set(roles.map((r) => r.id));
|
|
459
|
+
if (ids.length !== roles.length || new Set(ids).size !== ids.length || ids.some((id) => !known.has(id))) {
|
|
460
|
+
throw new ServiceError(400, 'the order must list every role exactly once');
|
|
461
|
+
}
|
|
462
|
+
const byId = new Map(roles.map((r) => [r.id, r]));
|
|
463
|
+
const out = [];
|
|
464
|
+
for (const [index, id] of ids.entries()) {
|
|
465
|
+
const role = byId.get(id);
|
|
466
|
+
const ranking = (ids.length - index) * RANKING_STEP;
|
|
467
|
+
if (role.ranking !== ranking) {
|
|
468
|
+
const next = { ...role, ranking, updatedAt: nowIso() };
|
|
469
|
+
await this.store.put(COLLECTIONS.roles, id, next, {
|
|
470
|
+
message: `Rank role ${role.name} ${index + 1} of ${ids.length}`,
|
|
471
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
472
|
+
});
|
|
473
|
+
out.push(next);
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
out.push(role);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return out;
|
|
480
|
+
}
|
|
481
|
+
/** Only a role nobody holds can go; the package's agents stay (install would seed them again). */
|
|
482
|
+
async delete(id, actor) {
|
|
483
|
+
const role = await this.get(id);
|
|
484
|
+
if (role.agentKey)
|
|
485
|
+
throw new ServiceError(409, `${role.name} is one of aigile's agents and cannot be deleted`);
|
|
486
|
+
const assignments = (await this.store.list(COLLECTIONS.roleAssignments)).map((a) => roleAssignmentSchema.parse(a));
|
|
487
|
+
if (assignments.some((a) => a.roleId === id))
|
|
488
|
+
throw new ServiceError(409, `role ${role.name} is still assigned`);
|
|
489
|
+
await this.store.delete(COLLECTIONS.roles, id, {
|
|
490
|
+
message: `Delete role ${role.name}`,
|
|
491
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
async assertNameFree(name, selfId) {
|
|
495
|
+
const clash = (await this.list()).find((r) => r.name.toLowerCase() === name.toLowerCase() && r.id !== selfId);
|
|
496
|
+
if (clash)
|
|
497
|
+
throw new ServiceError(409, `a role named ${clash.name} already exists`);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
export class RoleAssignmentService {
|
|
501
|
+
store;
|
|
502
|
+
constructor(store) {
|
|
503
|
+
this.store = store;
|
|
504
|
+
}
|
|
505
|
+
async list() {
|
|
506
|
+
const raw = await this.store.list(COLLECTIONS.roleAssignments);
|
|
507
|
+
return raw.map((a) => roleAssignmentSchema.parse(a));
|
|
508
|
+
}
|
|
509
|
+
async get(id) {
|
|
510
|
+
const raw = await this.store.get(COLLECTIONS.roleAssignments, id);
|
|
511
|
+
if (raw === null)
|
|
512
|
+
throw notFound('role assignment', id);
|
|
513
|
+
return roleAssignmentSchema.parse(raw);
|
|
514
|
+
}
|
|
515
|
+
async create(input, actor) {
|
|
516
|
+
const data = parse(roleAssignmentInputSchema, input);
|
|
517
|
+
await this.assertReferences(data);
|
|
518
|
+
const ts = nowIso();
|
|
519
|
+
const assignment = { id: newId(), ...data, createdAt: ts, updatedAt: ts };
|
|
520
|
+
await this.store.put(COLLECTIONS.roleAssignments, assignment.id, assignment, {
|
|
521
|
+
message: `Assign role ${data.roleId} to ${data.userId}`,
|
|
522
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
523
|
+
});
|
|
524
|
+
return assignment;
|
|
525
|
+
}
|
|
526
|
+
async update(id, input, actor) {
|
|
527
|
+
const current = await this.get(id);
|
|
528
|
+
const data = defined(parse(roleAssignmentInputSchema.partial(), input));
|
|
529
|
+
const next = { ...current, ...data, updatedAt: nowIso() };
|
|
530
|
+
await this.assertReferences(next);
|
|
531
|
+
await this.store.put(COLLECTIONS.roleAssignments, id, next, {
|
|
532
|
+
message: `Update role assignment of ${next.userId}`,
|
|
533
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
534
|
+
});
|
|
535
|
+
return next;
|
|
536
|
+
}
|
|
537
|
+
async delete(id, actor) {
|
|
538
|
+
const assignment = await this.get(id);
|
|
539
|
+
await this.store.delete(COLLECTIONS.roleAssignments, id, {
|
|
540
|
+
message: `Remove role assignment of ${assignment.userId}`,
|
|
541
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
async assertReferences(a) {
|
|
545
|
+
if ((await this.store.get(COLLECTIONS.users, a.userId)) === null)
|
|
546
|
+
throw notFound('user', a.userId);
|
|
547
|
+
if ((await this.store.get(COLLECTIONS.roles, a.roleId)) === null)
|
|
548
|
+
throw notFound('role', a.roleId);
|
|
549
|
+
for (const id of a.organizationIds) {
|
|
550
|
+
if ((await this.store.get(COLLECTIONS.organizations, id)) === null)
|
|
551
|
+
throw notFound('organization', id);
|
|
552
|
+
}
|
|
553
|
+
for (const id of a.projectIds) {
|
|
554
|
+
if ((await this.store.get(COLLECTIONS.projects, id)) === null)
|
|
555
|
+
throw notFound('project', id);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Conflicts between spec revisions: opened by the Spec Analyst (or a person),
|
|
561
|
+
* discussed in messages, and resolved or deferred by a person. While one is
|
|
562
|
+
* active the resolver skips the revisions it names.
|
|
563
|
+
*/
|
|
564
|
+
export class ConflictService {
|
|
565
|
+
store;
|
|
566
|
+
data;
|
|
567
|
+
constructor(store) {
|
|
568
|
+
this.store = store;
|
|
569
|
+
this.data = new DataAccess(store);
|
|
570
|
+
}
|
|
571
|
+
list() {
|
|
572
|
+
return this.data.conflicts();
|
|
573
|
+
}
|
|
574
|
+
async get(id) {
|
|
575
|
+
const raw = await this.store.get(COLLECTIONS.conflicts, id);
|
|
576
|
+
if (raw === null)
|
|
577
|
+
throw notFound('conflict', id);
|
|
578
|
+
return conflictSchema.parse(raw);
|
|
579
|
+
}
|
|
580
|
+
/** The active conflict between two specs, whichever side each is on, if any. */
|
|
581
|
+
async activeBetween(specA, specB) {
|
|
582
|
+
const pair = new Set([specA, specB]);
|
|
583
|
+
return ((await this.list()).find((c) => c.status === 'active' && pair.has(c.left.specId) && pair.has(c.right.specId) && c.left.specId !== c.right.specId) ?? null);
|
|
584
|
+
}
|
|
585
|
+
async open(input, actor) {
|
|
586
|
+
if (input.left.specId === input.right.specId)
|
|
587
|
+
throw new ServiceError(400, 'a conflict needs two different specs');
|
|
588
|
+
for (const side of [input.left, input.right]) {
|
|
589
|
+
if ((await this.store.get(COLLECTIONS.specs, side.specId)) === null)
|
|
590
|
+
throw notFound('spec', side.specId);
|
|
591
|
+
}
|
|
592
|
+
const ts = nowIso();
|
|
593
|
+
const conflict = { id: newId(), status: 'active', left: input.left, right: input.right, resolutions: [], detectedBy: input.detectedBy ?? 'user', createdAt: ts, updatedAt: ts };
|
|
594
|
+
await this.store.put(COLLECTIONS.conflicts, conflict.id, conflict, {
|
|
595
|
+
message: `Open conflict between specs ${input.left.specId} and ${input.right.specId}`,
|
|
596
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
597
|
+
});
|
|
598
|
+
return conflict;
|
|
599
|
+
}
|
|
600
|
+
async setStatus(id, status, actor) {
|
|
601
|
+
const current = await this.get(id);
|
|
602
|
+
const next = { ...current, status: parse(conflictStatusSchema, status), updatedAt: nowIso() };
|
|
603
|
+
await this.store.put(COLLECTIONS.conflicts, id, next, {
|
|
604
|
+
message: `Conflict ${id} ${next.status}`,
|
|
605
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
606
|
+
});
|
|
607
|
+
return next;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
/** The discussion on a conflict; people and agents both post. */
|
|
611
|
+
export class MessageService {
|
|
612
|
+
store;
|
|
613
|
+
data;
|
|
614
|
+
constructor(store) {
|
|
615
|
+
this.store = store;
|
|
616
|
+
this.data = new DataAccess(store);
|
|
617
|
+
}
|
|
618
|
+
async forConflict(conflictId) {
|
|
619
|
+
return (await this.data.messages()).filter((m) => m.conflictId === conflictId).sort((a, b) => a.timestamp.localeCompare(b.timestamp));
|
|
620
|
+
}
|
|
621
|
+
async post(conflictId, body, author, actor) {
|
|
622
|
+
const text = parse(z.string().trim().min(1), body);
|
|
623
|
+
if ((await this.store.get(COLLECTIONS.conflicts, conflictId)) === null)
|
|
624
|
+
throw notFound('conflict', conflictId);
|
|
625
|
+
const ts = nowIso();
|
|
626
|
+
const message = { id: newId(), conflictId, timestamp: ts, body: text, author, createdAt: ts, updatedAt: ts };
|
|
627
|
+
await this.store.put(COLLECTIONS.messages, message.id, messageSchema.parse(message), {
|
|
628
|
+
message: `Message on conflict ${conflictId}`,
|
|
629
|
+
...(actor.author ? { author: actor.author } : {}),
|
|
630
|
+
});
|
|
631
|
+
return message;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
/** Who administers the installation. */
|
|
635
|
+
export class AuthzService {
|
|
636
|
+
store;
|
|
637
|
+
constructor(store) {
|
|
638
|
+
this.store = store;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* An admin holds a role whose application role is owner or admin; the
|
|
642
|
+
* ranking (spec precedence) and a ranking override play no part. While
|
|
643
|
+
* nobody holds such a role, everyone is: a fresh installation has to be set
|
|
644
|
+
* up by somebody, and losing the last admin must not lock everyone out.
|
|
645
|
+
*/
|
|
646
|
+
async isAdmin(userId) {
|
|
647
|
+
const admins = await this.adminUserIds();
|
|
648
|
+
return admins.size === 0 || admins.has(userId);
|
|
649
|
+
}
|
|
650
|
+
/** True while the bootstrap rule is in force: nobody holds an owner or admin application role. */
|
|
651
|
+
async isBootstrap() {
|
|
652
|
+
return (await this.adminUserIds()).size === 0;
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Who decides for the agents (approve, reject, start a build, go or roll
|
|
656
|
+
* back a release): an owner, admin or maintainer. Under the bootstrap rule
|
|
657
|
+
* everyone decides, as everyone administers.
|
|
658
|
+
*/
|
|
659
|
+
async canDecide(userId) {
|
|
660
|
+
if (await this.isBootstrap())
|
|
661
|
+
return true;
|
|
662
|
+
return (await this.userIdsWith(DECIDING_APP_ROLES)).has(userId);
|
|
663
|
+
}
|
|
664
|
+
adminUserIds() {
|
|
665
|
+
return this.userIdsWith(ADMIN_APP_ROLES);
|
|
666
|
+
}
|
|
667
|
+
async userIdsWith(appRoles) {
|
|
668
|
+
const assignments = (await this.store.list(COLLECTIONS.roleAssignments)).map((a) => roleAssignmentSchema.parse(a));
|
|
669
|
+
const roles = (await this.store.list(COLLECTIONS.roles)).map((r) => parseRole(r));
|
|
670
|
+
const appRoleOf = new Map(roles.map((r) => [r.id, r.appRole]));
|
|
671
|
+
return new Set(assignments.filter((a) => appRoles.includes(appRoleOf.get(a.roleId) ?? 'viewer')).map((a) => a.userId));
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
//# sourceMappingURL=services.js.map
|