Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions docs/snippets/schemas/v3/index.schema.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"resyncConnectionPollingIntervalMs": {
"type": "number",
"description": "The polling rate (in milliseconds) at which the db should be checked for connections that need to be re-synced. Defaults to 1 second.",
"minimum": 1
"minimum": 1,
"deprecated": true
},
"reindexRepoPollingIntervalMs": {
"type": "number",
Expand All @@ -52,7 +53,8 @@
"maxRepoGarbageCollectionJobConcurrency": {
"type": "number",
"description": "The number of repo GC jobs to run concurrently. Defaults to 8.",
"minimum": 1
"minimum": 1,
"deprecated": true
},
"repoGarbageCollectionGracePeriodMs": {
"type": "number",
Expand Down Expand Up @@ -216,7 +218,8 @@
"resyncConnectionPollingIntervalMs": {
"type": "number",
"description": "The polling rate (in milliseconds) at which the db should be checked for connections that need to be re-synced. Defaults to 1 second.",
"minimum": 1
"minimum": 1,
"deprecated": true
},
"reindexRepoPollingIntervalMs": {
"type": "number",
Expand All @@ -236,7 +239,8 @@
"maxRepoGarbageCollectionJobConcurrency": {
"type": "number",
"description": "The number of repo GC jobs to run concurrently. Defaults to 8.",
"minimum": 1
"minimum": 1,
"deprecated": true
},
"repoGarbageCollectionGracePeriodMs": {
"type": "number",
Expand Down
8 changes: 5 additions & 3 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"vitest": "^4.1.4"
},
"dependencies": {
"@bull-board/api": "6.11.2",
"@bull-board/express": "6.11.2",
"@bull-board/ui": "6.11.2",
"@coderabbitai/bitbucket": "^1.1.3",
"@gitbeaker/rest": "^40.5.1",
"@octokit/app": "^16.1.1",
Expand All @@ -35,7 +38,7 @@
"@types/express": "^5.0.0",
"argparse": "^2.0.1",
"azure-devops-node-api": "^15.1.1",
"bullmq": "^5.34.10",
"bullmq": "^5.81.3",
"chokidar": "^4.0.3",
"cross-fetch": "^4.0.0",
"dotenv": "^16.4.5",
Expand All @@ -46,13 +49,12 @@
"gitea-js": "^1.22.0",
"glob": "^11.1.0",
"http-status-codes": "^2.3.0",
"ioredis": "^5.4.2",
"ioredis": "^5.11.1",
"lowdb": "^7.0.1",
"micromatch": "^4.0.8",
"p-limit": "^7.2.0",
"posthog-node": "^5.24.15",
"prom-client": "^15.1.3",
"redlock": "5.0.0-beta.2",
"simple-git": "^3.36.0",
"zod": "^3.25.76"
}
Expand Down
178 changes: 16 additions & 162 deletions packages/backend/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { PrismaClient, RepoIndexingJobType } from '@sourcebot/db';
import * as Sentry from '@sentry/node';
import { hasEntitlement } from './entitlements.js';
import { createLogger, doesIdpSupportPermissionSyncing, env } from '@sourcebot/shared';
import { createLogger, env } from '@sourcebot/shared';
import { createBullBoard } from '@bull-board/api';
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter.js';
import { ExpressAdapter } from '@bull-board/express';
import { Queue } from 'bullmq';
import express, { NextFunction, Request, Response } from 'express';
import 'express-async-errors';
import * as http from "http";
import { ConnectionManager } from './connectionManager.js';
import { AccountPermissionSyncer } from './ee/accountPermissionSyncer.js';
import { PromClient } from './promClient.js';
import { RepoIndexManager } from './repoIndexManager.js';
import { createGitHubRepoRecord } from './repoCompileUtils.js';
import { isNotFound } from './errors.js';
import { Octokit } from '@octokit/rest';
import { SINGLE_TENANT_ORG_ID } from './constants.js';
import z from 'zod';
import * as Sentry from "@sentry/node";

const logger = createLogger('api');

Expand All @@ -23,177 +17,37 @@ const PORT = Number(workerApiUrl.port) || (workerApiUrl.protocol === "https:" ?
export class Api {
private server: http.Server;

constructor(
promClient: PromClient,
private prisma: PrismaClient,
private connectionManager: ConnectionManager,
private repoIndexManager: RepoIndexManager,
private accountPermissionSyncer: AccountPermissionSyncer,
) {
constructor(promClient: PromClient, queues: Queue[]) {
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const bullBoardAdapter = new ExpressAdapter();
bullBoardAdapter.setBasePath('/admin/queues');
createBullBoard({
queues: queues.map(queue => new BullMQAdapter(queue, { readOnlyMode: true })),
serverAdapter: bullBoardAdapter,
});
app.use('/admin/queues', bullBoardAdapter.getRouter());

// Prometheus metrics endpoint
app.use('/metrics', async (_req: Request, res: Response) => {
res.set('Content-Type', promClient.registry.contentType);
const metrics = await promClient.registry.metrics();
res.end(metrics);
});

app.post('/api/sync-connection', this.syncConnection.bind(this));
app.post('/api/index-repo', this.indexRepo.bind(this));
app.post('/api/trigger-account-permission-sync', this.triggerAccountPermissionSync.bind(this));
app.post(`/api/experimental/add-github-repo`, this.experimental_addGithubRepo.bind(this));

app.use((error: unknown, _req: Request, _res: Response, next: NextFunction) => {
Sentry.captureException(error);
next(error);
});

this.server = app.listen(PORT, () => {
logger.debug(`API server is running on port ${PORT}`);
logger.debug(`Bull Board is available at ${workerApiUrl.origin}/admin/queues`);
});
}

private async syncConnection(req: Request, res: Response) {
const schema = z.object({
connectionId: z.number(),
}).strict();

const parsed = schema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: parsed.error.message });
return;
}

const { connectionId } = parsed.data;
const connection = await this.prisma.connection.findUnique({
where: {
id: connectionId,
}
});

if (!connection) {
res.status(404).json({ error: 'Connection not found' });
return;
}

const [jobId] = await this.connectionManager.createJobs([connection]);

res.status(200).json({ jobId });
}

private async indexRepo(req: Request, res: Response) {
const schema = z.object({
repoId: z.number(),
}).strict();

const parsed = schema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: parsed.error.message });
return;
}

const { repoId } = parsed.data;
const repo = await this.prisma.repo.findUnique({
where: { id: repoId },
});

if (!repo) {
res.status(404).json({ error: 'Repo not found' });
return;
}

const [jobId] = await this.repoIndexManager.createJobs([repo], RepoIndexingJobType.INDEX);
res.status(200).json({ jobId });
}

