jouet-github 0.0.2

jouet's GitHub, GitLab and Forgejo clients and the pull request model behind its pr and wait commands
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
//! Forgejo and Gitea pull requests behind [`Host`], through the REST API
//! (v1), which the two share.
//!
//! The API has no review threads, only review comments: comments on the
//! same file and line make one thread, as the web UI shows them, and a
//! thread's ID is its first comment's. A reply is a new single-comment
//! review on that line. Checks are the head commit's statuses, which is
//! where Forgejo Actions reports its jobs.
//!
//! Resolving a conversation is only in Gitea's API, from 1.26
//! (`POST /repos/{owner}/{repo}/pulls/comments/{id}/resolve`); Forgejo's
//! API has no such endpoint (checked through v16), so it is refused there.

use crate::forge::Forge;
use crate::host::{Feature, Host};
use crate::pr::status::{Checks, FetchResult, Fetched, Lookup, Mergeability, PrStatus, State};
use crate::pr::target::{Repo, Target};
use crate::pr::threads::{self, Comment, Kind, PrThreads, Pushed, Side, Thread};
use crate::rest::{MAX_PAGES, Rest, encode, utc};
use crate::{Failure, Method};
use jouet_output::time;
use serde_json::{Value, json};
use std::collections::BTreeMap;

pub struct Forgejo {
    rest: Rest,
    viewer: Option<String>,
    /// What `/version` said, once asked.
    version: Option<Option<String>>,
}

impl Forgejo {
    pub fn new(host: &str) -> Self {
        Self {
            rest: Rest::new(Forge::Forgejo, host),
            viewer: None,
            version: None,
        }
    }

    /// The REST client, for the CI reads `wait` makes.
    pub fn rest(&mut self) -> &mut Rest {
        &mut self.rest
    }

    /// The PR a target names: a number as is, a branch through its open PR,
    /// else its most recently updated. `None` when there is none.
    pub fn number(&mut self, target: &Target) -> Result<Option<u64>, Failure> {
        let (repo, branch) = match target {
            Target::Number(_, n) => return Ok(Some(*n)),
            Target::Branch(repo, branch) => (repo, branch),
        };
        for state in ["open", "all"] {
            let path = format!(
                "{}/pulls?state={state}&sort=recentupdate&limit=50",
                base(repo)
            );
            let prs = self.rest.list(&path)?.unwrap_or_default();
            if let Some(n) = prs
                .iter()
                .find(|p| p["head"]["ref"].as_str() == Some(branch))
                .and_then(|p| p["number"].as_u64())
            {
                return Ok(Some(n));
            }
        }
        Ok(None)
    }

    fn viewer(&mut self) -> String {
        if self.viewer.is_none() {
            // Reading a public PR needs no token, and then there is no
            // viewer.
            let login = self
                .rest
                .json("/user")
                .ok()
                .flatten()
                .and_then(|u| u["login"].as_str().map(str::to_string));
            self.viewer = Some(login.unwrap_or_default());
        }
        self.viewer.clone().unwrap_or_default()
    }

    /// The server's version, such as `1.26.0` (Gitea) or
    /// `16.0.0+gitea-1.22.0` (Forgejo).
    fn version(&mut self) -> Option<String> {
        if self.version.is_none() {
            let v = self.rest.json("/version").ok().flatten();
            self.version = Some(v.and_then(|v| v["version"].as_str().map(str::to_string)));
        }
        self.version.clone().flatten()
    }

