summaryrefslogtreecommitdiff
path: root/admin-panel/src/server/routes/gerrit.ts
blob: b48ea53c7d9b3d5a298c6c4f30e84e6c9095a78f (plain)
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
import { Router, Request, Response } from 'express';
import { getAdminDb, getProjectDb } from '../db.js';
import { getChange, getChangeByNumber } from '../services/gerrit.service.js';

const router = Router({ mergeParams: true });

type Params = { project: string; taskId: string };

// GET /api/projects/:project/gerrit/:taskId
router.get('/:taskId', async (req: Request<Params>, res: Response) => {
  const { project, taskId } = req.params;

  const adminDb = getAdminDb();
  if (!adminDb.prepare('SELECT id FROM projects WHERE name = ?').get(project)) {
    res.status(404).json({ error: 'Project not found' });
    return;
  }

  const db = getProjectDb(project);
  const task = db.prepare('SELECT gerrit_change_id, gerrit_change_number FROM tasks WHERE id = ?').get(parseInt(taskId)) as {
    gerrit_change_id: string | null;
    gerrit_change_number: number | null;
  } | undefined;

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

  if (!task.gerrit_change_id && !task.gerrit_change_number) {
    res.json({ change: null });
    return;
  }

  try {
    const change = task.gerrit_change_id
      ? await getChange(task.gerrit_change_id)
      : task.gerrit_change_number
        ? await getChangeByNumber(task.gerrit_change_number)
        : null;
    res.json({ change });
  } catch (err) {
    res.status(502).json({ error: `Gerrit API error: ${(err as Error).message}` });
  }
});

// GET /api/projects/:project/diff/:taskId
router.get('/diff/:taskId', (req: Request<Params>, res: Response) => {
  const { project, taskId } = req.params;

  const adminDb = getAdminDb();
  if (!adminDb.prepare('SELECT id FROM projects WHERE name = ?').get(project)) {
    res.status(404).json({ error: 'Project not found' });
    return;
  }

  const db = getProjectDb(project);
  const task = db.prepare('SELECT diff FROM tasks WHERE id = ?').get(parseInt(taskId)) as { diff: string | null } | undefined;

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

  res.json({ diff: task.diff || '' });
});

export default router;