@app-vitals/agent-admin 1.86.1 → 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.
@@ -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],