@app-vitals/agent-admin 1.35.0 → 1.35.1
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 +2 -392
- package/package.json +2 -2
- package/src/admin-boot.ts +1 -12
- package/src/admin-extension.ts +3 -3
- package/src/admin-ui-github-identity.smoke.test.ts +0 -114
- package/src/admin-ui-installation-repos-pages.ts +2 -2
- package/src/admin-ui-installation-repos.smoke.test.ts +0 -1
- package/src/admin-ui.ts +0 -82
- package/src/agents-api.smoke.test.ts +2 -352
- package/src/agents-api.ts +5 -269
- package/src/github-enterprise-isolation.unit.test.ts +4 -4
- package/src/{github-reconcile-wiring.ts → github-installation-wiring.ts} +18 -38
- package/src/github-installation-wiring.unit.test.ts +111 -0
- package/src/github-installation.fixtures.ts +76 -0
- package/src/installation-ports.integration.test.ts +89 -0
- package/src/installation-ports.ts +207 -0
- package/src/installation-ports.unit.test.ts +51 -0
- package/src/installation-repos-service.ts +12 -12
- package/src/installation-repos-service.unit.test.ts +3 -3
- package/src/lost-access-removal.ts +9 -9
- package/src/lost-access-removal.unit.test.ts +7 -7
- package/src/reconcile-open-work.ts +5 -7
- package/src/repo-access-rules.ts +58 -0
- package/src/repo-access-rules.unit.test.ts +98 -0
- package/src/admin-ui-github-reconcile-pages.ts +0 -208
- package/src/admin-ui-github-reconcile-pages.unit.test.ts +0 -226
- package/src/github-reconcile-routes.integration.test.ts +0 -206
- package/src/github-reconcile-wiring.unit.test.ts +0 -170
- package/src/reconcile-apply-service.integration.test.ts +0 -271
- package/src/reconcile-apply-service.ts +0 -439
- package/src/reconcile-apply-service.unit.test.ts +0 -279
- package/src/reconcile-planner.ts +0 -264
- package/src/reconcile-planner.unit.test.ts +0 -274
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/installation-ports.integration.test.ts
|
|
3
|
+
* Integration tests for the Prisma-backed agent and audit adapters against a
|
|
4
|
+
* real DB.
|
|
5
|
+
*
|
|
6
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
10
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
|
+
import { AgentEnvService } from "./agent-envs.ts";
|
|
12
|
+
import {
|
|
13
|
+
PrismaInstallationAgents,
|
|
14
|
+
PrismaInstallationAudit,
|
|
15
|
+
} from "./installation-ports.ts";
|
|
16
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
17
|
+
import { identityCrypto } from "./token-crypto.ts";
|
|
18
|
+
|
|
19
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
20
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
21
|
+
|
|
22
|
+
describeOrSkip("installation ports (integration)", () => {
|
|
23
|
+
let prisma: PrismaClient;
|
|
24
|
+
|
|
25
|
+
const env = () => new AgentEnvService(prisma, identityCrypto);
|
|
26
|
+
|
|
27
|
+
beforeEach(async () => {
|
|
28
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
29
|
+
await prisma.gitHubReconcileAudit.deleteMany();
|
|
30
|
+
await prisma.agentGitHubInstallationsSnapshot.deleteMany();
|
|
31
|
+
await prisma.agentWorkQueueSnapshot.deleteMany();
|
|
32
|
+
await prisma.agentChatTokenUsageDailyByModel.deleteMany();
|
|
33
|
+
await prisma.agentCronRun.deleteMany();
|
|
34
|
+
await prisma.agentToken.deleteMany();
|
|
35
|
+
await prisma.agentCronJob.deleteMany();
|
|
36
|
+
await prisma.agentTool.deleteMany();
|
|
37
|
+
await prisma.agentEnv.deleteMany();
|
|
38
|
+
await prisma.agentMember.deleteMany();
|
|
39
|
+
await prisma.agent.deleteMany();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterEach(async () => {
|
|
43
|
+
await prisma.$disconnect();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("reads the agent's own GH_APP_ID and writes its repos", async () => {
|
|
47
|
+
const a = await prisma.agent.create({
|
|
48
|
+
data: { name: "A", repos: ["acme/a"], githubUsername: "dev" },
|
|
49
|
+
});
|
|
50
|
+
const agents = new PrismaInstallationAgents(prisma, env());
|
|
51
|
+
expect((await agents.get(a.id))?.appId).toBeUndefined();
|
|
52
|
+
await env().patch(a.id, { GH_APP_ID: "42" });
|
|
53
|
+
expect(await agents.get(a.id)).toMatchObject({
|
|
54
|
+
id: a.id,
|
|
55
|
+
repos: ["acme/a"],
|
|
56
|
+
githubUsername: "dev",
|
|
57
|
+
appId: 42,
|
|
58
|
+
});
|
|
59
|
+
await agents.setRepos(a.id, ["acme/b"]);
|
|
60
|
+
expect((await agents.get(a.id))?.repos).toEqual(["acme/b"]);
|
|
61
|
+
expect(await agents.get("missing")).toBeNull();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("writes one audit row per entry and returns its id", async () => {
|
|
65
|
+
const a = await prisma.agent.create({ data: { name: "A", repos: [] } });
|
|
66
|
+
const audit = new PrismaInstallationAudit(prisma);
|
|
67
|
+
const id = await audit.write({
|
|
68
|
+
agentId: a.id,
|
|
69
|
+
caller: "dan@x.io",
|
|
70
|
+
planHash: "direct:add",
|
|
71
|
+
diff: {
|
|
72
|
+
installationAdds: ["acme/c"],
|
|
73
|
+
installationRemoves: [],
|
|
74
|
+
configAdds: [],
|
|
75
|
+
configRemoves: [],
|
|
76
|
+
},
|
|
77
|
+
result: { status: "applied" },
|
|
78
|
+
});
|
|
79
|
+
const rows = await prisma.gitHubReconcileAudit.findMany();
|
|
80
|
+
expect(rows).toHaveLength(1);
|
|
81
|
+
expect(rows[0]).toMatchObject({
|
|
82
|
+
id,
|
|
83
|
+
agentId: a.id,
|
|
84
|
+
caller: "dan@x.io",
|
|
85
|
+
planHash: "direct:add",
|
|
86
|
+
result: { status: "applied" },
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/installation-ports.ts
|
|
3
|
+
* Ports and Prisma/GitHub-backed adapters shared by the installation repos
|
|
4
|
+
* service (IRM-1.2) and the lost-access removal (IRM-1.3): the agent record
|
|
5
|
+
* with its own GH_APP_ID, installation list/add/remove scoped to that App, and
|
|
6
|
+
* the audit table (GitHubReconcileAudit) every change is recorded in.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
10
|
+
import type { AgentEnvService } from "./agent-envs.ts";
|
|
11
|
+
|
|
12
|
+
/** Parses a stored GH_APP_ID; unset or non-numeric -> undefined. */
|
|
13
|
+
function parseAppId(raw: string | undefined): number | undefined {
|
|
14
|
+
const n = Number(raw?.trim());
|
|
15
|
+
return Number.isInteger(n) && n > 0 ? n : undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** More removals than this need confirmRemovals. */
|
|
19
|
+
export const MAX_UNCONFIRMED_REMOVALS = 10;
|
|
20
|
+
|
|
21
|
+
export interface InstallationAgentRecord {
|
|
22
|
+
id: string;
|
|
23
|
+
repos: string[];
|
|
24
|
+
githubUsername: string | null;
|
|
25
|
+
/** The agent's own GitHub App id (its stored GH_APP_ID); undefined when unset or not numeric. */
|
|
26
|
+
appId?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface InstallationAgentsPort {
|
|
30
|
+
get(agentId: string): Promise<InstallationAgentRecord | null>;
|
|
31
|
+
setRepos(agentId: string, repos: string[]): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Repo access on an agent App's installations, per org login. Each agent has
|
|
36
|
+
* its own App, so every call names the App (`appId`) whose installation is used.
|
|
37
|
+
*/
|
|
38
|
+
export interface AppInstallationPort {
|
|
39
|
+
/** Repos the agent App covers in each org; only the named orgs are needed. */
|
|
40
|
+
listRepos(
|
|
41
|
+
appId: number,
|
|
42
|
+
orgs: readonly string[],
|
|
43
|
+
): Promise<Record<string, string[]>>;
|
|
44
|
+
/** Grant the installation access to repos (owner/repo, all in `org`). */
|
|
45
|
+
addRepos(appId: number, org: string, repos: readonly string[]): Promise<void>;
|
|
46
|
+
removeRepos(
|
|
47
|
+
appId: number,
|
|
48
|
+
org: string,
|
|
49
|
+
repos: readonly string[],
|
|
50
|
+
): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Repos changed by one audited change, on the installation and in the agent's config. */
|
|
54
|
+
export interface InstallationAuditDiff {
|
|
55
|
+
installationAdds: string[];
|
|
56
|
+
installationRemoves: string[];
|
|
57
|
+
configAdds: string[];
|
|
58
|
+
configRemoves: string[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface InstallationAuditEntry {
|
|
62
|
+
agentId: string;
|
|
63
|
+
caller: string;
|
|
64
|
+
/** Stored in the audit table's planHash column: a label for the kind of change. */
|
|
65
|
+
planHash: string;
|
|
66
|
+
diff: InstallationAuditDiff;
|
|
67
|
+
result: InstallationAuditResult;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface InstallationAuditResult {
|
|
71
|
+
status: "applied" | "failed";
|
|
72
|
+
error?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface InstallationAuditPort {
|
|
76
|
+
write(entry: InstallationAuditEntry): Promise<string>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Skip reason when the agent has no usable GH_APP_ID in its stored env. */
|
|
80
|
+
export const MISSING_APP_ID_REASON =
|
|
81
|
+
"agent has no GH_APP_ID in its stored env (its own GitHub App is not set up)";
|
|
82
|
+
|
|
83
|
+
/** Prisma-backed audit store (GitHubReconcileAudit). */
|
|
84
|
+
export class PrismaInstallationAudit implements InstallationAuditPort {
|
|
85
|
+
constructor(private readonly prisma: PrismaClient) {}
|
|
86
|
+
|
|
87
|
+
async write(entry: InstallationAuditEntry): Promise<string> {
|
|
88
|
+
const row = await this.prisma.gitHubReconcileAudit.create({
|
|
89
|
+
data: {
|
|
90
|
+
agentId: entry.agentId,
|
|
91
|
+
caller: entry.caller,
|
|
92
|
+
planHash: entry.planHash,
|
|
93
|
+
diff: entry.diff as unknown as object,
|
|
94
|
+
result: entry.result as unknown as object,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
return row.id;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Prisma-backed agent reads/writes. */
|
|
102
|
+
export class PrismaInstallationAgents implements InstallationAgentsPort {
|
|
103
|
+
constructor(
|
|
104
|
+
private readonly prisma: PrismaClient,
|
|
105
|
+
private readonly env: Pick<AgentEnvService, "getConfigBundle">,
|
|
106
|
+
) {}
|
|
107
|
+
|
|
108
|
+
async get(agentId: string): Promise<InstallationAgentRecord | null> {
|
|
109
|
+
const agent = await this.prisma.agent.findUnique({
|
|
110
|
+
where: { id: agentId },
|
|
111
|
+
select: { id: true, repos: true, githubUsername: true },
|
|
112
|
+
});
|
|
113
|
+
if (!agent) return null;
|
|
114
|
+
const raw = (await this.env.getConfigBundle(agentId))?.env.GH_APP_ID;
|
|
115
|
+
const appId = parseAppId(raw);
|
|
116
|
+
return appId === undefined ? agent : { ...agent, appId };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async setRepos(agentId: string, repos: string[]): Promise<void> {
|
|
120
|
+
await this.prisma.agent.update({
|
|
121
|
+
where: { id: agentId },
|
|
122
|
+
data: { repos },
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The slice of GithubEnterpriseClient the installation port needs. */
|
|
128
|
+
export interface InstallationRepoClient {
|
|
129
|
+
listOrgInstallations(org: string): Promise<{ id: number; appId: number }[]>;
|
|
130
|
+
listInstallationRepositories(
|
|
131
|
+
org: string,
|
|
132
|
+
installationId: number,
|
|
133
|
+
): Promise<{ fullName: string }[]>;
|
|
134
|
+
addRepositories(
|
|
135
|
+
org: string,
|
|
136
|
+
installationId: number,
|
|
137
|
+
names: readonly string[],
|
|
138
|
+
): Promise<unknown>;
|
|
139
|
+
removeRepositories(
|
|
140
|
+
org: string,
|
|
141
|
+
installationId: number,
|
|
142
|
+
names: readonly string[],
|
|
143
|
+
): Promise<unknown>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The agent App has no installation on the org (distinct from a GitHub failure). */
|
|
147
|
+
export class AppNotInstalledError extends Error {
|
|
148
|
+
constructor(
|
|
149
|
+
readonly appId: number,
|
|
150
|
+
readonly org: string,
|
|
151
|
+
) {
|
|
152
|
+
super(
|
|
153
|
+
`agent App ${appId} (the agent's GH_APP_ID) is not installed on org ${org}`,
|
|
154
|
+
);
|
|
155
|
+
this.name = "AppNotInstalledError";
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const baseName = (fullName: string): string => fullName.split("/")[1] ?? "";
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* AppInstallationPort over GithubEnterpriseClient. Each call
|
|
163
|
+
* is scoped to the named agent App's installation on the org.
|
|
164
|
+
*/
|
|
165
|
+
export function createInstallationPort(
|
|
166
|
+
client: InstallationRepoClient,
|
|
167
|
+
): AppInstallationPort {
|
|
168
|
+
const installationId = async (
|
|
169
|
+
appId: number,
|
|
170
|
+
org: string,
|
|
171
|
+
): Promise<number> => {
|
|
172
|
+
const found = (await client.listOrgInstallations(org)).find(
|
|
173
|
+
(i) => i.appId === appId,
|
|
174
|
+
);
|
|
175
|
+
if (!found) {
|
|
176
|
+
throw new AppNotInstalledError(appId, org);
|
|
177
|
+
}
|
|
178
|
+
return found.id;
|
|
179
|
+
};
|
|
180
|
+
return {
|
|
181
|
+
async listRepos(appId, orgs) {
|
|
182
|
+
const out: Record<string, string[]> = {};
|
|
183
|
+
for (const org of orgs) {
|
|
184
|
+
const repos = await client.listInstallationRepositories(
|
|
185
|
+
org,
|
|
186
|
+
await installationId(appId, org),
|
|
187
|
+
);
|
|
188
|
+
out[org] = repos.map((r) => r.fullName);
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
},
|
|
192
|
+
async addRepos(appId, org, repos) {
|
|
193
|
+
await client.addRepositories(
|
|
194
|
+
org,
|
|
195
|
+
await installationId(appId, org),
|
|
196
|
+
repos.map(baseName),
|
|
197
|
+
);
|
|
198
|
+
},
|
|
199
|
+
async removeRepos(appId, org, repos) {
|
|
200
|
+
await client.removeRepositories(
|
|
201
|
+
org,
|
|
202
|
+
await installationId(appId, org),
|
|
203
|
+
repos.map(baseName),
|
|
204
|
+
);
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/installation-ports.unit.test.ts
|
|
3
|
+
* Unit tests for the installation port over the GitHub client (fakes only).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "bun:test";
|
|
7
|
+
import {
|
|
8
|
+
AppNotInstalledError,
|
|
9
|
+
createInstallationPort,
|
|
10
|
+
} from "./installation-ports.ts";
|
|
11
|
+
|
|
12
|
+
describe("createInstallationPort", () => {
|
|
13
|
+
it("scopes to the given App's installation per call and sends simple names", async () => {
|
|
14
|
+
const seen: string[] = [];
|
|
15
|
+
const port = createInstallationPort({
|
|
16
|
+
listOrgInstallations: async () => [
|
|
17
|
+
{ id: 1, appId: 10 },
|
|
18
|
+
{ id: 2, appId: 20 },
|
|
19
|
+
],
|
|
20
|
+
listInstallationRepositories: async (_o, id) => {
|
|
21
|
+
seen.push(`list:${id}`);
|
|
22
|
+
return [{ fullName: "acme/a" }];
|
|
23
|
+
},
|
|
24
|
+
addRepositories: async (_o, id, names) => {
|
|
25
|
+
seen.push(`add:${id}:${names.join(",")}`);
|
|
26
|
+
},
|
|
27
|
+
removeRepositories: async (_o, id, names) => {
|
|
28
|
+
seen.push(`rm:${id}:${names.join(",")}`);
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
expect(await port.listRepos(20, ["acme"])).toEqual({ acme: ["acme/a"] });
|
|
32
|
+
await port.addRepos(20, "acme", ["acme/b"]);
|
|
33
|
+
await port.removeRepos(10, "acme", ["acme/c", "acme/d"]);
|
|
34
|
+
expect(seen).toEqual(["list:2", "add:2:b", "rm:1:c,d"]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("names the org when the App is not installed there", async () => {
|
|
38
|
+
const port = createInstallationPort({
|
|
39
|
+
listOrgInstallations: async () => [{ id: 1, appId: 10 }],
|
|
40
|
+
listInstallationRepositories: async () => [],
|
|
41
|
+
addRepositories: async () => {},
|
|
42
|
+
removeRepositories: async () => {},
|
|
43
|
+
});
|
|
44
|
+
await expect(port.listRepos(99, ["acme"])).rejects.toThrow(
|
|
45
|
+
/99.*not installed on org acme/,
|
|
46
|
+
);
|
|
47
|
+
await expect(port.listRepos(99, ["acme"])).rejects.toBeInstanceOf(
|
|
48
|
+
AppNotInstalledError,
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/installation-repos-service.ts
|
|
3
3
|
* Direct management of the repos on an agent's own GitHub App installation
|
|
4
|
-
* (IRM-1.2): list, add and remove
|
|
5
|
-
* installer App through the
|
|
4
|
+
* (IRM-1.2): list, add and remove directly on the installation. Uses the
|
|
5
|
+
* installer App through the installation port, and writes every
|
|
6
6
|
* add/remove to the existing GitHubReconcileAudit table (no schema change):
|
|
7
7
|
* `planHash` is "direct:add" / "direct:remove" and the changed repos land in
|
|
8
8
|
* the diff's installationAdds / installationRemoves.
|
|
@@ -12,18 +12,18 @@ import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
|
12
12
|
import {
|
|
13
13
|
AppNotInstalledError,
|
|
14
14
|
MISSING_APP_ID_REASON,
|
|
15
|
-
type
|
|
16
|
-
type
|
|
17
|
-
type
|
|
18
|
-
} from "./
|
|
19
|
-
import { repoOrg } from "./
|
|
15
|
+
type InstallationAgentsPort,
|
|
16
|
+
type InstallationAuditPort,
|
|
17
|
+
type AppInstallationPort,
|
|
18
|
+
} from "./installation-ports.ts";
|
|
19
|
+
import { repoOrg } from "./repo-access-rules.ts";
|
|
20
20
|
import type {
|
|
21
21
|
OrgAccessResult,
|
|
22
22
|
UserRepoAccessResult,
|
|
23
23
|
} from "./user-repo-access-resolver.ts";
|
|
24
24
|
|
|
25
25
|
/** Installation port plus discovery of the orgs the agent App is installed on. */
|
|
26
|
-
export interface InstallationReposPort extends
|
|
26
|
+
export interface InstallationReposPort extends AppInstallationPort {
|
|
27
27
|
listInstalled(appId: number): Promise<{ org: string; repos: string[] }[]>;
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -51,10 +51,10 @@ export type InstallationChangeResult =
|
|
|
51
51
|
| { status: "failed"; auditId: string; error: string };
|
|
52
52
|
|
|
53
53
|
export interface InstallationReposDeps {
|
|
54
|
-
agents: Pick<
|
|
54
|
+
agents: Pick<InstallationAgentsPort, "get">;
|
|
55
55
|
access: GithubAccessPreviewFn;
|
|
56
56
|
installation: InstallationReposPort;
|
|
57
|
-
audit:
|
|
57
|
+
audit: InstallationAuditPort;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
const orgKey = (org: string): string => org.toLowerCase();
|
|
@@ -211,11 +211,11 @@ export interface InstalledOrgsClient {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
/**
|
|
214
|
-
* Port over the
|
|
214
|
+
* Port over the installation port plus org discovery: the orgs the
|
|
215
215
|
* installer can reach that carry an installation of the agent's App.
|
|
216
216
|
*/
|
|
217
217
|
export function createInstallationReposPort(
|
|
218
|
-
base:
|
|
218
|
+
base: AppInstallationPort,
|
|
219
219
|
client: InstalledOrgsClient,
|
|
220
220
|
): InstallationReposPort {
|
|
221
221
|
return {
|
|
@@ -12,8 +12,8 @@ import {
|
|
|
12
12
|
} from "./installation-repos-service.ts";
|
|
13
13
|
import {
|
|
14
14
|
AppNotInstalledError,
|
|
15
|
-
type
|
|
16
|
-
} from "./
|
|
15
|
+
type InstallationAuditEntry,
|
|
16
|
+
} from "./installation-ports.ts";
|
|
17
17
|
import type {
|
|
18
18
|
OrgAccessResult,
|
|
19
19
|
UserRepoAccessResult,
|
|
@@ -115,7 +115,7 @@ function build(
|
|
|
115
115
|
notInstalled?: string;
|
|
116
116
|
} = {},
|
|
117
117
|
) {
|
|
118
|
-
const audits:
|
|
118
|
+
const audits: InstallationAuditEntry[] = [];
|
|
119
119
|
const calls: string[] = [];
|
|
120
120
|
const installation: InstallationReposPort = {
|
|
121
121
|
listInstalled: async () => [
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* The admin reviews a read-only preview, then sends the exact repos they
|
|
8
8
|
* confirmed; the server re-checks each against the user's current access and
|
|
9
9
|
* removes only those still without it (no plan hash). The safety rules are the
|
|
10
|
-
*
|
|
10
|
+
* shared access rules (accessSkipReason / orgSkipReason): unknown is never
|
|
11
11
|
* none, so orgs with a missing, partial or failed listing, or a selected-only
|
|
12
12
|
* lookup install, are skipped and reported; an unbound, not-found or
|
|
13
13
|
* unverified user skips the whole call. More than MAX_UNCONFIRMED_REMOVALS
|
|
@@ -23,17 +23,17 @@ import type { InstallationReposPort } from "./installation-repos-service.ts";
|
|
|
23
23
|
import {
|
|
24
24
|
MAX_UNCONFIRMED_REMOVALS,
|
|
25
25
|
MISSING_APP_ID_REASON,
|
|
26
|
-
type
|
|
27
|
-
type
|
|
28
|
-
type
|
|
29
|
-
} from "./
|
|
26
|
+
type InstallationAgentRecord,
|
|
27
|
+
type InstallationAgentsPort,
|
|
28
|
+
type InstallationAuditPort,
|
|
29
|
+
} from "./installation-ports.ts";
|
|
30
30
|
import type { OpenWorkItem, OpenWorkPort } from "./reconcile-open-work.ts";
|
|
31
31
|
import {
|
|
32
32
|
accessSkipReason,
|
|
33
33
|
type OrgSkip,
|
|
34
34
|
orgSkipReason,
|
|
35
35
|
repoOrg,
|
|
36
|
-
} from "./
|
|
36
|
+
} from "./repo-access-rules.ts";
|
|
37
37
|
import {
|
|
38
38
|
blockRepoRemovalWork,
|
|
39
39
|
previewRepoRemovalWork,
|
|
@@ -169,10 +169,10 @@ export function planLostAccessRemoval(
|
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
export interface LostAccessRemovalDeps {
|
|
172
|
-
agents:
|
|
172
|
+
agents: InstallationAgentsPort;
|
|
173
173
|
access: GithubAccessPreviewFn;
|
|
174
174
|
installation: Pick<InstallationReposPort, "listInstalled" | "removeRepos">;
|
|
175
|
-
audit:
|
|
175
|
+
audit: InstallationAuditPort;
|
|
176
176
|
/** Optional: with none, nothing is listed or blocked. */
|
|
177
177
|
openWork?: OpenWorkPort;
|
|
178
178
|
}
|
|
@@ -304,7 +304,7 @@ export class LostAccessRemovalService {
|
|
|
304
304
|
}
|
|
305
305
|
|
|
306
306
|
private async planFor(
|
|
307
|
-
agent:
|
|
307
|
+
agent: InstallationAgentRecord,
|
|
308
308
|
requested?: readonly string[],
|
|
309
309
|
): Promise<LostAccessPlan> {
|
|
310
310
|
const username = agent.githubUsername?.trim();
|
|
@@ -10,9 +10,9 @@ import {
|
|
|
10
10
|
STILL_PUSHABLE_REASON,
|
|
11
11
|
} from "./lost-access-removal.ts";
|
|
12
12
|
import type {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from "./
|
|
13
|
+
InstallationAgentRecord,
|
|
14
|
+
InstallationAuditEntry,
|
|
15
|
+
} from "./installation-ports.ts";
|
|
16
16
|
import type { OpenWorkPort } from "./reconcile-open-work.ts";
|
|
17
17
|
import { repoLostAccessBlockReason } from "./repo-removal-open-work.ts";
|
|
18
18
|
import {
|
|
@@ -117,7 +117,7 @@ describe("planLostAccessRemoval", () => {
|
|
|
117
117
|
expect(p.warnings[0]).toContain("acme");
|
|
118
118
|
});
|
|
119
119
|
|
|
120
|
-
test("selected-only reason matches the
|
|
120
|
+
test("selected-only reason matches the shared access-rules wording", () => {
|
|
121
121
|
const p = planLostAccessRemoval(
|
|
122
122
|
base({
|
|
123
123
|
access: access([org("acme", [], { reason: SELECTED_REPOS_REASON })]),
|
|
@@ -184,7 +184,7 @@ describe("planLostAccessRemoval", () => {
|
|
|
184
184
|
|
|
185
185
|
function harness(
|
|
186
186
|
opts: {
|
|
187
|
-
agent?: Partial<
|
|
187
|
+
agent?: Partial<InstallationAgentRecord> | null;
|
|
188
188
|
access?: Awaited<ReturnType<GithubAccessPreviewFn>>;
|
|
189
189
|
installed?: { org: string; repos: string[] }[];
|
|
190
190
|
removeFails?: boolean;
|
|
@@ -192,9 +192,9 @@ function harness(
|
|
|
192
192
|
} = {},
|
|
193
193
|
) {
|
|
194
194
|
const calls: unknown[][] = [];
|
|
195
|
-
const audits:
|
|
195
|
+
const audits: InstallationAuditEntry[] = [];
|
|
196
196
|
let repos = opts.agent?.repos ?? ["acme/a", "acme/b"];
|
|
197
|
-
const agent:
|
|
197
|
+
const agent: InstallationAgentRecord | null =
|
|
198
198
|
opts.agent === null
|
|
199
199
|
? null
|
|
200
200
|
: {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/reconcile-open-work.ts
|
|
3
|
-
* Optional open-work port
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Optional open-work port. The admin package knows nothing about task stores;
|
|
4
|
+
* a consumer that does (shipwright) may supply this port so open work can be
|
|
5
|
+
* listed and blocked when a repo leaves an agent's repos. Consumed by
|
|
6
|
+
* repo-removal-open-work.ts (repo removed from the agent) and
|
|
7
|
+
* lost-access-removal.ts (bound GitHub user lost push access).
|
|
6
8
|
* Additive and optional: with no port, nothing is listed or blocked.
|
|
7
9
|
*/
|
|
8
10
|
|
|
@@ -27,7 +29,3 @@ export interface OpenWorkPort {
|
|
|
27
29
|
reason: string,
|
|
28
30
|
): Promise<void>;
|
|
29
31
|
}
|
|
30
|
-
|
|
31
|
-
export function removedRepoBlockReason(repo: string): string {
|
|
32
|
-
return `repo removed from agent by GitHub reconcile (${repo})`;
|
|
33
|
-
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/repo-access-rules.ts
|
|
3
|
+
* Pure rules for deciding what a bound GitHub user's access result can safely
|
|
4
|
+
* justify (reused by the lost-access removal, IRM-1.3). No I/O.
|
|
5
|
+
*
|
|
6
|
+
* - Unknown is never none: an org whose resolver result is missing, partial,
|
|
7
|
+
* failed, or whose lookup installation is selected-only is skipped (with a
|
|
8
|
+
* reason); so is everything when the user is unbound, not found or
|
|
9
|
+
* unverified.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
type OrgAccessResult,
|
|
14
|
+
SELECTED_REPOS_REASON,
|
|
15
|
+
type UserRepoAccessResult,
|
|
16
|
+
} from "./user-repo-access-resolver.ts";
|
|
17
|
+
|
|
18
|
+
export interface OrgSkip {
|
|
19
|
+
org: string;
|
|
20
|
+
reason: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function repoOrg(fullName: string): string {
|
|
24
|
+
return fullName.split("/")[0] ?? "";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Why removals in one org must be skipped (unknown is never none): the org
|
|
29
|
+
* was not checked, its listing is partial or failed, or the lookup App is
|
|
30
|
+
* installed on selected repositories only. Undefined when the listing is
|
|
31
|
+
* complete.
|
|
32
|
+
*/
|
|
33
|
+
export function orgSkipReason(
|
|
34
|
+
result: OrgAccessResult | undefined,
|
|
35
|
+
): string | undefined {
|
|
36
|
+
if (!result) return "org was not checked";
|
|
37
|
+
if (result.reason === SELECTED_REPOS_REASON) {
|
|
38
|
+
return "lookup app is installed on selected repositories only";
|
|
39
|
+
}
|
|
40
|
+
if (result.status === "partial") return "access listing is incomplete";
|
|
41
|
+
if (result.status !== "ok") return `access lookup failed: ${result.status}`;
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Why a whole call must be skipped: no bound user, access not resolved, user
|
|
47
|
+
* not found or not verified. Undefined when the access result is usable.
|
|
48
|
+
*/
|
|
49
|
+
export function accessSkipReason(
|
|
50
|
+
username: string | null | undefined,
|
|
51
|
+
access: UserRepoAccessResult | null,
|
|
52
|
+
): string | undefined {
|
|
53
|
+
if (!username?.trim()) return "no GitHub username bound to the agent";
|
|
54
|
+
if (!access) return "access was not resolved";
|
|
55
|
+
if (access.outcome === "user_not_found") return "GitHub user not found";
|
|
56
|
+
if (!access.userVerified) return "GitHub user could not be verified";
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|