    fn status(&mut self, repo: &Repo, pr: &Value) -> Result<Fetched, Failure> {
        let n = pr["number"].as_u64().unwrap_or_default();
        let state = match (pr["state"].as_str(), pr["merged"].as_bool()) {
            (_, Some(true)) => State::Merged,
            (Some("closed"), _) => State::Closed,
            _ if pr["draft"].as_bool() == Some(true) => State::Draft,
            _ => State::Open,
        };
        let (mergeable, merge_state) = match pr["mergeable"].as_bool() {
            Some(true) if state == State::Draft => (Mergeability::Mergeable, "draft"),
            Some(true) => (Mergeability::Mergeable, "clean"),
            Some(false) => (Mergeability::Conflicting, "dirty"),
            None => (Mergeability::Unknown, "unknown"),
        };
        let head_oid = pr["head"]["sha"].as_str().unwrap_or_default().to_string();
        let reviews = self
            .rest
            .list(&format!("{}/pulls/{n}/reviews?limit=50", base(repo)))?
            .unwrap_or_default();
        let comments = review_comments(&mut self.rest, repo, n, &reviews)?;
        let unresolved = group(comments)
            .iter()
            .filter(|t| t[0]["resolver"].is_null())
            .count();
        let mut checks = Checks::default();
        if !head_oid.is_empty() {
            for s in statuses(&mut self.rest, repo, &head_oid)? {
                let name = s["context"].as_str().unwrap_or_default().to_string();
                match s["status"].as_str().unwrap_or_default() {
                    "success" => checks.pass += 1,
                    "failure" | "error" => {
                        checks.fail += 1;
                        checks.failing.push(name);
                    }
                    "warning" | "skipped" => checks.skipped += 1,
                    _ => checks.pending += 1,
                }
            }
        }
        let pushed_at = self
            .rest
            .list(&format!("{}/pulls/{n}/commits?limit=50", base(repo)))
            .ok()
            .flatten()
            .and_then(|c| utc(&c.last()?["commit"]["committer"]["date"]));
        Ok(Fetched::new(PrStatus {
            repo: repo.to_string(),
            number: n,
            title: pr["title"].as_str().unwrap_or_default().to_string(),
            url: pr["html_url"].as_str().unwrap_or_default().to_string(),
            state,
            base: pr["base"]["ref"].as_str().unwrap_or_default().to_string(),
            head: pr["head"]["ref"].as_str().unwrap_or_default().to_string(),
            head_sha: head_oid.chars().take(10).collect(),
            head_oid,
            forge: Some("forgejo"),
            host: repo.host.clone(),
            pushed_at,
            mergeable,
            merge_state: merge_state.to_string(),
            checks,
            review: decision(&reviews),
            unresolved_threads: u32::try_from(unresolved).unwrap_or(u32::MAX),
            auto_merge: false,
            // Entire Gates is a GitHub check run.
            entire: None,
            attention: Vec::new(),
        }))
    }

    fn commits(&mut self, pr: &PrThreads) -> Result<Vec<(String, i64)>, String> {
        let list = self
            .rest
            .list(&format!(
                "{}/pulls/{}/commits?limit=50",
                base(&pr.repo),
                pr.number
            ))?
            .unwrap_or_default();
        Ok(list
            .iter()
            .map(|c| {
                let oid = c["sha"].as_str().unwrap_or_default().to_string();
                let at = c["commit"]["committer"]["date"]
                    .as_str()
                    .and_then(time::parse_rfc3339)
                    .unwrap_or(0);
                (oid, at)
            })
            .collect())
    }
}

/// A link as Forgejo Actions writes it, by path alone
/// (`/owner/repo/actions/runs/1/jobs/0`), made absolute on the host's web
/// server.
pub fn link(rest: &Rest, url: &str) -> String {
    if url.starts_with('/') {
        format!("{}{url}", rest.web_root())
    } else {
        url.to_string()
    }
}

/// `/repos/OWNER/NAME`.
pub fn base(repo: &Repo) -> String {
    format!("/repos/{}/{}", encode(&repo.owner), encode(&repo.name))
}

