@app-vitals/agent-admin 1.35.0 → 1.36.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 +136 -766
- package/package.json +2 -2
- package/src/admin-boot.ts +4 -15
- package/src/admin-extension.ts +3 -3
- package/src/admin-ui-github-identity.smoke.test.ts +24 -116
- package/src/admin-ui-installation-repos-pages.ts +198 -231
- package/src/admin-ui-installation-repos-pages.unit.test.ts +199 -0
- package/src/admin-ui-installation-repos.smoke.test.ts +166 -173
- package/src/admin-ui-pages.ts +2 -2
- package/src/admin-ui.ts +103 -171
- package/src/agents-api.smoke.test.ts +195 -642
- package/src/agents-api.ts +86 -477
- package/src/github-enterprise-isolation.unit.test.ts +4 -4
- package/src/{github-reconcile-wiring.ts → github-installation-wiring.ts} +24 -46
- package/src/github-installation-wiring.unit.test.ts +115 -0
- package/src/github-installation.fixtures.ts +76 -0
- package/src/github-sync.ts +407 -0
- package/src/{lost-access-removal.unit.test.ts → github-sync.unit.test.ts} +220 -115
- 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 +10 -105
- package/src/installation-repos-service.unit.test.ts +5 -90
- 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/lost-access-removal.ts +0 -333
- 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,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 GitHub Sync (IRM-3.1): 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,29 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/installation-repos-service.ts
|
|
3
|
-
*
|
|
4
|
-
* (IRM-1.2)
|
|
5
|
-
* installer App through the
|
|
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.
|
|
3
|
+
* Read-only listing of the repos on an agent's own GitHub App installation
|
|
4
|
+
* (IRM-1.2), with the bound user's push hint. Changes go through the Sync flow
|
|
5
|
+
* (github-sync.ts, IRM-3.1). Uses the installer App through the installation port.
|
|
9
6
|
*/
|
|
10
7
|
|
|
11
8
|
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
12
9
|
import {
|
|
13
10
|
AppNotInstalledError,
|
|
14
11
|
MISSING_APP_ID_REASON,
|
|
15
|
-
type
|
|
16
|
-
type
|
|
17
|
-
|
|
18
|
-
} from "./reconcile-apply-service.ts";
|
|
19
|
-
import { repoOrg } from "./reconcile-planner.ts";
|
|
12
|
+
type InstallationAgentsPort,
|
|
13
|
+
type AppInstallationPort,
|
|
14
|
+
} from "./installation-ports.ts";
|
|
20
15
|
import type {
|
|
21
16
|
OrgAccessResult,
|
|
22
17
|
UserRepoAccessResult,
|
|
23
18
|
} from "./user-repo-access-resolver.ts";
|
|
24
19
|
|
|
25
20
|
/** Installation port plus discovery of the orgs the agent App is installed on. */
|
|
26
|
-
export interface InstallationReposPort extends
|
|
21
|
+
export interface InstallationReposPort extends AppInstallationPort {
|
|
27
22
|
listInstalled(appId: number): Promise<{ org: string; repos: string[] }[]>;
|
|
28
23
|
}
|
|
29
24
|
|
|
@@ -43,18 +38,10 @@ export type InstallationListResult =
|
|
|
43
38
|
| { status: "no_app_id"; reason: string }
|
|
44
39
|
| { status: "ok"; appId: number; orgs: InstallationOrgView[] };
|
|
45
40
|
|
|
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
41
|
export interface InstallationReposDeps {
|
|
54
|
-
agents: Pick<
|
|
42
|
+
agents: Pick<InstallationAgentsPort, "get">;
|
|
55
43
|
access: GithubAccessPreviewFn;
|
|
56
44
|
installation: InstallationReposPort;
|
|
57
|
-
audit: ReconcileAuditPort;
|
|
58
45
|
}
|
|
59
46
|
|
|
60
47
|
const orgKey = (org: string): string => org.toLowerCase();
|
|
@@ -92,15 +79,6 @@ export function userPushHints(
|
|
|
92
79
|
});
|
|
93
80
|
}
|
|
94
81
|
|
|
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
82
|
export class InstallationReposService {
|
|
105
83
|
constructor(private readonly deps: InstallationReposDeps) {}
|
|
106
84
|
|
|
@@ -130,79 +108,6 @@ export class InstallationReposService {
|
|
|
130
108
|
})),
|
|
131
109
|
};
|
|
132
110
|
}
|
|
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
111
|
}
|
|
207
112
|
|
|
208
113
|
/** The slice of the installer client the installation-repos port needs. */
|
|
@@ -211,11 +116,11 @@ export interface InstalledOrgsClient {
|
|
|
211
116
|
}
|
|
212
117
|
|
|
213
118
|
/**
|
|
214
|
-
* Port over the
|
|
119
|
+
* Port over the installation port plus org discovery: the orgs the
|
|
215
120
|
* installer can reach that carry an installation of the agent's App.
|
|
216
121
|
*/
|
|
217
122
|
export function createInstallationReposPort(
|
|
218
|
-
base:
|
|
123
|
+
base: AppInstallationPort,
|
|
219
124
|
client: InstalledOrgsClient,
|
|
220
125
|
): InstallationReposPort {
|
|
221
126
|
return {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/installation-repos-service.unit.test.ts
|
|
3
|
-
* IRM-1.2: the user-can-push hint logic and the list
|
|
3
|
+
* IRM-1.2: the user-can-push hint logic and the list service, with
|
|
4
4
|
* in-memory ports.
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -10,10 +10,6 @@ import {
|
|
|
10
10
|
type InstallationReposPort,
|
|
11
11
|
userPushHints,
|
|
12
12
|
} from "./installation-repos-service.ts";
|
|
13
|
-
import {
|
|
14
|
-
AppNotInstalledError,
|
|
15
|
-
type ReconcileAuditEntry,
|
|
16
|
-
} from "./reconcile-apply-service.ts";
|
|
17
13
|
import type {
|
|
18
14
|
OrgAccessResult,
|
|
19
15
|
UserRepoAccessResult,
|
|
@@ -111,29 +107,15 @@ function build(
|
|
|
111
107
|
| { status: "unconfigured" }
|
|
112
108
|
| { status: "ok"; access: UserRepoAccessResult }
|
|
113
109
|
>;
|
|
114
|
-
failAdd?: boolean;
|
|
115
|
-
notInstalled?: string;
|
|
116
110
|
} = {},
|
|
117
111
|
) {
|
|
118
|
-
const audits: ReconcileAuditEntry[] = [];
|
|
119
|
-
const calls: string[] = [];
|
|
120
112
|
const installation: InstallationReposPort = {
|
|
121
113
|
listInstalled: async () => [
|
|
122
114
|
{ org: "acme", repos: ["acme/push", "acme/unlisted"] },
|
|
123
115
|
],
|
|
124
|
-
listRepos: async (
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
},
|
|
116
|
+
listRepos: async () => ({}),
|
|
117
|
+
addRepos: async () => {},
|
|
118
|
+
removeRepos: async () => {},
|
|
137
119
|
};
|
|
138
120
|
const agent =
|
|
139
121
|
opts.agent === undefined
|
|
@@ -145,14 +127,8 @@ function build(
|
|
|
145
127
|
opts.access ??
|
|
146
128
|
(async () => ({ status: "ok", access: accessOf([orgResult()]) })),
|
|
147
129
|
installation,
|
|
148
|
-
audit: {
|
|
149
|
-
write: async (e) => {
|
|
150
|
-
audits.push(e);
|
|
151
|
-
return `audit-${audits.length}`;
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
130
|
});
|
|
155
|
-
return { svc
|
|
131
|
+
return { svc };
|
|
156
132
|
}
|
|
157
133
|
|
|
158
134
|
describe("InstallationReposService.list", () => {
|
|
@@ -207,64 +183,3 @@ describe("InstallationReposService.list", () => {
|
|
|
207
183
|
expect(r.status).toBe("no_app_id");
|
|
208
184
|
});
|
|
209
185
|
});
|
|
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
|
-
});
|
|
@@ -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
|
+
* github-sync.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 GitHub Sync, IRM-3.1). 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
|
+
}
|