onetaskgraph-github-projects 0.14.13

A onetaskgraph source over GitHub Projects.
Documentation
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! Relations on a GitHub board: an issue's parent, which is its sub-issue link when the source
//! reads sub-issues as children (`sub_issues: parent`), and the `relates-to` relation a link type
//! is mirrored as.
//!
//! Every write here goes in aliased documents of at most [`RELATION_BATCH`] fields, each field
//! answered on its own: GitHub runs each alias by itself and answers a refusal — a cycle, a
//! depth or children limit — at that alias's path. Every relation mutation is paced like every
//! other mutation this source sends, each aliased field one content creation. A relation write
//! whose answer never arrived — a gateway timeout after GitHub ran part of it — is read back and
//! each relation answered from what GitHub holds, never sent again blindly.

use std::collections::BTreeMap;

use onetaskgraph_plugin_api::{
    NativeId, NativeLink, ParentAnswer, ParentWrite, Relations, SourceError, version_at,
};
use serde_json::{Map, Value, json};

use super::batch::{alias_answer, errors_at};
use super::{
    BOARD_ITEMS_PAGE_SIZE, BoardKind, DETAIL_BATCH, GitHubProjectsSource, MAX_PAGE_SIZE,
    NESTED_PAGE_SIZE, RELATION_BATCH, Resolved, SubIssues, graphql, optional_str, required_str,
    unresolvable_node,
};
use onetaskgraph_plugin_api::ItemKind;

/// The one native word a link type may be mirrored as on a GitHub board.
pub(crate) const RELATES_TO: &str = "relates-to";

/// Why a `link_types` word this board could mean is never a native mirror here: blocking is
/// what a task's `depends_on` already writes as `blockedBy`, and a native duplicate closes the
/// issue it marks.
pub(crate) const REFUSED_WORDS: &[(&str, &str)] = &[
    (
        "blocks",
        "blocking is what a task's `depends_on` already means, written as GitHub's `blockedBy`, so no link type is mirrored as it",
    ),
    (
        "blocked-by",
        "blocking is what a task's `depends_on` already means, written as GitHub's `blockedBy`, so no link type is mirrored as it",
    ),
    (
        "duplicate",
        "GitHub marks a duplicate only by closing the issue, so a duplicate link stays in the store's metadata alone",
    ),
    (
        "similar",
        "GitHub has no similar relation; a duplicate or similar link stays in the store's metadata alone",
    ),
];

/// Which way a native `relates-to` write goes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum NativeWrite {
    /// `addRelatesTo`: afterwards the pair is related.
    Add,
    /// `removeRelatesTo`: afterwards the pair is not.
    Remove,
}

/// One parent write, as it goes out.
enum ParentOp {
    /// `addSubIssue` of `child` under `parent`, replacing the parent it has when `replace`.
    Add {
        parent: NativeId,
        child: NativeId,
        replace: bool,
    },
    /// `removeSubIssue` of `child` from `parent`.
    Remove { parent: NativeId, child: NativeId },
}

impl ParentOp {
    fn child(&self) -> &NativeId {
        match self {
            Self::Add { child, .. } | Self::Remove { child, .. } => child,
        }
    }

    /// The issue the write adds `child` under or removes it from.
    fn parent(&self) -> &NativeId {
        match self {
            Self::Add { parent, .. } | Self::Remove { parent, .. } => parent,
        }
    }
}

impl GitHubProjectsSource {
    /// Refuse a project write on a source reading sub-issues as children, naming the setting.
    pub(super) fn projectless(&self) -> Result<(), SourceError> {
        match self.sub_issues {
            SubIssues::Project => Ok(()),
            SubIssues::Parent => Err(SourceError::Refused {
                message: format!(
                    "source {} reads sub-issues as children (`sub_issues: parent`), so it holds \
                     no projects and refuses a project write; next: write the project to a source \
                     whose board keeps the default `sub_issues: project`",
                    self.name
                ),
            }),
        }
    }

    /// The refusal a parent write to a source reading sub-issues as projects answers.
    fn parentless(&self) -> SourceError {
        SourceError::Refused {
            message: format!(
                "source {} reads an issue's sub-issues as a project's tasks (`sub_issues: \
                 project`, the default), so it holds no parent relation; next: set \
                 `sources.{}.config.sub_issues: parent` on a source whose board is its own",
                self.name, self.name
            ),
        }
    }

