/ opencodehub / src / lib / external-ci.ts
Swadhin Biswas fix(ts): eliminate all 127 TypeScript errors · 4/21/2026
18087d6
493 lines · 12.6 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
/**
 * External CI Integration Library
 * Official integrations with external CI/CD systems
 */

import { getDatabase, schema } from "@/db";
import { externalBuilds, externalCIConfigs } from "@/db/schema/integrations";
import { and, eq } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { assertOnlineFeature } from "./air-gapped";
import { logger } from "./logger";

/**
 * External CI provider configurations
 */
/**
 * External CI provider configurations
 */
// externalCIConfigs imported from schema

/**
 * External build records
 */
// externalBuilds imported from schema

export type ExternalCIConfig = typeof externalCIConfigs.$inferSelect;
export type ExternalBuild = typeof externalBuilds.$inferSelect;

/**
 * Supported CI providers
 */
export const CI_PROVIDERS = {
  jenkins: {
    name: "Jenkins",
    icon: "jenkins",
    triggerEndpoint: "/job/{project}/build",
    statusEndpoint: "/job/{project}/{build}/api/json",
  },
  circleci: {
    name: "CircleCI",
    icon: "circleci",
    triggerEndpoint: "/api/v2/project/{project}/pipeline",
    statusEndpoint: "/api/v2/pipeline/{pipeline}",
  },
  buildkite: {
    name: "Buildkite",
    icon: "buildkite",
    // Expect baseUrl to include org/pipeline, e.g. https://api.buildkite.com/v2/organizations/acme/pipelines/app
    triggerEndpoint: "/builds",
    statusEndpoint: "/builds/{build}",
  },
  travis: {
    name: "Travis CI",
    icon: "travis",
    triggerEndpoint: "/repo/{project}/requests",
    statusEndpoint: "/build/{build}",
  },
  gitlab_ci: {
    name: "GitLab CI",
    icon: "gitlab",
    triggerEndpoint: "/api/v4/projects/{project}/trigger/pipeline",
    statusEndpoint: "/api/v4/projects/{project}/pipelines/{pipeline}",
  },
  gitlab: {
    name: "GitLab CI",
    icon: "gitlab",
    triggerEndpoint: "/api/v4/projects/{project}/trigger/pipeline",
    statusEndpoint: "/api/v4/projects/{project}/pipelines/{pipeline}",
  },
  azure_devops: {
    name: "Azure DevOps",
    icon: "azure",
    triggerEndpoint: "/{org}/{project}/_apis/build/builds",
    statusEndpoint: "/{org}/{project}/_apis/build/builds/{build}",
  },
  github_actions: {
    name: "GitHub Actions",
    icon: "github",
    triggerEndpoint:
      "/repos/{owner}/{repo}/actions/workflows/{workflow}/dispatches",
    statusEndpoint: "/repos/{owner}/{repo}/actions/runs/{run}",
  },
} as const;

/**
 * Configure external CI for a repository
 */
export async function configureExternalCI(options: {
  repositoryId: string;
  provider: keyof typeof CI_PROVIDERS;
  name: string;
  baseUrl: string;
  apiToken?: string;
  projectId?: string;
}): Promise<ExternalCIConfig> {
  const db = getDatabase();

  const config = {
    id: crypto.randomUUID(),
    repositoryId: options.repositoryId,
    provider: options.provider,
    name: options.name,
    baseUrl: options.baseUrl,
    apiToken: options.apiToken || null,
    projectId: options.projectId || null,
    webhookSecret: crypto.randomUUID(),
    isEnabled: true,
    syncStatus: true,
    createdAt: new Date(),
    updatedAt: new Date(),
  };
  // @ts-expect-error - Drizzle multi-db union type issue
  await db.insert(schema.externalCIConfigs).values(config);

  logger.info(
    {
      repoId: options.repositoryId,
      provider: options.provider,
    },
    "External CI configured",
  );

  return config as ExternalCIConfig;
}

/**
 * Get external CI configs for repository
 */
export async function getExternalCIConfigs(
  repositoryId: string,
): Promise<ExternalCIConfig[]> {
  const db = getDatabase();

  try {
    return (
      (await db.query.externalCIConfigs?.findMany({
        where: eq(schema.externalCIConfigs.repositoryId, repositoryId),
      })) || []
    );
  } catch {
    return [];
  }
}

/**
 * Trigger an external CI build
 */
