-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathpublicApiDocument.ts
More file actions
481 lines (452 loc) · 16.8 KB
/
publicApiDocument.ts
File metadata and controls
481 lines (452 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import { OpenApiGeneratorV3, OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import type { ComponentsObject, SchemaObject, SecuritySchemeObject } from 'openapi3-ts/oas30';
import { type ZodTypeAny } from 'zod';
import z from 'zod';
import {
publicEeAuditQuerySchema,
publicEeAuditResponseSchema,
publicEeDeleteUserResponseSchema,
publicEeUserSchema,
publicEeUsersResponseSchema,
publicFileSourceRequestSchema,
publicFileSourceResponseSchema,
publicFindSymbolsRequestSchema,
publicFindSymbolsResponseSchema,
publicGetDiffRequestSchema,
publicGetDiffResponseSchema,
publicGetTreeRequestSchema,
publicHealthResponseSchema,
publicListCommitsQuerySchema,
publicListCommitsResponseSchema,
publicListReposQueryParamsSchema,
publicListReposResponseSchema,
publicSearchRequestSchema,
publicSearchResponseSchema,
publicServiceErrorSchema,
publicVersionResponseSchema,
} from './publicApiSchemas.js';
import dedent from 'dedent';
const searchTag = { name: 'Search & Navigation', description: 'Code search and symbol navigation endpoints.' };
const reposTag = { name: 'Repositories', description: 'Repository listing and metadata endpoints.' };
const gitTag = { name: 'Git', description: 'Git history, diff, and file content endpoints.' };
const systemTag = { name: 'System', description: 'System health and version endpoints.' };
const eeTag = { name: 'Enterprise (EE)', description: 'Enterprise endpoints for user management and audit logging.' };
const EE_LICENSE_KEY_NOTE = dedent`
<Note>
This API is only available with an active Enterprise license. Please add your [license key](/docs/license-key) to activate it.
</Note>
`;
const publicFileTreeNodeSchema: SchemaObject = {
type: 'object',
properties: {
type: { type: 'string' },
path: { type: 'string' },
name: { type: 'string' },
children: {
type: 'array',
items: { $ref: '#/components/schemas/PublicFileTreeNode' },
},
},
required: ['type', 'path', 'name', 'children'],
additionalProperties: false,
};
const publicGetTreeResponseSchema: SchemaObject = {
type: 'object',
properties: {
tree: { $ref: '#/components/schemas/PublicFileTreeNode' },
},
required: ['tree'],
additionalProperties: false,
};
const securitySchemeNames = {
bearerToken: 'bearerToken',
apiKeyHeader: 'apiKeyHeader',
} as const;
const securitySchemes: Record<keyof typeof securitySchemeNames, SecuritySchemeObject> = {
[securitySchemeNames.bearerToken]: {
type: 'http',
scheme: 'bearer',
description: 'Bearer authentication header of the form `Bearer <token>`, where `<token>` is your API key.',
},
[securitySchemeNames.apiKeyHeader]: {
type: 'apiKey',
in: 'header',
name: 'X-Sourcebot-Api-Key',
description: 'Header of the form `X-Sourcebot-Api-Key: <token>`, where `<token>` is your API key.',
},
};
function jsonContent(schema: ZodTypeAny | SchemaObject) {
return {
'application/json': {
schema,
},
};
}
function errorJson(description: string) {
return {
description,
content: jsonContent(publicServiceErrorSchema),
};
}
export function createPublicOpenApiDocument(version: string) {
const registry = new OpenAPIRegistry();
registry.registerPath({
method: 'post',
path: '/api/search',
operationId: 'search',
tags: [searchTag.name],
summary: 'Search code',
description: 'Executes a blocking code search and returns all matching file chunks.',
request: {
body: {
required: true,
content: jsonContent(publicSearchRequestSchema),
},
},
responses: {
200: {
description: 'Search results.',
content: jsonContent(publicSearchResponseSchema),
},
400: errorJson('Invalid request body.'),
500: errorJson('Unexpected search failure.'),
},
'x-mint': {
content: dedent`
## Usage
The \`query\` field supports literal, regexp, and symbol searches with filters for repository, file, language, branch, and more. See the [search syntax reference](https://docs.sourcebot.dev/docs/features/search/syntax-reference) for the full query language.
`,
},
});
registry.registerPath({
method: 'get',
path: '/api/repos',
operationId: 'listRepositories',
tags: [reposTag.name],
summary: 'List repositories',
description: 'Returns a paginated list of repositories indexed by this Sourcebot instance.',
request: {
query: publicListReposQueryParamsSchema,
},
responses: {
200: {
description: 'Paginated repository list.',
headers: {
'X-Total-Count': {
description: 'Total number of repositories matching the query across all pages.',
schema: {
type: 'integer',
example: 137,
},
},
Link: {
description: 'Pagination links formatted per RFC 8288. Includes `rel=\"next\"`, `rel=\"prev\"`, `rel=\"first\"`, and `rel=\"last\"` when applicable.',
schema: {
type: 'string',
example: '</api/repos?page=2&perPage=30>; rel="next", </api/repos?page=5&perPage=30>; rel="last"',
},
},
},
content: jsonContent(publicListReposResponseSchema),
},
400: errorJson('Invalid query parameters.'),
500: errorJson('Unexpected repository listing failure.'),
},
});
registry.registerPath({
method: 'get',
path: '/api/version',
operationId: 'getVersion',
tags: [systemTag.name],
summary: 'Get Sourcebot version',
description: 'Returns the currently running Sourcebot version string.',
responses: {
200: {
description: 'Current Sourcebot version.',
content: jsonContent(publicVersionResponseSchema),
},
},
});
registry.registerPath({
method: 'get',
path: '/api/health',
operationId: 'getHealth',
tags: [systemTag.name],
summary: 'Health check',
responses: {
200: {
description: 'Service is healthy.',
content: jsonContent(publicHealthResponseSchema),
},
},
});
registry.registerPath({
method: 'get',
path: '/api/source',
operationId: 'getFileSource',
tags: [gitTag.name],
summary: 'Get file contents',
description: 'Returns the raw source content of a file at a given repository path and optional git ref.',
request: {
query: publicFileSourceRequestSchema,
},
responses: {
200: {
description: 'File source and metadata.',
content: jsonContent(publicFileSourceResponseSchema),
},
400: errorJson('Invalid query parameters or git ref.'),
404: errorJson('Repository or file not found.'),
500: errorJson('Unexpected file retrieval failure.'),
},
});
registry.registerPath({
method: 'post',
path: '/api/tree',
operationId: 'getFileTree',
tags: [gitTag.name],
summary: 'Get a file tree',
description: 'Returns the file tree for a repository at a given revision.',
request: {
body: {
required: true,
content: jsonContent(publicGetTreeRequestSchema),
},
},
responses: {
200: {
description: 'File tree for the requested repository revision.',
content: jsonContent(publicGetTreeResponseSchema),
},
400: errorJson('Invalid request body or git ref.'),
404: errorJson('Repository or path not found.'),
500: errorJson('Unexpected tree retrieval failure.'),
},
});
registry.registerPath({
method: 'get',
path: '/api/diff',
operationId: 'getDiff',
tags: [gitTag.name],
summary: 'Get diff between two commits',
description: 'Returns a structured diff between two git refs using a two-dot comparison. See [git-diff](https://git-scm.com/docs/git-diff) for details.',
request: {
query: publicGetDiffRequestSchema,
},
responses: {
200: {
description: 'Structured diff between the two refs.',
content: jsonContent(publicGetDiffResponseSchema),
},
400: errorJson('Invalid query parameters or git ref.'),
404: errorJson('Repository not found.'),
500: errorJson('Unexpected diff failure.'),
},
});
registry.registerPath({
method: 'post',
path: '/api/find_definitions',
operationId: 'findDefinitions',
tags: [searchTag.name],
summary: 'Find symbol definitions',
description: 'Returns all locations in the codebase where the given symbol is defined.',
request: {
body: {
required: true,
content: jsonContent(publicFindSymbolsRequestSchema),
},
},
responses: {
200: {
description: 'Symbol definition locations.',
content: jsonContent(publicFindSymbolsResponseSchema),
},
400: errorJson('Invalid request body.'),
500: errorJson('Unexpected failure.'),
},
});
registry.registerPath({
method: 'post',
path: '/api/find_references',
operationId: 'findReferences',
tags: [searchTag.name],
summary: 'Find symbol references',
description: 'Returns all locations in the codebase where the given symbol is referenced.',
request: {
body: {
required: true,
content: jsonContent(publicFindSymbolsRequestSchema),
},
},
responses: {
200: {
description: 'Symbol reference locations.',
content: jsonContent(publicFindSymbolsResponseSchema),
},
400: errorJson('Invalid request body.'),
500: errorJson('Unexpected failure.'),
},
});
registry.registerPath({
method: 'get',
path: '/api/commits',
operationId: 'listCommits',
tags: [gitTag.name],
summary: 'List commits',
description: 'Returns a paginated list of commits for a repository.',
request: {
query: publicListCommitsQuerySchema,
},
responses: {
200: {
description: 'Paginated commit list.',
headers: {
'X-Total-Count': {
description: 'Total number of commits matching the query across all pages.',
schema: { type: 'integer' },
},
Link: {
description: 'Pagination links formatted per RFC 8288.',
schema: { type: 'string' },
},
},
content: jsonContent(publicListCommitsResponseSchema),
},
400: errorJson('Invalid query parameters.'),
404: errorJson('Repository not found.'),
500: errorJson('Unexpected failure.'),
},
});
// EE: User Management
registry.registerPath({
method: 'get',
path: '/api/ee/user',
operationId: 'getUser',
tags: [eeTag.name],
summary: 'Get a user',
description: 'Fetches profile details for a single organization member by `userId`. Only organization owners can access this endpoint.',
request: {
query: z.object({
userId: z.string().describe('The ID of the user to retrieve.'),
}),
},
responses: {
200: {
description: 'User details.',
content: jsonContent(publicEeUserSchema),
},
400: errorJson('Missing userId parameter.'),
403: errorJson('Insufficient permissions or entitlement not enabled.'),
404: errorJson('User not found.'),
500: errorJson('Unexpected failure.'),
},
'x-mint': {
content: EE_LICENSE_KEY_NOTE,
},
});
registry.registerPath({
method: 'delete',
path: '/api/ee/user',
operationId: 'deleteUser',
tags: [eeTag.name],
summary: 'Delete a user',
description: 'Permanently deletes a user and all associated records. Only organization owners can delete other users.',
request: {
query: z.object({
userId: z.string().describe('The ID of the user to delete.'),
}),
},
responses: {
200: {
description: 'User deleted successfully.',
content: jsonContent(publicEeDeleteUserResponseSchema),
},
400: errorJson('Missing userId parameter or attempting to delete own account.'),
403: errorJson('Insufficient permissions.'),
404: errorJson('User not found.'),
500: errorJson('Unexpected failure.'),
},
'x-mint': {
content: EE_LICENSE_KEY_NOTE,
},
});
registry.registerPath({
method: 'get',
path: '/api/ee/users',
operationId: 'listUsers',
tags: [eeTag.name],
summary: 'List users',
description: 'Returns all members of the organization. Only organization owners can access this endpoint.',
responses: {
200: {
description: 'List of organization members.',
content: jsonContent(publicEeUsersResponseSchema),
},
403: errorJson('Insufficient permissions or entitlement not enabled.'),
500: errorJson('Unexpected failure.'),
},
'x-mint': {
content: EE_LICENSE_KEY_NOTE,
},
});
// EE: Audit
registry.registerPath({
method: 'get',
path: '/api/ee/audit',
operationId: 'listAuditRecords',
tags: [eeTag.name],
summary: 'List audit records',
description: 'Returns a paginated list of audit log entries. Only organization owners can access this endpoint.',
request: {
query: publicEeAuditQuerySchema,
},
responses: {
200: {
description: 'Paginated audit log.',
headers: {
'X-Total-Count': {
description: 'Total number of audit records matching the query across all pages.',
schema: { type: 'integer' },
},
Link: {
description: 'Pagination links formatted per RFC 8288.',
schema: { type: 'string' },
},
},
content: jsonContent(publicEeAuditResponseSchema),
},
400: errorJson('Invalid query parameters.'),
403: errorJson('Insufficient permissions or entitlement not enabled.'),
500: errorJson('Unexpected failure.'),
},
'x-mint': {
content: EE_LICENSE_KEY_NOTE,
},
});
const generator = new OpenApiGeneratorV3(registry.definitions);
const document = generator.generateDocument({
openapi: '3.0.3',
info: {
title: 'Sourcebot Public API',
version,
description: 'OpenAPI description for the public Sourcebot REST endpoints used for search, repository listing, and file browsing. Authentication is instance-dependent: API keys are the standard integration mechanism, OAuth bearer tokens are EE-only, and some instances may allow anonymous access.',
},
tags: [searchTag, reposTag, gitTag, systemTag, eeTag],
security: [
{ [securitySchemeNames.bearerToken]: [] },
{ [securitySchemeNames.apiKeyHeader]: [] },
{},
],
});
const components: ComponentsObject = document.components ?? {};
components.schemas = {
...(components.schemas ?? {}),
PublicFileTreeNode: publicFileTreeNodeSchema,
};
components.securitySchemes = {
...(components.securitySchemes ?? {}),
...securitySchemes,
};
document.components = components;
return document;
}