    /// [`TaskSource::read_relations`](onetaskgraph_plugin_api::TaskSource::read_relations):
    /// every id read together — with each issue's parent and a page of its sub-issues on a
    /// source reading them as children, and as a batched read of each item alone on one reading
    /// them as projects, where no task has a parent.
    ///
    /// Each answer is its own. A batch GitHub refuses because one of its ids resolves to no
    /// node is read again an id at a time, as [`Self::items_by_ids`] reads one, so that id
    /// answers `None` and every other id of the batch is answered; an issue answered malformed
    /// is that id's error alone.
    pub(super) async fn relations_of(
        &self,
        ids: &[NativeId],
    ) -> Vec<Result<Option<Relations>, SourceError>> {
        if self.sub_issues == SubIssues::Project {
            return match self.items_by_ids(ids).await {
                Err(error) => ids.iter().map(|_| Err(error.clone())).collect(),
                Ok(items) => items
                    .into_iter()
                    .map(|item| match item {
                        Some(item) if item.kind == BoardKind::Work(ItemKind::Task) => {
                            Ok(Some(Relations {
                                task: item.task()?,
                                parent: None,
                                children: Vec::new(),
                                children_complete: true,
                            }))
                        }
                        _ => Ok(None),
                    })
                    .collect(),
            };
        }
        let mut answers = Vec::with_capacity(ids.len());
        for batch in ids.chunks(DETAIL_BATCH) {
            match self.relations_batch(batch).await {
                Ok(each) => answers.extend(each),
                Err(error) if unresolvable_node(&error) && batch.len() > 1 => {
                    for id in batch {
                        answers.push(match self.relations_batch(std::slice::from_ref(id)).await {
                            Ok(mut each) => each.pop().unwrap_or(Ok(None)),
                            Err(error) if unresolvable_node(&error) => Ok(None),
                            Err(error) => Err(error),
                        });
                    }
                }
                Err(error) if unresolvable_node(&error) => answers.push(Ok(None)),
                Err(error) => answers.extend(batch.iter().map(|_| Err(error.clone()))),
            }
        }
        answers
    }

    /// One [`graphql::ISSUE_RELATIONS`] read of `batch` — at most [`DETAIL_BATCH`] ids — each
    /// id's answer its own; an error when the request was not answered.
    async fn relations_batch(
        &self,
        batch: &[NativeId],
    ) -> Result<Vec<Result<Option<Relations>, SourceError>>, SourceError> {
        let mut variables = Map::new();
        for slot in 0..DETAIL_BATCH {
            let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
            variables.insert(format!("id{slot}"), json!(id));
        }
        variables.insert("first".to_owned(), json!(MAX_PAGE_SIZE));
        variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
        variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
        variables.insert("duplicates".to_owned(), json!(true));
        // Not one of the documents whose answer keeps its errors beside its data
        // (`send_once`): a response carrying any GraphQL error — one at an alias's path
        // included — is refused whole here, so no slot below is read from a part-answer.
        let data = self
            .graphql(graphql::ISSUE_RELATIONS, Value::Object(variables))
            .await?;
        let mut answers = Vec::with_capacity(batch.len());
        for (slot, id) in batch.iter().enumerate() {
            answers.push(self.relations_slot(&data, slot, id).await);
        }
        Ok(answers)
    }

    /// The relations [`Self::relations_batch`]'s answer holds for `id` at `slot`.
    async fn relations_slot(
        &self,
        data: &Value,
        slot: usize,
        id: &NativeId,
    ) -> Result<Option<Relations>, SourceError> {
        let node = data
            .get(format!("i{slot}"))
            .ok_or_else(|| SourceError::Malformed {
                message: format!("GitHub answered a relations read with no item for {}", id.0),
            })?;
        if node.is_null() || optional_str(node, "__typename")? != Some("Issue") {
            return Ok(None);
        }
        if required_str(node, "id")? != id.0 {
            return Err(SourceError::Malformed {
                message: format!(
                    "GitHub answered the relations read of {} with issue {}",
                    id.0,
                    required_str(node, "id")?
                ),
            });
        }
        let Some(item) = self.resolve_issue(node).await? else {
            return Ok(None);
        };
        if item.kind != BoardKind::Work(ItemKind::Task) {
            return Ok(None);
        }
        let connection = node
            .get("subIssues")
            .ok_or_else(|| SourceError::Malformed {
                message: format!("GitHub issue {} is missing subIssues", id.0),
            })?;
        let children = connection
            .get("nodes")
            .and_then(Value::as_array)
            .ok_or_else(|| SourceError::Malformed {
                message: format!("GitHub issue {} subIssues.nodes is not an array", id.0),
            })?
            .iter()
            .map(|child| required_str(child, "id").map(|id| NativeId(id.to_owned())))
            .collect::<Result<Vec<_>, _>>()?;
        let more = connection
            .pointer("/pageInfo/hasNextPage")
            .and_then(Value::as_bool)
            .ok_or_else(|| SourceError::Malformed {
                message: format!("GitHub issue {} subIssues has no pageInfo", id.0),
            })?;
        Ok(Some(Relations {
            parent: item.issue_parent.clone(),
            task: item.task()?,
            children,
            children_complete: !more,
        }))
    }

