Skip to main content

artifactize_core/monitor/live/
waiting.rs

1//! The Human reviews waiting for a person, in every repository on this machine, those of
2//! removed branches included while their folder exists: what the pane's waiting stack lists.
3use super::{Label, Labels};
4use crate::{
5    review::search::{self, Entry, Query},
6    store::RequestView,
7    types::ReviewerId,
8};
9use std::path::Path;
10
11/// One waiting Human review, with the project and branch its Run ran on.
12#[derive(Debug, Clone)]
13pub struct Waiting {
14    pub view: RequestView,
15    pub entry: Entry,
16    pub label: Label,
17}
18
19/// Every waiting Human review, including claimed ones, newest first (a follower listed once,
20/// as its original), each named by `labels`, the project and branch
21/// of its Run. A review whose Run has no label (its record cannot be read) is named by its
22/// repository's folder.
23pub async fn waiting(
24    state: &Path,
25    reviewer: &ReviewerId,
26    labels: &Labels,
27) -> Result<Vec<Waiting>, String> {
28    let query: Query = "kind:human status:waiting_human"
29        .parse()
30        .expect("the monitor waiting query parses");
31    let mut found = Vec::new();
32    let mut start = 0;
33    loop {
34        let page = search::search(state, &query, reviewer, start).await?;
35        found.extend(page.requests);
36        match page.next {
37            Some(next) => start = next,
38            None => break,
39        }
40    }
41    found.sort_by(|(a, _), (b, _)| {
42        (b.request.created_at, &b.request.id).cmp(&(a.request.created_at, &a.request.id))
43    });
44    Ok(found
45        .into_iter()
46        .map(|(view, entry)| {
47            let label = labels
48                .get(&view.request.run_id)
49                .cloned()
50                .unwrap_or_else(|| Label {
51                    project: entry.repository.file_name().map_or_else(
52                        || crate::platform::path_text(&entry.repository),
53                        crate::platform::display_name,
54                    ),
55                    ..Label::default()
56                });
57            Waiting { view, entry, label }
58        })
59        .collect())
60}