@app-vitals/agent-admin 1.81.0 → 1.83.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 +150 -2
- package/package.json +2 -2
- package/src/admin-boot.ts +14 -3
- package/src/admin-ui-github-refresh.smoke.test.ts +96 -0
- package/src/admin-ui-installation-repos-pages.ts +119 -25
- package/src/admin-ui-installation-repos-pages.unit.test.ts +49 -1
- package/src/admin-ui-installation-repos.smoke.test.ts +71 -0
- package/src/admin-ui-pages.ts +14 -1
- package/src/admin-ui.ts +56 -9
- package/src/agents-api.smoke.test.ts +85 -5
- package/src/agents-api.ts +51 -3
- package/src/github-enterprise-api.smoke.test.ts +73 -0
- package/src/github-enterprise-api.ts +51 -0
- package/src/github-installation-wiring.ts +5 -3
- package/src/github-installation-wiring.unit.test.ts +8 -4
- package/src/github-installation.fixtures.ts +12 -1
- package/src/github-refresh-action.ts +45 -0
- package/src/github-refresh-action.unit.test.ts +121 -0
- package/src/github-repo-cache.ts +56 -0
- package/src/github-repo-refresher.ts +5 -0
- package/src/installation-repos-service.ts +208 -50
- package/src/installation-repos-service.unit.test.ts +191 -52
|
@@ -30,9 +30,9 @@ const portWith = (
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
describe("createGithubInstallationRepos", () => {
|
|
33
|
-
it("lists from the agent's own stored GH_APP_ID", async () => {
|
|
33
|
+
it("lists from the cache under the agent's own stored GH_APP_ID, without GitHub", async () => {
|
|
34
34
|
const seen: number[] = [];
|
|
35
|
-
const { prisma } = fakePrisma(["acme/old"]);
|
|
35
|
+
const { prisma, state } = fakePrisma(["acme/old"]);
|
|
36
36
|
const svc = createGithubInstallationRepos({
|
|
37
37
|
prisma,
|
|
38
38
|
appService: noApp,
|
|
@@ -41,8 +41,12 @@ describe("createGithubInstallationRepos", () => {
|
|
|
41
41
|
installationRepos: portWith(seen),
|
|
42
42
|
});
|
|
43
43
|
const res = await svc.list("a1");
|
|
44
|
-
|
|
45
|
-
expect(
|
|
44
|
+
// The port (live GitHub) is never used; the cache is read for App 77.
|
|
45
|
+
expect(seen).toEqual([]);
|
|
46
|
+
expect(state.snapshotQueries).toMatchObject([
|
|
47
|
+
{ where: { appKind: "agent", appRef: "77" } },
|
|
48
|
+
]);
|
|
49
|
+
expect(res).toMatchObject({ status: "ok", appId: 77, complete: false });
|
|
46
50
|
});
|
|
47
51
|
|
|
48
52
|
it("reports an agent with no GH_APP_ID, naming the missing var", async () => {
|
|
@@ -34,7 +34,11 @@ export const accessFor = (pushable: string[]): GithubAccessPreviewFn => {
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
export function fakePrisma(repos: string[]) {
|
|
37
|
-
const state = {
|
|
37
|
+
const state = {
|
|
38
|
+
repos: [...repos],
|
|
39
|
+
audits: 0,
|
|
40
|
+
snapshotQueries: [] as unknown[],
|
|
41
|
+
};
|
|
38
42
|
const prisma = {
|
|
39
43
|
agent: {
|
|
40
44
|
findUnique: async () => ({
|
|
@@ -46,6 +50,13 @@ export function fakePrisma(repos: string[]) {
|
|
|
46
50
|
state.repos = args.data.repos;
|
|
47
51
|
},
|
|
48
52
|
},
|
|
53
|
+
gitHubRepoSnapshot: {
|
|
54
|
+
findMany: async (args: unknown) => {
|
|
55
|
+
state.snapshotQueries.push(args);
|
|
56
|
+
return [];
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
gitHubRepoAccess: { findMany: async () => [] },
|
|
49
60
|
gitHubReconcileAudit: {
|
|
50
61
|
create: async () => {
|
|
51
62
|
state.audits += 1;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-refresh-action.ts
|
|
3
|
+
* The fire-and-forget Refresh action (GLC-2.4). requestRefresh() only writes
|
|
4
|
+
* to the database (ages the cache snapshots past the staleness cap, clears
|
|
5
|
+
* crawl and access backoff) and asks discovery to run again; it never calls
|
|
6
|
+
* GitHub and never waits on the refresher. The next tick does the work, still
|
|
7
|
+
* behind the snapshot lease and the rate-limit window.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { type Clock, SystemClock } from "./clock.ts";
|
|
11
|
+
import type { GitHubRepoCacheService } from "./github-repo-cache.ts";
|
|
12
|
+
|
|
13
|
+
export interface RefreshRequestResult {
|
|
14
|
+
snapshots: number;
|
|
15
|
+
accessPairs: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface GithubRefreshAction {
|
|
19
|
+
requestRefresh(): Promise<RefreshRequestResult>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createGithubRefreshAction(deps: {
|
|
23
|
+
cache: Pick<GitHubRepoCacheService, "markStale">;
|
|
24
|
+
/** Snapshots are aged past this so the planner treats them as capped work. */
|
|
25
|
+
stalenessCapMs: number;
|
|
26
|
+
/** Runs after the mark (e.g. refresher.invalidateDiscovery). Failures are ignored. */
|
|
27
|
+
onMarked?: () => void;
|
|
28
|
+
clock?: Clock;
|
|
29
|
+
}): GithubRefreshAction {
|
|
30
|
+
const clock = deps.clock ?? SystemClock();
|
|
31
|
+
return {
|
|
32
|
+
async requestRefresh() {
|
|
33
|
+
const staleBefore = new Date(
|
|
34
|
+
clock.now().getTime() - deps.stalenessCapMs - 1,
|
|
35
|
+
);
|
|
36
|
+
const res = await deps.cache.markStale(staleBefore);
|
|
37
|
+
try {
|
|
38
|
+
deps.onMarked?.();
|
|
39
|
+
} catch {
|
|
40
|
+
// discovery refresh is best effort
|
|
41
|
+
}
|
|
42
|
+
return res;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/github-refresh-action.unit.test.ts
|
|
3
|
+
* GLC-2.4: stale-marking resets the right rows (and only those), and the
|
|
4
|
+
* action never touches leases or rate-limit windows. Fake prisma; no DB.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
9
|
+
import { createGithubRefreshAction } from "./github-refresh-action.ts";
|
|
10
|
+
import { GitHubRepoCacheService } from "./github-repo-cache.ts";
|
|
11
|
+
|
|
12
|
+
type Call = { model: string; args: { where: unknown; data: unknown } };
|
|
13
|
+
|
|
14
|
+
function fakePrisma(counts = { snap: 2, access: 3 }) {
|
|
15
|
+
const calls: Call[] = [];
|
|
16
|
+
const model = (name: string, count: number) => ({
|
|
17
|
+
updateMany: async (args: Call["args"]) => {
|
|
18
|
+
calls.push({ model: name, args });
|
|
19
|
+
return { count };
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
const prisma = {
|
|
23
|
+
gitHubRepoSnapshot: model("snapshot", counts.snap),
|
|
24
|
+
gitHubRepoAccess: model("access", counts.access),
|
|
25
|
+
} as unknown as PrismaClient;
|
|
26
|
+
return { prisma, calls };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("GitHubRepoCacheService.markStale", () => {
|
|
30
|
+
const staleBefore = new Date("2026-11-01T00:00:00Z");
|
|
31
|
+
|
|
32
|
+
it("ages completed snapshots, clears backoff on snapshots and pending access pairs", async () => {
|
|
33
|
+
const { prisma, calls } = fakePrisma();
|
|
34
|
+
const res = await new GitHubRepoCacheService(prisma).markStale(staleBefore);
|
|
35
|
+
expect(res).toEqual({ snapshots: 2, accessPairs: 3 });
|
|
36
|
+
expect(calls).toEqual([
|
|
37
|
+
{
|
|
38
|
+
model: "snapshot",
|
|
39
|
+
args: {
|
|
40
|
+
where: { completedAt: { gt: staleBefore } },
|
|
41
|
+
data: { completedAt: staleBefore },
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
model: "snapshot",
|
|
46
|
+
args: {
|
|
47
|
+
where: { nextAttemptAt: { not: null } },
|
|
48
|
+
data: { nextAttemptAt: null },
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
model: "access",
|
|
53
|
+
args: {
|
|
54
|
+
where: { status: "error", nextAttemptAt: { not: null } },
|
|
55
|
+
data: { nextAttemptAt: null },
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("never writes the lease, rate-limit or terminal/ok-row columns", async () => {
|
|
62
|
+
const { prisma, calls } = fakePrisma();
|
|
63
|
+
await new GitHubRepoCacheService(prisma).markStale(staleBefore);
|
|
64
|
+
for (const c of calls) {
|
|
65
|
+
const written = Object.keys(c.args.data as object);
|
|
66
|
+
for (const col of [
|
|
67
|
+
"leaseHolder",
|
|
68
|
+
"leasedUntil",
|
|
69
|
+
"rateLimitResetAt",
|
|
70
|
+
"status",
|
|
71
|
+
"checkedAt",
|
|
72
|
+
]) {
|
|
73
|
+
expect(written).not.toContain(col);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const access = calls.find((c) => c.model === "access");
|
|
77
|
+
expect(JSON.stringify(access?.args.where)).toContain('"status":"error"');
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("createGithubRefreshAction", () => {
|
|
82
|
+
const now = new Date("2026-11-20T12:00:00Z");
|
|
83
|
+
|
|
84
|
+
it("marks snapshots past the staleness cap and notifies", async () => {
|
|
85
|
+
const seen: Date[] = [];
|
|
86
|
+
let notified = 0;
|
|
87
|
+
const action = createGithubRefreshAction({
|
|
88
|
+
cache: {
|
|
89
|
+
markStale: async (d: Date) => {
|
|
90
|
+
seen.push(d);
|
|
91
|
+
return { snapshots: 1, accessPairs: 0 };
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
stalenessCapMs: 6 * 3_600_000,
|
|
95
|
+
onMarked: () => {
|
|
96
|
+
notified++;
|
|
97
|
+
},
|
|
98
|
+
clock: { now: () => now },
|
|
99
|
+
});
|
|
100
|
+
expect(await action.requestRefresh()).toEqual({
|
|
101
|
+
snapshots: 1,
|
|
102
|
+
accessPairs: 0,
|
|
103
|
+
});
|
|
104
|
+
expect(seen[0]?.getTime()).toBeLessThan(now.getTime() - 6 * 3_600_000);
|
|
105
|
+
expect(notified).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("ignores a failing onMarked hook", async () => {
|
|
109
|
+
const action = createGithubRefreshAction({
|
|
110
|
+
cache: { markStale: async () => ({ snapshots: 0, accessPairs: 0 }) },
|
|
111
|
+
stalenessCapMs: 1000,
|
|
112
|
+
onMarked: () => {
|
|
113
|
+
throw new Error("boom");
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
await expect(action.requestRefresh()).resolves.toEqual({
|
|
117
|
+
snapshots: 0,
|
|
118
|
+
accessPairs: 0,
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
});
|
package/src/github-repo-cache.ts
CHANGED
|
@@ -146,6 +146,30 @@ export class GitHubRepoCacheService {
|
|
|
146
146
|
}));
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
+
/** Agent-App installation snapshots (with their repos) for one account scope and App id. Reads only the cache. */
|
|
150
|
+
async listAgentSnapshots(accountId: string, appRef: string) {
|
|
151
|
+
requireNonEmpty("accountId", accountId);
|
|
152
|
+
requireNonEmpty("appRef", appRef);
|
|
153
|
+
const rows = await this.prisma.gitHubRepoSnapshot.findMany({
|
|
154
|
+
where: { accountId, appKind: "agent", appRef },
|
|
155
|
+
include: { repos: { select: { fullName: true } } },
|
|
156
|
+
orderBy: { org: "asc" },
|
|
157
|
+
});
|
|
158
|
+
return rows.map((r) => ({
|
|
159
|
+
org: r.org,
|
|
160
|
+
completedAt: r.completedAt,
|
|
161
|
+
totalCount: r.totalCount,
|
|
162
|
+
lastError: r.lastError,
|
|
163
|
+
rateLimitResetAt: r.rateLimitResetAt,
|
|
164
|
+
attempts: r.attempts,
|
|
165
|
+
nextAttemptAt: r.nextAttemptAt,
|
|
166
|
+
leaseHolder: r.leaseHolder,
|
|
167
|
+
leasedUntil: r.leasedUntil,
|
|
168
|
+
lastTickAt: r.lastTickAt,
|
|
169
|
+
repos: r.repos,
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
|
|
149
173
|
/** Upsert repos for a snapshot in batches, stamping each with `seenAt`. */
|
|
150
174
|
async upsertRepos(
|
|
151
175
|
snapshotId: string,
|
|
@@ -222,6 +246,38 @@ export class GitHubRepoCacheService {
|
|
|
222
246
|
return count;
|
|
223
247
|
}
|
|
224
248
|
|
|
249
|
+
/**
|
|
250
|
+
* Refresh request (GLC-2.4): make every snapshot's work the next thing the
|
|
251
|
+
* refresher runs, without touching GitHub. A completed list is aged to
|
|
252
|
+
* `staleBefore` (callers pass a time past the staleness cap, so the planner
|
|
253
|
+
* ranks it ahead of expired-only work); any snapshot's crawl backoff
|
|
254
|
+
* (nextAttemptAt) is cleared, and so is the backoff on access pairs that
|
|
255
|
+
* are still pending (status error). Leases and rateLimitResetAt are left
|
|
256
|
+
* alone, so the lease and the rate-limit window still gate the work.
|
|
257
|
+
* Terminal and ok access rows are not touched.
|
|
258
|
+
*/
|
|
259
|
+
async markStale(staleBefore: Date): Promise<{
|
|
260
|
+
snapshots: number;
|
|
261
|
+
accessPairs: number;
|
|
262
|
+
}> {
|
|
263
|
+
const aged = await this.prisma.gitHubRepoSnapshot.updateMany({
|
|
264
|
+
where: { completedAt: { gt: staleBefore } },
|
|
265
|
+
data: { completedAt: staleBefore },
|
|
266
|
+
});
|
|
267
|
+
const backoff = await this.prisma.gitHubRepoSnapshot.updateMany({
|
|
268
|
+
where: { nextAttemptAt: { not: null } },
|
|
269
|
+
data: { nextAttemptAt: null },
|
|
270
|
+
});
|
|
271
|
+
const access = await this.prisma.gitHubRepoAccess.updateMany({
|
|
272
|
+
where: { status: "error", nextAttemptAt: { not: null } },
|
|
273
|
+
data: { nextAttemptAt: null },
|
|
274
|
+
});
|
|
275
|
+
return {
|
|
276
|
+
snapshots: Math.max(aged.count, backoff.count),
|
|
277
|
+
accessPairs: access.count,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
225
281
|
/**
|
|
226
282
|
* Take the org's lease for `ttlMs`. True when this holder now owns it; false
|
|
227
283
|
* when another holder's lease is still live or the snapshot does not exist.
|
|
@@ -313,6 +313,11 @@ export class GithubRepoRefresher {
|
|
|
313
313
|
deps.discoveryIntervalMs ?? DEFAULT_REFRESH_DISCOVERY_INTERVAL_MS;
|
|
314
314
|
}
|
|
315
315
|
|
|
316
|
+
/** Make the next tick re-discover targets (new or changed GitHub Apps). */
|
|
317
|
+
invalidateDiscovery(): void {
|
|
318
|
+
this.discoveredAt = null;
|
|
319
|
+
}
|
|
320
|
+
|
|
316
321
|
/** Debug snapshot of the last ticks (a copy). */
|
|
317
322
|
status(): RefreshStatus {
|
|
318
323
|
return {
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* admin/src/installation-repos-service.ts
|
|
3
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.
|
|
5
|
-
* (
|
|
4
|
+
* (IRM-1.2), with the bound user's push hint. list() reads only the repo/access
|
|
5
|
+
* cache (GLC-2.2): no GitHub call, sync detail alongside. Changes go through
|
|
6
|
+
* the Sync flow (github-sync.ts, IRM-3.1), which still uses the installer App
|
|
7
|
+
* through the installation port.
|
|
6
8
|
*/
|
|
7
9
|
|
|
8
10
|
import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
|
|
11
|
+
import { DEPLOYMENT_WIDE_ACCOUNT_KEY } from "./github-agent-installation-crawler.ts";
|
|
9
12
|
import {
|
|
10
|
-
AppNotInstalledError,
|
|
11
|
-
MISSING_APP_ID_REASON,
|
|
12
13
|
type AppIdentity,
|
|
13
|
-
type InstallationAgentsPort,
|
|
14
14
|
type AppInstallationPort,
|
|
15
|
+
AppNotInstalledError,
|
|
16
|
+
type InstallationAgentsPort,
|
|
17
|
+
MISSING_APP_ID_REASON,
|
|
15
18
|
} from "./installation-ports.ts";
|
|
16
19
|
import type {
|
|
17
20
|
OrgAccessResult,
|
|
@@ -43,21 +46,87 @@ export interface InstallationRepoView {
|
|
|
43
46
|
userCanPush?: boolean;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
|
-
|
|
49
|
+
/** Sync detail for one org's cached snapshot (all optional, additive). */
|
|
50
|
+
export interface InstallationSyncDetail {
|
|
51
|
+
/** True once the org's crawl completed. */
|
|
52
|
+
complete?: boolean;
|
|
53
|
+
/** ISO time of the completed crawl; absent until one completed. */
|
|
54
|
+
syncedAt?: string;
|
|
55
|
+
/** Repos in the cache for the org. */
|
|
56
|
+
reposSeen?: number;
|
|
57
|
+
/** GitHub's repo count for the org, when known ("N of M"). */
|
|
58
|
+
totalCount?: number;
|
|
59
|
+
lastError?: string;
|
|
60
|
+
/** ISO time the GitHub rate limit resets, while limited. */
|
|
61
|
+
rateLimitResetAt?: string;
|
|
62
|
+
/** Failed refresh attempts since the last success. */
|
|
63
|
+
attempts?: number;
|
|
64
|
+
/** ISO time of the next refresh attempt, while backing off. */
|
|
65
|
+
nextAttemptAt?: string;
|
|
66
|
+
/** Holder of a live refresh lease. */
|
|
67
|
+
leaseHolder?: string;
|
|
68
|
+
/** ISO time of the last refresh tick on this snapshot. */
|
|
69
|
+
lastTickAt?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface InstallationOrgView extends InstallationSyncDetail {
|
|
47
73
|
org: string;
|
|
48
74
|
repos: InstallationRepoView[];
|
|
49
75
|
}
|
|
50
76
|
|
|
77
|
+
/** One cached agent-App snapshot with its repos (GitHubRepoCacheService.listAgentSnapshots). */
|
|
78
|
+
export interface InstallationSnapshotRow {
|
|
79
|
+
org: string;
|
|
80
|
+
completedAt: Date | null;
|
|
81
|
+
totalCount: number | null;
|
|
82
|
+
lastError: string | null;
|
|
83
|
+
rateLimitResetAt: Date | null;
|
|
84
|
+
attempts?: number;
|
|
85
|
+
nextAttemptAt?: Date | null;
|
|
86
|
+
leaseHolder?: string | null;
|
|
87
|
+
leasedUntil?: Date | null;
|
|
88
|
+
lastTickAt?: Date | null;
|
|
89
|
+
repos: { fullName: string }[];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** A cached per-user access row (repo is the name without the org). */
|
|
93
|
+
export interface CachedAccessRow {
|
|
94
|
+
repo: string;
|
|
95
|
+
status: string;
|
|
96
|
+
canPush: boolean;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The slice of GitHubRepoCacheService the list reads; never calls GitHub. */
|
|
100
|
+
export interface InstallationCachePort {
|
|
101
|
+
listAgentSnapshots(
|
|
102
|
+
accountId: string,
|
|
103
|
+
appRef: string,
|
|
104
|
+
): Promise<InstallationSnapshotRow[]>;
|
|
105
|
+
getAccessRows(query: {
|
|
106
|
+
accountId: string;
|
|
107
|
+
org: string;
|
|
108
|
+
username: string;
|
|
109
|
+
repos: string[];
|
|
110
|
+
}): Promise<CachedAccessRow[]>;
|
|
111
|
+
}
|
|
112
|
+
|
|
51
113
|
export type InstallationListResult =
|
|
52
114
|
| { status: "agent_not_found" }
|
|
53
115
|
| { status: "no_app_id"; reason: string }
|
|
54
116
|
| { status: "skipped"; appId: number; reason: string }
|
|
55
|
-
| {
|
|
117
|
+
| ({
|
|
118
|
+
status: "ok";
|
|
119
|
+
appId: number;
|
|
120
|
+
orgs: InstallationOrgView[];
|
|
121
|
+
} & InstallationSyncDetail);
|
|
56
122
|
|
|
57
123
|
export interface InstallationReposDeps {
|
|
58
124
|
agents: Pick<InstallationAgentsPort, "get">;
|
|
125
|
+
/** Used by GitHub Sync, not by list(), which reads only the cache. */
|
|
59
126
|
access: GithubAccessPreviewFn;
|
|
60
127
|
installation: InstallationReposPort;
|
|
128
|
+
/** The repo/access cache list() reads. Without it list() reports nothing synced. */
|
|
129
|
+
cache?: InstallationCachePort;
|
|
61
130
|
}
|
|
62
131
|
|
|
63
132
|
/** The agent's App identity keys (client id, slug), when stored. */
|
|
@@ -75,31 +144,52 @@ export function identityOf(agent: {
|
|
|
75
144
|
|
|
76
145
|
const orgKey = (org: string): string => org.toLowerCase();
|
|
77
146
|
|
|
147
|
+
/** Cached access rows for one org and user, as read from the access cache. */
|
|
148
|
+
export interface CachedAccessRows {
|
|
149
|
+
cachedRows: readonly CachedAccessRow[];
|
|
150
|
+
}
|
|
151
|
+
|
|
78
152
|
/**
|
|
79
153
|
* The user-can-push hint for one org: true/false per repo only when the bound
|
|
80
|
-
* user is verified and this org's
|
|
81
|
-
*
|
|
82
|
-
* (never false).
|
|
154
|
+
* user is verified and this org's access data is complete (a live "ok" lookup
|
|
155
|
+
* with nothing unresolved, or cached rows with an "ok" row for every listed
|
|
156
|
+
* repo). Anything else is unknown, so it gets no hint (never false).
|
|
83
157
|
*/
|
|
84
158
|
export function userPushHints(
|
|
85
|
-
access: UserRepoAccessResult | undefined,
|
|
159
|
+
access: UserRepoAccessResult | CachedAccessRows | undefined,
|
|
86
160
|
org: string,
|
|
87
161
|
repos: readonly string[],
|
|
88
162
|
): InstallationRepoView[] {
|
|
89
|
-
const orgResult: OrgAccessResult | undefined = access?.orgs.find(
|
|
90
|
-
(o) => orgKey(o.org) === orgKey(org),
|
|
91
|
-
);
|
|
92
|
-
const complete =
|
|
93
|
-
access !== undefined &&
|
|
94
|
-
access.outcome === "ok" &&
|
|
95
|
-
access.userVerified &&
|
|
96
|
-
orgResult !== undefined &&
|
|
97
|
-
orgResult.status === "ok" &&
|
|
98
|
-
orgResult.unresolved === 0;
|
|
99
163
|
const pushByName = new Map<string, boolean>();
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
164
|
+
if (access && "cachedRows" in access) {
|
|
165
|
+
const byRepo = new Map<string, CachedAccessRow>();
|
|
166
|
+
for (const r of access.cachedRows) byRepo.set(r.repo.toLowerCase(), r);
|
|
167
|
+
const nameOf = (fullName: string) =>
|
|
168
|
+
fullName.slice(fullName.indexOf("/") + 1).toLowerCase();
|
|
169
|
+
const complete = repos.every((f) => byRepo.get(nameOf(f))?.status === "ok");
|
|
170
|
+
if (complete) {
|
|
171
|
+
for (const f of repos) {
|
|
172
|
+
pushByName.set(
|
|
173
|
+
f.toLowerCase(),
|
|
174
|
+
byRepo.get(nameOf(f))?.canPush ?? false,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
const orgResult: OrgAccessResult | undefined = access?.orgs.find(
|
|
180
|
+
(o) => orgKey(o.org) === orgKey(org),
|
|
181
|
+
);
|
|
182
|
+
const complete =
|
|
183
|
+
access !== undefined &&
|
|
184
|
+
access.outcome === "ok" &&
|
|
185
|
+
access.userVerified &&
|
|
186
|
+
orgResult !== undefined &&
|
|
187
|
+
orgResult.status === "ok" &&
|
|
188
|
+
orgResult.unresolved === 0;
|
|
189
|
+
if (complete) {
|
|
190
|
+
for (const r of orgResult.repos) {
|
|
191
|
+
pushByName.set(r.fullName.toLowerCase(), r.canPush);
|
|
192
|
+
}
|
|
103
193
|
}
|
|
104
194
|
}
|
|
105
195
|
return repos.map((fullName) => {
|
|
@@ -108,44 +198,112 @@ export function userPushHints(
|
|
|
108
198
|
});
|
|
109
199
|
}
|
|
110
200
|
|
|
201
|
+
const iso = (d: Date | null): string | undefined =>
|
|
202
|
+
d ? d.toISOString() : undefined;
|
|
203
|
+
|
|
204
|
+
/** Folds per-org sync detail into the page-level detail. */
|
|
205
|
+
function summarize(
|
|
206
|
+
orgs: readonly InstallationOrgView[],
|
|
207
|
+
): InstallationSyncDetail {
|
|
208
|
+
const complete = orgs.length > 0 && orgs.every((o) => o.complete === true);
|
|
209
|
+
const synced = orgs
|
|
210
|
+
.map((o) => o.syncedAt)
|
|
211
|
+
.filter((d): d is string => d !== undefined)
|
|
212
|
+
.sort();
|
|
213
|
+
const totals = orgs.map((o) => o.totalCount);
|
|
214
|
+
const totalCount = totals.every((t) => t !== undefined)
|
|
215
|
+
? totals.reduce<number>((a, t) => a + (t ?? 0), 0)
|
|
216
|
+
: undefined;
|
|
217
|
+
const lastError = orgs.find((o) => o.lastError)?.lastError;
|
|
218
|
+
const reset = orgs
|
|
219
|
+
.map((o) => o.rateLimitResetAt)
|
|
220
|
+
.filter((d): d is string => d !== undefined)
|
|
221
|
+
.sort()
|
|
222
|
+
.at(-1);
|
|
223
|
+
const attempts = Math.max(0, ...orgs.map((o) => o.attempts ?? 0));
|
|
224
|
+
const next = orgs
|
|
225
|
+
.map((o) => o.nextAttemptAt)
|
|
226
|
+
.filter((d): d is string => d !== undefined)
|
|
227
|
+
.sort()[0];
|
|
228
|
+
const tick = orgs
|
|
229
|
+
.map((o) => o.lastTickAt)
|
|
230
|
+
.filter((d): d is string => d !== undefined)
|
|
231
|
+
.sort()
|
|
232
|
+
.at(-1);
|
|
233
|
+
const holder = orgs.find((o) => o.leaseHolder)?.leaseHolder;
|
|
234
|
+
return {
|
|
235
|
+
complete,
|
|
236
|
+
...(attempts > 0 ? { attempts } : {}),
|
|
237
|
+
...(next ? { nextAttemptAt: next } : {}),
|
|
238
|
+
...(holder ? { leaseHolder: holder } : {}),
|
|
239
|
+
...(tick ? { lastTickAt: tick } : {}),
|
|
240
|
+
reposSeen: orgs.reduce((a, o) => a + (o.reposSeen ?? 0), 0),
|
|
241
|
+
...(complete && synced[0] ? { syncedAt: synced[0] } : {}),
|
|
242
|
+
...(orgs.length > 0 && totalCount !== undefined ? { totalCount } : {}),
|
|
243
|
+
...(lastError ? { lastError } : {}),
|
|
244
|
+
...(reset ? { rateLimitResetAt: reset } : {}),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
111
248
|
export class InstallationReposService {
|
|
112
249
|
constructor(private readonly deps: InstallationReposDeps) {}
|
|
113
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Reads the agent-installation cache (crawled in the background) and the
|
|
253
|
+
* cached access rows; it never calls GitHub. A hint appears only when the
|
|
254
|
+
* org's snapshot is complete and every repo has an ok access row.
|
|
255
|
+
*/
|
|
114
256
|
async list(agentId: string): Promise<InstallationListResult> {
|
|
115
257
|
const agent = await this.deps.agents.get(agentId);
|
|
116
258
|
if (!agent) return { status: "agent_not_found" };
|
|
117
259
|
if (agent.appId === undefined) {
|
|
118
260
|
return { status: "no_app_id", reason: MISSING_APP_ID_REASON };
|
|
119
261
|
}
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return {
|
|
126
|
-
status: "skipped",
|
|
127
|
-
appId: agent.appId,
|
|
128
|
-
reason: NO_INSTALLATION_REASON,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
262
|
+
const cache = this.deps.cache;
|
|
263
|
+
const accountId = agent.accountId || DEPLOYMENT_WIDE_ACCOUNT_KEY;
|
|
264
|
+
const snapshots = cache
|
|
265
|
+
? await cache.listAgentSnapshots(accountId, String(agent.appId))
|
|
266
|
+
: [];
|
|
131
267
|
const username = agent.githubUsername?.trim();
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
268
|
+
const orgs: InstallationOrgView[] = [];
|
|
269
|
+
for (const s of snapshots) {
|
|
270
|
+
const names = s.repos.map((r) => r.fullName);
|
|
271
|
+
let repos: InstallationRepoView[] = names.map((fullName) => ({
|
|
272
|
+
fullName,
|
|
273
|
+
}));
|
|
274
|
+
if (cache && username && s.completedAt && names.length > 0) {
|
|
275
|
+
try {
|
|
276
|
+
const rows = await cache.getAccessRows({
|
|
277
|
+
accountId,
|
|
278
|
+
org: s.org,
|
|
279
|
+
username,
|
|
280
|
+
repos: names.map((f) => f.slice(f.indexOf("/") + 1)),
|
|
281
|
+
});
|
|
282
|
+
repos = userPushHints({ cachedRows: rows }, s.org, names);
|
|
283
|
+
} catch {
|
|
284
|
+
// The hint is advisory: a failed cache read omits it.
|
|
285
|
+
}
|
|
139
286
|
}
|
|
287
|
+
orgs.push({
|
|
288
|
+
org: s.org,
|
|
289
|
+
repos,
|
|
290
|
+
complete: s.completedAt !== null,
|
|
291
|
+
reposSeen: names.length,
|
|
292
|
+
...(s.completedAt ? { syncedAt: iso(s.completedAt) } : {}),
|
|
293
|
+
...(s.totalCount !== null ? { totalCount: s.totalCount } : {}),
|
|
294
|
+
...(s.lastError ? { lastError: s.lastError } : {}),
|
|
295
|
+
...(s.rateLimitResetAt
|
|
296
|
+
? { rateLimitResetAt: iso(s.rateLimitResetAt) }
|
|
297
|
+
: {}),
|
|
298
|
+
...(s.attempts ? { attempts: s.attempts } : {}),
|
|
299
|
+
...(s.nextAttemptAt ? { nextAttemptAt: iso(s.nextAttemptAt) } : {}),
|
|
300
|
+
...(s.leaseHolder && s.leasedUntil && s.leasedUntil > new Date()
|
|
301
|
+
? { leaseHolder: s.leaseHolder }
|
|
302
|
+
: {}),
|
|
303
|
+
...(s.lastTickAt ? { lastTickAt: iso(s.lastTickAt) } : {}),
|
|
304
|
+
});
|
|
140
305
|
}
|
|
141
|
-
return {
|
|
142
|
-
status: "ok",
|
|
143
|
-
appId: agent.appId,
|
|
144
|
-
orgs: installed.orgs.map(({ org, repos }) => ({
|
|
145
|
-
org,
|
|
146
|
-
repos: userPushHints(access, org, repos),
|
|
147
|
-
})),
|
|
148
|
-
};
|
|
306
|
+
return { status: "ok", appId: agent.appId, orgs, ...summarize(orgs) };
|
|
149
307
|
}
|
|
150
308
|
}
|
|
151
309
|
|