@codyn06/cboard 0.3.6 → 0.3.7

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.
package/dist/api.d.ts CHANGED
@@ -72,7 +72,9 @@ export declare function submitBatch<T>(api: ContextApi, group: T[], make: (items
72
72
  */
73
73
  export declare function reconcile(api: ContextApi, snapshot: Snapshot, namespace: string, supersede?: boolean,
74
74
  /** Nodes outside the snapshot (other namespaces) that edges may point to, by client key. */
75
- anchors?: Map<string, string>): Promise<{
75
+ anchors?: Map<string, string>,
76
+ /** Nodes outside the namespace (hand-written) the snapshot takes over, by client key; revised in place, never superseded. */
77
+ adopted?: Map<string, import("./model.js").RemoteNode>): Promise<{
76
78
  contributions: number;
77
79
  digest: string;
78
80
  }>;
package/dist/api.js CHANGED
@@ -310,10 +310,15 @@ const KEY_LOOKUP_CONCURRENCY = 5;
310
310
  */
311
311
  export async function reconcile(api, snapshot, namespace, supersede = true,
312
312
  /** Nodes outside the snapshot (other namespaces) that edges may point to, by client key. */
313
- anchors = new Map()) {
313
+ anchors = new Map(),
314
+ /** Nodes outside the namespace (hand-written) the snapshot takes over, by client key; revised in place, never superseded. */
315
+ adopted = new Map()) {
314
316
  // Active heads only: history is not re-downloaded on every sync (#145).
315
317
  const { rows: existing, steady } = await api.nodeHeads(namespace);
316
318
  const byKey = new Map(existing.map((n) => [n.clientKey, n]));
319
+ for (const [key, n] of adopted)
320
+ if (!byKey.has(key))
321
+ byKey.set(key, n);
317
322
  // Absent keys are new, superseded/withdrawn in this namespace (revived below), or legacy nodes
318
323
  // outside the namespace with the same client key. A few are looked up by key; many use one
319
324
  // project-wide scan of the snapshot's node types, so a first sync costs pages, not N requests.
package/dist/cboard.js CHANGED
@@ -50,6 +50,11 @@ repository node; run "cboard sync" first. Reads through "gh api", or GITHUB_TOKE
50
50
  unavailable. --state open or --limit mirrors a partial list and keeps issues it did not see.
51
51
  Closed issues become resolved, wontfix (label wontfix or not_planned) or moot (label duplicate/invalid);
52
52
  bodies cut at 4000 characters set bodyTruncated.
53
+ An existing active open_issue node in the default namespace that is titled "#<number> ..." (or cites that
54
+ issue's URL) is taken over, not duplicated: it is revised in place, marked with the issueMirror property, and
55
+ kept up to date and linked to code from then on. It gets the GitHub title, body and status (earlier versions
56
+ stay in the node history) and keeps its other properties. Two such nodes for one issue, or an issue whose
57
+ earlier mirror node is inactive, are skipped with a warning; a node another repository's mirror took over is left alone.
53
58
  Closed issues also get "implements" edges to the files (in the code snapshot) changed by the merged pull
54
59
  requests that closed them, at most 50 files and skipping pull requests that changed more; --link-code off
55
60
  disables this. The pull request lookup costs one request per closed issue changed since the last run,
package/dist/issues.js CHANGED
@@ -158,13 +158,90 @@ export function issueStatus(i) {
158
158
  return "moot";
159
159
  return "resolved";
160
160
  }
161
- function snapshotOf(issues, namespace, repoKey) {
161
+ const ISSUE_URL = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/issues\/(\d+)\/?$/i;
162
+ /** Property that marks a hand-written node as taken over by the mirror (its own client key cannot change). */
163
+ const MIRROR_MARK = "issueMirror";
164
+ /**
165
+ * The issue number a hand-written open_issue node stands for: its "#N " title prefix (every
166
+ * hand-written issue node has one; most cite a pull request, not the issue, so the URL alone
167
+ * would miss them), else the URL of one of this repository's issues in its provenance. A node that
168
+ * cites another repository's issue is nobody's match.
169
+ */
170
+ function handWrittenNumber(n, owner, name) {
171
+ const prefix = /^#(\d+) /.exec(n.title);
172
+ if (prefix)
173
+ return Number(prefix[1]);
174
+ let fromUrl;
175
+ for (const p of n.provenance) {
176
+ const m = p.kind === "url" ? ISSUE_URL.exec(p.ref ?? "") : null;
177
+ if (!m)
178
+ continue;
179
+ const same = m[1]?.toLowerCase() === owner.toLowerCase() && m[2]?.toLowerCase() === name.toLowerCase();
180
+ if (!same)
181
+ return undefined;
182
+ fromUrl = Number(m[3]);
183
+ }
184
+ return fromUrl;
185
+ }
186
+ /**
187
+ * Hand-written active open_issue nodes (namespace "default") the mirror takes over, by its client
188
+ * key. A node it already took over (marked) wins over later look-alikes. Two candidates for one
189
+ * issue are ambiguous: that issue is returned in `skipped`, with a warning, and nothing is guessed.
190
+ */
191
+ async function findAdoptable(api, issues, namespace, repo) {
192
+ const adopted = new Map();
193
+ const skipped = new Set();
194
+ const present = new Set((await api.nodeHeads(namespace)).rows.map((n) => n.clientKey));
195
+ const missing = issues.filter((i) => !present.has(`${namespace}:#${String(i.number)}`));
196
+ if (!missing.length)
197
+ return { adopted, skipped };
198
+ const candidates = (await api.all({ types: "open_issue" })).nodes.filter((n) => n.type === "open_issue" && n.lifecycle === "active" && n.namespace === "default");
199
+ for (const i of missing) {
200
+ const key = `${namespace}:#${String(i.number)}`;
201
+ // A node another mirror (another repository in this project) took over is not ours to take back.
202
+ const matches = candidates.filter((n) => handWrittenNumber(n, repo.owner, repo.name) === i.number &&
203
+ (n.properties[MIRROR_MARK] ?? key) === key);
204
+ const owned = matches.filter((n) => n.properties[MIRROR_MARK] === key);
205
+ const pool = owned.length ? owned : matches;
206
+ if (pool.length > 1) {
207
+ skipped.add(i.number);
208
+ console.error(`#${String(i.number)}: ambiguous, ${String(pool.length)} hand-written open_issue nodes match (${pool.map((n) => n.id).join(", ")}); skipped`);
209
+ }
210
+ else if (pool[0]) {
211
+ // The server's duplicate probe also reads inactive heads: a retired mirror node with this
212
+ // title would reject the revision and fail the batch.
213
+ const retired = (await api.all({ client_key: key, include_inactive: "true" })).nodes.some((n) => n.namespace === namespace && n.lifecycle !== "active");
214
+ if (retired) {
215
+ skipped.add(i.number);
216
+ console.error(`#${String(i.number)}: the mirror's earlier node for this issue is inactive (superseded or withdrawn); not adopting ${pool[0].id}, skipped`);
217
+ }
218
+ else
219
+ adopted.set(key, pool[0]);
220
+ }
221
+ }
222
+ return { adopted, skipped };
223
+ }
224
+ /** A taken-over node keeps its own properties; the mirror's overwrite theirs (a stale truncation flag goes). */
225
+ const handProperties = (old) => {
226
+ const rest = { ...old?.properties };
227
+ delete rest.bodyTruncated;
228
+ return rest;
229
+ };
230
+ function snapshotOf(issues, namespace, repoKey, adopted) {
162
231
  const nodes = [];
163
232
  const edges = [];
164
233
  for (const i of issues) {
165
234
  const clientKey = `${namespace}:#${String(i.number)}`;
166
235
  // Issue nodes cite the issue URL (the graph's url provenance), not a repository commit.
167
- const provenance = [{ kind: "url", ref: i.html_url }];
236
+ const url = { kind: "url", ref: i.html_url };
237
+ const old = adopted.get(clientKey);
238
+ // A taken-over node keeps its own evidence and gains the issue URL.
239
+ const own = (old?.provenance ?? []);
240
+ const provenance = (old
241
+ ? own.some((p) => p.kind === "url" && p.ref === url.ref)
242
+ ? own
243
+ : [...own, url]
244
+ : [url]);
168
245
  const full = (i.body ?? "").trim();
169
246
  nodes.push({
170
247
  clientKey,
@@ -174,8 +251,10 @@ function snapshotOf(issues, namespace, repoKey) {
174
251
  body: full.slice(0, BODY_MAX),
175
252
  status: issueStatus(i),
176
253
  properties: {
254
+ ...handProperties(old),
177
255
  url: i.html_url,
178
256
  labels: labelNames(i),
257
+ ...(old ? { [MIRROR_MARK]: clientKey } : {}),
179
258
  ...(full.length > BODY_MAX ? { bodyTruncated: true } : {}),
180
259
  },
181
260
  provenance,
@@ -185,7 +264,7 @@ function snapshotOf(issues, namespace, repoKey) {
185
264
  type: "part_of",
186
265
  from: clientKey,
187
266
  to: repoKey,
188
- provenance,
267
+ provenance: [url],
189
268
  derivation: "extracted",
190
269
  properties: {},
191
270
  });
@@ -198,7 +277,7 @@ function snapshotOf(issues, namespace, repoKey) {
198
277
  * unchanged issue costs no request. Reconciled in a namespace of its own: only these edges are
199
278
  * added or retired, hand-written edges are never touched.
200
279
  */
201
- async function linkIssuesToCode(api, identity, issues, complete, source, store, repoNode) {
280
+ async function linkIssuesToCode(api, identity, issues, complete, source, store, repoNode, adopted) {
202
281
  const { owner, name } = parseGithubRemote(identity.repo);
203
282
  const issuesNs = issuesNamespace(identity.repo);
204
283
  const cacheKey = `issue-prs|${identity.repo}`;
@@ -262,6 +341,8 @@ async function linkIssuesToCode(api, identity, issues, complete, source, store,
262
341
  const issueIds = new Map((await api.nodeHeads(issuesNs)).rows
263
342
  .filter((n) => n.type === "open_issue" && n.lifecycle === "active")
264
343
  .map((n) => [n.clientKey, n.id]));
344
+ for (const [key, n] of adopted)
345
+ issueIds.set(key, n.id);
265
346
  const commit = repoNode.provenance[0]?.commit ?? "";
266
347
  const anchors = new Map();
267
348
  const edges = [];
@@ -331,15 +412,17 @@ export async function runIssuesSync(api, identity, source, options, links) {
331
412
  throw new Error("The repository node is missing from the graph; run `cboard sync` first");
332
413
  const namespace = issuesNamespace(identity.repo);
333
414
  const fetched = await source({ owner, name, state: options.state, limit: options.limit });
334
- const issues = fetched
415
+ const listed = fetched
335
416
  .filter((i) => !i.pull_request)
336
417
  .sort((a, b) => a.number - b.number)
337
418
  .slice(0, options.limit);
338
- const snapshot = snapshotOf(issues, namespace, repoKey);
419
+ const { adopted, skipped } = await findAdoptable(api, listed, namespace, { owner, name });
420
+ const issues = listed.filter((i) => !skipped.has(i.number));
421
+ const snapshot = snapshotOf(issues, namespace, repoKey, adopted);
339
422
  const complete = options.state === "all" && options.limit === undefined;
340
- const result = await reconcile(api, snapshot, namespace, complete, new Map([[repoKey, repoNode.id]]));
423
+ const result = await reconcile(api, snapshot, namespace, complete, new Map([[repoKey, repoNode.id]]), adopted);
341
424
  const linked = links && options.linkCode !== false
342
- ? await linkIssuesToCode(api, identity, issues, complete, links.source, links.store, repoNode)
425
+ ? await linkIssuesToCode(api, identity, issues, complete, links.source, links.store, repoNode, adopted)
343
426
  : {};
344
427
  return { issues: issues.length, contributions: result.contributions, ...linked };
345
428
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codyn06/cboard",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "Client for cboard project graphs: connect a repository, sync its code layer, check the connection.",
5
5
  "keywords": [
6
6
  "cboard",