@app-vitals/agent-admin 1.31.0 → 1.33.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/openapi.platform.json +498 -1
- package/package.json +2 -2
- package/src/admin-boot.ts +14 -4
- package/src/agents-api.smoke.test.ts +546 -3
- package/src/agents-api.ts +312 -3
- package/src/github-reconcile-routes.integration.test.ts +6 -2
- package/src/github-reconcile-wiring.ts +45 -18
- package/src/github-reconcile-wiring.unit.test.ts +45 -21
- package/src/installation-repos-service.ts +237 -0
- package/src/installation-repos-service.unit.test.ts +270 -0
- package/src/reconcile-apply-service.integration.test.ts +11 -3
- package/src/reconcile-apply-service.ts +88 -22
- package/src/reconcile-apply-service.unit.test.ts +70 -24
- package/src/repo-removal-open-work.ts +59 -0
- package/src/repo-removal-open-work.unit.test.ts +81 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/installation-repos-service.ts
|
|
3
|
+
* Direct management of the repos on an agent's own GitHub App installation
|
|
4
|
+
* (IRM-1.2): list, add and remove without a reconcile plan step. Uses the
|
|
5
|
+
* installer App through the reconcile installation port, and writes every
|
|
6
|
+
* add/remove to the existing GitHubReconcileAudit table (no schema change):
|
|
7
|
+
* `planHash` is "direct:add" / "direct:remove" and the changed repos land in
|
|
8
|
+
* the diff's installationAdds / installationRemoves.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
12
|
+
import {
|
|
13
|
+
AppNotInstalledError,
|
|
14
|
+
MISSING_APP_ID_REASON,
|
|
15
|
+
type ReconcileAgentsPort,
|
|
16
|
+
type ReconcileAuditPort,
|
|
17
|
+
type ReconcileInstallationPort,
|
|
18
|
+
} from "./reconcile-apply-service.ts";
|
|
19
|
+
import { repoOrg } from "./reconcile-planner.ts";
|
|
20
|
+
import type {
|
|
21
|
+
OrgAccessResult,
|
|
22
|
+
UserRepoAccessResult,
|
|
23
|
+
} from "./user-repo-access-resolver.ts";
|
|
24
|
+
|
|
25
|
+
/** Installation port plus discovery of the orgs the agent App is installed on. */
|
|
26
|
+
export interface InstallationReposPort extends ReconcileInstallationPort {
|
|
27
|
+
listInstalled(appId: number): Promise<{ org: string; repos: string[] }[]>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface InstallationRepoView {
|
|
31
|
+
fullName: string;
|
|
32
|
+
/** Present only when the bound user's access to this repo is known. */
|
|
33
|
+
userCanPush?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface InstallationOrgView {
|
|
37
|
+
org: string;
|
|
38
|
+
repos: InstallationRepoView[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type InstallationListResult =
|
|
42
|
+
| { status: "agent_not_found" }
|
|
43
|
+
| { status: "no_app_id"; reason: string }
|
|
44
|
+
| { status: "ok"; appId: number; orgs: InstallationOrgView[] };
|
|
45
|
+
|
|
46
|
+
export type InstallationChangeResult =
|
|
47
|
+
| { status: "agent_not_found" }
|
|
48
|
+
| { status: "no_app_id"; reason: string }
|
|
49
|
+
| { status: "not_installed"; org: string; reason: string }
|
|
50
|
+
| { status: "applied"; auditId: string; repos: string[] }
|
|
51
|
+
| { status: "failed"; auditId: string; error: string };
|
|
52
|
+
|
|
53
|
+
export interface InstallationReposDeps {
|
|
54
|
+
agents: Pick<ReconcileAgentsPort, "get">;
|
|
55
|
+
access: GithubAccessPreviewFn;
|
|
56
|
+
installation: InstallationReposPort;
|
|
57
|
+
audit: ReconcileAuditPort;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const orgKey = (org: string): string => org.toLowerCase();
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The user-can-push hint for one org: true/false per repo only when the bound
|
|
64
|
+
* user is verified and this org's lookup result is complete ("ok", nothing
|
|
65
|
+
* unresolved). A repo the lookup did not list is unknown, so it gets no hint
|
|
66
|
+
* (never false).
|
|
67
|
+
*/
|
|
68
|
+
export function userPushHints(
|
|
69
|
+
access: UserRepoAccessResult | undefined,
|
|
70
|
+
org: string,
|
|
71
|
+
repos: readonly string[],
|
|
72
|
+
): InstallationRepoView[] {
|
|
73
|
+
const orgResult: OrgAccessResult | undefined = access?.orgs.find(
|
|
74
|
+
(o) => orgKey(o.org) === orgKey(org),
|
|
75
|
+
);
|
|
76
|
+
const complete =
|
|
77
|
+
access !== undefined &&
|
|
78
|
+
access.outcome === "ok" &&
|
|
79
|
+
access.userVerified &&
|
|
80
|
+
orgResult !== undefined &&
|
|
81
|
+
orgResult.status === "ok" &&
|
|
82
|
+
orgResult.unresolved === 0;
|
|
83
|
+
const pushByName = new Map<string, boolean>();
|
|
84
|
+
if (complete) {
|
|
85
|
+
for (const r of orgResult.repos) {
|
|
86
|
+
pushByName.set(r.fullName.toLowerCase(), r.canPush);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return repos.map((fullName) => {
|
|
90
|
+
const hint = pushByName.get(fullName.toLowerCase());
|
|
91
|
+
return hint === undefined ? { fullName } : { fullName, userCanPush: hint };
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function groupByOrg(repos: readonly string[]): Map<string, string[]> {
|
|
96
|
+
const byOrg = new Map<string, string[]>();
|
|
97
|
+
for (const r of repos) {
|
|
98
|
+
const org = repoOrg(r);
|
|
99
|
+
byOrg.set(org, [...(byOrg.get(org) ?? []), r]);
|
|
100
|
+
}
|
|
101
|
+
return byOrg;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export class InstallationReposService {
|
|
105
|
+
constructor(private readonly deps: InstallationReposDeps) {}
|
|
106
|
+
|
|
107
|
+
async list(agentId: string): Promise<InstallationListResult> {
|
|
108
|
+
const agent = await this.deps.agents.get(agentId);
|
|
109
|
+
if (!agent) return { status: "agent_not_found" };
|
|
110
|
+
if (agent.appId === undefined) {
|
|
111
|
+
return { status: "no_app_id", reason: MISSING_APP_ID_REASON };
|
|
112
|
+
}
|
|
113
|
+
const installed = await this.deps.installation.listInstalled(agent.appId);
|
|
114
|
+
const username = agent.githubUsername?.trim();
|
|
115
|
+
let access: UserRepoAccessResult | undefined;
|
|
116
|
+
if (username) {
|
|
117
|
+
try {
|
|
118
|
+
const preview = await this.deps.access(username);
|
|
119
|
+
if (preview.status === "ok") access = preview.access;
|
|
120
|
+
} catch {
|
|
121
|
+
// The hint is advisory: a failed lookup omits it.
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
status: "ok",
|
|
126
|
+
appId: agent.appId,
|
|
127
|
+
orgs: installed.map(({ org, repos }) => ({
|
|
128
|
+
org,
|
|
129
|
+
repos: userPushHints(access, org, repos),
|
|
130
|
+
})),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
add(
|
|
135
|
+
agentId: string,
|
|
136
|
+
caller: string,
|
|
137
|
+
repos: readonly string[],
|
|
138
|
+
): Promise<InstallationChangeResult> {
|
|
139
|
+
return this.change("add", agentId, caller, repos);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
remove(
|
|
143
|
+
agentId: string,
|
|
144
|
+
caller: string,
|
|
145
|
+
repos: readonly string[],
|
|
146
|
+
): Promise<InstallationChangeResult> {
|
|
147
|
+
return this.change("remove", agentId, caller, repos);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private async change(
|
|
151
|
+
kind: "add" | "remove",
|
|
152
|
+
agentId: string,
|
|
153
|
+
caller: string,
|
|
154
|
+
input: readonly string[],
|
|
155
|
+
): Promise<InstallationChangeResult> {
|
|
156
|
+
const agent = await this.deps.agents.get(agentId);
|
|
157
|
+
if (!agent) return { status: "agent_not_found" };
|
|
158
|
+
if (agent.appId === undefined) {
|
|
159
|
+
return { status: "no_app_id", reason: MISSING_APP_ID_REASON };
|
|
160
|
+
}
|
|
161
|
+
const appId = agent.appId;
|
|
162
|
+
const repos = [...new Set(input)];
|
|
163
|
+
const byOrg = groupByOrg(repos);
|
|
164
|
+
const { installation } = this.deps;
|
|
165
|
+
// Nothing is written (or audited) when an org has no installation.
|
|
166
|
+
try {
|
|
167
|
+
await installation.listRepos(appId, [...byOrg.keys()]);
|
|
168
|
+
} catch (err) {
|
|
169
|
+
if (err instanceof AppNotInstalledError) {
|
|
170
|
+
return { status: "not_installed", org: err.org, reason: err.message };
|
|
171
|
+
}
|
|
172
|
+
throw err;
|
|
173
|
+
}
|
|
174
|
+
const changed: string[] = [];
|
|
175
|
+
let error: string | undefined;
|
|
176
|
+
try {
|
|
177
|
+
for (const [org, orgRepos] of byOrg) {
|
|
178
|
+
if (kind === "add") await installation.addRepos(appId, org, orgRepos);
|
|
179
|
+
else await installation.removeRepos(appId, org, orgRepos);
|
|
180
|
+
changed.push(...orgRepos);
|
|
181
|
+
}
|
|
182
|
+
} catch (err) {
|
|
183
|
+
error = err instanceof Error ? err.message : String(err);
|
|
184
|
+
}
|
|
185
|
+
// Record what actually changed; on failure, only the repos that landed.
|
|
186
|
+
const done = error === undefined ? repos : changed;
|
|
187
|
+
const auditId = await this.deps.audit.write({
|
|
188
|
+
agentId,
|
|
189
|
+
caller,
|
|
190
|
+
planHash: `direct:${kind}`,
|
|
191
|
+
diff: {
|
|
192
|
+
installationAdds: kind === "add" ? done : [],
|
|
193
|
+
installationRemoves: kind === "remove" ? done : [],
|
|
194
|
+
configAdds: [],
|
|
195
|
+
configRemoves: [],
|
|
196
|
+
},
|
|
197
|
+
result:
|
|
198
|
+
error === undefined
|
|
199
|
+
? { status: "applied" }
|
|
200
|
+
: { status: "failed", error },
|
|
201
|
+
});
|
|
202
|
+
return error === undefined
|
|
203
|
+
? { status: "applied", auditId, repos }
|
|
204
|
+
: { status: "failed", auditId, error };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** The slice of the installer client the installation-repos port needs. */
|
|
209
|
+
export interface InstalledOrgsClient {
|
|
210
|
+
listInstallableOrganizations(): Promise<{ login: string }[]>;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Port over the reconcile installation port plus org discovery: the orgs the
|
|
215
|
+
* installer can reach that carry an installation of the agent's App.
|
|
216
|
+
*/
|
|
217
|
+
export function createInstallationReposPort(
|
|
218
|
+
base: ReconcileInstallationPort,
|
|
219
|
+
client: InstalledOrgsClient,
|
|
220
|
+
): InstallationReposPort {
|
|
221
|
+
return {
|
|
222
|
+
...base,
|
|
223
|
+
async listInstalled(appId) {
|
|
224
|
+
const out: { org: string; repos: string[] }[] = [];
|
|
225
|
+
for (const { login } of await client.listInstallableOrganizations()) {
|
|
226
|
+
try {
|
|
227
|
+
const repos = await base.listRepos(appId, [login]);
|
|
228
|
+
out.push({ org: login, repos: repos[login] ?? [] });
|
|
229
|
+
} catch (err) {
|
|
230
|
+
if (err instanceof AppNotInstalledError) continue;
|
|
231
|
+
throw err;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return out;
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/installation-repos-service.unit.test.ts
|
|
3
|
+
* IRM-1.2: the user-can-push hint logic and the list/add/remove service, with
|
|
4
|
+
* in-memory ports.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import {
|
|
9
|
+
InstallationReposService,
|
|
10
|
+
type InstallationReposPort,
|
|
11
|
+
userPushHints,
|
|
12
|
+
} from "./installation-repos-service.ts";
|
|
13
|
+
import {
|
|
14
|
+
AppNotInstalledError,
|
|
15
|
+
type ReconcileAuditEntry,
|
|
16
|
+
} from "./reconcile-apply-service.ts";
|
|
17
|
+
import type {
|
|
18
|
+
OrgAccessResult,
|
|
19
|
+
UserRepoAccessResult,
|
|
20
|
+
} from "./user-repo-access-resolver.ts";
|
|
21
|
+
|
|
22
|
+
const orgResult = (over: Partial<OrgAccessResult> = {}): OrgAccessResult => ({
|
|
23
|
+
org: "acme",
|
|
24
|
+
status: "ok",
|
|
25
|
+
unresolved: 0,
|
|
26
|
+
repos: [
|
|
27
|
+
{ fullName: "acme/push", level: "write", canPush: true },
|
|
28
|
+
{ fullName: "acme/readonly", level: "read", canPush: false },
|
|
29
|
+
],
|
|
30
|
+
...over,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const accessOf = (
|
|
34
|
+
orgs: OrgAccessResult[],
|
|
35
|
+
over: Partial<UserRepoAccessResult> = {},
|
|
36
|
+
): UserRepoAccessResult => ({
|
|
37
|
+
username: "dev",
|
|
38
|
+
outcome: "ok",
|
|
39
|
+
userVerified: true,
|
|
40
|
+
orgs,
|
|
41
|
+
unresolved: 0,
|
|
42
|
+
complete: true,
|
|
43
|
+
checkedAt: "2026-11-11T00:00:00.000Z",
|
|
44
|
+
...over,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("userPushHints", () => {
|
|
48
|
+
const repos = ["acme/push", "acme/readonly", "acme/unlisted"];
|
|
49
|
+
|
|
50
|
+
it("reports true/false for listed repos and omits unlisted ones", () => {
|
|
51
|
+
expect(userPushHints(accessOf([orgResult()]), "acme", repos)).toEqual([
|
|
52
|
+
{ fullName: "acme/push", userCanPush: true },
|
|
53
|
+
{ fullName: "acme/readonly", userCanPush: false },
|
|
54
|
+
{ fullName: "acme/unlisted" },
|
|
55
|
+
]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("matches org and repo names case-insensitively", () => {
|
|
59
|
+
expect(
|
|
60
|
+
userPushHints(accessOf([orgResult()]), "ACME", ["Acme/Push"]),
|
|
61
|
+
).toEqual([{ fullName: "Acme/Push", userCanPush: true }]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("omits every hint when there is no access result", () => {
|
|
65
|
+
expect(userPushHints(undefined, "acme", repos).every(noHint)).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("omits every hint when the org's result is partial, errored, unresolved or missing", () => {
|
|
69
|
+
for (const o of [
|
|
70
|
+
orgResult({ status: "partial" }),
|
|
71
|
+
orgResult({ status: "error" }),
|
|
72
|
+
orgResult({ status: "rate_limited" }),
|
|
73
|
+
orgResult({ status: "lookup_app_not_installed" }),
|
|
74
|
+
orgResult({ unresolved: 2 }),
|
|
75
|
+
]) {
|
|
76
|
+
expect(userPushHints(accessOf([o]), "acme", repos).every(noHint)).toBe(
|
|
77
|
+
true,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
expect(
|
|
81
|
+
userPushHints(accessOf([orgResult()]), "other", repos).every(noHint),
|
|
82
|
+
).toBe(true);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("omits hints for an unverified or missing user", () => {
|
|
86
|
+
expect(
|
|
87
|
+
userPushHints(
|
|
88
|
+
accessOf([orgResult()], { userVerified: false }),
|
|
89
|
+
"acme",
|
|
90
|
+
repos,
|
|
91
|
+
).every(noHint),
|
|
92
|
+
).toBe(true);
|
|
93
|
+
expect(
|
|
94
|
+
userPushHints(
|
|
95
|
+
accessOf([orgResult()], { outcome: "user_not_found" }),
|
|
96
|
+
"acme",
|
|
97
|
+
repos,
|
|
98
|
+
).every(noHint),
|
|
99
|
+
).toBe(true);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
function noHint(r: { userCanPush?: boolean }): boolean {
|
|
104
|
+
return r.userCanPush === undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function build(
|
|
108
|
+
opts: {
|
|
109
|
+
agent?: { githubUsername: string | null; appId?: number } | null;
|
|
110
|
+
access?: () => Promise<
|
|
111
|
+
| { status: "unconfigured" }
|
|
112
|
+
| { status: "ok"; access: UserRepoAccessResult }
|
|
113
|
+
>;
|
|
114
|
+
failAdd?: boolean;
|
|
115
|
+
notInstalled?: string;
|
|
116
|
+
} = {},
|
|
117
|
+
) {
|
|
118
|
+
const audits: ReconcileAuditEntry[] = [];
|
|
119
|
+
const calls: string[] = [];
|
|
120
|
+
const installation: InstallationReposPort = {
|
|
121
|
+
listInstalled: async () => [
|
|
122
|
+
{ org: "acme", repos: ["acme/push", "acme/unlisted"] },
|
|
123
|
+
],
|
|
124
|
+
listRepos: async (appId, orgs) => {
|
|
125
|
+
if (opts.notInstalled && orgs.includes(opts.notInstalled)) {
|
|
126
|
+
throw new AppNotInstalledError(appId, opts.notInstalled);
|
|
127
|
+
}
|
|
128
|
+
return {};
|
|
129
|
+
},
|
|
130
|
+
addRepos: async (_a, org, repos) => {
|
|
131
|
+
if (opts.failAdd && org === "beta") throw new Error("boom");
|
|
132
|
+
calls.push(`add ${org} ${repos.join(",")}`);
|
|
133
|
+
},
|
|
134
|
+
removeRepos: async (_a, org, repos) => {
|
|
135
|
+
calls.push(`remove ${org} ${repos.join(",")}`);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
const agent =
|
|
139
|
+
opts.agent === undefined
|
|
140
|
+
? { id: "a1", repos: [], githubUsername: "dev", appId: 42 }
|
|
141
|
+
: opts.agent && { id: "a1", repos: [], ...opts.agent };
|
|
142
|
+
const svc = new InstallationReposService({
|
|
143
|
+
agents: { get: async () => agent },
|
|
144
|
+
access:
|
|
145
|
+
opts.access ??
|
|
146
|
+
(async () => ({ status: "ok", access: accessOf([orgResult()]) })),
|
|
147
|
+
installation,
|
|
148
|
+
audit: {
|
|
149
|
+
write: async (e) => {
|
|
150
|
+
audits.push(e);
|
|
151
|
+
return `audit-${audits.length}`;
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
return { svc, audits, calls };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
describe("InstallationReposService.list", () => {
|
|
159
|
+
it("returns repos per org with hints for a bound user", async () => {
|
|
160
|
+
const { svc } = build();
|
|
161
|
+
expect(await svc.list("a1")).toEqual({
|
|
162
|
+
status: "ok",
|
|
163
|
+
appId: 42,
|
|
164
|
+
orgs: [
|
|
165
|
+
{
|
|
166
|
+
org: "acme",
|
|
167
|
+
repos: [
|
|
168
|
+
{ fullName: "acme/push", userCanPush: true },
|
|
169
|
+
{ fullName: "acme/unlisted" },
|
|
170
|
+
],
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("omits hints for an unbound agent without calling access", async () => {
|
|
177
|
+
let called = false;
|
|
178
|
+
const { svc } = build({
|
|
179
|
+
agent: { githubUsername: null, appId: 42 },
|
|
180
|
+
access: async () => {
|
|
181
|
+
called = true;
|
|
182
|
+
return { status: "unconfigured" };
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
const r = await svc.list("a1");
|
|
186
|
+
expect(called).toBe(false);
|
|
187
|
+
expect(r.status === "ok" && r.orgs[0]?.repos.every(noHint)).toBe(true);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("omits hints when access is unconfigured or the lookup throws", async () => {
|
|
191
|
+
for (const access of [
|
|
192
|
+
async () => ({ status: "unconfigured" as const }),
|
|
193
|
+
async () => {
|
|
194
|
+
throw new Error("502");
|
|
195
|
+
},
|
|
196
|
+
]) {
|
|
197
|
+
const r = await build({ access }).svc.list("a1");
|
|
198
|
+
expect(r.status === "ok" && r.orgs[0]?.repos.every(noHint)).toBe(true);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("reports unknown agent and a missing GH_APP_ID distinctly", async () => {
|
|
203
|
+
expect(await build({ agent: null }).svc.list("x")).toEqual({
|
|
204
|
+
status: "agent_not_found",
|
|
205
|
+
});
|
|
206
|
+
const r = await build({ agent: { githubUsername: "dev" } }).svc.list("a1");
|
|
207
|
+
expect(r.status).toBe("no_app_id");
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
describe("InstallationReposService.add/remove", () => {
|
|
212
|
+
it("adds per org, dedupes, and writes one audit entry with the caller", async () => {
|
|
213
|
+
const { svc, audits, calls } = build();
|
|
214
|
+
const r = await svc.add("a1", "root", ["acme/a", "acme/a", "beta/b"]);
|
|
215
|
+
expect(r).toEqual({
|
|
216
|
+
status: "applied",
|
|
217
|
+
auditId: "audit-1",
|
|
218
|
+
repos: ["acme/a", "beta/b"],
|
|
219
|
+
});
|
|
220
|
+
expect(calls).toEqual(["add acme acme/a", "add beta beta/b"]);
|
|
221
|
+
expect(audits).toHaveLength(1);
|
|
222
|
+
expect(audits[0]).toMatchObject({
|
|
223
|
+
agentId: "a1",
|
|
224
|
+
caller: "root",
|
|
225
|
+
planHash: "direct:add",
|
|
226
|
+
diff: {
|
|
227
|
+
installationAdds: ["acme/a", "beta/b"],
|
|
228
|
+
installationRemoves: [],
|
|
229
|
+
configAdds: [],
|
|
230
|
+
configRemoves: [],
|
|
231
|
+
},
|
|
232
|
+
result: { status: "applied" },
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("remove records installationRemoves", async () => {
|
|
237
|
+
const { svc, audits } = build();
|
|
238
|
+
await svc.remove("a1", "root", ["acme/a"]);
|
|
239
|
+
expect(audits[0]?.diff.installationRemoves).toEqual(["acme/a"]);
|
|
240
|
+
expect(audits[0]?.planHash).toBe("direct:remove");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("not installed: distinct status, nothing changed or audited", async () => {
|
|
244
|
+
const { svc, audits, calls } = build({ notInstalled: "beta" });
|
|
245
|
+
const r = await svc.add("a1", "root", ["acme/a", "beta/b"]);
|
|
246
|
+
expect(r).toMatchObject({ status: "not_installed", org: "beta" });
|
|
247
|
+
expect(calls).toEqual([]);
|
|
248
|
+
expect(audits).toEqual([]);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("a mid-way GitHub failure audits a failed entry with only the repos that landed", async () => {
|
|
252
|
+
const { svc, audits } = build({ failAdd: true });
|
|
253
|
+
const r = await svc.add("a1", "root", ["acme/a", "beta/b"]);
|
|
254
|
+
expect(r).toEqual({ status: "failed", auditId: "audit-1", error: "boom" });
|
|
255
|
+
expect(audits[0]?.diff.installationAdds).toEqual(["acme/a"]);
|
|
256
|
+
expect(audits[0]?.result).toEqual({ status: "failed", error: "boom" });
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("unknown agent and no GH_APP_ID write nothing", async () => {
|
|
260
|
+
const a = build({ agent: null });
|
|
261
|
+
expect((await a.svc.add("x", "root", ["acme/a"])).status).toBe(
|
|
262
|
+
"agent_not_found",
|
|
263
|
+
);
|
|
264
|
+
const b = build({ agent: { githubUsername: null } });
|
|
265
|
+
expect((await b.svc.remove("a1", "root", ["acme/a"])).status).toBe(
|
|
266
|
+
"no_app_id",
|
|
267
|
+
);
|
|
268
|
+
expect(a.audits.concat(b.audits)).toEqual([]);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
10
10
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
|
+
import { AgentEnvService } from "./agent-envs.ts";
|
|
11
12
|
import type { GithubAccessPreview } from "./github-access-preview.ts";
|
|
12
13
|
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
13
14
|
import {
|
|
@@ -15,6 +16,7 @@ import {
|
|
|
15
16
|
PrismaReconcileAudit,
|
|
16
17
|
ReconcileApplyService,
|
|
17
18
|
} from "./reconcile-apply-service.ts";
|
|
19
|
+
import { identityCrypto } from "./token-crypto.ts";
|
|
18
20
|
|
|
19
21
|
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
20
22
|
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
@@ -55,10 +57,13 @@ describeOrSkip("ReconcileApplyService (integration)", () => {
|
|
|
55
57
|
const service = (): ReconcileApplyService =>
|
|
56
58
|
new ReconcileApplyService({
|
|
57
59
|
agents: {
|
|
58
|
-
get: (id) => new PrismaReconcileAgents(prisma).get(id),
|
|
60
|
+
get: (id) => new PrismaReconcileAgents(prisma, envService()).get(id),
|
|
59
61
|
setRepos: async (id, repos) => {
|
|
60
62
|
calls.push("config");
|
|
61
|
-
await new PrismaReconcileAgents(prisma).setRepos(
|
|
63
|
+
await new PrismaReconcileAgents(prisma, envService()).setRepos(
|
|
64
|
+
id,
|
|
65
|
+
repos,
|
|
66
|
+
);
|
|
62
67
|
},
|
|
63
68
|
},
|
|
64
69
|
access: async () =>
|
|
@@ -77,6 +82,8 @@ describeOrSkip("ReconcileApplyService (integration)", () => {
|
|
|
77
82
|
audit: new PrismaReconcileAudit(prisma),
|
|
78
83
|
});
|
|
79
84
|
|
|
85
|
+
const envService = () => new AgentEnvService(prisma, identityCrypto);
|
|
86
|
+
|
|
80
87
|
async function makeAgent(
|
|
81
88
|
repos: string[],
|
|
82
89
|
githubUsername: string | null = "dev",
|
|
@@ -84,6 +91,7 @@ describeOrSkip("ReconcileApplyService (integration)", () => {
|
|
|
84
91
|
const a = await prisma.agent.create({
|
|
85
92
|
data: { name: "A", repos, githubUsername },
|
|
86
93
|
});
|
|
94
|
+
await envService().patch(a.id, { GH_APP_ID: "42" });
|
|
87
95
|
installed = [...repos];
|
|
88
96
|
return a.id;
|
|
89
97
|
}
|
|
@@ -238,7 +246,7 @@ describeOrSkip("ReconcileApplyService (integration)", () => {
|
|
|
238
246
|
const base = service();
|
|
239
247
|
const plan = await base.plan(id);
|
|
240
248
|
const failing = new ReconcileApplyService({
|
|
241
|
-
agents: new PrismaReconcileAgents(prisma),
|
|
249
|
+
agents: new PrismaReconcileAgents(prisma, envService()),
|
|
242
250
|
access: async () => accessFor(pushable),
|
|
243
251
|
installation: {
|
|
244
252
|
listRepos: async () => ({ acme: [...installed] }),
|