    /// [`TaskSource::set_parents`](onetaskgraph_plugin_api::TaskSource::set_parents): every child
    /// read together, each write decided on that read, and the writes sent in
    /// [`graphql::SET_PARENTS`] documents of at most [`RELATION_BATCH`] fields.
    pub(super) async fn parents_set(
        &self,
        writes: &[ParentWrite],
    ) -> Vec<Result<ParentAnswer, SourceError>> {
        if self.sub_issues == SubIssues::Project {
            return writes.iter().map(|_| Err(self.parentless())).collect();
        }
        let mut answers: Vec<Option<Result<ParentAnswer, SourceError>>> =
            writes.iter().map(|_| None).collect();
        let children: Vec<NativeId> = writes.iter().fold(Vec::new(), |mut ids, write| {
            if !ids.contains(&write.child) {
                ids.push(write.child.clone());
            }
            ids
        });
        let read = match self.items_by_ids(&children).await {
            Ok(read) => read,
            Err(error) => return writes.iter().map(|_| Err(error.clone())).collect(),
        };
        let mut held: BTreeMap<NativeId, Resolved> = children
            .into_iter()
            .zip(read)
            .filter_map(|(id, item)| {
                item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
                    .map(|item| (id, item))
            })
            .collect();
        // The writes of one child go in batch order, one a round: each later one is decided on
        // what the one before it left — a refused edge leaving the parent it found, a written one
        // leaving its own and a version nothing read — and never sent beside it, because one
        // document runs its `addSubIssue` fields before its `removeSubIssue` ones.
        let mut rounds: Vec<Vec<usize>> = Vec::new();
        let mut seen: BTreeMap<&NativeId, usize> = BTreeMap::new();
        for (at, write) in writes.iter().enumerate() {
            let round = seen.entry(&write.child).or_insert(0);
            if rounds.len() <= *round {
                rounds.push(Vec::new());
            }
            rounds[*round].push(at);
            *round += 1;
        }
        // Each child's version as last known: as read, and unknown once this call wrote it.
        let mut versions: BTreeMap<NativeId, Option<String>> = held
            .iter()
            .map(|(id, item)| (id.clone(), item.version()))
            .collect();
        for round in rounds {
            let mut ops: Vec<(usize, ParentOp)> = Vec::new();
            for at in round {
                let write = &writes[at];
                let Some(child) = held.get(&write.child) else {
                    answers[at] = Some(Ok(ParentAnswer::Missing {
                        what: write.child.clone(),
                    }));
                    continue;
                };
                let version = versions.get(&write.child).cloned().flatten();
                if let Some(expected) = &write.if_version
                    && !version_at(self, version.as_deref(), expected)
                {
                    answers[at] = Some(Ok(ParentAnswer::VersionMoved { found: version }));
                    continue;
                }
                if child.issue_parent == write.parent {
                    answers[at] = Some(Ok(ParentAnswer::Unchanged { version }));
                    continue;
                }
                let op = match (&write.parent, &child.issue_parent) {
                    (Some(parent), current) => ParentOp::Add {
                        parent: parent.clone(),
                        child: write.child.clone(),
                        replace: current.is_some(),
                    },
                    (None, Some(current)) => ParentOp::Remove {
                        parent: current.clone(),
                        child: write.child.clone(),
                    },
                    (None, None) => unreachable!("an unchanged parent was answered above"),
                };
                ops.push((at, op));
            }
            for chunk in ops.chunks(RELATION_BATCH) {
                for (at, answer) in self.send_parents(chunk).await {
                    let write = &writes[at];
                    if matches!(answer, Ok(ParentAnswer::Written { .. }))
                        && let Some(child) = held.get_mut(&write.child)
                    {
                        child.issue_parent.clone_from(&write.parent);
                        versions.insert(write.child.clone(), None);
                    }
                    answers[at] = Some(answer);
                }
                for (_, op) in chunk {
                    let _ = self.forget(op.child());
                }
            }
        }
        answers
            .into_iter()
            .map(|answer| {
                answer.unwrap_or_else(|| {
                    Err(SourceError::Malformed {
                        message: "a parent write was left unanswered".into(),
                    })
                })
            })
            .collect()
    }

