@installagent/sdk 0.1.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/dist/agent-templates.d.ts +90 -0
- package/dist/agent-templates.js +211 -0
- package/dist/client-build.d.ts +50 -0
- package/dist/client-build.js +155 -0
- package/dist/client-context.d.ts +40 -0
- package/dist/client-context.js +70 -0
- package/dist/client-drive.d.ts +67 -0
- package/dist/client-drive.js +278 -0
- package/dist/client-intake.d.ts +68 -0
- package/dist/client-intake.js +189 -0
- package/dist/client-linear.d.ts +30 -0
- package/dist/client-linear.js +85 -0
- package/dist/docs-search.d.ts +44 -0
- package/dist/docs-search.js +187 -0
- package/dist/doctor.d.ts +43 -0
- package/dist/doctor.js +425 -0
- package/dist/env-file.d.ts +25 -0
- package/dist/env-file.js +69 -0
- package/dist/env-fill.d.ts +43 -0
- package/dist/env-fill.js +161 -0
- package/dist/env-push.d.ts +37 -0
- package/dist/env-push.js +137 -0
- package/dist/env-registry.d.ts +49 -0
- package/dist/env-registry.js +230 -0
- package/dist/exec.d.ts +14 -0
- package/dist/exec.js +16 -0
- package/dist/fathom-webhook.d.ts +44 -0
- package/dist/fathom-webhook.js +123 -0
- package/dist/fs-utils.d.ts +9 -0
- package/dist/fs-utils.js +34 -0
- package/dist/gcp-service-account-key.d.ts +37 -0
- package/dist/gcp-service-account-key.js +66 -0
- package/dist/gemini-api-key.d.ts +47 -0
- package/dist/gemini-api-key.js +132 -0
- package/dist/git.d.ts +16 -0
- package/dist/git.js +29 -0
- package/dist/github-token.d.ts +24 -0
- package/dist/github-token.js +30 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +28 -0
- package/dist/init.d.ts +47 -0
- package/dist/init.js +101 -0
- package/dist/linear-project.d.ts +81 -0
- package/dist/linear-project.js +192 -0
- package/dist/new-client.d.ts +32 -0
- package/dist/new-client.js +89 -0
- package/dist/ping.d.ts +6 -0
- package/dist/ping.js +7 -0
- package/dist/portal-admin.d.ts +133 -0
- package/dist/portal-admin.js +144 -0
- package/dist/sync-templates.d.ts +58 -0
- package/dist/sync-templates.js +109 -0
- package/dist/template-sync.d.ts +55 -0
- package/dist/template-sync.js +90 -0
- package/dist/templates.d.ts +19 -0
- package/dist/templates.js +24 -0
- package/dist/wizard.d.ts +78 -0
- package/dist/wizard.js +213 -0
- package/package.json +30 -0
- package/templates/env.example +60 -0
- package/templates/workspace-root/.claude/settings.json +39 -0
- package/templates/workspace-root/AGENT.md +205 -0
- package/templates/workspace-root/AGENTS.md +11 -0
- package/templates/workspace-root/CLAUDE.md +63 -0
- package/templates/workspace-root/GEMINI.md +24 -0
- package/templates/workspace-root/ONBOARDING.md +164 -0
- package/templates/workspace-root/README.md +67 -0
- package/templates/workspace-root/skills/CLAUDE.md +79 -0
- package/templates/workspace-root/skills/client-deliverables/SKILL.md +131 -0
- package/templates/workspace-root/skills/client-deliverables/references/package-layout.md +103 -0
- package/templates/workspace-root/skills/client-facing-docs/SKILL.md +131 -0
- package/templates/workspace-root/skills/client-facing-docs/references/doc-structures.md +81 -0
- package/templates/workspace-root/skills/client-llm-setup/SKILL.md +97 -0
- package/templates/workspace-root/skills/client-llm-setup/references/guide-template.md +87 -0
- package/templates/workspace-root/skills/client-workflow-diagram/SKILL.md +141 -0
- package/templates/workspace-root/skills/client-workflow-diagram/references/excalidraw-format.md +140 -0
- package/templates/workspace-root/skills/contract-tech-agnostic/SKILL.md +99 -0
- package/templates/workspace-root/skills/contract-tech-agnostic/references/Install_Agent_SOW_Template.docx +0 -0
- package/templates/workspace-root/skills/design-system/SKILL.md +118 -0
- package/templates/workspace-root/skills/design-system/references/tokens.md +139 -0
- package/templates/workspace-root/skills/engagement-spec/SKILL.md +272 -0
- package/templates/workspace-root/skills/engagement-spec/references/spec-templates.md +220 -0
- package/templates/workspace-root/skills/house-voice/SKILL.md +158 -0
- package/templates/workspace-root/skills/house-voice/references/source-copy.md +173 -0
- package/templates/workspace-root/skills/ia-cli/SKILL.md +87 -0
- package/templates/workspace-root/skills/installagent-brand-kit/SKILL.md +112 -0
- package/templates/workspace-root/skills/installagent-brand-kit/assets/installagent-logo.svg +1 -0
- package/templates/workspace-root/skills/installagent-brand-kit/assets/installagent-mark.svg +1 -0
- package/templates/workspace-root/skills/installagent-brand-kit/references/brand-tokens.md +71 -0
- package/templates/workspace-root/skills/installagent-brand-kit/references/branded-pdf.md +66 -0
- package/templates/workspace-root/skills/installagent-brand-kit/references/excalidraw-red-theme.md +73 -0
- package/templates/workspace-root/skills/installagent-brand-kit/scripts/brand.py +175 -0
- package/templates/workspace-root/skills/linear/SKILL.md +186 -0
- package/templates/workspace-root/skills/tech-call-prep/SKILL.md +141 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { createSign } from "node:crypto";
|
|
2
|
+
import { SLUG_PATTERN } from "./new-client.js";
|
|
3
|
+
// Track ③ of docs/agent-context-sources-plan.md. Two credentials, both
|
|
4
|
+
// injectable for tests via fetchImpl — no new runtime dependencies, same
|
|
5
|
+
// shape as client-build.ts's raw-fetch GitHub client.
|
|
6
|
+
export const AIRTABLE_API_TOKEN_ENV = "AIRTABLE_API_TOKEN";
|
|
7
|
+
export const GOOGLE_SERVICE_ACCOUNT_KEY_ENV = "GOOGLE_SERVICE_ACCOUNT_KEY";
|
|
8
|
+
const AIRTABLE_API_BASE_URL = "https://api.airtable.com/v0";
|
|
9
|
+
/** "Clients - Master" — verified 2026-07-11 against the live base. */
|
|
10
|
+
const DEFAULT_AIRTABLE_BASE_ID = "appceZOwAY5Hktx56";
|
|
11
|
+
const DEFAULT_AIRTABLE_TABLE_ID = "tblqyf5OpOSKddvl2";
|
|
12
|
+
/** Field names, not IDs — Airtable's list-records API accepts either. */
|
|
13
|
+
const SLUG_FIELD = "Slug";
|
|
14
|
+
const DRIVE_FIELD = "Google Drive";
|
|
15
|
+
const GOOGLE_TOKEN_URL = "https://oauth2.googleapis.com/token";
|
|
16
|
+
const DRIVE_API_BASE_URL = "https://www.googleapis.com/drive/v3";
|
|
17
|
+
/** Read-only: this SA never needs to write. */
|
|
18
|
+
const DRIVE_SCOPE = "https://www.googleapis.com/auth/drive.readonly";
|
|
19
|
+
const JWT_LIFETIME_SECONDS = 3600;
|
|
20
|
+
const GOOGLE_DOC_MIME_TYPE = "application/vnd.google-apps.document";
|
|
21
|
+
const GOOGLE_FOLDER_MIME_TYPE = "application/vnd.google-apps.folder";
|
|
22
|
+
const DEFAULT_QUERY_LIMIT = 5;
|
|
23
|
+
const FETCH_CONCURRENCY = 6;
|
|
24
|
+
const MAX_FILES = 500;
|
|
25
|
+
const FOLDER_URL_PATTERN = /drive\.google\.com\/drive\/folders\/([\w-]+)/;
|
|
26
|
+
/** Thrown when the Airtable token or Google service-account key is missing or rejected. */
|
|
27
|
+
export class ClientDriveAuthError extends Error {
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Parse a service-account key JSON blob into the two fields the JWT grant
|
|
31
|
+
* needs. Exported for direct unit testing (mirrors the equivalent helper in
|
|
32
|
+
* team-web/apps/portal/lib/drive/google-auth.ts — same idea, not shared code:
|
|
33
|
+
* different repos never cross-import here).
|
|
34
|
+
*/
|
|
35
|
+
export function parseServiceAccountKey(raw) {
|
|
36
|
+
let parsed;
|
|
37
|
+
try {
|
|
38
|
+
parsed = JSON.parse(raw);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
throw new Error(`${GOOGLE_SERVICE_ACCOUNT_KEY_ENV} is not valid JSON — expected the full service-account key file contents`);
|
|
42
|
+
}
|
|
43
|
+
const obj = parsed;
|
|
44
|
+
const clientEmail = obj?.client_email;
|
|
45
|
+
const privateKey = obj?.private_key;
|
|
46
|
+
if (typeof clientEmail !== "string" || typeof privateKey !== "string") {
|
|
47
|
+
throw new Error(`${GOOGLE_SERVICE_ACCOUNT_KEY_ENV} is missing client_email/private_key — expected a full service-account key JSON`);
|
|
48
|
+
}
|
|
49
|
+
return { clientEmail, privateKey };
|
|
50
|
+
}
|
|
51
|
+
/** Exported for direct unit testing of the claim shape. */
|
|
52
|
+
export function buildAssertionClaims(clientEmail, nowMs) {
|
|
53
|
+
const iat = Math.floor(nowMs / 1000);
|
|
54
|
+
return {
|
|
55
|
+
iss: clientEmail,
|
|
56
|
+
scope: DRIVE_SCOPE,
|
|
57
|
+
aud: GOOGLE_TOKEN_URL,
|
|
58
|
+
iat,
|
|
59
|
+
exp: iat + JWT_LIFETIME_SECONDS,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function signAssertion(claims, privateKeyPem) {
|
|
63
|
+
const header = { alg: "RS256", typ: "JWT" };
|
|
64
|
+
const signingInput = `${base64url(JSON.stringify(header))}.${base64url(JSON.stringify(claims))}`;
|
|
65
|
+
const signature = createSign("RSA-SHA256").update(signingInput).end().sign(privateKeyPem);
|
|
66
|
+
return `${signingInput}.${base64url(signature)}`;
|
|
67
|
+
}
|
|
68
|
+
function base64url(input) {
|
|
69
|
+
return Buffer.from(input).toString("base64url");
|
|
70
|
+
}
|
|
71
|
+
async function getDriveAccessToken(serviceAccountKeyRaw, fetchImpl, now) {
|
|
72
|
+
const { clientEmail, privateKey } = parseServiceAccountKey(serviceAccountKeyRaw);
|
|
73
|
+
const assertion = signAssertion(buildAssertionClaims(clientEmail, now()), privateKey);
|
|
74
|
+
const res = await fetchImpl(GOOGLE_TOKEN_URL, {
|
|
75
|
+
method: "POST",
|
|
76
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
77
|
+
body: new URLSearchParams({
|
|
78
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
79
|
+
assertion,
|
|
80
|
+
}),
|
|
81
|
+
});
|
|
82
|
+
if (!res.ok) {
|
|
83
|
+
throw new ClientDriveAuthError(`Google rejected the service-account assertion for ${clientEmail} (HTTP ${res.status}) — ` +
|
|
84
|
+
`check ${GOOGLE_SERVICE_ACCOUNT_KEY_ENV} is current and the account has Drive access. ${await res.text()}`);
|
|
85
|
+
}
|
|
86
|
+
const body = (await res.json());
|
|
87
|
+
if (!body.access_token) {
|
|
88
|
+
throw new ClientDriveAuthError("Google's token response had no access_token");
|
|
89
|
+
}
|
|
90
|
+
return body.access_token;
|
|
91
|
+
}
|
|
92
|
+
/** Resolve the client's Drive folder ID from Airtable, or explain why it isn't one yet. */
|
|
93
|
+
async function resolveDriveFolderId(slug, fetchImpl, airtableToken, baseId, tableId) {
|
|
94
|
+
const url = new URL(`${AIRTABLE_API_BASE_URL}/${baseId}/${tableId}`);
|
|
95
|
+
url.searchParams.set("filterByFormula", `{${SLUG_FIELD}}='${slug}'`);
|
|
96
|
+
url.searchParams.set("maxRecords", "1");
|
|
97
|
+
url.searchParams.set("fields[]", DRIVE_FIELD);
|
|
98
|
+
const res = await fetchImpl(url, { headers: { Authorization: `Bearer ${airtableToken}` } });
|
|
99
|
+
if (res.status === 401 || res.status === 403) {
|
|
100
|
+
throw new ClientDriveAuthError(`Airtable rejected ${AIRTABLE_API_TOKEN_ENV} reading base ${baseId} (HTTP ${res.status}) — ` +
|
|
101
|
+
"check the token is valid and has data.records:read on it.");
|
|
102
|
+
}
|
|
103
|
+
if (!res.ok) {
|
|
104
|
+
throw new Error(`Airtable lookup for "${slug}" failed: HTTP ${res.status}`);
|
|
105
|
+
}
|
|
106
|
+
const body = (await res.json());
|
|
107
|
+
const record = body.records?.[0];
|
|
108
|
+
if (!record) {
|
|
109
|
+
throw new Error(`no "Clients - Master" record found for slug "${slug}" (Airtable base ${baseId}) — ` +
|
|
110
|
+
"check the Slug field matches exactly");
|
|
111
|
+
}
|
|
112
|
+
const folderUrl = record.fields?.[DRIVE_FIELD];
|
|
113
|
+
if (typeof folderUrl !== "string" || !folderUrl) {
|
|
114
|
+
throw new Error(`client "${slug}" has no "${DRIVE_FIELD}" link recorded in Airtable yet`);
|
|
115
|
+
}
|
|
116
|
+
const folderMatch = folderUrl.match(FOLDER_URL_PATTERN);
|
|
117
|
+
if (!folderMatch) {
|
|
118
|
+
throw new Error(`client "${slug}"'s "${DRIVE_FIELD}" field points at a Doc, not a Drive folder (${folderUrl}) — ` +
|
|
119
|
+
"this client isn't provisioned with a Drive folder yet (still a lead/prospect), so there's nothing to search");
|
|
120
|
+
}
|
|
121
|
+
return { folderId: folderMatch[1], folderUrl };
|
|
122
|
+
}
|
|
123
|
+
async function listFilesRecursive(folderId, accessToken, fetchImpl, path = "") {
|
|
124
|
+
const files = [];
|
|
125
|
+
const subfolders = [];
|
|
126
|
+
let pageToken;
|
|
127
|
+
do {
|
|
128
|
+
const url = new URL(`${DRIVE_API_BASE_URL}/files`);
|
|
129
|
+
url.searchParams.set("q", `'${folderId}' in parents and trashed=false`);
|
|
130
|
+
url.searchParams.set("fields", "nextPageToken,files(id,name,mimeType)");
|
|
131
|
+
url.searchParams.set("supportsAllDrives", "true");
|
|
132
|
+
url.searchParams.set("includeItemsFromAllDrives", "true");
|
|
133
|
+
url.searchParams.set("pageSize", "1000");
|
|
134
|
+
if (pageToken)
|
|
135
|
+
url.searchParams.set("pageToken", pageToken);
|
|
136
|
+
const res = await fetchImpl(url, { headers: { Authorization: `Bearer ${accessToken}` } });
|
|
137
|
+
if (res.status === 401 || res.status === 403) {
|
|
138
|
+
throw new ClientDriveAuthError(`Google Drive rejected the service account listing folder ${folderId} (HTTP ${res.status}) — ` +
|
|
139
|
+
"check the service account is a member of the client's Drive folder/shared drive.");
|
|
140
|
+
}
|
|
141
|
+
if (!res.ok) {
|
|
142
|
+
throw new Error(`Drive files.list on folder ${folderId} failed: HTTP ${res.status}`);
|
|
143
|
+
}
|
|
144
|
+
const body = (await res.json());
|
|
145
|
+
for (const entry of body.files ?? []) {
|
|
146
|
+
const entryPath = path ? `${path}/${entry.name}` : entry.name;
|
|
147
|
+
if (entry.mimeType === GOOGLE_FOLDER_MIME_TYPE) {
|
|
148
|
+
subfolders.push({ id: entry.id, path: entryPath });
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
files.push({ ...entry, path: entryPath });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
pageToken = body.nextPageToken;
|
|
155
|
+
} while (pageToken && files.length + subfolders.length < MAX_FILES);
|
|
156
|
+
for (const subfolder of subfolders) {
|
|
157
|
+
if (files.length >= MAX_FILES)
|
|
158
|
+
break;
|
|
159
|
+
files.push(...(await listFilesRecursive(subfolder.id, accessToken, fetchImpl, subfolder.path)));
|
|
160
|
+
}
|
|
161
|
+
return files.slice(0, MAX_FILES);
|
|
162
|
+
}
|
|
163
|
+
async function exportDocText(fileId, accessToken, fetchImpl) {
|
|
164
|
+
const url = new URL(`${DRIVE_API_BASE_URL}/files/${fileId}/export`);
|
|
165
|
+
url.searchParams.set("mimeType", "text/plain");
|
|
166
|
+
const res = await fetchImpl(url, { headers: { Authorization: `Bearer ${accessToken}` } });
|
|
167
|
+
if (!res.ok)
|
|
168
|
+
return null;
|
|
169
|
+
return res.text();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Read-only slice of a client's Google Drive folder (call transcripts,
|
|
173
|
+
* deliverables) — resolved via the client's Airtable "Google Drive" link,
|
|
174
|
+
* never guessed. This is the richest, heaviest of the three client context
|
|
175
|
+
* sources (docs/agent-context-sources-plan.md, track ③); complements
|
|
176
|
+
* client_context's registry brief and client_build's GitHub repo slice.
|
|
177
|
+
*
|
|
178
|
+
* Only Google Docs are text-exported and content-scored (transcripts and
|
|
179
|
+
* deliverables are Docs in practice); other file types are listed by name
|
|
180
|
+
* only. Strictly read-only. Never writes to Airtable or Drive.
|
|
181
|
+
*/
|
|
182
|
+
export async function clientDriveSearch(slug, options = {}) {
|
|
183
|
+
if (slug.startsWith("client-")) {
|
|
184
|
+
throw new Error(`pass the bare slug ("${slug.slice("client-".length)}"), not "${slug}" — the repo prefix is added for you`);
|
|
185
|
+
}
|
|
186
|
+
if (!SLUG_PATTERN.test(slug)) {
|
|
187
|
+
throw new Error(`invalid slug "${slug}" — use lowercase letters, digits, and hyphens`);
|
|
188
|
+
}
|
|
189
|
+
const airtableToken = options.airtableToken ?? process.env[AIRTABLE_API_TOKEN_ENV];
|
|
190
|
+
if (!airtableToken) {
|
|
191
|
+
throw new ClientDriveAuthError(`Airtable token required — set ${AIRTABLE_API_TOKEN_ENV} (a read-only PAT with data.records:read ` +
|
|
192
|
+
"on the Clients - Master base) or pass airtableToken");
|
|
193
|
+
}
|
|
194
|
+
const serviceAccountKey = options.serviceAccountKey ?? process.env[GOOGLE_SERVICE_ACCOUNT_KEY_ENV];
|
|
195
|
+
if (!serviceAccountKey) {
|
|
196
|
+
throw new ClientDriveAuthError(`Google service-account key required — set ${GOOGLE_SERVICE_ACCOUNT_KEY_ENV} (the full key JSON) ` +
|
|
197
|
+
"or pass serviceAccountKey");
|
|
198
|
+
}
|
|
199
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
200
|
+
const now = options.now ?? Date.now;
|
|
201
|
+
const baseId = options.airtableBaseId ?? DEFAULT_AIRTABLE_BASE_ID;
|
|
202
|
+
const tableId = options.airtableTableId ?? DEFAULT_AIRTABLE_TABLE_ID;
|
|
203
|
+
const { folderId, folderUrl } = await resolveDriveFolderId(slug, fetchImpl, airtableToken, baseId, tableId);
|
|
204
|
+
const accessToken = await getDriveAccessToken(serviceAccountKey, fetchImpl, now);
|
|
205
|
+
const files = await listFilesRecursive(folderId, accessToken, fetchImpl);
|
|
206
|
+
const query = options.query?.trim();
|
|
207
|
+
if (!query) {
|
|
208
|
+
return {
|
|
209
|
+
slug,
|
|
210
|
+
folderUrl,
|
|
211
|
+
docs: files
|
|
212
|
+
.map((file) => ({
|
|
213
|
+
path: file.path,
|
|
214
|
+
title: file.name,
|
|
215
|
+
mimeType: file.mimeType,
|
|
216
|
+
snippet: "",
|
|
217
|
+
}))
|
|
218
|
+
.sort((a, b) => a.path.localeCompare(b.path)),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
const terms = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
222
|
+
const docFiles = files.filter((f) => f.mimeType === GOOGLE_DOC_MIME_TYPE);
|
|
223
|
+
const contentByFileId = new Map();
|
|
224
|
+
for (let i = 0; i < docFiles.length; i += FETCH_CONCURRENCY) {
|
|
225
|
+
const chunk = await Promise.all(docFiles.slice(i, i + FETCH_CONCURRENCY).map(async (file) => {
|
|
226
|
+
const text = await exportDocText(file.id, accessToken, fetchImpl);
|
|
227
|
+
return text !== null ? [file.id, text] : undefined;
|
|
228
|
+
}));
|
|
229
|
+
for (const entry of chunk) {
|
|
230
|
+
if (entry)
|
|
231
|
+
contentByFileId.set(entry[0], entry[1]);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const limit = options.limit ?? DEFAULT_QUERY_LIMIT;
|
|
235
|
+
const matches = files
|
|
236
|
+
.map((file) => {
|
|
237
|
+
const content = contentByFileId.get(file.id);
|
|
238
|
+
const nameLower = file.name.toLowerCase();
|
|
239
|
+
let score = 0;
|
|
240
|
+
for (const term of terms) {
|
|
241
|
+
score += countOccurrences(nameLower, term) * 5;
|
|
242
|
+
if (content)
|
|
243
|
+
score += countOccurrences(content.toLowerCase(), term);
|
|
244
|
+
}
|
|
245
|
+
return { file, content, score };
|
|
246
|
+
})
|
|
247
|
+
.filter((entry) => entry.score > 0)
|
|
248
|
+
.sort((a, b) => b.score - a.score)
|
|
249
|
+
.slice(0, limit);
|
|
250
|
+
return {
|
|
251
|
+
slug,
|
|
252
|
+
folderUrl,
|
|
253
|
+
query,
|
|
254
|
+
docs: matches.map(({ file, content }) => ({
|
|
255
|
+
path: file.path,
|
|
256
|
+
title: file.name,
|
|
257
|
+
mimeType: file.mimeType,
|
|
258
|
+
snippet: content ? makeSnippet(content, terms) : `(not text-previewable — ${file.mimeType})`,
|
|
259
|
+
})),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
function countOccurrences(text, term) {
|
|
263
|
+
return term ? text.split(term).length - 1 : 0;
|
|
264
|
+
}
|
|
265
|
+
function makeSnippet(text, terms) {
|
|
266
|
+
const lower = text.toLowerCase();
|
|
267
|
+
let first = -1;
|
|
268
|
+
for (const term of terms) {
|
|
269
|
+
const idx = lower.indexOf(term);
|
|
270
|
+
if (idx !== -1 && (first === -1 || idx < first))
|
|
271
|
+
first = idx;
|
|
272
|
+
}
|
|
273
|
+
if (first === -1)
|
|
274
|
+
return text.slice(0, 160).trim();
|
|
275
|
+
const start = Math.max(0, first - 60);
|
|
276
|
+
const end = Math.min(text.length, first + 100);
|
|
277
|
+
return `${start > 0 ? "…" : ""}${text.slice(start, end).trim()}${end < text.length ? "…" : ""}`;
|
|
278
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { ExecFileFn } from "./exec.js";
|
|
2
|
+
import type { CloneFn } from "./git.js";
|
|
3
|
+
export declare const LINEAR_CLIENT_TEAM_ID_ENV = "LINEAR_CLIENT_TEAM_ID";
|
|
4
|
+
/** Thrown when the GitHub or Linear token is missing or rejected for a create call. */
|
|
5
|
+
export declare class ClientIntakeAuthError extends Error {
|
|
6
|
+
}
|
|
7
|
+
export interface ClientIntakeInput {
|
|
8
|
+
slug: string;
|
|
9
|
+
/** Client display name — used for the GitHub repo description and the Linear project name. */
|
|
10
|
+
name: string;
|
|
11
|
+
/** An EXISTING Airtable "Clients - Master" record URL; this op never creates one. */
|
|
12
|
+
airtableRecordUrl?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ClientIntakeOptions {
|
|
15
|
+
/** The installagent/ workspace root (the dir that contains clients/). */
|
|
16
|
+
workspaceDir: string;
|
|
17
|
+
/** GitHub token; defaults to $GITHUB_API_TOKEN, then `gh auth token`. Needs repo-creation rights on the org — broader than client-build's contents:read. */
|
|
18
|
+
githubToken?: string;
|
|
19
|
+
/** Injectable for tests (gh CLI probe); defaults to real execFile. */
|
|
20
|
+
execFileImpl?: ExecFileFn;
|
|
21
|
+
/** Linear API key; defaults to $LINEAR_API_TOKEN. */
|
|
22
|
+
linearToken?: string;
|
|
23
|
+
/** The fixed Linear team every client project is created under; defaults to $LINEAR_CLIENT_TEAM_ID. */
|
|
24
|
+
linearTeamId?: string;
|
|
25
|
+
/** Defaults to "InstallAgent". */
|
|
26
|
+
owner?: string;
|
|
27
|
+
/** Injectable for tests; defaults to global fetch. */
|
|
28
|
+
fetchImpl?: typeof fetch;
|
|
29
|
+
/** Injectable for tests; passed through to newClient. */
|
|
30
|
+
clone?: CloneFn;
|
|
31
|
+
}
|
|
32
|
+
export interface ClientIntakeStep {
|
|
33
|
+
name: string;
|
|
34
|
+
status: "created" | "failed";
|
|
35
|
+
detail: string;
|
|
36
|
+
}
|
|
37
|
+
export interface ClientIntakeResult {
|
|
38
|
+
slug: string;
|
|
39
|
+
/** Absolute path of the created clients/client-<slug>/ repo. */
|
|
40
|
+
dir: string;
|
|
41
|
+
ok: boolean;
|
|
42
|
+
steps: ClientIntakeStep[];
|
|
43
|
+
repoUrl: string | null;
|
|
44
|
+
linearProjectUrl: string | null;
|
|
45
|
+
/** Deterministic per-client vault name — printed as a next step, never created here. */
|
|
46
|
+
vaultName: string;
|
|
47
|
+
nextSteps: string[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Kick off a new client engagement end to end: scaffold the build repo
|
|
51
|
+
* (newClient), create its GitHub repo and Linear project (fixed team), and
|
|
52
|
+
* fill in what CLIENT.md can know deterministically. The GitHub and Linear
|
|
53
|
+
* calls are each attempted independently and recorded in `steps` — one
|
|
54
|
+
* failing doesn't stop the other, so a partial run's result shows exactly
|
|
55
|
+
* what still needs a retry or a manual step. Re-running after a partial
|
|
56
|
+
* failure is NOT supported (the scaffold step refuses to overwrite); finish
|
|
57
|
+
* remaining steps by hand per `nextSteps`.
|
|
58
|
+
*
|
|
59
|
+
* Deliberately stops short of: pushing the scaffolded repo to GitHub
|
|
60
|
+
* (prints the remote+push commands instead), creating the 1Password vault
|
|
61
|
+
* (prints the `op vault create` command — never touches 1Password, same
|
|
62
|
+
* "print, never auto-write" precedent as `secrets mint`), and anything in
|
|
63
|
+
* team-clients or Airtable — a client's registry PROFILE.md is written once
|
|
64
|
+
* at lead intake by the team-new-lead workflow, not here; this op only ever
|
|
65
|
+
* records an Airtable URL passed in, never fetches or creates one (golden
|
|
66
|
+
* rule 7).
|
|
67
|
+
*/
|
|
68
|
+
export declare function clientIntake(input: ClientIntakeInput, options: ClientIntakeOptions): Promise<ClientIntakeResult>;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { GITHUB_TOKEN_FIX, resolveGithubToken } from "./github-token.js";
|
|
4
|
+
import { LINEAR_API_TOKEN_ENV, linearRequest } from "./linear-project.js";
|
|
5
|
+
import { newClient } from "./new-client.js";
|
|
6
|
+
const GITHUB_API_BASE_URL = "https://api.github.com";
|
|
7
|
+
const DEFAULT_OWNER = "InstallAgent";
|
|
8
|
+
const DEFAULT_REPO_PREFIX = "client-";
|
|
9
|
+
export const LINEAR_CLIENT_TEAM_ID_ENV = "LINEAR_CLIENT_TEAM_ID";
|
|
10
|
+
// Literal placeholder tokens from clients/client-template/CLIENT.md — kept
|
|
11
|
+
// in sync by hand, same coupling newClient already has to the template's
|
|
12
|
+
// `<slug>` token.
|
|
13
|
+
const LINEAR_PLACEHOLDER = "<LINEAR_PROJECT_URL_HERE>";
|
|
14
|
+
const AIRTABLE_PLACEHOLDER = "<AIRTABLE_RECORD_URL_HERE>";
|
|
15
|
+
const VAULT_PLACEHOLDER = "<ONEPASSWORD_VAULT_NAME_HERE>";
|
|
16
|
+
/** Thrown when the GitHub or Linear token is missing or rejected for a create call. */
|
|
17
|
+
export class ClientIntakeAuthError extends Error {
|
|
18
|
+
}
|
|
19
|
+
async function createGithubRepo(fetchImpl, token, owner, slug, description) {
|
|
20
|
+
const res = await fetchImpl(`${GITHUB_API_BASE_URL}/orgs/${owner}/repos`, {
|
|
21
|
+
method: "POST",
|
|
22
|
+
headers: {
|
|
23
|
+
Authorization: `Bearer ${token}`,
|
|
24
|
+
Accept: "application/vnd.github+json",
|
|
25
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
26
|
+
"Content-Type": "application/json",
|
|
27
|
+
},
|
|
28
|
+
body: JSON.stringify({ name: `${DEFAULT_REPO_PREFIX}${slug}`, private: true, description }),
|
|
29
|
+
});
|
|
30
|
+
if (res.status === 401 || res.status === 403) {
|
|
31
|
+
throw new ClientIntakeAuthError(`GitHub rejected the token creating ${owner}/${DEFAULT_REPO_PREFIX}${slug} ` +
|
|
32
|
+
`(HTTP ${res.status}) — check it is valid and has repo-creation rights on ${owner}.`);
|
|
33
|
+
}
|
|
34
|
+
if (!res.ok) {
|
|
35
|
+
const body = (await res.json().catch(() => null));
|
|
36
|
+
throw new Error(`POST ${owner}/repos failed creating ${DEFAULT_REPO_PREFIX}${slug}: HTTP ${res.status}` +
|
|
37
|
+
(body?.message ? ` — ${body.message}` : ""));
|
|
38
|
+
}
|
|
39
|
+
const data = (await res.json());
|
|
40
|
+
return { htmlUrl: data.html_url, sshUrl: data.ssh_url };
|
|
41
|
+
}
|
|
42
|
+
const CREATE_PROJECT_MUTATION = `
|
|
43
|
+
mutation CreateProject($input: ProjectCreateInput!) {
|
|
44
|
+
projectCreate(input: $input) {
|
|
45
|
+
success
|
|
46
|
+
project { id name url }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
`;
|
|
50
|
+
async function createLinearProject(fetchImpl, token, teamId, name) {
|
|
51
|
+
const data = await linearRequest(fetchImpl, token, CREATE_PROJECT_MUTATION, { input: { name, teamIds: [teamId] } });
|
|
52
|
+
if (!data.projectCreate.success || !data.projectCreate.project) {
|
|
53
|
+
throw new Error(`Linear refused to create a project named "${name}"`);
|
|
54
|
+
}
|
|
55
|
+
return data.projectCreate.project;
|
|
56
|
+
}
|
|
57
|
+
async function patchClientMd(dir, values) {
|
|
58
|
+
const path = join(dir, "CLIENT.md");
|
|
59
|
+
let content = await readFile(path, "utf8");
|
|
60
|
+
if (values.linearProjectUrl) {
|
|
61
|
+
content = content.replace(LINEAR_PLACEHOLDER, values.linearProjectUrl);
|
|
62
|
+
}
|
|
63
|
+
if (values.airtableRecordUrl) {
|
|
64
|
+
content = content.replace(AIRTABLE_PLACEHOLDER, values.airtableRecordUrl);
|
|
65
|
+
}
|
|
66
|
+
content = content.replace(VAULT_PLACEHOLDER, values.vaultName);
|
|
67
|
+
await writeFile(path, content);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Kick off a new client engagement end to end: scaffold the build repo
|
|
71
|
+
* (newClient), create its GitHub repo and Linear project (fixed team), and
|
|
72
|
+
* fill in what CLIENT.md can know deterministically. The GitHub and Linear
|
|
73
|
+
* calls are each attempted independently and recorded in `steps` — one
|
|
74
|
+
* failing doesn't stop the other, so a partial run's result shows exactly
|
|
75
|
+
* what still needs a retry or a manual step. Re-running after a partial
|
|
76
|
+
* failure is NOT supported (the scaffold step refuses to overwrite); finish
|
|
77
|
+
* remaining steps by hand per `nextSteps`.
|
|
78
|
+
*
|
|
79
|
+
* Deliberately stops short of: pushing the scaffolded repo to GitHub
|
|
80
|
+
* (prints the remote+push commands instead), creating the 1Password vault
|
|
81
|
+
* (prints the `op vault create` command — never touches 1Password, same
|
|
82
|
+
* "print, never auto-write" precedent as `secrets mint`), and anything in
|
|
83
|
+
* team-clients or Airtable — a client's registry PROFILE.md is written once
|
|
84
|
+
* at lead intake by the team-new-lead workflow, not here; this op only ever
|
|
85
|
+
* records an Airtable URL passed in, never fetches or creates one (golden
|
|
86
|
+
* rule 7).
|
|
87
|
+
*/
|
|
88
|
+
export async function clientIntake(input, options) {
|
|
89
|
+
const scaffold = await newClient(input.slug, {
|
|
90
|
+
workspaceDir: options.workspaceDir,
|
|
91
|
+
clone: options.clone,
|
|
92
|
+
});
|
|
93
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
94
|
+
const owner = options.owner ?? DEFAULT_OWNER;
|
|
95
|
+
const vaultName = `${DEFAULT_REPO_PREFIX}${input.slug}`;
|
|
96
|
+
const steps = [];
|
|
97
|
+
let repoUrl = null;
|
|
98
|
+
let repoSshUrl = null;
|
|
99
|
+
let linearProjectUrl = null;
|
|
100
|
+
const resolvedGithub = await resolveGithubToken({
|
|
101
|
+
token: options.githubToken,
|
|
102
|
+
execFileImpl: options.execFileImpl,
|
|
103
|
+
});
|
|
104
|
+
const githubToken = resolvedGithub?.token;
|
|
105
|
+
if (!githubToken) {
|
|
106
|
+
steps.push({
|
|
107
|
+
name: "GitHub repo",
|
|
108
|
+
status: "failed",
|
|
109
|
+
detail: `no GitHub auth — ${GITHUB_TOKEN_FIX}, or create ${owner}/${DEFAULT_REPO_PREFIX}${input.slug} by hand.`,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
try {
|
|
114
|
+
const repo = await createGithubRepo(fetchImpl, githubToken, owner, input.slug, `Install Agent build repo — ${input.name}`);
|
|
115
|
+
repoUrl = repo.htmlUrl;
|
|
116
|
+
repoSshUrl = repo.sshUrl;
|
|
117
|
+
steps.push({ name: "GitHub repo", status: "created", detail: repo.htmlUrl });
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
steps.push({
|
|
121
|
+
name: "GitHub repo",
|
|
122
|
+
status: "failed",
|
|
123
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const linearToken = options.linearToken ?? process.env[LINEAR_API_TOKEN_ENV];
|
|
128
|
+
const linearTeamId = options.linearTeamId ?? process.env[LINEAR_CLIENT_TEAM_ID_ENV];
|
|
129
|
+
if (!linearToken || !linearTeamId) {
|
|
130
|
+
steps.push({
|
|
131
|
+
name: "Linear project",
|
|
132
|
+
status: "failed",
|
|
133
|
+
detail: !linearToken
|
|
134
|
+
? `${LINEAR_API_TOKEN_ENV} not set — create the Linear project by hand.`
|
|
135
|
+
: `${LINEAR_CLIENT_TEAM_ID_ENV} not set — create the Linear project by hand.`,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
try {
|
|
140
|
+
const project = await createLinearProject(fetchImpl, linearToken, linearTeamId, input.name);
|
|
141
|
+
linearProjectUrl = project.url;
|
|
142
|
+
steps.push({ name: "Linear project", status: "created", detail: project.url });
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
steps.push({
|
|
146
|
+
name: "Linear project",
|
|
147
|
+
status: "failed",
|
|
148
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
await patchClientMd(scaffold.dir, {
|
|
154
|
+
linearProjectUrl: linearProjectUrl ?? undefined,
|
|
155
|
+
airtableRecordUrl: input.airtableRecordUrl,
|
|
156
|
+
vaultName,
|
|
157
|
+
});
|
|
158
|
+
steps.push({ name: "CLIENT.md", status: "created", detail: "filled known keys" });
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
steps.push({
|
|
162
|
+
name: "CLIENT.md",
|
|
163
|
+
status: "failed",
|
|
164
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const nextSteps = [
|
|
168
|
+
repoSshUrl
|
|
169
|
+
? `Push the scaffolded repo: cd ${scaffold.dir} && git remote add origin ${repoSshUrl} && git push -u origin main`
|
|
170
|
+
: `Create ${owner}/${DEFAULT_REPO_PREFIX}${input.slug} on GitHub and push the scaffolded repo.`,
|
|
171
|
+
`Create the 1Password vault "${vaultName}" (e.g. \`op vault create ${vaultName}\`) and share it with the client's point of contact.`,
|
|
172
|
+
];
|
|
173
|
+
if (!input.airtableRecordUrl) {
|
|
174
|
+
nextSteps.push("Fill CLIENT.md's airtable_record once the Airtable deal record exists.");
|
|
175
|
+
}
|
|
176
|
+
nextSteps.push(`Confirm team-clients/clients/${input.slug}/PROFILE.md already exists (written once at lead ` +
|
|
177
|
+
"intake by the team-new-lead workflow) — this operation never creates or edits the registry.");
|
|
178
|
+
nextSteps.push("Run `node ci/repo-lint.mjs` inside the new repo once CLIENT.md keys are filled.");
|
|
179
|
+
return {
|
|
180
|
+
slug: input.slug,
|
|
181
|
+
dir: scaffold.dir,
|
|
182
|
+
ok: steps.every((step) => step.status !== "failed"),
|
|
183
|
+
steps,
|
|
184
|
+
repoUrl,
|
|
185
|
+
linearProjectUrl,
|
|
186
|
+
vaultName,
|
|
187
|
+
nextSteps,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ExecFileFn } from "./exec.js";
|
|
2
|
+
import { type LinearProjectOptions, type LinearProjectResult } from "./linear-project.js";
|
|
3
|
+
/** Thrown when the GitHub token is missing or rejected while fetching CLIENT.md. */
|
|
4
|
+
export declare class ClientLinearAuthError extends Error {
|
|
5
|
+
}
|
|
6
|
+
export interface ClientLinearProjectOptions extends LinearProjectOptions {
|
|
7
|
+
/** GitHub token for reading CLIENT.md; defaults to $GITHUB_API_TOKEN, then `gh auth token`. */
|
|
8
|
+
githubToken?: string;
|
|
9
|
+
/** Injectable for tests (gh CLI probe); defaults to real execFile. */
|
|
10
|
+
execFileImpl?: ExecFileFn;
|
|
11
|
+
/** Defaults to "InstallAgent". */
|
|
12
|
+
owner?: string;
|
|
13
|
+
/** Defaults to "client-". */
|
|
14
|
+
repoPrefix?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Open (not completed/canceled/duplicate) issues in a client's Linear
|
|
18
|
+
* project — "what's left to do" for that engagement. The project is
|
|
19
|
+
* resolved from the client's own `CLIENT.md` `linear_project` field (a
|
|
20
|
+
* `linear.app/.../project/<slugId>` URL, read via the GitHub contents API —
|
|
21
|
+
* same source of truth `clientBuild` reads from, never cloned) rather than
|
|
22
|
+
* guessed from the client's name: Linear project display names don't
|
|
23
|
+
* reliably match a client slug (spacing, capitalization, rebrands), and a
|
|
24
|
+
* guessed name could silently resolve to the wrong project. Once resolved,
|
|
25
|
+
* behaves exactly like `linearProject` — see `linearProjectIssues`.
|
|
26
|
+
*
|
|
27
|
+
* Strictly read-only: never touches CLIENT.md or Linear beyond GraphQL
|
|
28
|
+
* queries.
|
|
29
|
+
*/
|
|
30
|
+
export declare function clientLinearProject(slug: string, options?: ClientLinearProjectOptions): Promise<LinearProjectResult>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { GITHUB_TOKEN_FIX, resolveGithubToken } from "./github-token.js";
|
|
2
|
+
import { findProjectBySlugId, LINEAR_API_TOKEN_ENV, LinearAuthError, linearProjectIssues, } from "./linear-project.js";
|
|
3
|
+
import { SLUG_PATTERN } from "./new-client.js";
|
|
4
|
+
const GITHUB_API_BASE_URL = "https://api.github.com";
|
|
5
|
+
const DEFAULT_OWNER = "InstallAgent";
|
|
6
|
+
const DEFAULT_REPO_PREFIX = "client-";
|
|
7
|
+
const CLIENT_MD_PATH = "CLIENT.md";
|
|
8
|
+
const LINEAR_PROJECT_FIELD_PATTERN = /^linear_project:\s*(\S+)\s*$/m;
|
|
9
|
+
/** Thrown when the GitHub token is missing or rejected while fetching CLIENT.md. */
|
|
10
|
+
export class ClientLinearAuthError extends Error {
|
|
11
|
+
}
|
|
12
|
+
function parseSlugId(clientMd, slug) {
|
|
13
|
+
const match = clientMd.match(LINEAR_PROJECT_FIELD_PATTERN);
|
|
14
|
+
if (!match) {
|
|
15
|
+
throw new Error(`"${slug}"'s CLIENT.md has no linear_project field — can't resolve its Linear project.`);
|
|
16
|
+
}
|
|
17
|
+
const url = match[1];
|
|
18
|
+
const slugId = url.split("/").filter(Boolean).pop();
|
|
19
|
+
if (!slugId) {
|
|
20
|
+
throw new Error(`Could not parse a Linear project slug id out of "${url}" (from "${slug}"'s CLIENT.md).`);
|
|
21
|
+
}
|
|
22
|
+
return slugId;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Open (not completed/canceled/duplicate) issues in a client's Linear
|
|
26
|
+
* project — "what's left to do" for that engagement. The project is
|
|
27
|
+
* resolved from the client's own `CLIENT.md` `linear_project` field (a
|
|
28
|
+
* `linear.app/.../project/<slugId>` URL, read via the GitHub contents API —
|
|
29
|
+
* same source of truth `clientBuild` reads from, never cloned) rather than
|
|
30
|
+
* guessed from the client's name: Linear project display names don't
|
|
31
|
+
* reliably match a client slug (spacing, capitalization, rebrands), and a
|
|
32
|
+
* guessed name could silently resolve to the wrong project. Once resolved,
|
|
33
|
+
* behaves exactly like `linearProject` — see `linearProjectIssues`.
|
|
34
|
+
*
|
|
35
|
+
* Strictly read-only: never touches CLIENT.md or Linear beyond GraphQL
|
|
36
|
+
* queries.
|
|
37
|
+
*/
|
|
38
|
+
export async function clientLinearProject(slug, options = {}) {
|
|
39
|
+
if (slug.startsWith("client-")) {
|
|
40
|
+
throw new Error(`pass the bare slug ("${slug.slice("client-".length)}"), not "${slug}" — the repo prefix is added for you`);
|
|
41
|
+
}
|
|
42
|
+
if (!SLUG_PATTERN.test(slug)) {
|
|
43
|
+
throw new Error(`invalid slug "${slug}" — use lowercase letters, digits, and hyphens`);
|
|
44
|
+
}
|
|
45
|
+
const resolvedGithub = await resolveGithubToken({
|
|
46
|
+
token: options.githubToken,
|
|
47
|
+
execFileImpl: options.execFileImpl,
|
|
48
|
+
});
|
|
49
|
+
if (!resolvedGithub) {
|
|
50
|
+
throw new ClientLinearAuthError(`GitHub auth required to read CLIENT.md — ${GITHUB_TOKEN_FIX}`);
|
|
51
|
+
}
|
|
52
|
+
const githubToken = resolvedGithub.token;
|
|
53
|
+
const owner = options.owner ?? DEFAULT_OWNER;
|
|
54
|
+
const repo = `${options.repoPrefix ?? DEFAULT_REPO_PREFIX}${slug}`;
|
|
55
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
56
|
+
const res = await fetchImpl(`${GITHUB_API_BASE_URL}/repos/${owner}/${repo}/contents/${CLIENT_MD_PATH}`, {
|
|
57
|
+
headers: {
|
|
58
|
+
Authorization: `Bearer ${githubToken}`,
|
|
59
|
+
Accept: "application/vnd.github+json",
|
|
60
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
if (res.status === 401 || res.status === 403) {
|
|
64
|
+
throw new ClientLinearAuthError(`GitHub rejected the ${resolvedGithub.source} token reading ${owner}/${repo}/${CLIENT_MD_PATH} ` +
|
|
65
|
+
`(HTTP ${res.status}) — check it is valid and can read ${owner}/${repo}.`);
|
|
66
|
+
}
|
|
67
|
+
if (res.status === 404) {
|
|
68
|
+
throw new Error(`${owner}/${repo} has no ${CLIENT_MD_PATH} — can't resolve "${slug}"'s Linear project.`);
|
|
69
|
+
}
|
|
70
|
+
if (!res.ok) {
|
|
71
|
+
throw new Error(`GET ${owner}/${repo}/${CLIENT_MD_PATH} failed: HTTP ${res.status}`);
|
|
72
|
+
}
|
|
73
|
+
const data = (await res.json());
|
|
74
|
+
if (data.type !== "file" || !data.content) {
|
|
75
|
+
throw new Error(`${owner}/${repo}/${CLIENT_MD_PATH} is not a readable file.`);
|
|
76
|
+
}
|
|
77
|
+
const clientMd = Buffer.from(data.content, data.encoding ?? "base64").toString("utf8");
|
|
78
|
+
const slugId = parseSlugId(clientMd, slug);
|
|
79
|
+
const linearToken = options.token ?? process.env[LINEAR_API_TOKEN_ENV];
|
|
80
|
+
if (!linearToken) {
|
|
81
|
+
throw new LinearAuthError(`Linear API key required — set ${LINEAR_API_TOKEN_ENV} (a personal API key with read access) or pass token`);
|
|
82
|
+
}
|
|
83
|
+
const project = await findProjectBySlugId(fetchImpl, linearToken, slugId);
|
|
84
|
+
return linearProjectIssues(fetchImpl, linearToken, project, options);
|
|
85
|
+
}
|