export async function triggerExternalBuild(options: {
  configId: string;
  pullRequestId?: string;
  branch?: string;
  commitSha?: string;
}): Promise<ExternalBuild | null> {
  assertOnlineFeature("External CI build triggering");
  const db = getDatabase();

  const config = await db.query.externalCIConfigs?.findFirst({
    where: eq(schema.externalCIConfigs.id, options.configId),
  });

  if (!config || !config.isEnabled) {
    return null;
  }

  const provider = CI_PROVIDERS[config.provider as keyof typeof CI_PROVIDERS];
  if (!provider) {
    logger.error({ provider: config.provider }, "Unknown CI provider");
    return null;
  }

  // Build the trigger URL
  let triggerUrl = `${config.baseUrl}${provider.triggerEndpoint}`;
  triggerUrl = triggerUrl.replace("{project}", config.projectId || "");

  try {
    // Make API call to trigger build
    const response = await fetch(triggerUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        ...(config.apiToken && { Authorization: `Bearer ${config.apiToken}` }),
      },
      body: JSON.stringify({
        ref: options.branch || "main",
        sha: options.commitSha,
      }),
    });

    if (!response.ok) {
      logger.error(
        {
          provider: config.provider,
          status: response.status,
        },
        "Failed to trigger external build",
      );
      return null;
    }

    const data = await response.json();
    const externalBuildId =
      data.id || data.build_id || data.number || crypto.randomUUID();

    const build = {
      id: crypto.randomUUID(),
      configId: options.configId,
      pullRequestId: options.pullRequestId || null,
      externalBuildId: String(externalBuildId),
      buildNumber: data.number || null,
      status: "pending",
      url: data.url || data.web_url || null,
      startedAt: new Date(),
      completedAt: null,
      createdAt: new Date(),
    };

    // @ts-expect-error - Drizzle multi-db union type issue
    await db.insert(schema.externalBuilds).values(build);

    logger.info(
      {
        configId: options.configId,
        buildId: externalBuildId,
      },
      "External build triggered",
    );

    return build as ExternalBuild;
  } catch (error) {
    logger.error(
      { error, configId: options.configId },
      "Error triggering external build",
    );
    return null;
  }
}

/**
 * Sync build status from external CI
 */
export async function syncBuildStatus(
  buildId: string,
): Promise<ExternalBuild | null> {
  assertOnlineFeature("External CI status synchronization");
  const db = getDatabase();

  const build = await db.query.externalBuilds?.findFirst({
    where: eq(schema.externalBuilds.id, buildId),
  });

  if (!build) return null;

  const config = await db.query.externalCIConfigs?.findFirst({
    where: eq(schema.externalCIConfigs.id, build.configId),
  });

  if (!config) return null;

  const provider = CI_PROVIDERS[config.provider as keyof typeof CI_PROVIDERS];
  if (!provider) return null;

  let statusUrl = `${config.baseUrl}${provider.statusEndpoint}`;
  statusUrl = statusUrl
    .replace("{project}", config.projectId || "")
    .replace("{build}", build.externalBuildId)
    .replace("{pipeline}", build.externalBuildId);

  try {
    const response = await fetch(statusUrl, {
      headers: {
        ...(config.apiToken && { Authorization: `Bearer ${config.apiToken}` }),
      },
    });

    if (!response.ok) return null;

    const data = await response.json();

    // Normalize status across providers
    const status = normalizeStatus(
      config.provider,
      data.status || data.result || data.state,
    );
    const completedAt =
      status !== "pending" && status !== "running" ? new Date() : null;

    await (db as NodePgDatabase<typeof schema>)
      .update(schema.externalBuilds)
      .set({
        status,
        completedAt,
        url: data.url || data.web_url || build.url,
      })
      .where(eq(schema.externalBuilds.id, buildId));

    // If syncing to PR, update PR check
    if (build.pullRequestId && config.syncStatus) {
      await syncStatusToPR(build.pullRequestId, config.name, status);
    }

    return { ...build, status, completedAt };
  } catch (error) {
    logger.error({ error, buildId }, "Error syncing build status");
    return null;
  }
}

/**
 * Normalize status across different CI providers
 */