/// Every status on `sha`, the latest of each context, through every page.
pub fn statuses(rest: &mut Rest, repo: &Repo, sha: &str) -> Result<Vec<Value>, Failure> {
    let mut out: Vec<Value> = Vec::new();
    for page in 1..=MAX_PAGES {
        let path = format!("{}/commits/{sha}/status?limit=50&page={page}", base(repo));
        let Some(combined) = rest.json(&path)? else {
            break;
        };
        let list = combined["statuses"].as_array().cloned().unwrap_or_default();
        let total = combined["total_count"].as_u64().unwrap_or(0);
        let empty = list.is_empty();
        out.extend(list);
        if empty || out.len() as u64 >= total {
            break;
        }
    }
    Ok(out)
}

/// The review decision: changes requested if any reviewer's latest
/// standing review asks for them, else approved if one approves.
fn decision(reviews: &[Value]) -> Option<String> {
    let mut latest: BTreeMap<&str, &str> = BTreeMap::new();
    for r in reviews {
        if r["dismissed"].as_bool() == Some(true) {
            continue;
        }
        let (Some(user), Some(state)) = (r["user"]["login"].as_str(), r["state"].as_str()) else {
            continue;
        };
        if matches!(state, "APPROVED" | "REQUEST_CHANGES") {
            latest.insert(user, state);
        }
    }
    if latest.values().any(|s| *s == "REQUEST_CHANGES") {
        Some("changes_requested".into())
    } else if latest.values().any(|s| *s == "APPROVED") {
        Some("approved".into())
    } else {
        None
    }
}

/// The line comments of every review that has any.
fn review_comments(
    rest: &mut Rest,
    repo: &Repo,
    n: u64,
    reviews: &[Value],
) -> Result<Vec<Value>, Failure> {
    let mut out = Vec::new();
    for r in reviews {
        let (Some(id), Some(count)) = (r["id"].as_u64(), r["comments_count"].as_u64()) else {
            continue;
        };
        if count == 0 {
            continue;
        }
        let path = format!("{}/pulls/{n}/reviews/{id}/comments", base(repo));
        out.extend(rest.list(&path)?.unwrap_or_default());
    }
    Ok(out)
}

/// The side and line of a review comment: `position` on the head's side,
/// `original_position` on the base's.
fn place(c: &Value) -> (Side, Option<u32>) {
    let line = |k: &str| {
        c[k].as_u64()
            .filter(|l| *l > 0)
            .and_then(|l| u32::try_from(l).ok())
    };
    match line("position") {
        Some(l) => (Side::Right, Some(l)),
        None => (Side::Left, line("original_position")),
    }
}

/// Review comments grouped into threads by file, side and line, oldest
/// first in each and threads in the order they started.
fn group(mut comments: Vec<Value>) -> Vec<Vec<Value>> {
    comments.sort_by_key(|c| {
        (
            c["created_at"]
                .as_str()
                .and_then(time::parse_rfc3339)
                .unwrap_or(0),
            c["id"].as_u64().unwrap_or(0),
        )
    });
    let mut threads: Vec<Vec<Value>> = Vec::new();
    for c in comments {
        let key = |c: &Value| (c["path"].as_str().map(str::to_string), place(c));
        match threads.iter_mut().find(|t| key(&t[0]) == key(&c)) {
            Some(t) => t.push(c),
            None => threads.push(vec![c]),
        }
    }
    threads
}

fn comment(c: &Value) -> Comment {
    let created_at = utc(&c["created_at"]).unwrap_or_default();
    Comment {
        id: c["id"].as_u64().unwrap_or_default(),
        author: c["user"]["login"].as_str().unwrap_or("ghost").to_string(),
        created: time::parse_utc(&created_at).unwrap_or(0),
        created_at,
        body: c["body"].as_str().unwrap_or_default().to_string(),
        body_truncated: false,
        suggestion: None,
        url: c["html_url"].as_str().unwrap_or_default().to_string(),
    }
}

