@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.
@@ -288,6 +288,10 @@ export function createGithubDoubleApp(baseUrl = 'http://github.double') {
288
288
  };
289
289
  // ── account ──────────────────────────────────────────────────────────────
290
290
  router.get('/user', (_req, res) => res.json({ login: state.user.login, id: state.user.id, avatar_url: '', type: 'User' }));
291
+ router.get('/search/users', (req, res) => {
292
+ const q = String(req.query.q ?? '');
293
+ res.json({ total_count: 1, items: q ? [{ login: q, avatar_url: null }] : [] });
294
+ });
291
295
  router.get('/users/:login', (req, res) => {
292
296
  const account = state.ownerOf(req.params.login);
293
297
  if (!account)
@@ -379,7 +383,29 @@ export function createGithubDoubleApp(baseUrl = 'http://github.double') {
379
383
  const repo = repoOr404(req, res);
380
384
  if (!repo)
381
385
  return;
382
- res.json(repo.collaborators.map((c) => ({ login: c.login, id: c.id, role_name: c.role, permissions: permissionsOf(c.role) })));
386
+ // `affiliation=direct` leaves out what comes from the organization (a collaborator marked `inherited`)
387
+ const list = req.query.affiliation === 'direct' ? repo.collaborators.filter((c) => !c.inherited) : repo.collaborators;
388
+ res.json(list.map((c) => ({ login: c.login, id: c.id, role_name: c.role, permissions: permissionsOf(c.role) })));
389
+ });
390
+ router.put(`${R}/collaborators/:user`, (req, res) => {
391
+ const repo = repoOr404(req, res);
392
+ if (!repo)
393
+ return;
394
+ const role = { pull: 'read', push: 'write', maintain: 'maintain', admin: 'admin' }[req.body?.permission] ?? 'read';
395
+ const found = repo.collaborators.find((c) => c.login === req.params.user && !c.inherited);
396
+ if (found) {
397
+ found.role = role;
398
+ return res.status(204).end();
399
+ }
400
+ repo.collaborators.push({ login: req.params.user, id: 100 + repo.collaborators.length, role });
401
+ res.status(201).json({ id: 1, permissions: permissionsOf(role) });
402
+ });
403
+ router.delete(`${R}/collaborators/:user`, (req, res) => {
404
+ const repo = repoOr404(req, res);
405
+ if (!repo)
406
+ return;
407
+ repo.collaborators = repo.collaborators.filter((c) => c.login !== req.params.user || c.inherited);
408
+ res.status(204).end();
383
409
  });
384
410
  router.get(`${R}/collaborators/:user/permission`, (req, res) => {
385
411
  const repo = repoOr404(req, res);
@@ -908,7 +934,9 @@ export function createGithubDoubleApp(baseUrl = 'http://github.double') {
908
934
  const issue = {
909
935
  number: repo.nextPull++, title, body: body ?? '', state: 'open', author: 'dev', created_at: now, updated_at: now, closed_at: null,
910
936
  labels: labels ?? [], assignees: assignees ?? [], milestone: repo.milestones.find((m) => m.number === milestone) ?? null, comments: [],
937
+ events: [],
911
938
  };
939
+ recordLabels(issue, [], issue.labels);
912
940
  repo.issues.push(issue);
913
941
  res.status(201).json(issueJson(repo, issue));
914
942
  });
@@ -935,8 +963,10 @@ export function createGithubDoubleApp(baseUrl = 'http://github.double') {
935
963
  issue.title = body.title;
936
964
  if (body.body !== undefined)
937
965
  issue.body = body.body;
938
- if (body.labels !== undefined)
966
+ if (body.labels !== undefined) {
967
+ recordLabels(issue, issue.labels, body.labels);
939
968
  issue.labels = body.labels;
969
+ }
940
970
  if (body.assignees !== undefined)
941
971
  issue.assignees = body.assignees;
942
972
  if (body.milestone !== undefined)
@@ -948,6 +978,19 @@ export function createGithubDoubleApp(baseUrl = 'http://github.double') {
948
978
  issue.updated_at = new Date().toISOString();
949
979
  res.json(issueJson(repo, issue));
950
980
  });
981
+ // what GitHub keeps of each label put on an issue or taken off it, among the issue's events
982
+ function recordLabels(issue, before, after) {
983
+ if (!issue.events)
984
+ return;
985
+ const at = new Date().toISOString();
986
+ const event = (name, kind) => ({ id: Math.floor(Math.random() * 1e9), event: kind, label: { name, color: 'ededed' }, actor: userJson('dev'), created_at: at });
987
+ issue.events.push(...before.filter((l) => !after.includes(l)).map((l) => event(l, 'unlabeled')), ...after.filter((l) => !before.includes(l)).map((l) => event(l, 'labeled')));
988
+ }
989
+ router.get(`${R}/issues/:n/events`, (req, res) => {
990
+ const [, issue] = issueOr404(req, res);
991
+ if (issue)
992
+ res.json(paged(req, issue.events ?? []));
993
+ });
951
994
  router.get(`${R}/issues/:n/comments`, (req, res) => {
952
995
  const [, issue] = issueOr404(req, res);
953
996
  if (issue)
@@ -974,13 +1017,16 @@ export function createGithubDoubleApp(baseUrl = 'http://github.double') {
974
1017
  const [repo, item] = labelled(req, res);
975
1018
  if (!item)
976
1019
  return;
977
- item.labels = [...new Set([...(item.labels ?? []), ...(req.body?.labels ?? [])])];
1020
+ const labels = [...new Set([...(item.labels ?? []), ...(req.body?.labels ?? [])])];
1021
+ recordLabels(item, item.labels ?? [], labels);
1022
+ item.labels = labels;
978
1023
  res.json(labelsJson(repo, item));
979
1024
  });
980
1025
  router.put(`${R}/issues/:n/labels`, (req, res) => {
981
1026
  const [repo, item] = labelled(req, res);
982
1027
  if (!item)
983
1028
  return;
1029
+ recordLabels(item, item.labels ?? [], req.body?.labels ?? []);
984
1030
  item.labels = req.body?.labels ?? [];
985
1031
  res.json(labelsJson(repo, item));
986
1032
  });