@lbdudc/linea-git 0.2.2 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,9 @@ import { createGitHttp } from './git-http.js';
5
5
  import { unifiedDiff } from './diff.js';
6
6
  const CLIENT_ERROR_JSON = (message) => ({ error: message, message });
7
7
  const sha1For = (seedText) => crypto.createHash('sha1').update(seedText).digest('hex');
8
+ // What a tree holds, as one digest: a commit id that does not depend on it names two different states with the same id (as two projects
9
+ // numbered alike that publish different files under one message do), and anything that keys a cache by commit would serve the wrong one.
10
+ const treeDigest = (tree) => sha1For([...tree.entries()].sort(([x], [y]) => (x < y ? -1 : x > y ? 1 : 0)).map(([filePath, content]) => `${filePath}:${sha1For(content)}`).join('\n'));
8
11
  const shortId = (sha) => sha.slice(0, 8);
9
12
  const slugify = (name) => String(name)
10
13
  .trim()
@@ -53,7 +56,7 @@ class ProjectState {
53
56
  }
54
57
  #newCommit({ tree, parent, message }) {
55
58
  const seq = this.commits.size + 1;
56
- const id = sha1For(`${this.id}|${seq}|${message}`);
59
+ const id = sha1For(`${this.id}|${seq}|${message}|${treeDigest(tree)}`);
57
60
  const commit = {
58
61
  id,
59
62
  short_id: shortId(id),
@@ -601,7 +604,45 @@ export function createGitlabDoubleApp(webBase = 'http://gitlab-double.local') {
601
604
  router.get('/projects/:id/members/all', (req, res) => {
602
605
  const project = projectOr404(req, res);
603
606
  if (project)
604
- res.status(200).json(project.members);
607
+ res.status(200).json(project.members.map(({ inherited, ...member }) => member));
608
+ });
609
+ // the members set on the project itself (a member with `inherited: true` comes from its group: only `/members/all` lists it)
610
+ router.get('/projects/:id/members', (req, res) => {
611
+ const project = projectOr404(req, res);
612
+ if (project)
613
+ res.status(200).json(project.members.filter((member) => !member.inherited).map(({ inherited, ...member }) => member));
614
+ });
615
+ router.post('/projects/:id/members', (req, res) => {
616
+ const project = projectOr404(req, res);
617
+ if (!project)
618
+ return;
619
+ const username = usernameOf(Number(req.body?.user_id));
620
+ if (project.members.some((member) => member.id === Number(req.body?.user_id) && !member.inherited))
621
+ return sendError(res, 409, 'Member already exists');
622
+ project.members = project.members.filter((member) => member.id !== Number(req.body?.user_id));
623
+ const member = { id: Number(req.body.user_id), username, access_level: Number(req.body.access_level) };
624
+ project.members.push(member);
625
+ res.status(201).json(member);
626
+ });
627
+ router.put('/projects/:id/members/:userId', (req, res) => {
628
+ const project = projectOr404(req, res);
629
+ if (!project)
630
+ return;
631
+ const member = project.members.find((candidate) => candidate.id === Number(req.params.userId) && !candidate.inherited);
632
+ if (!member)
633
+ return sendError(res, 404, '404 Not found');
634
+ member.access_level = Number(req.body.access_level);
635
+ res.status(200).json(member);
636
+ });
637
+ router.delete('/projects/:id/members/:userId', (req, res) => {
638
+ const project = projectOr404(req, res);
639
+ if (!project)
640
+ return;
641
+ const at = project.members.findIndex((candidate) => candidate.id === Number(req.params.userId) && !candidate.inherited);
642
+ if (at < 0)
643
+ return sendError(res, 404, '404 Not found');
644
+ project.members.splice(at, 1);
645
+ res.status(204).end();
605
646
  });
606
647
  router.get('/projects', (req, res) => {
607
648
  const perPageRaw = req.query.per_page;
@@ -768,7 +809,7 @@ export function createGitlabDoubleApp(webBase = 'http://gitlab-double.local') {
768
809
  revertedTree.set(path, parentTree.get(path));
769
810
  }
770
811
  const seq = project.commits.size + 1;
771
- const id = sha1For(`${project.id}|revert|${seq}`);
812
+ const id = sha1For(`${project.id}|revert|${seq}|${treeDigest(revertedTree)}`);
772
813
  const revertCommit = {
773
814
  id, short_id: shortId(id), title: `Revert "${target.title}"`, message: `Revert "${target.title}"`,
774
815
  parent_ids: [headSha], created_at: new Date().toISOString(), author_name: 'gitlab-double',
@@ -1404,7 +1445,7 @@ export function createGitlabDoubleApp(webBase = 'http://gitlab-double.local') {
1404
1445
  return [project, issue];
1405
1446
  };
1406
1447
  // a user is any name: the double knows no accounts but `dev`
1407
- router.get('/users', (req, res) => res.json(req.query.username ? [userJson(String(req.query.username))] : []));
1448
+ router.get('/users', (req, res) => res.json(req.query.username ? [userJson(String(req.query.username))] : req.query.search ? [userJson(String(req.query.search))] : []));
1408
1449
  router.get('/projects/:id/labels', (req, res) => { const project = projectOr404(req, res); if (project)
1409
1450
  res.json(project.labels); });
1410
1451
  router.post('/projects/:id/labels', (req, res) => {
@@ -1443,7 +1484,9 @@ export function createGitlabDoubleApp(webBase = 'http://gitlab-double.local') {
1443
1484
  assignees: (assignee_ids ?? []).map(usernameOf),
1444
1485
  milestone: project.milestones.find((m) => m.id === milestone_id) ?? null,
1445
1486
  notes: [],
1487
+ labelEvents: [],
1446
1488
  };
1489
+ recordLabels(issue, [], issue.labels);
1447
1490
  project.issues.set(issue.iid, issue);
1448
1491
  res.status(201).json(issueJson(project, issue));
1449
1492
  });
@@ -1479,8 +1522,11 @@ export function createGitlabDoubleApp(webBase = 'http://gitlab-double.local') {
1479
1522
  issue.title = body.title;
1480
1523
  if (body.description !== undefined)
1481
1524
  issue.description = body.description;
1482
- if (body.labels !== undefined)
1483
- issue.labels = String(body.labels).split(',').filter(Boolean);
1525
+ if (body.labels !== undefined) {
1526
+ const labels = String(body.labels).split(',').filter(Boolean);
1527
+ recordLabels(issue, issue.labels, labels);
1528
+ issue.labels = labels;
1529
+ }
1484
1530
  if (body.assignee_ids !== undefined)
1485
1531
  issue.assignees = body.assignee_ids.map(usernameOf);
1486
1532
  if (body.milestone_id !== undefined)
@@ -1496,6 +1542,18 @@ export function createGitlabDoubleApp(webBase = 'http://gitlab-double.local') {
1496
1542
  issue.updated_at = new Date().toISOString();
1497
1543
  res.json(issueJson(project, issue));
1498
1544
  });
1545
+ // what GitLab keeps of each label put on an issue or taken off it
1546
+ let labelEventId = 1;
1547
+ function recordLabels(issue, before, after) {
1548
+ const at = new Date().toISOString();
1549
+ const event = (name, action) => ({ id: labelEventId++, action, label: { id: 0, name }, user: userJson('dev'), created_at: at, resource_type: 'Issue', resource_id: issue.iid });
1550
+ issue.labelEvents.push(...before.filter((l) => !after.includes(l)).map((l) => event(l, 'remove')), ...after.filter((l) => !before.includes(l)).map((l) => event(l, 'add')));
1551
+ }
1552
+ router.get('/projects/:id/issues/:iid/resource_label_events', (req, res) => {
1553
+ const [, issue] = issueOr404(req, res);
1554
+ if (issue)
1555
+ res.json(issue.labelEvents);
1556
+ });
1499
1557
  router.get('/projects/:id/issues/:iid/notes', (req, res) => {
1500
1558
  const [, issue] = issueOr404(req, res);
1501
1559
  if (issue)