    /// One [`graphql::SET_PARENTS`] request of `chunk`, answering each write by its position.
    async fn send_parents(
        &self,
        chunk: &[(usize, ParentOp)],
    ) -> Vec<(usize, Result<ParentAnswer, SourceError>)> {
        let Some((_, first)) = chunk.first() else {
            return Vec::new();
        };
        let filler = match first {
            ParentOp::Add { parent, child, .. } | ParentOp::Remove { parent, child } => {
                json!({"issueId": parent.0, "subIssueId": child.0})
            }
        };
        let mut variables = Map::new();
        for slot in 0..RELATION_BATCH {
            for (prefix, send) in [("parent", "sendParent"), ("unparent", "sendUnparent")] {
                variables.insert(format!("{prefix}{slot}"), filler.clone());
                variables.insert(format!("{send}{slot}"), json!(false));
            }
        }
        let mut aliases = Vec::with_capacity(chunk.len());
        let (mut adds, mut removes) = (0, 0);
        for (at, op) in chunk {
            let alias = match op {
                ParentOp::Add {
                    parent,
                    child,
                    replace,
                } => {
                    let alias = format!("parent{adds}");
                    let mut input = json!({"issueId": parent.0, "subIssueId": child.0});
                    if *replace {
                        input["replaceParent"] = json!(true);
                    }
                    variables.insert(alias.clone(), input);
                    variables.insert(format!("sendParent{adds}"), json!(true));
                    adds += 1;
                    alias
                }
                ParentOp::Remove { parent, child } => {
                    let alias = format!("unparent{removes}");
                    variables.insert(
                        alias.clone(),
                        json!({"issueId": parent.0, "subIssueId": child.0}),
                    );
                    variables.insert(format!("sendUnparent{removes}"), json!(true));
                    removes += 1;
                    alias
                }
            };
            aliases.push((*at, alias, op.child().clone(), op.parent().clone()));
        }
        let doing = "writing several issues' parents";
        let data = match self
            .graphql(graphql::SET_PARENTS, Value::Object(variables))
            .await
        {
            Ok(data) => data,
            Err(error) => {
                return aliases
                    .into_iter()
                    .map(|(at, _, _, _)| (at, Err(error.clone())))
                    .collect();
            }
        };
        aliases
            .into_iter()
            .map(|(at, alias, child, parent)| {
                let answer = match alias_answer(&data, &alias, doing) {
                    Err(error) => Err(error),
                    // Written only when GitHub names both issues the write was about: the child,
                    // and the parent it was added under or removed from.
                    Ok(Some(answered))
                        if answered.pointer("/subIssue/id").and_then(Value::as_str)
                            == Some(child.0.as_str())
                            && answered.pointer("/issue/id").and_then(Value::as_str)
                                == Some(parent.0.as_str()) =>
                    {
                        Ok(ParentAnswer::Written { version: None })
                    }
                    Ok(Some(_)) => Err(SourceError::Malformed {
                        message: format!(
                            "GitHub answered {alias} while {doing} about another issue than \
                             {} under {}",
                            child.0, parent.0
                        ),
                    }),
                    Ok(None) => {
                        let said = errors_at(&data, &alias);
                        Ok(ParentAnswer::Refused {
                            reason: if said.is_empty() {
                                format!("GitHub answered nothing for the parent of {}", child.0)
                            } else {
                                format!(
                                    "GitHub refused the parent of {}: {}",
                                    child.0,
                                    said.join("; ")
                                )
                            },
                        })
                    }
                };
                (at, answer)
            })
            .collect()
    }

    /// Add or remove each native `relates-to` relation, in [`graphql::ADD_RELATES_TO`] or
    /// [`graphql::REMOVE_RELATES_TO`] documents of at most [`RELATION_BATCH`] fields; a field
    /// GitHub refused, or a document whose answer never arrived, read back and answered from
    /// what GitHub holds.
    pub(super) async fn native_links(
        &self,
        links: &[NativeLink],
        write: NativeWrite,
    ) -> Vec<Result<(), SourceError>> {
        let mut answers = Vec::with_capacity(links.len());
        for chunk in links.chunks(RELATION_BATCH) {
            answers.extend(self.send_native_links(chunk, write).await);
        }
        answers
    }