fn other(kind: Kind, c: Comment, state: Option<String>, bots: &[String]) -> Thread {
    Thread {
        id: c.id,
        node_id: String::new(),
        kind,
        path: None,
        line: None,
        start_line: None,
        original_line: None,
        side: Side::Right,
        outdated: false,
        resolved: false,
        is_bot: threads::is_bot(&c.author, bots),
        author: c.author.clone(),
        state,
        url: c.url.clone(),
        comments: vec![c],
        context: None,
    }
}

/// The major and minor of a Gitea version, or `None` for Forgejo (whose
/// version names the Gitea it forked from after a `+`) or one unreadable.
fn gitea_version(version: &str) -> Option<(u32, u32)> {
    if version.contains("+gitea") || version.contains("forgejo") {
        return None;
    }
    let mut parts = version.trim_start_matches('v').split(['.', '+', '-']);
    let major: u32 = parts.next()?.parse().ok()?;
    let minor: u32 = parts.next()?.parse().ok()?;
    // Forgejo numbers its releases from 7 up; Gitea is still 1.x.
    (major == 1).then_some((major, minor))
}

impl Host for Forgejo {
    fn forge(&self) -> Forge {
        Forge::Forgejo
    }

    fn lacks(&mut self, feature: Feature) -> Option<String> {
        let host = self.rest.host().to_string();
        match feature {
            Feature::Search => Some(
                "--search takes GitHub's search syntax, which Forgejo has no equivalent of".into(),
            ),
            Feature::Runs => Some(format!(
                "jouet reads Forgejo Actions through the commit statuses it posts, and {host} gives no run of its own; wait on the PR with `jouet wait ci` instead"
            )),
            Feature::Explain => Some(
                "`ci explain` judges a failure by GitHub Actions' run history, which Forgejo's API does not give; use `jouet wait ci` for the failed checks".into(),
            ),
            Feature::Resolve => match self.version() {
                Some(v) => match gitea_version(&v) {
                    Some((1, minor)) if minor >= 26 => None,
                    Some(_) => Some(format!(
                        "{host} runs Gitea {v}, whose API cannot resolve review conversations (Gitea 1.26 added it)"
                    )),
                    None => Some(format!(
                        "{host} runs Forgejo {v}, whose API cannot resolve review conversations (none through Forgejo 16 can)"
                    )),
                },
                // An unknown version is tried; the server says if it cannot.
                None => None,
            },
            Feature::RequiredChecks => Some(
                "Forgejo's API does not say which checks branch protection requires, so --required would select none; name them with --check"
                    .into(),
            ),
            Feature::Land => Some(
                "`pr land` merges GitHub PRs only so far; merge the PR with `tea pulls merge` or on the web".into(),
            ),
        }
    }

    fn fetch(&mut self, lookups: Vec<Lookup>) -> Result<FetchResult, String> {
        let mut result = FetchResult::default();
        for lookup in lookups {
            match lookup {
                Lookup::Pr(target) => {
                    let repo = target.repo().clone();
                    let pr = match self.number(&target)? {
                        Some(n) => self.rest.json(&format!("{}/pulls/{n}", base(&repo)))?,
                        None => None,
                    };
                    match pr {
                        Some(pr) => result.prs.push(self.status(&repo, &pr)?),
                        None => result.not_found.push(target.to_string()),
                    }
                }
                Lookup::Mine(repo) => {
                    let viewer = self.viewer();
                    if viewer.is_empty() {
                        return Err(format!(
                            "--mine needs to know who you are, and {} answered no user for the token; set FORGEJO_TOKEN",
                            self.rest.host()
                        ));
                    }
                    let path = format!(
                        "{}/pulls?state=open&poster={}&limit=50",
                        base(&repo),
                        encode(&viewer)
                    );
                    for pr in self.rest.list(&path)?.unwrap_or_default() {
                        result.prs.push(self.status(&repo, &pr)?);
                    }
                }
                Lookup::Search { .. } | Lookup::Threads { .. } | Lookup::Contexts { .. } => {}
            }
        }
        Ok(result)
    }