private async triggerAccountPermissionSync(req: Request, res: Response) {
if (env.PERMISSION_SYNC_ENABLED !== 'true' || !await hasEntitlement('permission-syncing')) {
res.status(403).json({ error: 'Permission syncing is not enabled.' });
return;
}

const schema = z.object({
accountId: z.string(),
}).strict();

const parsed = schema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: parsed.error.message });
return;
}

const { accountId } = parsed.data;
const account = await this.prisma.account.findUnique({
where: { id: accountId },
});

if (!account) {
res.status(404).json({ error: 'Account not found' });
return;
}

if (!doesIdpSupportPermissionSyncing(account.providerType)) {
res.status(400).json({ error: `Provider '${account.providerType}' does not support permission syncing.` });
return;
}

const jobId = await this.accountPermissionSyncer.schedulePermissionSyncForAccount(account);
res.status(200).json({ jobId });
}

private async experimental_addGithubRepo(req: Request, res: Response) {
const schema = z.object({
owner: z.string(),
repo: z.string(),
}).strict();

const parsed = schema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: parsed.error.message });
return;
}

const octokit = new Octokit();
let response;
try {
response = await octokit.rest.repos.get({
owner: parsed.data.owner,
repo: parsed.data.repo,
});
} catch (error) {
if (isNotFound(error)) {
res.status(404).json({ error: 'Repository not found on GitHub' });
return;
}
throw error;
}

const record = createGitHubRepoRecord({
repo: response.data,
hostUrl: 'https://github.com',
isAutoCleanupDisabled: true,
});