    async fn send_native_links(
        &self,
        chunk: &[NativeLink],
        write: NativeWrite,
    ) -> Vec<Result<(), SourceError>> {
        let Some(first) = chunk.first() else {
            return Vec::new();
        };
        if let Some(link) = chunk.iter().find(|link| link.native.as_str() != RELATES_TO) {
            let refused = SourceError::Refused {
                message: format!(
                    "source {} mirrors a link only as `{RELATES_TO}`, and was asked for `{}`",
                    self.name, link.native
                ),
            };
            return chunk.iter().map(|_| Err(refused.clone())).collect();
        }
        let (document, prefix, doing) = if write == NativeWrite::Add {
            (
                graphql::ADD_RELATES_TO,
                "relate",
                "adding several relates-to relations",
            )
        } else {
            (
                graphql::REMOVE_RELATES_TO,
                "unrelate",
                "removing several relates-to relations",
            )
        };
        let input =
            |link: &NativeLink| json!({"issueId": link.from.0, "relatedIssueId": link.to.0});
        let mut variables = Map::new();
        for slot in 0..RELATION_BATCH {
            let link = chunk.get(slot);
            variables.insert(format!("{prefix}{slot}"), input(link.unwrap_or(first)));
            variables.insert(format!("send{slot}"), json!(link.is_some()));
        }
        let sent = self.graphql(document, Value::Object(variables)).await;
        let mut answers: Vec<Result<(), SourceError>> = match &sent {
            Err(error) => chunk.iter().map(|_| Err(error.clone())).collect(),
            Ok(data) => (0..chunk.len())
                .map(|slot| {
                    let alias = format!("{prefix}{slot}");
                    let link = &chunk[slot];
                    match alias_answer(data, &alias, doing) {
                        // An answer about another pair says nothing of this one.
                        Ok(Some(answered))
                            if answered.pointer("/issue/id").and_then(Value::as_str)
                                == Some(link.from.0.as_str())
                                && answered.pointer("/relatedIssue/id").and_then(Value::as_str)
                                    == Some(link.to.0.as_str()) =>
                        {
                            Ok(())
                        }
                        Ok(Some(_)) => Err(SourceError::Malformed {
                            message: format!(
                                "GitHub answered {alias} while {doing} about another pair than \
                                 {} and {}",
                                link.from.0, link.to.0
                            ),
                        }),
                        Ok(None) => Err(SourceError::Refused {
                            message: format!(
                                "GitHub refused {alias} while {doing}: {}",
                                errors_at(data, &alias).join("; ")
                            ),
                        }),
                        Err(error) => Err(error),
                    }
                })
                .collect(),
        };
        if answers.iter().all(Result::is_ok) {
            return answers;
        }
        // What is still in doubt is answered from what GitHub holds now: a relation that is
        // there was added, and one that is not was removed, whatever the answer said — and only
        // where the read-back establishes it: an issue it could not read whole leaves its links
        // as they were answered.
        let doubted: Vec<usize> = (0..chunk.len())
            .filter(|at| answers[*at].is_err())
            .collect();
        let froms: Vec<NativeId> = doubted.iter().fold(Vec::new(), |mut ids, at| {
            if !ids.contains(&chunk[*at].from) {
                ids.push(chunk[*at].from.clone());
            }
            ids
        });
        let Ok(related) = self.relates_to(&froms).await else {
            return answers;
        };
        for at in doubted {
            let link = &chunk[at];
            match related.get(&link.from) {
                Some(Ok(others)) => {
                    if others.contains(&link.to) == (write == NativeWrite::Add) {
                        answers[at] = Ok(());
                    }
                }
                Some(Err(error)) => answers[at] = Err(error.clone()),
                None => {}
            }
        }
        answers
    }

