lastlight-core 0.23.8 → 0.24.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/deploy/.env.production.example +3 -1
- package/deploy/native/lastlight.env.example +3 -1
- package/dist/admin/routes.js +32 -1
- package/dist/admin/routes.js.map +1 -1
- package/dist/config/config.d.ts +11 -2
- package/dist/config/config.js +2 -1
- package/dist/config/config.js.map +1 -1
- package/dist/connectors/github-webhook.d.ts +12 -0
- package/dist/connectors/github-webhook.js +54 -13
- package/dist/connectors/github-webhook.js.map +1 -1
- package/dist/engine/agent-executor.js +66 -21
- package/dist/engine/agent-executor.js.map +1 -1
- package/dist/engine/github/app-jwt.d.ts +12 -0
- package/dist/engine/github/app-jwt.js +25 -0
- package/dist/engine/github/app-jwt.js.map +1 -0
- package/dist/engine/github/git-auth.js +2 -12
- package/dist/engine/github/git-auth.js.map +1 -1
- package/dist/engine/github/github-tools.d.ts +16 -4
- package/dist/engine/github/github-tools.js +58 -3
- package/dist/engine/github/github-tools.js.map +1 -1
- package/dist/engine/github/github.d.ts +68 -9
- package/dist/engine/github/github.js +167 -42
- package/dist/engine/github/github.js.map +1 -1
- package/dist/engine/github/installations.d.ts +147 -0
- package/dist/engine/github/installations.js +296 -0
- package/dist/engine/github/installations.js.map +1 -0
- package/dist/index.js +69 -27
- package/dist/index.js.map +1 -1
- package/dist/managed-repos.d.ts +14 -7
- package/dist/managed-repos.js +48 -22
- package/dist/managed-repos.js.map +1 -1
- package/dist/workflows/handlers/post-review.js +0 -1
- package/dist/workflows/handlers/post-review.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One installation of the GitHub App — i.e. one account (user or org) that
|
|
3
|
+
* installed it. A single App has N of these, and **each has its own
|
|
4
|
+
* installation id**, which is what an installation-token mint is scoped to.
|
|
5
|
+
*/
|
|
6
|
+
export interface AppInstallation {
|
|
7
|
+
/** Installation id, as a string (the mint URL takes it verbatim). */
|
|
8
|
+
id: string;
|
|
9
|
+
/** Account login the App is installed on (`cliftonc`, `mirevue`, …). */
|
|
10
|
+
account: string;
|
|
11
|
+
/** `Organization` | `User` — decides the settings URL shape, see below. */
|
|
12
|
+
accountType: string;
|
|
13
|
+
/** `all` = every repo in the account; `selected` = an explicit subset. */
|
|
14
|
+
repositorySelection: "all" | "selected";
|
|
15
|
+
suspended: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* GitHub's settings page for one installation — where an operator goes to
|
|
19
|
+
* change its repo grant, suspend it, or uninstall.
|
|
20
|
+
*
|
|
21
|
+
* The path differs by account type, which is the whole reason this is a
|
|
22
|
+
* function rather than a template: an **org** install lives under
|
|
23
|
+
* `/organizations/<login>/settings/installations/<id>`, a **personal** one under
|
|
24
|
+
* `/settings/installations/<id>` (no login in the path — GitHub scopes it to the
|
|
25
|
+
* viewer). Guessing wrong 404s.
|
|
26
|
+
*
|
|
27
|
+
* Returns `undefined` when the account type isn't known yet — a record seeded
|
|
28
|
+
* from a webhook that carried no `account.type` — so a caller renders plain text
|
|
29
|
+
* rather than a link that might not resolve.
|
|
30
|
+
*/
|
|
31
|
+
export declare function installationSettingsUrl(installation: AppInstallation): string | undefined;
|
|
32
|
+
export interface InstallationDirectoryConfig {
|
|
33
|
+
appId: string;
|
|
34
|
+
privateKeyPath: string;
|
|
35
|
+
/**
|
|
36
|
+
* Override the GitHub REST base URL. Prod leaves this unset →
|
|
37
|
+
* `api.github.com`. Mirrors `GitHubAppClientConfig.baseUrl` — a test/eval
|
|
38
|
+
* escape hatch only.
|
|
39
|
+
*/
|
|
40
|
+
baseUrl?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Optional legacy seed: the single `GITHUB_APP_INSTALLATION_ID` an older
|
|
43
|
+
* deployment configured. It carries no account login, so it can't answer
|
|
44
|
+
* `resolve()` on its own — it's kept only as the last-resort answer when the
|
|
45
|
+
* App JWT lookup is unavailable (no network, a revoked PEM), so an existing
|
|
46
|
+
* single-installation deployment degrades to exactly its old behaviour rather
|
|
47
|
+
* than to no behaviour.
|
|
48
|
+
*/
|
|
49
|
+
fallbackInstallationId?: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The single authority for **owner → installation id**.
|
|
53
|
+
*
|
|
54
|
+
* A GitHub App is installed per *account*, so an App installed on two accounts
|
|
55
|
+
* has two installation ids and a token minted against the wrong one 422s with
|
|
56
|
+
* "at least one repository ... is not accessible to the parent installation".
|
|
57
|
+
* Last Light used to carry one statically-configured id
|
|
58
|
+
* (`GITHUB_APP_INSTALLATION_ID`) threaded into every mint and every
|
|
59
|
+
* App-authenticated Octokit, which is exactly that bug for every account but the
|
|
60
|
+
* first.
|
|
61
|
+
*
|
|
62
|
+
* Two feeds, both cheap:
|
|
63
|
+
* - **`note()`** — every webhook delivery carries `payload.installation.id`
|
|
64
|
+
* alongside the account login. Authoritative, free, and covers the account
|
|
65
|
+
* the event came from before we ever ask GitHub anything.
|
|
66
|
+
* - **`refresh()`** — `GET /app/installations` under an App JWT. Needed for the
|
|
67
|
+
* routes with no webhook behind them (boot discovery, cron fan-out, CLI/API
|
|
68
|
+
* triggers).
|
|
69
|
+
*
|
|
70
|
+
* A miss triggers at most one in-flight refresh (`REFRESH_MIN_INTERVAL_MS`
|
|
71
|
+
* apart), so a cron fanning out over N repos in an uninstalled org costs one
|
|
72
|
+
* request, not N.
|
|
73
|
+
*/
|
|
74
|
+
export declare class InstallationDirectory {
|
|
75
|
+
private readonly config;
|
|
76
|
+
/** owner login (lowercased) → installation id. */
|
|
77
|
+
private byOwner;
|
|
78
|
+
/** installation id → the full record, for the admin/ops surface. */
|
|
79
|
+
private byId;
|
|
80
|
+
/** owner login (lowercased) → epoch ms when "not installed" was concluded. */
|
|
81
|
+
private negative;
|
|
82
|
+
private inFlight;
|
|
83
|
+
private lastRefreshAt;
|
|
84
|
+
constructor(config: InstallationDirectoryConfig);
|
|
85
|
+
/**
|
|
86
|
+
* Record an owner→installation mapping learned out of band (a webhook
|
|
87
|
+
* payload). Cheaper and fresher than any lookup, so it always wins.
|
|
88
|
+
*/
|
|
89
|
+
note(owner: string, id: string | number | undefined | null,
|
|
90
|
+
/** `account.type` from the payload — lets the admin surface link the install. */
|
|
91
|
+
accountType?: string): void;
|
|
92
|
+
/** Drop an installation entirely (the App was uninstalled from that account). */
|
|
93
|
+
forget(id: string | number): void;
|
|
94
|
+
/**
|
|
95
|
+
* Mark an installation suspended or restored.
|
|
96
|
+
*
|
|
97
|
+
* A suspended installation still EXISTS — it stays in the listing and keeps
|
|
98
|
+
* its id — but every token mint against it 403s. So it is dropped from
|
|
99
|
+
* `resolve()` (the run fails fast, before a sandbox) while staying in
|
|
100
|
+
* `list()`, where the admin surface can say "suspended" rather than the
|
|
101
|
+
* account simply vanishing.
|
|
102
|
+
*/
|
|
103
|
+
setSuspended(id: string | number, suspended: boolean): void;
|
|
104
|
+
/** Cached snapshot — never triggers a fetch. Ordered by account login. */
|
|
105
|
+
list(): AppInstallation[];
|
|
106
|
+
/**
|
|
107
|
+
* The installation id when the App is installed on exactly ONE account.
|
|
108
|
+
*
|
|
109
|
+
* The answer for a run that names no owner (a Slack-scoped `explore`, say):
|
|
110
|
+
* it still wants a GitHub token, and with a single installation there is no
|
|
111
|
+
* ambiguity about which — that is the deployment this harness supported
|
|
112
|
+
* before it grew multi-installation support, so it keeps behaving identically.
|
|
113
|
+
* With several installations there is no defensible pick, so the caller goes
|
|
114
|
+
* without a token rather than guessing an account.
|
|
115
|
+
*/
|
|
116
|
+
soleInstallationId(): Promise<string | undefined>;
|
|
117
|
+
/**
|
|
118
|
+
* The installation id for `owner`, or `undefined` when the App isn't
|
|
119
|
+
* installed on that account. Cache → one shared refresh → a cached negative.
|
|
120
|
+
*
|
|
121
|
+
* `undefined` means **we asked and the App is genuinely not installed there**,
|
|
122
|
+
* so the caller can fail with an actionable "install the App on `<owner>`"
|
|
123
|
+
* message instead of letting a mint 422 with GitHub's opaque wording. A
|
|
124
|
+
* lookup *failure* (network, bad PEM) is a different thing and falls back to
|
|
125
|
+
* the legacy single id rather than claiming the account isn't installed.
|
|
126
|
+
*/
|
|
127
|
+
resolve(owner: string): Promise<string | undefined>;
|
|
128
|
+
/**
|
|
129
|
+
* Re-read every installation of the App. Concurrent callers share one
|
|
130
|
+
* request — a cron fan-out resolves N owners against a single round-trip.
|
|
131
|
+
*/
|
|
132
|
+
refresh(): Promise<AppInstallation[]>;
|
|
133
|
+
private fetchAll;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Install the process-wide directory. Called once at boot from the App creds in
|
|
137
|
+
* runtime config; safe to call again (a re-init replaces the cache).
|
|
138
|
+
*/
|
|
139
|
+
export declare function initInstallationDirectory(config: InstallationDirectoryConfig): InstallationDirectory;
|
|
140
|
+
/**
|
|
141
|
+
* The process-wide directory, or `undefined` when no GitHub App is configured
|
|
142
|
+
* (PAT / chat-only mode). Callers that need a mapping must handle `undefined` —
|
|
143
|
+
* they are in token mode, where there is no installation to resolve.
|
|
144
|
+
*/
|
|
145
|
+
export declare function getInstallationDirectory(): InstallationDirectory | undefined;
|
|
146
|
+
/** Test-only: drop the singleton so each test starts from a clean directory. */
|
|
147
|
+
export declare function resetInstallationDirectoryForTests(): void;
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { logger } from "../../logging/logger.js";
|
|
2
|
+
import { appJwt } from "./app-jwt.js";
|
|
3
|
+
const log = logger("installations");
|
|
4
|
+
/**
|
|
5
|
+
* GitHub's settings page for one installation — where an operator goes to
|
|
6
|
+
* change its repo grant, suspend it, or uninstall.
|
|
7
|
+
*
|
|
8
|
+
* The path differs by account type, which is the whole reason this is a
|
|
9
|
+
* function rather than a template: an **org** install lives under
|
|
10
|
+
* `/organizations/<login>/settings/installations/<id>`, a **personal** one under
|
|
11
|
+
* `/settings/installations/<id>` (no login in the path — GitHub scopes it to the
|
|
12
|
+
* viewer). Guessing wrong 404s.
|
|
13
|
+
*
|
|
14
|
+
* Returns `undefined` when the account type isn't known yet — a record seeded
|
|
15
|
+
* from a webhook that carried no `account.type` — so a caller renders plain text
|
|
16
|
+
* rather than a link that might not resolve.
|
|
17
|
+
*/
|
|
18
|
+
export function installationSettingsUrl(installation) {
|
|
19
|
+
if (installation.accountType === "Organization") {
|
|
20
|
+
return `https://github.com/organizations/${installation.account}/settings/installations/${installation.id}`;
|
|
21
|
+
}
|
|
22
|
+
if (installation.accountType === "User") {
|
|
23
|
+
return `https://github.com/settings/installations/${installation.id}`;
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
/** How long a "this owner has no installation" answer is trusted. */
|
|
28
|
+
const NEGATIVE_TTL_MS = 60_000;
|
|
29
|
+
/** Floor between two full refreshes triggered by a cache miss. */
|
|
30
|
+
const REFRESH_MIN_INTERVAL_MS = 10_000;
|
|
31
|
+
/**
|
|
32
|
+
* The single authority for **owner → installation id**.
|
|
33
|
+
*
|
|
34
|
+
* A GitHub App is installed per *account*, so an App installed on two accounts
|
|
35
|
+
* has two installation ids and a token minted against the wrong one 422s with
|
|
36
|
+
* "at least one repository ... is not accessible to the parent installation".
|
|
37
|
+
* Last Light used to carry one statically-configured id
|
|
38
|
+
* (`GITHUB_APP_INSTALLATION_ID`) threaded into every mint and every
|
|
39
|
+
* App-authenticated Octokit, which is exactly that bug for every account but the
|
|
40
|
+
* first.
|
|
41
|
+
*
|
|
42
|
+
* Two feeds, both cheap:
|
|
43
|
+
* - **`note()`** — every webhook delivery carries `payload.installation.id`
|
|
44
|
+
* alongside the account login. Authoritative, free, and covers the account
|
|
45
|
+
* the event came from before we ever ask GitHub anything.
|
|
46
|
+
* - **`refresh()`** — `GET /app/installations` under an App JWT. Needed for the
|
|
47
|
+
* routes with no webhook behind them (boot discovery, cron fan-out, CLI/API
|
|
48
|
+
* triggers).
|
|
49
|
+
*
|
|
50
|
+
* A miss triggers at most one in-flight refresh (`REFRESH_MIN_INTERVAL_MS`
|
|
51
|
+
* apart), so a cron fanning out over N repos in an uninstalled org costs one
|
|
52
|
+
* request, not N.
|
|
53
|
+
*/
|
|
54
|
+
export class InstallationDirectory {
|
|
55
|
+
config;
|
|
56
|
+
/** owner login (lowercased) → installation id. */
|
|
57
|
+
byOwner = new Map();
|
|
58
|
+
/** installation id → the full record, for the admin/ops surface. */
|
|
59
|
+
byId = new Map();
|
|
60
|
+
/** owner login (lowercased) → epoch ms when "not installed" was concluded. */
|
|
61
|
+
negative = new Map();
|
|
62
|
+
inFlight = null;
|
|
63
|
+
lastRefreshAt = 0;
|
|
64
|
+
constructor(config) {
|
|
65
|
+
this.config = config;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Record an owner→installation mapping learned out of band (a webhook
|
|
69
|
+
* payload). Cheaper and fresher than any lookup, so it always wins.
|
|
70
|
+
*/
|
|
71
|
+
note(owner, id,
|
|
72
|
+
/** `account.type` from the payload — lets the admin surface link the install. */
|
|
73
|
+
accountType) {
|
|
74
|
+
if (!owner || id === undefined || id === null || id === "")
|
|
75
|
+
return;
|
|
76
|
+
const key = owner.toLowerCase();
|
|
77
|
+
const value = String(id);
|
|
78
|
+
// Never resurrect a suspended installation: it still delivers its own
|
|
79
|
+
// `installation` events, and resolving to it would put the account back in
|
|
80
|
+
// service against tokens GitHub will 403.
|
|
81
|
+
if (this.byId.get(value)?.suspended)
|
|
82
|
+
return;
|
|
83
|
+
this.negative.delete(key);
|
|
84
|
+
const known = this.byId.get(value);
|
|
85
|
+
if (known) {
|
|
86
|
+
// Fill in a type we didn't have — a record seeded by an earlier event
|
|
87
|
+
// that carried no `account.type` can't be linked until we learn it.
|
|
88
|
+
if (!known.accountType && accountType)
|
|
89
|
+
known.accountType = accountType;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
this.byId.set(value, {
|
|
93
|
+
id: value,
|
|
94
|
+
account: owner,
|
|
95
|
+
accountType: accountType ?? "",
|
|
96
|
+
repositorySelection: "all",
|
|
97
|
+
suspended: false,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (this.byOwner.get(key) === value)
|
|
101
|
+
return;
|
|
102
|
+
this.byOwner.set(key, value);
|
|
103
|
+
log.info("Learned installation from event", { owner, installationId: value });
|
|
104
|
+
}
|
|
105
|
+
/** Drop an installation entirely (the App was uninstalled from that account). */
|
|
106
|
+
forget(id) {
|
|
107
|
+
const value = String(id);
|
|
108
|
+
const record = this.byId.get(value);
|
|
109
|
+
this.byId.delete(value);
|
|
110
|
+
for (const [owner, installationId] of this.byOwner) {
|
|
111
|
+
if (installationId === value)
|
|
112
|
+
this.byOwner.delete(owner);
|
|
113
|
+
}
|
|
114
|
+
log.info("Forgot installation", { installationId: value, account: record?.account });
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Mark an installation suspended or restored.
|
|
118
|
+
*
|
|
119
|
+
* A suspended installation still EXISTS — it stays in the listing and keeps
|
|
120
|
+
* its id — but every token mint against it 403s. So it is dropped from
|
|
121
|
+
* `resolve()` (the run fails fast, before a sandbox) while staying in
|
|
122
|
+
* `list()`, where the admin surface can say "suspended" rather than the
|
|
123
|
+
* account simply vanishing.
|
|
124
|
+
*/
|
|
125
|
+
setSuspended(id, suspended) {
|
|
126
|
+
const value = String(id);
|
|
127
|
+
const record = this.byId.get(value);
|
|
128
|
+
if (record)
|
|
129
|
+
record.suspended = suspended;
|
|
130
|
+
const account = record?.account;
|
|
131
|
+
if (suspended) {
|
|
132
|
+
for (const [owner, installationId] of this.byOwner) {
|
|
133
|
+
if (installationId === value)
|
|
134
|
+
this.byOwner.delete(owner);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else if (account) {
|
|
138
|
+
this.byOwner.set(account.toLowerCase(), value);
|
|
139
|
+
this.negative.delete(account.toLowerCase());
|
|
140
|
+
}
|
|
141
|
+
log.info(suspended ? "Installation suspended" : "Installation unsuspended", {
|
|
142
|
+
installationId: value,
|
|
143
|
+
account,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
/** Cached snapshot — never triggers a fetch. Ordered by account login. */
|
|
147
|
+
list() {
|
|
148
|
+
return [...this.byId.values()].sort((a, b) => a.account.localeCompare(b.account));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The installation id when the App is installed on exactly ONE account.
|
|
152
|
+
*
|
|
153
|
+
* The answer for a run that names no owner (a Slack-scoped `explore`, say):
|
|
154
|
+
* it still wants a GitHub token, and with a single installation there is no
|
|
155
|
+
* ambiguity about which — that is the deployment this harness supported
|
|
156
|
+
* before it grew multi-installation support, so it keeps behaving identically.
|
|
157
|
+
* With several installations there is no defensible pick, so the caller goes
|
|
158
|
+
* without a token rather than guessing an account.
|
|
159
|
+
*/
|
|
160
|
+
async soleInstallationId() {
|
|
161
|
+
if (this.byId.size === 0) {
|
|
162
|
+
try {
|
|
163
|
+
await this.refresh();
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return this.config.fallbackInstallationId;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (this.byId.size === 1)
|
|
170
|
+
return [...this.byId.keys()][0];
|
|
171
|
+
return this.byId.size === 0 ? this.config.fallbackInstallationId : undefined;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The installation id for `owner`, or `undefined` when the App isn't
|
|
175
|
+
* installed on that account. Cache → one shared refresh → a cached negative.
|
|
176
|
+
*
|
|
177
|
+
* `undefined` means **we asked and the App is genuinely not installed there**,
|
|
178
|
+
* so the caller can fail with an actionable "install the App on `<owner>`"
|
|
179
|
+
* message instead of letting a mint 422 with GitHub's opaque wording. A
|
|
180
|
+
* lookup *failure* (network, bad PEM) is a different thing and falls back to
|
|
181
|
+
* the legacy single id rather than claiming the account isn't installed.
|
|
182
|
+
*/
|
|
183
|
+
async resolve(owner) {
|
|
184
|
+
if (!owner)
|
|
185
|
+
return undefined;
|
|
186
|
+
const key = owner.toLowerCase();
|
|
187
|
+
const cached = this.byOwner.get(key);
|
|
188
|
+
if (cached)
|
|
189
|
+
return cached;
|
|
190
|
+
const deniedAt = this.negative.get(key);
|
|
191
|
+
if (deniedAt !== undefined && Date.now() - deniedAt < NEGATIVE_TTL_MS) {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
if (Date.now() - this.lastRefreshAt >= REFRESH_MIN_INTERVAL_MS || this.inFlight) {
|
|
195
|
+
try {
|
|
196
|
+
await this.refresh();
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
// Never cache a negative off a failed lookup — that would report a
|
|
200
|
+
// perfectly good installation as missing for the next minute.
|
|
201
|
+
log.warn("Installation lookup failed", { owner, err });
|
|
202
|
+
return this.config.fallbackInstallationId;
|
|
203
|
+
}
|
|
204
|
+
const found = this.byOwner.get(key);
|
|
205
|
+
if (found)
|
|
206
|
+
return found;
|
|
207
|
+
}
|
|
208
|
+
this.negative.set(key, Date.now());
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Re-read every installation of the App. Concurrent callers share one
|
|
213
|
+
* request — a cron fan-out resolves N owners against a single round-trip.
|
|
214
|
+
*/
|
|
215
|
+
refresh() {
|
|
216
|
+
if (this.inFlight)
|
|
217
|
+
return this.inFlight;
|
|
218
|
+
this.inFlight = this.fetchAll().finally(() => {
|
|
219
|
+
this.inFlight = null;
|
|
220
|
+
this.lastRefreshAt = Date.now();
|
|
221
|
+
});
|
|
222
|
+
return this.inFlight;
|
|
223
|
+
}
|
|
224
|
+
async fetchAll() {
|
|
225
|
+
const jwt = appJwt(this.config.appId, this.config.privateKeyPath);
|
|
226
|
+
const base = (this.config.baseUrl ?? "https://api.github.com").replace(/\/+$/, "");
|
|
227
|
+
const found = [];
|
|
228
|
+
// `GET /app/installations` is App-JWT-authed and paginated. 100/page is the
|
|
229
|
+
// maximum; an App with more than a handful of installations is unusual, but
|
|
230
|
+
// paginating costs nothing and a truncated list would silently look like
|
|
231
|
+
// "not installed" for every account past the first page.
|
|
232
|
+
for (let page = 1; page <= 20; page++) {
|
|
233
|
+
const res = await fetch(`${base}/app/installations?per_page=100&page=${page}`, {
|
|
234
|
+
headers: {
|
|
235
|
+
Authorization: `Bearer ${jwt}`,
|
|
236
|
+
Accept: "application/vnd.github+json",
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
if (!res.ok) {
|
|
240
|
+
throw new Error(`GitHub App installations request failed (${res.status}): ${await res.text()}`);
|
|
241
|
+
}
|
|
242
|
+
const batch = (await res.json());
|
|
243
|
+
if (!Array.isArray(batch) || batch.length === 0)
|
|
244
|
+
break;
|
|
245
|
+
for (const raw of batch) {
|
|
246
|
+
const account = raw.account?.login;
|
|
247
|
+
if (!account)
|
|
248
|
+
continue;
|
|
249
|
+
found.push({
|
|
250
|
+
id: String(raw.id),
|
|
251
|
+
account,
|
|
252
|
+
accountType: raw.account?.type ?? "",
|
|
253
|
+
repositorySelection: raw.repository_selection === "selected" ? "selected" : "all",
|
|
254
|
+
suspended: !!raw.suspended_at,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
if (batch.length < 100)
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
// Replace wholesale — an installation that vanished from the listing was
|
|
261
|
+
// uninstalled, and keeping its stale mapping would keep minting 404s. A
|
|
262
|
+
// SUSPENDED one is still listed but cannot mint, so it is kept for the admin
|
|
263
|
+
// surface and withheld from resolution.
|
|
264
|
+
this.byOwner = new Map(found.filter((i) => !i.suspended).map((i) => [i.account.toLowerCase(), i.id]));
|
|
265
|
+
this.byId = new Map(found.map((i) => [i.id, i]));
|
|
266
|
+
this.negative.clear();
|
|
267
|
+
log.info("Refreshed App installations", {
|
|
268
|
+
count: found.length,
|
|
269
|
+
accounts: found.map((i) => `${i.account}=${i.id}`).join(","),
|
|
270
|
+
});
|
|
271
|
+
return found;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
// ── Module singleton ────────────────────────────────────────────────
|
|
275
|
+
let directory = null;
|
|
276
|
+
/**
|
|
277
|
+
* Install the process-wide directory. Called once at boot from the App creds in
|
|
278
|
+
* runtime config; safe to call again (a re-init replaces the cache).
|
|
279
|
+
*/
|
|
280
|
+
export function initInstallationDirectory(config) {
|
|
281
|
+
directory = new InstallationDirectory(config);
|
|
282
|
+
return directory;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* The process-wide directory, or `undefined` when no GitHub App is configured
|
|
286
|
+
* (PAT / chat-only mode). Callers that need a mapping must handle `undefined` —
|
|
287
|
+
* they are in token mode, where there is no installation to resolve.
|
|
288
|
+
*/
|
|
289
|
+
export function getInstallationDirectory() {
|
|
290
|
+
return directory ?? undefined;
|
|
291
|
+
}
|
|
292
|
+
/** Test-only: drop the singleton so each test starts from a clean directory. */
|
|
293
|
+
export function resetInstallationDirectoryForTests() {
|
|
294
|
+
directory = null;
|
|
295
|
+
}
|
|
296
|
+
//# sourceMappingURL=installations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"installations.js","sourceRoot":"","sources":["../../../src/engine/github/installations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,MAAM,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AAmBpC;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,uBAAuB,CAAC,YAA6B;IACnE,IAAI,YAAY,CAAC,WAAW,KAAK,cAAc,EAAE,CAAC;QAChD,OAAO,oCAAoC,YAAY,CAAC,OAAO,2BAA2B,YAAY,CAAC,EAAE,EAAE,CAAC;IAC9G,CAAC;IACD,IAAI,YAAY,CAAC,WAAW,KAAK,MAAM,EAAE,CAAC;QACxC,OAAO,6CAA6C,YAAY,CAAC,EAAE,EAAE,CAAC;IACxE,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAsBD,qEAAqE;AACrE,MAAM,eAAe,GAAG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AAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACjD,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QAEtB,GAAG,CAAC,IAAI,CAAC,6BAA6B,EAAE;YACtC,KAAK,EAAE,KAAK,CAAC,MAAM;YACnB,QAAQ,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;SAC7D,CAAC,CAAC;QACH,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAED,uEAAuE;AAEvE,IAAI,SAAS,GAAiC,IAAI,CAAC;AAEnD;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CACvC,MAAmC;IAEnC,SAAS,GAAG,IAAI,qBAAqB,CAAC,MAAM,CAAC,CAAC;IAC9C,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB;IACtC,OAAO,SAAS,IAAI,SAAS,CAAC;AAChC,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,kCAAkC;IAChD,SAAS,GAAG,IAAI,CAAC;AACnB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,7 @@ import { configureWorkflowAssets, validateAssets, getWorkflow } from "./workflow
|
|
|
12
12
|
import { ChatRunner } from "./engine/chat/chat-runner.js";
|
|
13
13
|
import { buildReadSkillTool, loadChatSkillCatalogue } from "./engine/chat/chat-skills.js";
|
|
14
14
|
import { configureGitAuth } from "./engine/github/git-auth.js";
|
|
15
|
+
import { getInstallationDirectory, initInstallationDirectory, } from "./engine/github/installations.js";
|
|
15
16
|
import { StateDb, isTriggerActorType } from "./state/db.js";
|
|
16
17
|
import { CronScheduler } from "./cron/scheduler.js";
|
|
17
18
|
import { getJobs } from "./cron/jobs.js";
|
|
@@ -73,9 +74,13 @@ function validateConfig(config) {
|
|
|
73
74
|
process.exit(78); // EX_CONFIG — sysexits.h convention
|
|
74
75
|
};
|
|
75
76
|
if (config.githubApp) {
|
|
76
|
-
const { appId, privateKeyPath
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
const { appId, privateKeyPath } = config.githubApp;
|
|
78
|
+
// No installation-id check: installations are DISCOVERED from the App JWT
|
|
79
|
+
// and resolved per repo owner, because an App installed on several accounts
|
|
80
|
+
// has one id per account. `GITHUB_APP_INSTALLATION_ID` is honoured as a
|
|
81
|
+
// legacy fallback when set, and is no longer required.
|
|
82
|
+
if (!appId) {
|
|
83
|
+
fatal("GITHUB_APP_ID is required when the GitHub App is configured.");
|
|
79
84
|
}
|
|
80
85
|
if (!existsSync(resolve(privateKeyPath))) {
|
|
81
86
|
fatal(`GITHUB_APP_PRIVATE_KEY_PATH points to "${privateKeyPath}" which does not exist.`);
|
|
@@ -210,21 +215,16 @@ async function main() {
|
|
|
210
215
|
else {
|
|
211
216
|
chatLog.warn("No skills loaded — frontmatter missing or no matching SKILL.md found");
|
|
212
217
|
}
|
|
213
|
-
//
|
|
214
|
-
//
|
|
215
|
-
//
|
|
218
|
+
// The App may be installed on several ACCOUNTS, each with its own installation
|
|
219
|
+
// id — and a token minted against the wrong one is rejected. The directory is
|
|
220
|
+
// the single owner→installation authority every mint and every App-authed
|
|
221
|
+
// Octokit resolves through, so it has to exist before either.
|
|
216
222
|
if (config.githubApp) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
botLogin: config.botLogin,
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
catch (err) {
|
|
226
|
-
logger("git-auth").warn("Initial token mint failed (will retry per-execution)", { err });
|
|
227
|
-
}
|
|
223
|
+
initInstallationDirectory({
|
|
224
|
+
appId: config.githubApp.appId,
|
|
225
|
+
privateKeyPath: config.githubApp.privateKeyPath,
|
|
226
|
+
fallbackInstallationId: config.githubApp.installationId,
|
|
227
|
+
});
|
|
228
228
|
}
|
|
229
229
|
// GitHub API client for harness-level operations (posting comments, fetching
|
|
230
230
|
// issues). App auth when configured; otherwise a PAT (read-only unless the
|
|
@@ -244,21 +244,60 @@ async function main() {
|
|
|
244
244
|
botLogin: config.botLogin,
|
|
245
245
|
botMention: `@${config.botName}`,
|
|
246
246
|
});
|
|
247
|
-
// Discover the repos the App
|
|
248
|
-
// list. When the overlay's `managedRepos` is empty this
|
|
249
|
-
// allowlist (getManagedRepos falls back to
|
|
250
|
-
// Kept live afterwards by installation webhooks
|
|
251
|
-
// on failure we fall back to whatever
|
|
252
|
-
// before the HTTP listener opens, so the
|
|
247
|
+
// Discover the repos the App can access — across EVERY installation — and seed
|
|
248
|
+
// the managed-repo list. When the overlay's `managedRepos` is empty this
|
|
249
|
+
// becomes the effective allowlist (getManagedRepos falls back to the union); a
|
|
250
|
+
// configured list still wins. Kept live afterwards by installation webhooks
|
|
251
|
+
// (github-webhook.ts). Non-fatal: on failure we fall back to whatever
|
|
252
|
+
// `managedRepos` config provides. Runs before the HTTP listener opens, so the
|
|
253
|
+
// list is warm before the first event.
|
|
253
254
|
if (github && config.githubApp) {
|
|
254
255
|
const githubLog = logger("github");
|
|
255
256
|
try {
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
257
|
+
const grants = await github.listAllInstallationRepos();
|
|
258
|
+
for (const grant of grants) {
|
|
259
|
+
if (grant.error) {
|
|
260
|
+
githubLog.warn("Installation repo discovery failed for one account", {
|
|
261
|
+
account: grant.account,
|
|
262
|
+
installationId: grant.installationId,
|
|
263
|
+
error: grant.error,
|
|
264
|
+
});
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
setInstallationRepos(grant.installationId, grant.repos);
|
|
268
|
+
}
|
|
269
|
+
githubLog.info("Discovered App installations", {
|
|
270
|
+
installations: grants.length,
|
|
271
|
+
accounts: grants.map((g) => `${g.account}=${g.installationId}(${g.repos.length})`).join(","),
|
|
272
|
+
});
|
|
273
|
+
if (grants.length === 0) {
|
|
274
|
+
githubLog.warn("The GitHub App has no installations — install it on an account before it can act");
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
catch (err) {
|
|
278
|
+
githubLog.warn("Installation discovery failed", { err });
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// Configure git with a GitHub App token — only meaningful when the operator
|
|
282
|
+
// opted into a global `~/.gitconfig` write (LASTLIGHT_WRITE_GLOBAL_GIT=1) and
|
|
283
|
+
// only defensible with a single installation, since a global credential can
|
|
284
|
+
// name exactly one. Every agent run mints its own owner-scoped token
|
|
285
|
+
// regardless, so this is non-fatal and skipped when ambiguous.
|
|
286
|
+
if (config.githubApp) {
|
|
287
|
+
const gitAuthLog = logger("git-auth");
|
|
288
|
+
try {
|
|
289
|
+
const soleInstallation = await getInstallationDirectory()?.soleInstallationId();
|
|
290
|
+
if (soleInstallation) {
|
|
291
|
+
await configureGitAuth({
|
|
292
|
+
appId: config.githubApp.appId,
|
|
293
|
+
privateKeyPath: config.githubApp.privateKeyPath,
|
|
294
|
+
installationId: soleInstallation,
|
|
295
|
+
botLogin: config.botLogin,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
259
298
|
}
|
|
260
299
|
catch (err) {
|
|
261
|
-
|
|
300
|
+
gitAuthLog.warn("Initial token mint failed (will retry per-execution)", { err });
|
|
262
301
|
}
|
|
263
302
|
}
|
|
264
303
|
// Late-bound: constructed after resumeOpts (below) because it closes over
|
|
@@ -895,6 +934,9 @@ async function main() {
|
|
|
895
934
|
// and a repo that opts itself into `after-checks` under an `eager`
|
|
896
935
|
// operator is covered by the 30-minute `check-prs-awaiting-review` sweep.
|
|
897
936
|
reviewTrigger: () => config.review.trigger,
|
|
937
|
+
// The harness client memoizes an Octokit per installation; drop it when
|
|
938
|
+
// the App is uninstalled so a re-install can't be served by a dead one.
|
|
939
|
+
onInstallationRemoved: (installationId) => github?.forgetInstallation(installationId),
|
|
898
940
|
});
|
|
899
941
|
registry.register(githubConnector);
|
|
900
942
|
}
|