Skip to content

Commit 0ce9cc6

Browse files
further wip
1 parent 10db36b commit 0ce9cc6

37 files changed

Lines changed: 312 additions & 328 deletions

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ COPY ./packages/schemas ./packages/schemas
4343
COPY ./packages/crypto ./packages/crypto
4444
COPY ./packages/error ./packages/error
4545
COPY ./packages/logger ./packages/logger
46+
COPY ./packages/shared ./packages/shared
4647

4748
RUN yarn workspace @sourcebot/db install
4849
RUN yarn workspace @sourcebot/schemas install
4950
RUN yarn workspace @sourcebot/crypto install
5051
RUN yarn workspace @sourcebot/error install
5152
RUN yarn workspace @sourcebot/logger install
53+
RUN yarn workspace @sourcebot/shared install
5254
# ------------------------------------
5355

5456
# ------ Build Web ------
@@ -92,6 +94,7 @@ COPY --from=shared-libs-builder /app/packages/schemas ./packages/schemas
9294
COPY --from=shared-libs-builder /app/packages/crypto ./packages/crypto
9395
COPY --from=shared-libs-builder /app/packages/error ./packages/error
9496
COPY --from=shared-libs-builder /app/packages/logger ./packages/logger
97+
COPY --from=shared-libs-builder /app/packages/shared ./packages/shared
9598

9699
# Fixes arm64 timeouts
97100
RUN yarn workspace @sourcebot/web install
@@ -132,6 +135,7 @@ COPY --from=shared-libs-builder /app/packages/schemas ./packages/schemas
132135
COPY --from=shared-libs-builder /app/packages/crypto ./packages/crypto
133136
COPY --from=shared-libs-builder /app/packages/error ./packages/error
134137
COPY --from=shared-libs-builder /app/packages/logger ./packages/logger
138+
COPY --from=shared-libs-builder /app/packages/shared ./packages/shared
135139
RUN yarn workspace @sourcebot/backend install
136140
RUN yarn workspace @sourcebot/backend build
137141

@@ -215,6 +219,7 @@ COPY --from=shared-libs-builder /app/packages/schemas ./packages/schemas
215219
COPY --from=shared-libs-builder /app/packages/crypto ./packages/crypto
216220
COPY --from=shared-libs-builder /app/packages/error ./packages/error
217221
COPY --from=shared-libs-builder /app/packages/logger ./packages/logger
222+
COPY --from=shared-libs-builder /app/packages/shared ./packages/shared
218223

219224
# Configure dependencies
220225
RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervisor uuidgen curl perl jq redis postgresql postgresql-contrib openssl util-linux unzip

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Copyright (c) 2025 Taqla Inc.
22

33
Portions of this software are licensed as follows:
44

5-
- All content that resides under the "ee/", "packages/web/src/ee/", and "packages/backend/src/ee/" directories of this repository, if these directories exist, is licensed under the license defined in "ee/LICENSE".
5+
- All content that resides under the "ee/", "packages/web/src/ee/", and "packages/shared/src/ee/" directories of this repository, if these directories exist, is licensed under the license defined in "ee/LICENSE".
66
- All third party components incorporated into the Sourcebot Software are licensed under the original license provided by the owner of the applicable component.
77
- Content outside of the above mentioned directories or restrictions above is available under the "MIT Expat" license as defined below.
88

packages/backend/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
"@sourcebot/error": "workspace:*",
3434
"@sourcebot/logger": "workspace:*",
3535
"@sourcebot/schemas": "workspace:*",
36+
"@sourcebot/shared": "workspace:*",
3637
"@t3-oss/env-core": "^0.12.0",
3738
"@types/express": "^5.0.0",
38-
"ajv": "^8.17.1",
3939
"argparse": "^2.0.1",
4040
"bullmq": "^5.34.10",
4141
"cross-fetch": "^4.0.0",
@@ -50,7 +50,6 @@
5050
"posthog-node": "^4.2.1",
5151
"prom-client": "^15.1.3",
5252
"simple-git": "^3.27.0",
53-
"strip-json-comments": "^5.0.1",
5453
"zod": "^3.24.3"
5554
}
5655
}

packages/backend/src/connectionManager.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { BackendError, BackendException } from "@sourcebot/error";
99
import { captureEvent } from "./posthog.js";
1010
import { env } from "./env.js";
1111
import * as Sentry from "@sentry/node";
12-
import { syncSearchContexts } from "./ee/syncSearchContexts.js";
13-
import { AppContext } from "./types.js";
12+
import { loadConfig, syncSearchContexts } from "@sourcebot/shared";
1413