function normalizeStatus(provider: string, rawStatus: string): string {
  const statusMap: Record<string, Record<string, string>> = {
    jenkins: {
      SUCCESS: "success",
      FAILURE: "failure",
      UNSTABLE: "failure",
      ABORTED: "cancelled",
      BUILDING: "running",
      NOT_BUILT: "pending",
    },
    circleci: {
      success: "success",
      failed: "failure",
      error: "failure",
      canceled: "cancelled",
      running: "running",
      pending: "pending",
    },
    travis: {
      passed: "success",
      failed: "failure",
      errored: "failure",
      canceled: "cancelled",
      started: "running",
      created: "pending",
    },
    gitlab_ci: {
      success: "success",
      failed: "failure",
      canceled: "cancelled",
      running: "running",
      pending: "pending",
      created: "pending",
    },
    gitlab: {
      success: "success",
      failed: "failure",
      canceled: "cancelled",
      running: "running",
      pending: "pending",
      created: "pending",
    },
    buildkite: {
      passed: "success",
      failed: "failure",
      blocked: "cancelled",
      canceled: "cancelled",
      canceling: "running",
      running: "running",
      scheduled: "pending",
      creating: "pending",
    },
    azure_devops: {
      succeeded: "success",
      failed: "failure",
      canceled: "cancelled",
      inProgress: "running",
      notStarted: "pending",
    },
  };

  const providerMap = statusMap[provider] || {};
  return providerMap[rawStatus] || "pending";
}

/**
 * Sync external CI status to PR
 */
async function syncStatusToPR(
  prId: string,
  checkName: string,
  status: string,
): Promise<void> {
  const db = getDatabase();
  const pr = await db.query.pullRequests.findFirst({
    where: eq(schema.pullRequests.id, prId),
  });
  if (!pr) return;

  const conclusion =
    status === "success"
      ? "success"
      : status === "failure"
        ? "failure"
        : status === "cancelled"
          ? "cancelled"
          : null;

  // Upsert PR check
  const existing = await db.query.pullRequestChecks.findFirst({
    where: and(
      eq(schema.pullRequestChecks.pullRequestId, prId),
      eq(schema.pullRequestChecks.name, checkName),
    ),
  });

  if (existing) {
    await (db as NodePgDatabase<typeof schema>)
      .update(schema.pullRequestChecks)
      .set({
        headSha: pr.headSha,
        status: status === "running" ? "in_progress" : "completed",
        conclusion,
      })
      .where(eq(schema.pullRequestChecks.id, existing.id));
  } else {
    await (db as NodePgDatabase<typeof schema>).insert(schema.pullRequestChecks).values({
      id: crypto.randomUUID(),
      pullRequestId: prId,
      name: checkName,
      headSha: pr.headSha,
      status: status === "running" ? "in_progress" : "completed",
      conclusion,
      createdAt: new Date(),
    });
  }
}

/**
 * Handle webhook from external CI
 */
export async function handleExternalCIWebhook(
  webhookSecret: string,
  payload: Record<string, unknown>,
): Promise<boolean> {
  assertOnlineFeature("External CI webhooks");
  const db = getDatabase();

  // Find config by webhook secret
  const config = await db.query.externalCIConfigs?.findFirst({
    where: eq(schema.externalCIConfigs.webhookSecret, webhookSecret),
  });

  if (!config) {
    logger.warn(
      { webhookSecret: webhookSecret.slice(0, 8) },
      "Unknown webhook secret",
    );
    return false;
  }

  // Extract build info from payload (provider-specific)
  const buildId = payload.build_id || payload.id || (payload.build as any)?.id;
  const status = payload.status || payload.state || payload.result;

  if (buildId && status) {
    // Find and update build
    const build = (await db.query.externalBuilds?.findFirst({
      where: and(
        eq(schema.externalBuilds.configId, config.id),
        eq(schema.externalBuilds.externalBuildId, String(buildId)),
      ),
    })) as ExternalBuild | undefined;

    if (build) {
      const normalizedStatus = normalizeStatus(config.provider, String(status));

      await (db as NodePgDatabase<typeof schema>)
        .update(schema.externalBuilds)
        .set({
          status: normalizedStatus,
          completedAt: ["success", "failure", "cancelled"].includes(
            normalizedStatus,
          )
            ? new Date()
            : null,
        })
        .where(eq(schema.externalBuilds.id, build.id));

      // Sync to PR
      if (build.pullRequestId && config.syncStatus) {
        await syncStatusToPR(
          build.pullRequestId,
          config.name,
          normalizedStatus,
        );
      }
    }
  }

  return true;
}