    fn threads(
        &mut self,
        target: &Target,
        extra: bool,
        bots: &[String],
    ) -> Result<Option<PrThreads>, String> {
        let repo = target.repo().clone();
        let Some(n) = self.number(target)? else {
            return Ok(None);
        };
        let Some(raw) = self.rest.json(&format!("{}/pulls/{n}", base(&repo)))? else {
            return Ok(None);
        };
        let head_repo = raw["head"]["repo"]["full_name"]
            .as_str()
            .filter(|f| *f != repo.to_string())
            .and_then(|f| Repo::on(self.rest.host(), f));
        let viewer = self.viewer();
        let mut pr = PrThreads {
            repo: repo.clone(),
            number: n,
            node_id: String::new(),
            head_ref: raw["head"]["ref"].as_str().unwrap_or_default().to_string(),
            head_oid: raw["head"]["sha"].as_str().unwrap_or_default().to_string(),
            base_oid: raw["merge_base"].as_str().unwrap_or_default().to_string(),
            head_repo,
            viewer,
            threads: Vec::new(),
            others: Vec::new(),
            unreadable: 0,
        };
        let reviews = self
            .rest
            .list(&format!("{}/pulls/{n}/reviews?limit=50", base(&repo)))?
            .unwrap_or_default();
        for comments in group(review_comments(&mut self.rest, &repo, n, &reviews)?) {
            let first = &comments[0];
            let (side, line) = place(first);
            let mut t = other(Kind::Thread, comment(first), None, bots);
            t.path = first["path"].as_str().map(str::to_string);
            t.side = side;
            t.line = line;
            t.original_line = line;
            t.resolved = !first["resolver"].is_null();
            t.comments = comments.iter().map(comment).collect();
            pr.threads.push(t);
        }
        if extra {
            for r in &reviews {
                let body = r["body"].as_str().unwrap_or_default();
                if body.trim().is_empty() || r["state"].as_str() == Some("PENDING") {
                    continue;
                }
                let state = match r["state"].as_str().unwrap_or_default() {
                    "REQUEST_CHANGES" => "changes_requested".to_string(),
                    "COMMENT" => "commented".to_string(),
                    s => s.to_ascii_lowercase(),
                };
                let mut c = comment(r);
                c.created_at = utc(&r["submitted_at"]).unwrap_or_default();
                c.created = time::parse_utc(&c.created_at).unwrap_or(0);
                pr.others.push(other(Kind::Review, c, Some(state), bots));
            }
            let path = format!("{}/issues/{n}/comments?limit=50", base(&repo));
            for c in self.rest.list(&path)?.unwrap_or_default() {
                if c["body"].as_str().is_some_and(|b| !b.trim().is_empty()) {
                    pr.others
                        .push(other(Kind::Comment, comment(&c), None, bots));
                }
            }
            pr.others
                .sort_by_key(|t| t.comments.first().map_or(0, |c| c.created));
        }
        Ok(Some(pr))
    }

    /// Forgejo keeps no push log, so a commit's push time is its commit
    /// time, which can only be earlier.
    fn pushed_at(&mut self, pr: &PrThreads, sha: &str) -> Result<Pushed, threads::Lookup> {
        let sha = threads::sha_prefix(sha)?;
        let commits = self.commits(pr).map_err(threads::Lookup::Remote)?;
        threads::pushed_from(&commits, &[], &sha)
    }

    fn files(
        &mut self,
        pr: &PrThreads,
        oid: &str,
        paths: &[String],
    ) -> Result<Vec<(String, String)>, String> {
        let mut out = Vec::new();
        for path in paths {
            let segments: Vec<String> = path.split('/').map(encode).collect();
            let url = format!("{}/raw/{}?ref={oid}", base(&pr.repo), segments.join("/"));
            let resp = self.rest.get(&url, None)?;
            if resp.status == 200 {
                out.push((path.clone(), resp.body));
            }
        }
        Ok(out)
    }