1514
interface IConnectionManager {
1615
scheduleConnectionSync: (connection: Connection) => Promise<void>;
@@ -40,7 +39,6 @@ export class ConnectionManager implements IConnectionManager {
4039
private db: PrismaClient,
4140
private settings: Settings,
4241
redis: Redis,
43-
private ctx: AppContext,
4442
) {
4543
this.queue = new Queue<JobPayload>(QUEUE_NAME, {
4644
connection: redis,
@@ -267,7 +265,7 @@ export class ConnectionManager implements IConnectionManager {
267265

268266
private async onSyncJobCompleted(job: Job<JobPayload>, result: JobResult) {
269267
this.logger.info(`Connection sync job for connection ${job.data.connectionName} (id: ${job.data.connectionId}, jobId: ${job.id}) completed`);
270-
const { connectionId } = job.data;
268+
const { connectionId, orgId } = job.data;
271269

272270
let syncStatusMetadata: Record<string, unknown> = (await this.db.connection.findUnique({
273271
where: { id: connectionId },
@@ -294,7 +292,18 @@ export class ConnectionManager implements IConnectionManager {
294292
}
295293
});
296294

297-
await syncSearchContexts(this.db, this.ctx.config?.contexts);
295+
// After a connection has synced, we need to re-sync the org's search contexts as
296+
// there may be new repos that match the search context's include/exclude patterns.
297+
if (env.CONFIG_PATH) {
298+
const config = await loadConfig(env.CONFIG_PATH);
299+
300+
await syncSearchContexts({
301+
db: this.db,
302+
orgId,
303+
contexts: config.contexts,
304+
});
305+
}
306+
298307

299308
captureEvent('backend_connection_sync_job_completed', {
300309
connectionId: connectionId,

packages/backend/src/index.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,9 @@ import { main } from "./main.js"
99
import { PrismaClient } from "@sourcebot/db";
1010
import { env } from "./env.js";
1111
import { createLogger } from "@sourcebot/logger";
12-
import { isRemotePath } from './utils.js';
13-
import { readFile } from 'fs/promises';
14-
import stripJsonComments from 'strip-json-comments';
15-
import { SourcebotConfig } from '@sourcebot/schemas/v3/index.type';
16-
import { indexSchema } from '@sourcebot/schemas/v3/index.schema';
17-
import { Ajv } from "ajv";
1812

1913
const logger = createLogger('backend-entrypoint');
20-
const ajv = new Ajv({
21-
validateFormats: false,
22-
});
23-
24-
const loadConfig = async (configPath?: string) => {
25-
if (!configPath) {
26-
return undefined;
27-
}
28-
29-
const configContent = await (async () => {
30-
if (isRemotePath(configPath)) {
31-
const response = await fetch(configPath);
32-
if (!response.ok) {
33-
throw new Error(`Failed to fetch config file ${configPath}: ${response.statusText}`);
34-
}
35-
return response.text();
36-
} else {
37-
return readFile(configPath, { encoding: 'utf-8' });
38-
}
39-
})();
4014

41-
const config = JSON.parse(stripJsonComments(configContent)) as SourcebotConfig;
42-
const isValidConfig = ajv.validate(indexSchema, config);
43-
if (!isValidConfig) {
44-
throw new Error(`Config file '${configPath}' is invalid: ${ajv.errorsText(ajv.errors)}`);
45-
}
46-
47-
return config;
48-
}
4915

5016
// Register handler for normal exit
5117
process.on('exit', (code) => {
@@ -85,13 +51,10 @@ if (!existsSync(indexPath)) {
8551
await mkdir(indexPath, { recursive: true });
8652
}
8753

88-
const config = await loadConfig(env.CONFIG_PATH);
89-
9054
const context: AppContext = {
9155
indexPath,
9256
reposPath,
9357
cachePath: cacheDir,
94-
config,
9558
}
9659

9760
const prisma = new PrismaClient();

packages/backend/src/main.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
import { PrismaClient } from '@sourcebot/db';
22
import { createLogger } from "@sourcebot/logger";
33
import { AppContext } from "./types.js";
4-
import { DEFAULT_SETTINGS, SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_ID, SINGLE_TENANT_ORG_NAME } from './constants.js';
4+
import { DEFAULT_SETTINGS } from './constants.js';
55
import { Redis } from 'ioredis';
66
import { ConnectionManager } from './connectionManager.js';
77
import { RepoManager } from './repoManager.js';
88
import { env } from './env.js';
99
import { PromClient } from './promClient.js';
10-
import { syncSearchContexts } from './ee/syncSearchContexts.js';
10+
import { loadConfig } from '@sourcebot/shared';
1111

1212
const logger = createLogger('backend-main');
1313

14+
const getSettings = async (configPath?: string) => {
15+
if (!configPath) {
16+
return DEFAULT_SETTINGS;
17+
}
18+
19+
const config = await loadConfig(configPath);
20+
21+
return {
22+
...DEFAULT_SETTINGS,
23+
...config.settings,
24+
}
25+
}
26+
1427
export const main = async (db: PrismaClient, context: AppContext) => {
1528
const redis = new Redis(env.REDIS_URL, {
1629
maxRetriesPerRequest: null
@@ -23,28 +36,11 @@ export const main = async (db: PrismaClient, context: AppContext) => {
2336
process.exit(1);
2437
});
2538

26-
const settings = {
27-
...DEFAULT_SETTINGS,
28-
...context.config?.settings,
29-
}
30-
31-
await db.org.upsert({
32-
where: {
33-
id: SINGLE_TENANT_ORG_ID,
34-
},
35-
update: {},
36-
create: {
37-
name: SINGLE_TENANT_ORG_NAME,
38-
domain: SINGLE_TENANT_ORG_DOMAIN,
39-
id: SINGLE_TENANT_ORG_ID
40-
}
41-
});
42-
43-
await syncSearchContexts(db, context.config?.contexts);
39+
const settings = await getSettings(env.CONFIG_PATH);
4440

4541
const promClient = new PromClient();
4642

47-
const connectionManager = new ConnectionManager(db, settings, redis, context);
43+
const connectionManager = new ConnectionManager(db, settings, redis);
4844
connectionManager.registerPollingCallback();
4945

5046
const repoManager = new RepoManager(db, settings, redis, promClient, context);

packages/backend/src/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Settings as SettingsSchema, SourcebotConfig } from "@sourcebot/schemas/v3/index.type";
1+
import { Settings as SettingsSchema } from "@sourcebot/schemas/v3/index.type";
22
import { z } from "zod";
33

44
export type AppContext = {
@@ -13,8 +13,6 @@ export type AppContext = {
1313
indexPath: string;
1414

1515
cachePath: string;
16-
17-
config?: SourcebotConfig;
1816
}
1917

2018
export type Settings = Required<SettingsSchema>;

packages/backend/src/utils.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ export const marshalBool = (value?: boolean) => {
2020
return !!value ? '1' : '0';
2121
}
2222

23-
export const isRemotePath = (path: string) => {
24-
return path.startsWith('https://') || path.startsWith('http://');
25-
}
26-
2723
export const getTokenFromConfig = async (token: any, orgId: number, db: PrismaClient, logger?: Logger) => {
2824
try {
2925
return await getTokenFromConfigBase(token, orgId, db);

packages/shared/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
*.tsbuildinfo

packages/shared/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@sourcebot/shared",
3+
"version": "0.1.0",
4+
"type": "module",
5+
"private": true,
6+
"scripts": {
7+
"build": "tsc",
8+
"build:watch": "tsc-watch --preserveWatchOutput",
9+
"postinstall": "yarn build"
10+
},
11+
"dependencies": {
12+
"@sourcebot/crypto": "workspace:*",
13+
"@sourcebot/db": "workspace:*",
14+
"@sourcebot/logger": "workspace:*",
15+
"@sourcebot/schemas": "workspace:*",
16+
"@t3-oss/env-core": "^0.12.0",
17+
"ajv": "^8.17.1",
18+
"server-only": "^0.0.1",
19+
"strip-json-comments": "^5.0.1",
20+
"zod": "^3.24.3"
21+
},
22+
"devDependencies": {
23+
"@types/node": "^22.7.5",
24+
"tsc-watch": "6.2.1",
25+
"typescript": "^5.7.3"
26+
},
27+
"exports": {
28+
".": "./dist/index.server.js",
29+
"./client": "./dist/index.client.js"
30+
}
31+
}

0 commit comments

Comments
 (0)