@app-vitals/agent-admin 1.86.0 → 1.87.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.
@@ -25,6 +25,11 @@
25
25
  */
26
26
 
27
27
  import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
28
+ import {
29
+ GITHUB_APPS_STATE_REASONS,
30
+ type GithubAppsState,
31
+ type GithubAppsStateFn,
32
+ } from "./github-apps-state.ts";
28
33
  import { DEPLOYMENT_WIDE_ACCOUNT_KEY } from "./github-agent-installation-crawler.ts";
29
34
  import { DEFAULT_REFRESH_STALENESS_CAP_MS } from "./github-repo-refresher.ts";
30
35
  import {
@@ -104,6 +109,8 @@ export interface NotAppliedRepo {
104
109
  export interface GithubSyncPlan {
105
110
  skipped: boolean;
106
111
  skipReason?: string;
112
+ /** Set when the skip is because an App role is missing (additive). */
113
+ appsState?: Exclude<GithubAppsState, "both">;
107
114
  appAdds: string[];
108
115
  appRemoves: string[];
109
116
  agentRemoves: string[];
@@ -291,11 +298,17 @@ export interface GithubSyncDeps {
291
298
  audit: InstallationAuditPort;
292
299
  /** Optional: with none, nothing is listed or blocked. */
293
300
  openWork?: OpenWorkPort;
301
+ /** Optional: which Apps are usable; without both, Sync is skipped with a specific reason. */
302
+ appsState?: GithubAppsStateFn;
294
303
  }
295
304
 
296
305
  export type GithubSyncPreviewResult =
297
306
  | { status: "agent_not_found" }
298
- | { status: "skipped"; reason: string }
307
+ | {
308
+ status: "skipped";
309
+ reason: string;
310
+ appsState?: Exclude<GithubAppsState, "both">;
311
+ }
299
312
  | {
300
313
  status: "previewed";
301
314
  plan: GithubSyncPlan;
@@ -312,7 +325,11 @@ export interface GithubSyncApplyInput extends SyncLists {
312
325
 
313
326
  export type GithubSyncApplyResult =
314
327
  | { status: "agent_not_found" }
315
- | { status: "skipped"; reason: string }
328
+ | {
329
+ status: "skipped";
330
+ reason: string;
331
+ appsState?: Exclude<GithubAppsState, "both">;
332
+ }
316
333
  | { status: "noop"; plan: GithubSyncPlan }
317
334
  | { status: "removal_cap"; plan: GithubSyncPlan; removals: number }
318
335
  | {
@@ -341,7 +358,11 @@ export class GithubSyncService {
341
358
  if (!agent) return { status: "agent_not_found" };
342
359
  const plan = await this.planFor(agent);
343
360
  if (plan.skipped) {
344
- return { status: "skipped", reason: plan.skipReason ?? "skipped" };
361
+ return {
362
+ status: "skipped",
363
+ reason: plan.skipReason ?? "skipped",
364
+ ...(plan.appsState ? { appsState: plan.appsState } : {}),
365
+ };
345
366
  }
346
367
  const work = await previewRepoRemovalWork(
347
368
  this.deps.openWork,
@@ -361,7 +382,11 @@ export class GithubSyncService {
361
382
  if (!agent) return { status: "agent_not_found" };
362
383
  let plan = await this.planFor(agent, input);
363
384
  if (plan.skipped) {
364
- return { status: "skipped", reason: plan.skipReason ?? "skipped" };
385
+ return {
386
+ status: "skipped",
387
+ reason: plan.skipReason ?? "skipped",
388
+ ...(plan.appsState ? { appsState: plan.appsState } : {}),
389
+ };
365
390
  }
366
391
  plan = await this.reverifyRemovals(plan, agent);
367
392
  if (plan.appAdds.length + plan.removedRepos.length === 0) {
@@ -568,6 +593,15 @@ export class GithubSyncService {
568
593
  if (!username) {
569
594
  return skippedPlan(accessSkipReason(username, null) as string);
570
595
  }
596
+ if (this.deps.appsState) {
597
+ const state = await this.deps.appsState(agent.accountId);
598
+ if (state !== "both") {
599
+ return {
600
+ ...skippedPlan(GITHUB_APPS_STATE_REASONS[state]),
601
+ appsState: state,
602
+ };
603
+ }
604
+ }
571
605
  let liveAccess: UserRepoAccessResult | null = null;
572
606
  if (!this.deps.cache) {
573
607
  const preview = await this.deps.access(username, agent.accountId);
@@ -7,6 +7,11 @@
7
7
  * through the installation port.
8
8
  */
9
9
 
10
+ import {
11
+ GITHUB_APPS_STATE_REASONS,
12
+ type GithubAppsState,
13
+ type GithubAppsStateFn,
14
+ } from "./github-apps-state.ts";
10
15
  import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
11
16
  import { DEPLOYMENT_WIDE_ACCOUNT_KEY } from "./github-agent-installation-crawler.ts";
12
17
  import {
@@ -128,6 +133,9 @@ export type InstallationListResult =
128
133
  status: "ok";
129
134
  appId: number;
130
135
  orgs: InstallationOrgView[];
136
+ /** Set when an App role is missing; the listing is the cached crawl only (additive). */
137
+ appsState?: Exclude<GithubAppsState, "both">;
138
+ reason?: string;
131
139
  } & InstallationSyncDetail);
132
140
 
133
141
  export interface InstallationReposDeps {
@@ -137,6 +145,8 @@ export interface InstallationReposDeps {
137
145
  installation: InstallationReposPort;
138
146
  /** The repo/access cache list() reads. Without it list() reports nothing synced. */
139
147
  cache?: InstallationCachePort;
148
+ /** Optional: which Apps are usable; without both, list() says why (additive fields). */
149
+ appsState?: GithubAppsStateFn;
140
150
  }
141
151
 
142
152
  /** The agent's App identity keys (client id, slug), when stored. */
@@ -338,7 +348,18 @@ export class InstallationReposService {
338
348
  ...(s.lastTickAt ? { lastTickAt: iso(s.lastTickAt) } : {}),
339
349
  });
340
350
  }
341
- return { status: "ok", appId: agent.appId, orgs, ...summarize(orgs) };
351
+ const state = this.deps.appsState
352
+ ? await this.deps.appsState(agent.accountId)
353
+ : "both";
354
+ return {
355
+ status: "ok",
356
+ appId: agent.appId,
357
+ orgs,
358
+ ...summarize(orgs),
359
+ ...(state !== "both"
360
+ ? { appsState: state, reason: GITHUB_APPS_STATE_REASONS[state] }
361
+ : {}),
362
+ };
342
363
  }
343
364
  }
344
365
 
@@ -123,8 +123,16 @@ export interface LookupPort {
123
123
  ): Promise<RepoRole>;
124
124
  }
125
125
 
126
+ /** Lookup-App org source: GithubLookupClient satisfies this. */
127
+ export interface LookupInstallationsPort {
128
+ listInstallations(): Promise<{ login: string }[]>;
129
+ }
130
+
126
131
  export interface UserRepoAccessResolverOptions {
127
- installer: InstallerPort;
132
+ /** Org source when configured: the orgs the installer App can install on. */
133
+ installer?: InstallerPort;
134
+ /** Org source when no installer is configured: the lookup App's own installations. */
135
+ lookupInstallations?: LookupInstallationsPort;
128
136
  lookup: LookupPort;
129
137
  now?: () => Date;
130
138
  /** Max concurrent per-repo permission calls (default 5). */
@@ -149,13 +157,15 @@ function kindOf(e: unknown): string | undefined {
149
157
  }
150
158
 
151
159
  export class UserRepoAccessResolver {
152
- private readonly installer: InstallerPort;
160
+ private readonly installer?: InstallerPort;
161
+ private readonly lookupInstallations?: LookupInstallationsPort;
153
162
  private readonly lookup: LookupPort;
154
163
  private readonly now: () => Date;
155
164
  private readonly concurrency: number;
156
165
 
157
166
  constructor(opts: UserRepoAccessResolverOptions) {
158
167
  this.installer = opts.installer;
168
+ this.lookupInstallations = opts.lookupInstallations;
159
169
  this.lookup = opts.lookup;
160
170
  this.now = opts.now ?? (() => new Date());
161
171
  this.concurrency = Math.max(
@@ -165,9 +175,7 @@ export class UserRepoAccessResolver {
165
175
  }
166
176
 
167
177
  async resolve(username: string): Promise<UserRepoAccessResult> {
168
- const candidates = (
169
- await this.installer.listInstallableOrganizations()
170
- ).map((o) => o.login);
178
+ const candidates = await this.listOrgs();
171
179
  const verdict = await this.verifyUser(candidates, username);
172
180
  if (verdict === "not_found") {
173
181
  return this.result(username, "user_not_found", true, []);
@@ -179,6 +187,19 @@ export class UserRepoAccessResolver {
179
187
  return this.result(username, "ok", verdict === "verified", orgs);
180
188
  }
181
189
 
190
+ /** The installer's installable orgs when configured, else the lookup App's installations. */
191
+ private async listOrgs(): Promise<string[]> {
192
+ const source = this.installer
193
+ ? await this.installer.listInstallableOrganizations()
194
+ : await this.lookupInstallations?.listInstallations();
195
+ if (!source) {
196
+ throw new Error(
197
+ "UserRepoAccessResolver needs an org source: installer or lookupInstallations",
198
+ );
199
+ }
200
+ return source.map((o) => o.login);
201
+ }
202
+
182
203
  /** One getUser check up front, on the first org that can mint a token. */
183
204
  private async verifyUser(
184
205
  orgs: string[],
@@ -103,6 +103,86 @@ describe("selected-repositories installation", () => {
103
103
  });
104
104
  });
105
105
 
106
+ describe("org source", () => {
107
+ function build(opts: {
108
+ installer?: string[];
109
+ lookupInstallations?: string[];
110
+ }) {
111
+ const calls: string[] = [];
112
+ const repos = { one: ["a"], two: ["b"], three: ["c"] } as Record<
113
+ string,
114
+ string[]
115
+ >;
116
+ const lookup: LookupPort = {
117
+ getOrgToken: async () => ({
118
+ token: "t",
119
+ expiresAt: "x",
120
+ installationId: 1,
121
+ }),
122
+ getUser: async (_o, login) => ({ id: 1, login, type: "User" }),
123
+ listInstallationRepositories: async (org) =>
124
+ (repos[org] ?? []).map((n) => repo(org, n)),
125
+ getCollaboratorPermission: async () => "write",
126
+ };
127
+ return {
128
+ calls,
129
+ resolver: new UserRepoAccessResolver({
130
+ ...(opts.installer
131
+ ? {
132
+ installer: {
133
+ listInstallableOrganizations: async () => {
134
+ calls.push("installer");
135
+ return (opts.installer ?? []).map((login, id) => ({
136
+ id,
137
+ login,
138
+ }));
139
+ },
140
+ },
141
+ }
142
+ : {}),
143
+ ...(opts.lookupInstallations
144
+ ? {
145
+ lookupInstallations: {
146
+ listInstallations: async () => {
147
+ calls.push("lookup");
148
+ return (opts.lookupInstallations ?? []).map((login, id) => ({
149
+ id,
150
+ login,
151
+ }));
152
+ },
153
+ },
154
+ }
155
+ : {}),
156
+ lookup,
157
+ now: () => NOW,
158
+ }),
159
+ };
160
+ }
161
+
162
+ it("lookup-only: orgs come from the lookup App installations", async () => {
163
+ const { resolver, calls } = build({ lookupInstallations: ["one", "two"] });
164
+ const res = await resolver.resolve("octocat");
165
+ expect(res.orgs.map((o) => o.org)).toEqual(["one", "two"]);
166
+ expect(res.outcome).toBe("ok");
167
+ expect(calls).toEqual(["lookup"]);
168
+ });
169
+
170
+ it("installer configured: the installer's orgs win and lookup is not listed", async () => {
171
+ const { resolver, calls } = build({
172
+ installer: ["three"],
173
+ lookupInstallations: ["one", "two"],
174
+ });
175
+ const res = await resolver.resolve("octocat");
176
+ expect(res.orgs.map((o) => o.org)).toEqual(["three"]);
177
+ expect(calls).toEqual(["installer"]);
178
+ });
179
+
180
+ it("no org source at all fails loudly", async () => {
181
+ const { resolver } = build({});
182
+ await expect(resolver.resolve("octocat")).rejects.toThrow(/org source/);
183
+ });
184
+ });
185
+
106
186
  describe("canPushForRole", () => {
107
187
  const cases: Array<[RepoRole, boolean]> = [
108
188
  ["admin", true],