    fn merge_base(&mut self, pr: &PrThreads) -> Result<String, String> {
        if pr.base_oid.is_empty() {
            return Err("Forgejo sent the PR without its merge base".into());
        }
        Ok(pr.base_oid.clone())
    }

    fn on_branch(&mut self, pr: &PrThreads, head: &str) -> Result<bool, String> {
        Ok(self.commits(pr)?.iter().any(|(oid, _)| oid == head))
    }

    fn reply(&mut self, pr: &PrThreads, thread: &Thread, body: &str) -> Result<String, String> {
        let path = thread.path.as_deref().ok_or("the thread is on no file")?;
        let line = thread
            .original_line
            .ok_or("the thread is on no line of the file")?;
        let mut comment = json!({ "path": path, "body": body });
        let key = match thread.side {
            Side::Right => "new_position",
            Side::Left => "old_position",
        };
        comment[key] = line.into();
        let url = format!("{}/pulls/{}/reviews", base(&pr.repo), pr.number);
        let request = json!({
            "event": "COMMENT",
            "body": "",
            "commit_id": pr.head_oid,
            "comments": [comment],
        });
        let resp = self.rest.write(Method::Post, &url, &request)?;
        let review = self
            .rest
            .read(&url, resp)?
            .ok_or("Forgejo did not post the reply")?;
        Ok(review["html_url"]
            .as_str()
            .filter(|u| !u.is_empty())
            .map_or_else(|| thread.url.clone(), str::to_string))
    }

    fn set_resolved(
        &mut self,
        pr: &PrThreads,
        thread: &Thread,
        resolved: bool,
    ) -> Result<(), String> {
        let verb = if resolved { "resolve" } else { "unresolve" };
        let path = format!("{}/pulls/comments/{}/{verb}", base(&pr.repo), thread.id);
        let resp = self.rest.write(Method::Post, &path, &json!({}))?;
        match resp.status {
            200..=299 => Ok(()),
            404 | 405 => Err(format!(
                "{} has no API to {verb} review conversations (Gitea 1.26 added it; Forgejo has none)",
                self.rest.host()
            )),
            status => Err(self.rest.refused(status, &resp.body).message()),
        }
    }
}

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

    #[test]
    fn forgejo_versions_are_told_from_gitea_ones() {
        assert_eq!(gitea_version("1.26.0"), Some((1, 26)));
        assert_eq!(gitea_version("1.25.3"), Some((1, 25)));
        assert_eq!(gitea_version("16.0.0-dev-798-568c49e1+gitea-1.22.0"), None);
        assert_eq!(gitea_version("11.0.1+gitea-1.22.0"), None);
    }

    #[test]
    fn comments_on_one_line_are_one_thread() {
        let c = |id: u64, line: u64, at: &str| json!({ "id": id, "path": "a.rs", "position": line, "created_at": at });
        let threads = group(vec![
            c(3, 5, "2026-01-01T00:02:00Z"),
            c(1, 5, "2026-01-01T00:00:00Z"),
            c(2, 9, "2026-01-01T00:01:00Z"),
        ]);
        let ids: Vec<Vec<u64>> = threads
            .iter()
            .map(|t| t.iter().map(|c| c["id"].as_u64().unwrap()).collect())
            .collect();
        assert_eq!(ids, [vec![1, 3], vec![2]]);
    }

    #[test]
    fn a_standing_request_for_changes_wins() {
        let r = |user: &str, state: &str| json!({ "user": { "login": user }, "state": state });
        assert_eq!(
            decision(&[r("a", "APPROVED"), r("b", "REQUEST_CHANGES")]).as_deref(),
            Some("changes_requested")
        );
        assert_eq!(
            decision(&[r("a", "REQUEST_CHANGES"), r("a", "APPROVED")]).as_deref(),
            Some("approved")
        );
        assert_eq!(decision(&[r("a", "COMMENT")]), None);
    }
}