    /// What each of `ids` relates to, read in [`graphql::RELATES_TO_READS`] documents — an issue
    /// whose connection is missing, malformed or longer than one page answered as an error, never
    /// as relating to nothing.
    async fn relates_to(
        &self,
        ids: &[NativeId],
    ) -> Result<BTreeMap<NativeId, Result<Vec<NativeId>, SourceError>>, SourceError> {
        let mut related = BTreeMap::new();
        for batch in ids.chunks(DETAIL_BATCH) {
            let mut variables = Map::new();
            for slot in 0..DETAIL_BATCH {
                let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
                variables.insert(format!("id{slot}"), json!(id));
            }
            variables.insert("first".to_owned(), json!(MAX_PAGE_SIZE));
            // As the relations read: a response carrying any GraphQL error is refused whole, so
            // the read-back answers every link of it as unknown rather than as absent.
            let data = self
                .graphql(graphql::RELATES_TO_READS, Value::Object(variables))
                .await?;
            for (slot, id) in batch.iter().enumerate() {
                related.insert(id.clone(), related_ids(&data, slot, id));
            }
        }
        Ok(related)
    }
}

/// The ids alias `r<slot>` of a [`graphql::RELATES_TO_READS`] answer says `id` relates to, or why
/// that answer establishes nothing.
fn related_ids(data: &Value, slot: usize, id: &NativeId) -> Result<Vec<NativeId>, SourceError> {
    let malformed = |why: &str| SourceError::Malformed {
        message: format!(
            "GitHub's read of what {} relates to {why}, so whether the relation is there is not \
             known; next: run the call again",
            id.0
        ),
    };
    let node = data
        .get(format!("r{slot}"))
        .filter(|node| !node.is_null())
        .ok_or_else(|| malformed("answered no issue"))?;
    if node.get("id").and_then(Value::as_str) != Some(id.0.as_str()) {
        return Err(malformed("answered about another issue"));
    }
    let connection = node
        .get("relatesTo")
        .ok_or_else(|| malformed("has no relatesTo"))?;
    if connection.pointer("/pageInfo/hasNextPage") != Some(&Value::Bool(false)) {
        return Err(malformed(
            "is longer than one page, or says nothing of its end",
        ));
    }
    connection
        .get("nodes")
        .and_then(Value::as_array)
        .ok_or_else(|| malformed("has no list of nodes"))?
        .iter()
        .map(|node| {
            node.get("id")
                .and_then(Value::as_str)
                .map(|other| NativeId(other.to_owned()))
                .ok_or_else(|| malformed("holds a node with no id"))
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn each_relation_field_sent_is_one_creation() {
        let mut variables = Map::new();
        for slot in 0..RELATION_BATCH {
            variables.insert(
                format!("relate{slot}"),
                json!({"issueId": "A", "relatedIssueId": "B"}),
            );
            variables.insert(format!("send{slot}"), json!(slot < 30));
        }
        let charge =
            crate::pacing::Charge::graphql(graphql::ADD_RELATES_TO, &Value::Object(variables));
        assert_eq!(charge.creations, 30);
    }

    #[test]
    fn a_read_back_that_cannot_establish_what_an_issue_relates_to_is_an_error_never_absence() {
        let id = NativeId("A".into());
        let whole = json!({"r0": {"id": "A", "relatesTo": {"nodes": [{"id": "B"}],
                                                            "pageInfo": {"hasNextPage": false}}}});
        assert_eq!(related_ids(&whole, 0, &id).unwrap(), [NativeId("B".into())]);
        for (answer, why) in [
            (json!({}), "answered no issue"),
            (json!({"r0": null}), "answered no issue"),
            (
                json!({"r0": {"id": "Z", "relatesTo": {"nodes": [],
                "pageInfo": {"hasNextPage": false}}}}),
                "another issue",
            ),
            (json!({"r0": {"id": "A"}}), "has no relatesTo"),
            (
                json!({"r0": {"id": "A", "relatesTo": {"nodes": [],
                "pageInfo": {"hasNextPage": true}}}}),
                "longer than one page",
            ),
            (
                json!({"r0": {"id": "A", "relatesTo": {"nodes": []}}}),
                "longer than one page",
            ),
            (
                json!({"r0": {"id": "A", "relatesTo": {
                "pageInfo": {"hasNextPage": false}}}}),
                "no list of nodes",
            ),
            (
                json!({"r0": {"id": "A", "relatesTo": {"nodes": [{"name": "B"}],
                "pageInfo": {"hasNextPage": false}}}}),
                "a node with no id",
            ),
        ] {
            let Err(SourceError::Malformed { message }) = related_ids(&answer, 0, &id) else {
                panic!("{answer} was read as an answer");
            };
            assert!(message.contains(why), "{message}");
        }
    }
}