-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathactions.ts
More file actions
585 lines (534 loc) · 19.7 KB
/
Copy pathactions.ts
File metadata and controls
585 lines (534 loc) · 19.7 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
'use server';
import { createAudit } from "@/ee/features/audit/audit";
import { env, getSMTPConnectionURL } from "@sourcebot/shared";
import { ErrorCode } from "@/lib/errorCodes";
import { notAuthenticated, notFound, ServiceError } from "@/lib/serviceError";
import { __unsafePrisma } from "@/prisma";
import { render } from "@react-email/components";
import { generateApiKey, getTokenFromConfig } from "@sourcebot/shared";
import { ConnectionSyncJobStatus, OrgRole, Prisma, RepoIndexingJobStatus, RepoIndexingJobType } from "@sourcebot/db";
import { createLogger } from "@sourcebot/shared";
import { GiteaConnectionConfig } from "@sourcebot/schemas/v3/gitea.type";
import { GithubConnectionConfig } from "@sourcebot/schemas/v3/github.type";
import { GitlabConnectionConfig } from "@sourcebot/schemas/v3/gitlab.type";
import { StatusCodes } from "http-status-codes";
import { cookies } from "next/headers";
import { createTransport } from "nodemailer";
import JoinRequestSubmittedEmail from "./emails/joinRequestSubmittedEmail";
import { AGENTIC_SEARCH_TUTORIAL_DISMISSED_COOKIE_NAME, MOBILE_UNSUPPORTED_SPLASH_SCREEN_DISMISSED_COOKIE_NAME, SINGLE_TENANT_ORG_ID } from "./lib/constants";
import { RepositoryQuery } from "./lib/types";
import { getAuthenticatedUser, withAuth, withOptionalAuth } from "./middleware/withAuth";
import { getBrowsePath } from "./app/(app)/browse/hooks/utils";
import { sew } from "@/middleware/sew";
const logger = createLogger('web-actions');
////// Actions ///////
export const completeOnboarding = async (): Promise<{ success: boolean } | ServiceError> => sew(() =>
withAuth(async ({ org, prisma }) => {
await prisma.org.update({
where: { id: org.id },
data: {
isOnboarded: true,
}
});
return {
success: true,
}
}));
export const createApiKey = async (name: string): Promise<{ key: string } | ServiceError> => sew(() =>
withAuth(async ({ org, user, role, prisma }) => {
if ((env.DISABLE_API_KEY_CREATION_FOR_NON_OWNER_USERS === 'true' || env.DISABLE_API_KEY_USAGE_FOR_NON_OWNER_USERS === 'true') && role !== OrgRole.OWNER) {
logger.error(`API key creation is disabled for non-admin users. User ${user.id} is not an owner.`);
return {
statusCode: StatusCodes.FORBIDDEN,
errorCode: ErrorCode.INSUFFICIENT_PERMISSIONS,
message: "API key creation is disabled for non-admin users.",
} satisfies ServiceError;
}
const existingApiKey = await prisma.apiKey.findFirst({
where: {
createdById: user.id,
name,
},
});
if (existingApiKey) {
await createAudit({
action: "api_key.creation_failed",
actor: {
id: user.id,
type: "user"
},
target: {
id: org.id.toString(),
type: "org"
},
orgId: org.id,
metadata: {
message: `API key ${name} already exists`,
api_key: name
}
});
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.API_KEY_ALREADY_EXISTS,
message: `API key ${name} already exists`,
} satisfies ServiceError;
}
const { key, hash } = generateApiKey();
const apiKey = await prisma.apiKey.create({
data: {
name,
hash,
orgId: org.id,
createdById: user.id,
}
});
await createAudit({
action: "api_key.created",
actor: {
id: user.id,
type: "user"
},
target: {
id: apiKey.hash,
type: "api_key"
},
orgId: org.id
});
return {
key,
}
}));
export const deleteApiKey = async (name: string): Promise<{ success: boolean } | ServiceError> => sew(() =>
withAuth(async ({ org, user, prisma }) => {
const apiKey = await prisma.apiKey.findFirst({
where: {
name,
createdById: user.id,
},
});
if (!apiKey) {
await createAudit({
action: "api_key.deletion_failed",
actor: {
id: user.id,
type: "user"
},
target: {
id: org.id.toString(),
type: "org"
},
orgId: org.id,
metadata: {
message: `API key ${name} not found for user ${user.id}`,
api_key: name
}
});
return {
statusCode: StatusCodes.NOT_FOUND,
errorCode: ErrorCode.API_KEY_NOT_FOUND,
message: `API key ${name} not found for user ${user.id}`,
} satisfies ServiceError;
}
await prisma.apiKey.delete({
where: {
hash: apiKey.hash,
},
});
await createAudit({
action: "api_key.deleted",
actor: {
id: user.id,
type: "user"
},
target: {
id: apiKey.hash,
type: "api_key"
},
orgId: org.id,
metadata: {
api_key: name
}
});
return {
success: true,
}
}));
export const getUserApiKeys = async (): Promise<{ name: string; createdAt: Date; lastUsedAt: Date | null }[] | ServiceError> => sew(() =>
withAuth(async ({ org, user, prisma }) => {
const apiKeys = await prisma.apiKey.findMany({
where: {
orgId: org.id,
createdById: user.id,
},
orderBy: {
createdAt: 'desc',
}
});
return apiKeys.map((apiKey) => ({
name: apiKey.name,
createdAt: apiKey.createdAt,
lastUsedAt: apiKey.lastUsedAt,
}));
}));
export const getRepos = async ({
where,
take,
}: {
where?: Prisma.RepoWhereInput,
take?: number
} = {}) => sew(() =>
withOptionalAuth(async ({ org, prisma }) => {
const repos = await prisma.repo.findMany({
where: {
orgId: org.id,
...where,
},
take,
});
const baseUrl = env.AUTH_URL;
return repos.map((repo) => ({
codeHostType: repo.external_codeHostType,
repoId: repo.id,
repoName: repo.name,
repoDisplayName: repo.displayName ?? undefined,
webUrl: `${baseUrl}${getBrowsePath({
repoName: repo.name,
path: '',
pathType: 'tree',
})}`,
externalWebUrl: repo.webUrl ?? undefined,
imageUrl: repo.imageUrl ?? undefined,
indexedAt: repo.indexedAt ?? undefined,
pushedAt: repo.pushedAt ?? undefined,
defaultBranch: repo.defaultBranch ?? undefined,
isFork: repo.isFork,
isArchived: repo.isArchived,
} satisfies RepositoryQuery))
}));
/**
* Returns a set of aggregated stats about the repos in the org
*/
export const getReposStats = async () => sew(() =>
withOptionalAuth(async ({ org, prisma }) => {
const [
// Total number of repos.
numberOfRepos,
// Number of repos with their first time indexing jobs either
// pending or in progress.
numberOfReposWithFirstTimeIndexingJobsInProgress,
// Number of repos that have been indexed at least once.
numberOfReposWithIndex,
] = await Promise.all([
prisma.repo.count({
where: {
orgId: org.id,
}
}),
prisma.repo.count({
where: {
orgId: org.id,
indexedAt: null,
jobs: {
some: {
type: RepoIndexingJobType.INDEX,
status: {
in: [
RepoIndexingJobStatus.PENDING,
RepoIndexingJobStatus.IN_PROGRESS,
]
}
},
},
}
}),
prisma.repo.count({
where: {
orgId: org.id,
NOT: {
indexedAt: null,
}
}
})
]);
return {
numberOfRepos,
numberOfReposWithFirstTimeIndexingJobsInProgress,
numberOfReposWithIndex,
};
})
)
export const getConnectionStats = async () => sew(() =>
withAuth(async ({ org, prisma }) => {
const [
numberOfConnections,
numberOfConnectionsWithFirstTimeSyncJobsInProgress,
] = await Promise.all([
prisma.connection.count({
where: {
orgId: org.id,
}
}),
prisma.connection.count({
where: {
orgId: org.id,
syncedAt: null,
syncJobs: {
some: {
status: {
in: [
ConnectionSyncJobStatus.PENDING,
ConnectionSyncJobStatus.IN_PROGRESS,
]
}
}
}
}
})
]);
return {
numberOfConnections,
numberOfConnectionsWithFirstTimeSyncJobsInProgress,
};
})
);
export const getRepoInfoByName = async (repoName: string) => sew(() =>
withOptionalAuth(async ({ org, prisma }) => {
// @note: repo names are represented by their remote url
// on the code host. E.g.,:
// - github.com/sourcebot-dev/sourcebot
// - gitlab.com/gitlab-org/gitlab
// - gerrit.wikimedia.org/r/mediawiki/extensions/OnionsPorFavor
// etc.
//
// For most purposes, repo names are unique within an org, so using
// findFirst is equivalent to findUnique. Duplicates _can_ occur when
// a repository is specified by its remote url in a generic `git`
// connection. For example:
//
// ```json
// {
// "connections": {
// "connection-1": {
// "type": "github",
// "repos": [
// "sourcebot-dev/sourcebot"
// ]
// },
// "connection-2": {
// "type": "git",
// "url": "file:///tmp/repos/sourcebot"
// }
// }
// }
// ```
//
// In this scenario, both repos will be named "github.com/sourcebot-dev/sourcebot".
// We will leave this as an edge case for now since it's unlikely to happen in practice.
//
// @v4-todo: we could add a unique constraint on repo name + orgId to help de-duplicate
// these cases.
// @see: repoCompileUtils.ts
const repo = await prisma.repo.findFirst({
where: {
name: repoName,
orgId: org.id,
},
});
if (!repo) {
return notFound();
}
return {
id: repo.id,
name: repo.name,
displayName: repo.displayName ?? undefined,
codeHostType: repo.external_codeHostType,
externalWebUrl: repo.webUrl ?? undefined,
imageUrl: repo.imageUrl ?? undefined,
indexedAt: repo.indexedAt ?? undefined,
}
}));
// eslint-disable-next-line authz/require-auth-wrapper -- calls getAuthenticatedUser() directly; runs pre-org-membership so cannot use withAuth
export const createAccountRequest = async () => sew(async () => {
const authResult = await getAuthenticatedUser();
if (!authResult) {
return notAuthenticated();
}
const { user } = authResult;
const org = await __unsafePrisma.org.findUnique({
where: {
id: SINGLE_TENANT_ORG_ID,
},
});
if (!org) {
return notFound("Organization not found");
}
const existingRequest = await __unsafePrisma.accountRequest.findUnique({
where: {
requestedById_orgId: {
requestedById: user.id,
orgId: org.id,
},
},
});
if (existingRequest) {
logger.warn(`User ${user.id} already has an account request for org ${org.id}. Skipping account request creation.`);
return {
success: true,
existingRequest: true,
}
}
if (!existingRequest) {
await __unsafePrisma.accountRequest.create({
data: {
requestedById: user.id,
orgId: org.id,
},
});
const smtpConnectionUrl = getSMTPConnectionURL();
if (smtpConnectionUrl && env.EMAIL_FROM_ADDRESS) {
// TODO: This is needed because we can't fetch the origin from the request headers when this is called
// on user creation (the header isn't set when next-auth calls onCreateUser for some reason)
const deploymentUrl = env.AUTH_URL;
const owners = await __unsafePrisma.user.findMany({
where: {
orgs: {
some: {
orgId: org.id,
role: "OWNER",
},
},
},
});
if (owners.length === 0) {
logger.error(`Failed to find any owners for org ${org.id} when drafting email for account request from ${user.id}`);
} else {
const html = await render(JoinRequestSubmittedEmail({
baseUrl: deploymentUrl,
requestor: {
name: user.name ?? undefined,
email: user.email,
avatarUrl: user.image ?? undefined,
},
orgName: org.name,
orgImageUrl: org.imageUrl ?? undefined,
}));
const ownerEmails = owners
.map((owner) => owner.email)
.filter((email): email is string => email !== null);
const transport = createTransport(smtpConnectionUrl);
const result = await transport.sendMail({
to: ownerEmails,
from: env.EMAIL_FROM_ADDRESS,
subject: `New account request for ${org.name} on Sourcebot`,
html,
text: `New account request for ${org.name} on Sourcebot by ${user.name ?? user.email}`,
});
const failed = result.rejected.concat(result.pending).filter(Boolean);
if (failed.length > 0) {
logger.error(`Failed to send account request email to ${ownerEmails.join(', ')}: ${failed}`);
}
}
} else {
logger.warn(`SMTP_CONNECTION_URL or EMAIL_FROM_ADDRESS not set. Skipping account request email to owner`);
}
}
return {
success: true,
existingRequest: false,
}
});
export const getSearchContexts = async () => sew(() =>
withOptionalAuth(async ({ org, prisma }) => {
const searchContexts = await prisma.searchContext.findMany({
where: {
orgId: org.id,
},
include: {
repos: true,
},
});
return searchContexts.map((context) => ({
id: context.id,
name: context.name,
description: context.description ?? undefined,
repoNames: context.repos.map((repo) => repo.name),
}));
}));
export const getRepoImage = async (repoId: number): Promise<ArrayBuffer | ServiceError> => sew(async () => {
return await withOptionalAuth(async ({ org, prisma }) => {
const repo = await prisma.repo.findUnique({
where: {
id: repoId,
orgId: org.id,
},
include: {
connections: {
include: {
connection: true,
}
}
},
});
if (!repo || !repo.imageUrl) {
return notFound();
}
const authHeaders: Record<string, string> = {};
for (const { connection } of repo.connections) {
try {
if (connection.connectionType === 'github') {
const config = connection.config as unknown as GithubConnectionConfig;
if (config.token) {
const token = await getTokenFromConfig(config.token);
authHeaders['Authorization'] = `token ${token}`;
break;
}
} else if (connection.connectionType === 'gitlab') {
const config = connection.config as unknown as GitlabConnectionConfig;
if (config.token) {
const token = await getTokenFromConfig(config.token);
authHeaders['PRIVATE-TOKEN'] = token;
break;
}
} else if (connection.connectionType === 'gitea') {
const config = connection.config as unknown as GiteaConnectionConfig;
if (config.token) {
const token = await getTokenFromConfig(config.token);
authHeaders['Authorization'] = `token ${token}`;
break;
}
}
} catch (error) {
logger.warn(`Failed to get token for connection ${connection.id}:`, error);
}
}
try {
const response = await fetch(repo.imageUrl, {
headers: authHeaders,
});
if (!response.ok) {
logger.warn(`Failed to fetch image from ${repo.imageUrl}: ${response.status}`);
return notFound();
}
const imageBuffer = await response.arrayBuffer();
return imageBuffer;
} catch (error) {
logger.error(`Error proxying image for repo ${repoId}:`, error);
return notFound();
}
})
});
// eslint-disable-next-line authz/require-auth-wrapper -- UI-only preference cookie, no DB access
export const setAgenticSearchTutorialDismissedCookie = async (dismissed: boolean) => sew(async () => {
const cookieStore = await cookies();
cookieStore.set(AGENTIC_SEARCH_TUTORIAL_DISMISSED_COOKIE_NAME, dismissed ? "true" : "false", {
httpOnly: false, // Allow client-side access
maxAge: 365 * 24 * 60 * 60, // 1 year in seconds
});
return true;
});
// eslint-disable-next-line authz/require-auth-wrapper -- UI-only preference cookie, no DB access
export const dismissMobileUnsupportedSplashScreen = async () => sew(async () => {
const cookieStore = await cookies();
cookieStore.set(MOBILE_UNSUPPORTED_SPLASH_SCREEN_DISMISSED_COOKIE_NAME, 'true');
return true;
});