const repo = await this.prisma.repo.upsert({
where: {
external_id_external_codeHostUrl_orgId: {
external_id: record.external_id,
external_codeHostUrl: record.external_codeHostUrl,
orgId: SINGLE_TENANT_ORG_ID,
}
},
update: record,
create: record,
});

const [jobId ] = await this.repoIndexManager.createJobs([repo], RepoIndexingJobType.INDEX);

res.status(200).json({ jobId, repoId: repo.id });
}

public async dispose() {
return new Promise<void>((resolve, reject) => {
this.server.close((err) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ export const getReposForAuthenticatedBitbucketServerUser = async (
* @note This only covers direct user-to-repo grants. It does NOT include users who have access via:
* - Project-level permissions (inherited by all repos in the project)
* - Group membership
* These users will still gain access through account-driven syncing (accountPermissionSyncer).
* These users will still gain access through account-driven permission syncing.
*
* @see https://developer.atlassian.com/server/bitbucket/rest/v906/api-group-repository/#api-rest-api-latest-projects-projectkey-repos-reposlug-permissions-users-get
*/
Expand Down
26 changes: 14 additions & 12 deletions packages/backend/src/configManager.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Prisma, PrismaClient } from "@sourcebot/db";
import { Prisma } from "@sourcebot/db";
import { createLogger, env } from "@sourcebot/shared";
import { ConnectionConfig } from "@sourcebot/schemas/v3/connection.type";
import { loadConfig } from "@sourcebot/shared";
import chokidar, { FSWatcher } from 'chokidar';
import { ConnectionManager } from "./connectionManager.js";
import { SINGLE_TENANT_ORG_ID } from "./constants.js";
import { syncSearchContexts } from "./ee/syncSearchContexts.js";
import isEqual from 'fast-deep-equal';
import { JobManager } from "./types.js";
import { prisma } from "./prisma.js";

const logger = createLogger('config-manager');

export class ConfigManager {
private watcher: FSWatcher;

constructor(
private db: PrismaClient,
private connectionManager: ConnectionManager,
private jobManager: JobManager,
configPath: string,
) {
this.watcher = chokidar.watch(configPath, {
Expand Down Expand Up @@ -46,14 +46,13 @@ export class ConfigManager {
await syncSearchContexts({
contexts: config.contexts,
orgId: SINGLE_TENANT_ORG_ID,
db: this.db,
});
}

private syncConnections = async (connections?: { [key: string]: ConnectionConfig }) => {
if (connections) {
for (const [key, newConnectionConfig] of Object.entries(connections)) {
const existingConnection = await this.db.connection.findUnique({
const existingConnection = await prisma.connection.findUnique({
where: {
name_orgId: {
name: key,
Expand All @@ -73,7 +72,7 @@ export class ConfigManager {

// Either update the existing connection or create a new one.
const connection = existingConnection ?
await this.db.connection.update({
await prisma.connection.update({
where: {
id: existingConnection.id,
},
Expand All @@ -84,7 +83,7 @@ export class ConfigManager {
enforcePermissionsForPublicRepos,
}
}) :
await this.db.connection.create({
await prisma.connection.create({
data: {
name: key,
config: newConnectionConfig as unknown as Prisma.InputJsonValue,
Expand All @@ -102,13 +101,16 @@ export class ConfigManager {

if (connectionNeedsSyncing) {
logger.debug(`Change detected for connection '${key}' (id: ${connection.id}). Creating sync job.`);
await this.connectionManager.createJobs([connection]);
await this.jobManager.trigger('connection-sync', {
connectionId: connection.id,
orgId: SINGLE_TENANT_ORG_ID,
})
}
}
}

// Delete any connections that are no longer in the config.
const deletedConnections = await this.db.connection.findMany({
const deletedConnections = await prisma.connection.findMany({
where: {
isDeclarative: true,
name: {
Expand All @@ -120,7 +122,7 @@ export class ConfigManager {

for (const connection of deletedConnections) {
logger.debug(`Deleting connection with name '${connection.name}'. Connection ID: ${connection.id}`);
await this.db.connection.delete({
await prisma.connection.delete({
where: {
id: connection.id,
}
Expand All @@ -131,4 +133,4 @@ export class ConfigManager {
public dispose = async () => {
await this.watcher.close();
}
}
}
Loading